Example #1
0
 def screenWrap(self):
     if self.ship1.rect.left == self.screen.get_width():
         self.ship1 = ships.Ship(self.screen, 0, 340)
         self.ship1.draw()
     if self.ship2.rect.left == self.screen.get_width():
         self.ship2 = ships.Ship(self.screen, -200, 340)
         self.ship2.draw()
     if self.ship3.rect.left == self.screen.get_width():
         self.ship3 = ships.Ship(self.screen, -400, 340)
         self.ship3.draw()       
Example #2
0
 def __init__(self):
     pygame.init()
     self.score = 0
     self.scoreText = pygame.font.Font("Minecraft.ttf", 30)
     self.timeText = pygame.font.Font("Minecraft.ttf", 30)
     self.width = 1000
     self.height = 700
     self.screen = pygame.display.set_mode((self.width, self.height))
     self.background = pygame.Surface(self.screen.get_size())
     self.background = self.background.convert()
     self.screen.fill((200,200,200)) # Values can be changed as needed. Example values
     self.bg = background.Background(self.screen, 0, 375)
     self.ship1Y = random.randint(340, 375)
     self.ship2Y = random.randint(375, 385)
     self.ship3Y = random.randint(385, 395)
     self.ship1 = ships.Ship(self.screen, 0, self.ship1Y)
     self.ship2 = ships.Ship(self.screen, -50, self.ship2Y)
     self.ship3 = ships.Ship(self.screen, -80, self.ship3Y) 
     self.scope = scope.Scope(self.screen)
     self.linev = scope.Scope(self.screen)
     self.lineh = scope.Scope(self.screen)
     self.P1 = (500, 700)
     self.dx = 0
     self.dy = 0
     self.torpedo = None
     self.ship1Speed = random.randint(1,3)
     self.ship2Speed = random.randint(1,3)
     self.ship3Speed = random.randint(1,3)
     self.explosionLocX = None
     self.explosionLocY = None
     self.explosion = None
     self.LENGTHOFGAME = 90
     self.time = 90
     self.boVoiceCounter = 0
     self.explodeSound = pygame.mixer.Sound("explode.wav")
     self.torpedoSound = pygame.mixer.Sound("torpedoMove.wav")
     self.voiceSink = pygame.mixer.Sound("ShipDestroyed1.wav")
     #self.voiceShoot = pygame.mixer.Sound("")
     self.voicePowerPlus = pygame.mixer.Sound("HavePower.wav")
     self.voicePowerGone = pygame.mixer.Sound("NoPower.wav")
     #self.voiceTorp = pygame.mixer.Sound("Torpedo1.wav")
     self.cooldown = 300
     self.voiceCounter = 0
     self.voiceTorpedo = pygame.mixer.Sound("firingTorpedo.wav")
     self.channel1 = pygame.mixer.Channel(0)
     self.channel2 = pygame.mixer.Channel(1)
     self.channel3 = pygame.mixer.Channel(2)
     self.channel4 = pygame.mixer.Channel(3)
     self.channel5 = pygame.mixer.Channel(4)
Example #3
0
def createWorld():
    planets = selectDifficulty()
    for i in range(planets):
        worlds.append(
            Planet("Planet-" + str(i), "Welcome to " + "Planet-" + str(i),
                   i * random.randint(0, 100), updates))
    for i in worlds:
        numberOfTrees = random.randint(0, 100)
        for j in range(numberOfTrees):
            tree = Tree.Tree(updates)
            tree.putInRoom(i)
        if random.random() > .5:
            river = River(i, updates)
            i.addLocation(river)

    player.location = worlds[0]
    player.ship = ships.Ship(player, updates)

    #return jim to his house on planet 0
    jim = Jim.Jim(updates)
    jim.putInRoom(worlds[random.randint(1, planets - 1)])
    jimsHouse = House('An empty house',
                      'You see a handful of red vines on the windowsill',
                      player, updates)
    worlds[0].addLocation(jimsHouse)
    jimBait = RedVines.RedVines(updates)
    jimBait.putInRoom(jimsHouse)
    jimsHouse.key.putInRoom(worlds[0])

    i = Rock.Rock(updates)
    i.putInRoom(worlds[0])
Example #4
0
 def addShip(self):
     ship = ships.Ship(" Unknown", " Unknown", " Unknown")
     self.ships.addShip(ship)
     self.populateList()
     self.populateTree()
     self.populateTable(id(ship))
     self.tableWidget.setFocus()
     self.tableWidget.editItem(self.tableWidget.currentItem())
Example #5
0
 def collisionShip3 (self):
     if pygame.sprite.collide_rect(self.torpedo, self.ship3):
         print("explosion 3 found")
         self.explosionLocX = self.ship3.rect.x
         self.explosionLocY = self.ship3.rect.y
         self.explodeSound.play()
         self.explosion = explosion.Explosion(self.screen, self.explosionLocX, self.explosionLocY)
         del(self.ship3)
         self.ship3 = ships.Ship(self.screen, -600, 340)
         for i in range(3):
             self.explosion.animate(i)
         self.score += self.ship3Speed * 100
Example #6
0
def load_ship(filename, all_systems, all_weapons):
    with open(filename, 'r') as infile:
        ship_json = json.load(infile)
    ship = ships.Ship(ship_json, all_systems, all_weapons)
    return ship
Example #7
0
def play_battleship():

    user_width = 0
    play_again = 0
    turns = 0

    while play_again != 2:

        if play_again == 0:

            os.system('cls')

            #prompt the user to play a game or exit.
            print("Would you like to play a game of Battleship?")
            print("Enter 1 to play")
            print("Enter 2 to exit")
            play_again = int(input("Make a selection: "))

        if play_again == 1:
            os.system('cls')
            #Checking to be sure the users board size is within the limit. IF it is we create a board object
            while user_width % 2 != 1 or user_width > 15:
                user_width = int(
                    input(
                        "Please enter an odd numerical value for the width of the board between 1 and 15: "
                    ))
                while turns < 1 or turns > 10:

                    turns = int(
                        input(
                            "How many turns would you like (no more than 10): "
                        ))

                if user_width % 2 == 1 and user_width < 16:
                    board = gameboard.Board(user_width)

                else:
                    print(
                        "That was not an odd numberical value between 1 and 15..."
                    )

            #Checking to make sure our board is within regulation. IF it is we create the board and print it to the screen.
            if user_width != len(board.board):

                # if this isn't here the program prints out the previous board as well as the new board.
                board.board = []
                board.create_board()
                #board.print_board()

            #initialize a ship object
            ship_1 = ships.Ship()
            ship_row = ship_1.row(board)
            ship_col = ship_1.col(board)

            #Turn counter and logic to take user guess and check if they are on the board, correct or wrong.
            for turn in range(turns):

                os.system('cls')
                board.print_board()

                #for debugging purposes
                print("The ship is in row: ", ship_row)
                print("The ship is in col: ", ship_col)

                #prints the turn number and prompts the user for their guess
                print("Turn: ", turn + 1, " out of ", turns)
                guess_row = int(input("Guess Row: ")) - 1
                guess_col = int(input("Guess Col: ")) - 1

                #checks to see if the users guess is correct
                if guess_row == ship_row and guess_col == ship_col:
                    print("Congratulations! You sank my battleship!")
                    play_again = 0
                    user_width = 0
                    turns = 0
                    time.sleep(2)
                    break

                else:
                    if guess_row not in range(
                            user_width) or guess_col not in range(user_width):
                        print("Oops, That's not even in the ocean.")
                        time.sleep(1)

                        if turn == turns - 1:
                            os.system('cls')
                            print("Game Over!")
                            play_again = 0
                            user_width = 0
                            turns = 0
                            time.sleep(2)
                            break

                    elif board.board[guess_row][guess_col] == "X":
                        print("You guessed that one alread!")
                        time.sleep(1)

                        if turn == turns - 1:
                            os.system('cls')
                            print("Game Over!")
                            play_again = 0
                            user_width = 0
                            turns = 0
                            time.sleep(2)
                            break
                    else:
                        print("You missed my battleship!")
                        board.board[guess_row][guess_col] = "X"
                        time.sleep(1)

                        if turn == turns - 1:
                            os.system('cls')
                            print("Game Over!")
                            play_again = 0
                            user_width = 0
                            turns = 0
                            time.sleep(2)
                            break
Example #8
0
    def runGame(self):
        pygame.key.set_repeat(500, 30) # Values can be changed as needed. Example values

        startGameTime = time.time()
        self.last = pygame.time.get_ticks()
        rand_blackoutSt = random.randint(5, 8)
        rand_blackoutEnd = random.randint(9, 12)
        blackout2st = random.randint(25, 28)
        blackout2End = random.randint(29, 33)
        blackVocCounter = 0
        blackOut3 = random.randint(50, 52)
        blackout3st = 51
        blackout3End = random.randint(54, 56)
        #print(blackout3)
        while 1:
            for event in pygame.event.get(): # Handles figuring out even 
                if event.type == pygame.QUIT:
                    sys.exit()
            pygame.display.update()
            #seconds = pygame.time.get_ticks()//1000
            self.screen.fill((200,200,200)) # Values can be changed as needed. Example values
            self.time = 90
            self.bg.drawSea()
            self.bg.drawSky()
            self.ship1.draw()
            self.ship2.draw()
            self.ship3.draw()
            secondsPassed = time.time() - startGameTime
            #1st and 2nd Blackout
            if time.time() - startGameTime >= rand_blackoutSt and time.time() - startGameTime <= rand_blackoutEnd or time.time() - startGameTime >= blackout2st and time.time() - startGameTime <= blackout2End:
                if self.voiceCounter == 0:
                    self.channel3.play(self.voicePowerGone, 0)
                if int(time.time()) - int(startGameTime) == int(rand_blackoutEnd) and self.boVoiceCounter == 0:
                    self.channel5.play(self.voicePowerPlus, 0)
                    self.boVoiceCounter += 1
                    print("blackout over")
                startTime = int(time.time())
                endTime = startTime + 10
                while not startTime == endTime:
                    self.screen.fill((0,0,0))
                    startTime += 1
                    self.voiceCounter += 1
            #3rd blackout
            if time.time() - startGameTime >= blackOut3 and time.time() - startGameTime <= blackout3End and blackOut3 == 51:
                if self.voiceCounter == 0:
                    self.channel3.play(self.voicePowerGone, 0)
                if int(time.time()) - int(startGameTime) == int(rand_blackoutEnd) and self.boVoiceCounter == 0:
                    self.channel5.play(self.voicePowerPlus, 0)
                    self.boVoiceCounter += 1
                    print("blackout over")
                startTime = int(time.time())
                endTime = startTime + 10
                while not startTime == endTime:
                    self.screen.fill((0,0,0))
                    startTime += 1
                    self.voiceCounter += 1
            print(pygame.time.get_ticks())
            #if time.time()*1000%1000 ==0:
                #self.time -= 1
            self.scope.move()
            self.moveShips()
            self.screenWrap()
                   

            if time.time() - startGameTime >= self.LENGTHOFGAME and self.torpedo == None:
                pygame.mixer.music.stop()
                pygame.mixer.music.load("menu_bgm.wav")
                pygame.mixer.music.play(-1, 0.0)
                go = gameover.GameOver(self.score)
                g = go.runGameOver()
                if g:
                    return
                else:
                    pygame.init()
                    # re-setup the class game to play the  game
                    self.score = 0
                    self.scoreText = pygame.font.Font("Minecraft.ttf", 30)
                    self.width = 1000
                    self.height = 700
                    self.screen = pygame.display.set_mode((self.width, self.height))
                    self.background = pygame.Surface(self.screen.get_size())
                    self.background = self.background.convert()
                    self.screen.fill((200,200,200)) # Values can be changed as needed. Example values
                    self.bg = background.Background(self.screen, 0, 375)
                    self.ship1Y = random.randint(340, 375)
                    self.ship2Y = random.randint(350, 385)
                    self.ship3Y = random.randint(360, 395)
                    self.ship1 = ships.Ship(self.screen, 0, self.ship1Y)
                    self.ship2 = ships.Ship(self.screen, -200, self.ship2Y)
                    self.ship3 = ships.Ship(self.screen, -400, self.ship3Y) 
                    self.scope = scope.Scope(self.screen)
                    self.linev = scope.Scope(self.screen)
                    self.lineh = scope.Scope(self.screen)
                    self.P1 = (500, 700)
                    self.dx = 0
                    self.dy = 0
                    self.torpedo = None
                    self.ship1Speed = random.randint(1,3)
                    self.ship2Speed = random.randint(1,3)
                    self.ship3Speed = random.randint(1,3)
                    self.explosionLocX = None
                    self.explosionLocY = None
                    self.explosion = None
                    startGameTime = time.time()


            
            key = pygame.key.get_pressed()
            # Catching the ZeroDivisionError using an exception
            self.now = pygame.time.get_ticks()
            try:
                if key[pygame.K_SPACE]:
                    print(self.calculateSlope())
                    #self.torpedoSound.play()
                    if time.time() - startGameTime <= self.LENGTHOFGAME:
                        self.torpedo = torpedo.Torpedo(self.screen, self.dx, self.dy)
                        self.channel1.play(self.torpedoSound, 0)
                        self.channel4.play(self.voiceTorpedo, 0)
                        #self.channel2.play(self.voiceTorp, 0)
                        self.torpedoSound.set_volume(1.0)
            except ZeroDivisionError:
                if time.time() - startGameTime <= self.LENGTHOFGAME:
                    self.torpedo = torpedo.Torpedo(self.screen, self.dx, self.dy)
                    self.torpedo.move()
                    self.channel1.play(self.torpedoSound, 0)
                    #self.channel2.play(self.voiceTorp, 0)
                    self.torpedoSound.set_volume(1.0)
                    pass
            if not self.torpedo == None:
                oldVolume = self.torpedoSound.get_volume()
                self.torpedoSound.set_volume(oldVolume - .005)
                self.torpedo.move()
                if self.collision() or self.torpedo.rect.top < 440:
                    self.collisionShip1()
                    self.collisionShip2()
                    self.collisionShip3()
                    del(self.torpedo)
                    self.torpedoSound.stop()
                    time.sleep(.03)
                    self.torpedo = None
                #if self.torpedo.rect.top < 420:
                    #print("="*15)
                    #del(self.torpedo)
                    #self.torpedo = None
            self.updateScore()
            self.time = self.time - secondsPassed
            self.time = int(self.time)
            #if time.time() - startGameTime <= self.LENGTHOFGAME:
            if time.time() - startGameTime >= self.LENGTHOFGAME:
                self.screen.blit(self.textDis,(830, 35))
            else:
                self.updateTime()
Example #9
0
    def place_ships(self):
        self.desktop()
        self.draw_board()
        self.air_ship = ships.Ship(4, 370)
        self.battle1 = ships.Ship(3, 370)
        self.battle2 = ships.Ship(3, 370)
        self.submarine1 = ships.Ship(2, 370)
        self.submarine2 = ships.Ship(2, 370)
        self.submarine3 = ships.Ship(2, 370)
        self.war1 = ships.Ship(1, 370)
        self.war2 = ships.Ship(1, 370)
        self.war3 = ships.Ship(1, 370)
        self.war4 = ships.Ship(1, 370)

        pygame.display.update()
        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit()
                elif (event.type == MOUSEBUTTONDOWN):
                    if (air_rect.collidepoint(event.pos)
                            and self.air_ship.placed == False):
                        self.air_ship.placeBoat(self.screen, self.user_ships)
                    elif (battle1_rect.collidepoint(event.pos)
                          and self.battle1.placed == False):
                        self.battle1.placeBoat(self.screen, self.user_ships)
                    elif (battle2_rect.collidepoint(event.pos)
                          and self.battle2.placed == False):
                        self.battle2.placeBoat(self.screen, self.user_ships)
                    elif (sub1_rect.collidepoint(event.pos)
                          and self.submarine1.placed == False):
                        self.submarine1.placeBoat(self.screen, self.user_ships)
                    elif (sub2_rect.collidepoint(event.pos)
                          and self.submarine2.placed == False):
                        self.submarine2.placeBoat(self.screen, self.user_ships)
                    elif (sub3_rect.collidepoint(event.pos)
                          and self.submarine3.placed == False):
                        self.submarine3.placeBoat(self.screen, self.user_ships)
                    elif (war1_rect.collidepoint(event.pos)
                          and self.war1.placed == False):
                        self.war1.placeBoat(self.screen, self.user_ships)
                    elif (war2_rect.collidepoint(event.pos)
                          and self.war2.placed == False):
                        self.war2.placeBoat(self.screen, self.user_ships)
                    elif (war3_rect.collidepoint(event.pos)
                          and self.war3.placed == False):
                        self.war3.placeBoat(self.screen, self.user_ships)
                    elif (war4_rect.collidepoint(event.pos)
                          and self.war4.placed == False):
                        self.war4.placeBoat(self.screen, self.user_ships)
                    elif (ready_rect.collidepoint(event.pos)
                          and self.air_ship.placed == True
                          and self.battle1.placed == True
                          and self.battle2.placed == True
                          and self.submarine1.placed == True
                          and self.submarine2.placed == True
                          and self.submarine3.placed == True
                          and self.war1.placed == True
                          and self.war2.placed == True
                          and self.war3.placed == True
                          and self.war4.placed == True):
                        return
            if (self.air_ship.placed == True):
                pygame.draw.rect(self.screen, GREEN, (900, 50, 300, 85), 5)
                pygame.display.update()
            if (self.battle1.placed == True):
                pygame.draw.rect(self.screen, GREEN, (900, 145, 300, 85), 5)
                pygame.display.update()
            if (self.battle2.placed == True):
                pygame.draw.rect(self.screen, GREEN, (900, 240, 300, 85), 5)
                pygame.display.update()
            if (self.submarine1.placed == True):
                pygame.draw.rect(self.screen, GREEN, (900, 335, 145, 60), 5)
                pygame.display.update()
            if (self.submarine2.placed == True):
                pygame.draw.rect(self.screen, GREEN, (1055, 335, 145, 60), 5)
                pygame.display.update()
            if (self.submarine3.placed == True):
                pygame.draw.rect(self.screen, GREEN, (900, 405, 145, 60), 5)
                pygame.display.update()
            if (self.war1.placed == True):
                pygame.draw.rect(self.screen, GREEN, (1055, 405, 145, 60), 5)
                pygame.display.update()
            if (self.war2.placed == True):
                pygame.draw.rect(self.screen, GREEN, (900, 475, 145, 60), 5)
                pygame.display.update()
            if (self.war3.placed == True):
                pygame.draw.rect(self.screen, GREEN, (1055, 475, 145, 60), 5)
                pygame.display.update()
            if (self.war4.placed == True):
                pygame.draw.rect(self.screen, GREEN, (978, 545, 145, 60), 5)
                pygame.display.update()