def fire_event(self, ref_att, ref_vic):
     """ Takes in the names of an attacker and a victim for the fire event """
     if isinstance(ref_att, str):
         attacker = ref_att
         victim = ref_vic
     else:
         attacker = ref_att.id
         victim = ref_vic.id
     att_node = self.battlescene.nodes[attacker]
     vic_node = self.battlescene.nodes[victim]
     weapontype = att_node.getAttachedObject(
         attacker).getUserObject().battle_entity.weapontype
     self.log_event("%s fired at %s" % (attacker, victim))
     if weapontype == "torpedo":
         if not self.torpedo:
             self.torpedo = torpedo.Torpedo(self.sceneManager)
         self.torpedo.fire(self.battlescene.nodes[attacker],
                           self.battlescene.nodes[victim])
         self.post_event = self.torpedo.clear
     else:
         if not self.laser:
             self.laser = laser.LaserManager(
                 self.sceneManager, "Laser/Laser/Solid"
             )  # Laser/Laser/PNG exists too, but I haven't been able to get it to work
         self.laser.fire(self.battlescene.nodes[attacker],
                         self.battlescene.nodes[victim])
         self.post_event = self.laser.clear
Esempio n. 2
0
 def newTorpedo(velocity, player):
     if len(torpedoPool) == 0:
         return torpedo.Torpedo(velocity,player,layers)
     else:
         t = torpedoPool.pop()
         t.reinit(velocity,player)
         return t
Esempio n. 3
0
 def fire_new_torp(self):
     torpedo_speed = get_torpedo_speed(self.__main_ship)
     torpedo_location = self.__main_ship.get_location()
     new_torpedo = torpedo.Torpedo(torpedo_location[X_AXIS],
                                   torpedo_speed[X_AXIS],
                                   torpedo_location[Y_AXIS],
                                   torpedo_speed[Y_AXIS],
                                   self.__main_ship.get_direction())
     self.__torpedos_list.append(new_torpedo)
     self.__screen.register_torpedo(new_torpedo)
Esempio n. 4
0
 def initialize_torpedo(self):
     """ function to create a torpedo with the right information and taking
       care of it's appearance on the screen by calling the functions to
      do so"""
     torpedo1 = torpedo.Torpedo(self.__ship1.get_heading(),
                                self.__ship1.get_cords()[0],
                                self.__ship1.get_cords()[1],
                                self.__ship1.get_speed()[0],
                                self.__ship1.get_speed()[1])
     if self.__torpedo_count <= MAX_TORPEDO:
         self._screen.register_torpedo(torpedo1)
         self.add_to_obj_list(torpedo1)
         self.__torpedo_count += 1
Esempio n. 5
0
 def add_torpedo(self):
     """
     Creates a new torpedo with the ship's position as it's initial one,
     uses the ship's current heading and speed as it's own constants.
     Adds it to the torpedoes list if the list is not full
     (there are no more than 15 torpedoes in the game at the same time)
     and registers it so it would appear on the sceen
     """
     pos = self.ship.get_pos()
     heading = self.ship.get_heading()
     speed = self.ship.get_speed()
     if len(self.torpedoes) < 15:
         new_torpedo = torpedo.Torpedo(pos, speed, heading)
         self.torpedoes.append(new_torpedo)
         self._screen.register_torpedo(new_torpedo)
Esempio n. 6
0
 def create_torpedo(self):
     """
     This function will create a torpedo using the Torpedo
     class constructor
     """
     # Setting torpedo velocity on both axis by given formula.
     torpedo_velocity_x = self.velocity_x + TORPEDO_ACCELERATOR_FACTOR \
                                 * math.cos(self.get_ship_heading_rad())
     torpedo_velocity_y = self.velocity_y + TORPEDO_ACCELERATOR_FACTOR \
                                 * math.sin(self.get_ship_heading_rad())
     # Creating a new torpedo object with current ship position
     #  and heading
     # direction, with new velocity.
     return torpedo.Torpedo(self.pos_x, self.pos_y, torpedo_velocity_x,
                            torpedo_velocity_y, self.heading)
Esempio n. 7
0
    def push_the_button(self):
        """ checks if the user pushed any buttons and
        responds accordingly.
        """
        # update ship status according to user's steps:
        if self._screen.is_left_pressed():
            self._ship.change_direction("left")

        if self._screen.is_right_pressed():
            self._ship.change_direction("right")

        if self._screen.is_up_pressed():
            self._ship.accelerate()

        if self._screen.is_space_pressed():
            if len(self.torpedo_list) < MAX_TORPEDO_NUMBER:
                a_torpedo = torpedo.Torpedo(self._ship)
                self._screen.register_torpedo(a_torpedo)
                self.torpedo_list.append(a_torpedo)
            else:
                pass
Esempio n. 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()
    def _game_loop(self):
        """
        This function receives no external parameters.
        Checks whether the game ended.
        If not, the function will go over one more  game loop, moving all the
        objects on the board and checking for encounters between asteroids 
        and any other objects.
        :return: None
        """
        hit_asteroids = []
        intact_asteroids = []
        hit_torpedos = []
        intact_torpedos = []
        # checking whether the game should end
        if self._screen.should_end():
            print(QUIT_MSG)
            self.exit_game()
        if self.ship.get_life() == 0:
            print(LOSER_MSG)
            self.exit_game()
        if not self.asteroids:
            print(WIN_MSG)
            self.exit_game()

        self._screen.draw_ship(self.ship.get_x_pos(), self.ship.get_y_pos(),
                               self.ship.get_direction())
        # checking for user input
        if self._screen.is_left_pressed():
            self.ship.turn(TURN_LEFT)
        elif self._screen.is_right_pressed():
            self.ship.turn(TURN_RIGHT)
        elif self._screen.is_up_pressed():
            self.ship.accelerate()
        elif self._screen.is_space_pressed() and \
                        len(self.torpedos) < MAX_TORPEDOS:
            self.torpedos.append(torpedo.Torpedo(self.ship))
            self._screen.register_torpedo(
                self.torpedos[NEW_TORP_PLACE_IN_LIST])
        # start moving objects
        self.ship.move()

        for torp in self.torpedos:
            self._screen.draw_torpedo(torp, torp.get_x_pos(), torp.get_y_pos(),
                                      torp.get_direction())
            torp.move()
            # checking for torpedos that are outdated
            if torp.get_life_time() == 0:
                self._screen.unregister_torpedo(torp)
                hit_torpedos.append(torp)
        # removing outdated torpedos
        for torp in self.torpedos:
            if torp not in hit_torpedos:
                intact_torpedos.append(torp)
        self.torpedos = intact_torpedos
        intact_torpedos = []

        for asteroid in self.asteroids:
            self._screen.draw_asteroid(asteroid, asteroid.get_x_pos(),
                                       asteroid.get_y_pos())
            asteroid.move()
            # checking for encounters with ship
            if asteroid.has_intersection(self.ship):
                self.ship.reduce_life()
                self._screen.remove_life()
                self._screen.show_message(HIT_TITLE,
                                          HIT_MESSAGE % self.ship.get_life())
                self._screen.unregister_asteroid(asteroid)
                hit_asteroids.append(asteroid)
            else:
                for torp in self.torpedos:
                    # checking for encounters with torpedos
                    if asteroid.has_intersection(torp):
                        # adding points and removing\splitting asteroids
                        if asteroid.get_size() == BIG_ASTEROID:
                            self.score += BIG_POINTS
                            new_astr1, new_astr2 = self.split_asteroid(
                                asteroid, torp)
                            intact_asteroids.append(new_astr1)
                            intact_asteroids.append(new_astr2)
                        elif asteroid.get_size() == MED_ASTEROID:
                            self.score += MED_POINTS
                            new_astr1, new_astr2 = self.split_asteroid(
                                asteroid, torp)
                            intact_asteroids.append(new_astr1)
                            intact_asteroids.append(new_astr2)
                        elif asteroid.get_size() == SMALL_ASTEROID:
                            self.score += SMALL_POINTS
                            if asteroid.is_register():
                                self._screen.unregister_asteroid(asteroid)
                                asteroid.un_register()
                        self._screen.set_score(self.score)
                        hit_asteroids.append(asteroid)
                        hit_torpedos.append(torp)
                        self._screen.unregister_torpedo(torp)
                # removing torpedos which hit asteroids
                for torp in self.torpedos:
                    if torp not in hit_torpedos:
                        intact_torpedos.append(torp)
                self.torpedos = intact_torpedos
                intact_torpedos = []
        # removing demolished asteroids
        for asteroid in self.asteroids:
            if asteroid not in hit_asteroids:
                intact_asteroids.append(asteroid)
        self.asteroids = intact_asteroids