def keys_press(self): """ this method will check if any action keys we're pressed and will assign the right action to it. is_left_pressed():moving the ship to the left using it's private method is_right_pressed(): - " - right using it's private method is_up_pressed(): accelerating the ship self._screen.is_space_pressed(): SHOOTING TORPEDOES! """ if self._screen.is_left_pressed(): self.ship.turn_ship_left() elif self._screen.is_right_pressed(): self.ship.turn_ship_right() elif self._screen.is_up_pressed(): self.ship.acceleration() elif self._screen.is_space_pressed(): # here we shoot TORPEDOES # checking if the ship is out of torpedoes by using the # torpedoes list length (MAX_TORPEDOS = 15) if len(self.torpedos) < MAX_TORPEDOS: # creating a new torpedo using the ship coordinates and heading new_torpedo = Torpedo(self.ship.x_coord, self.ship.y_coord, self.ship.degrees) # generating the torpedoes speed using the ship's speed new_torpedo._speed(self.ship.x_speed, self.ship.y_speed) # showing the torpedo on screen using the GUI and appending # it to our list self._screen.register_torpedo(new_torpedo) self.torpedos.append(new_torpedo)
def fire_torpedo(self): if not self.torpedo.sprite: torpedo = Torpedo(self.rect.centerx,self.rect.y) del self.powerups[Torpedo.name] self.torpedo.sprite = torpedo self.has_torpedo = False
def add_trop(self): location = self.__x_wing.get_location() speed = self.__x_wing.get_speed() angle = self.__x_wing.get_angle() new_torp = Torpedo(location, speed, angle, TORPEDO_SPAN) self.__torpedoes.add(new_torp) self.__screen.register_torpedo(new_torp)
def launch_torpedo(self): """ Launches torpedo from current position of the space ship. :return: """ # Get launch parameters from current # position and angle of the ship position = self.__ship.get_position() velocity = self.__ship.get_velocity() angle = self.__ship.get_angle() # Calculate velocity in both axis # accordingly v_x = velocity[0] + 2 * math.cos( angle * math.pi / 180 ) v_y = velocity[1] + 2 * math.sin( angle * math.pi / 180 ) # Add torpedo to draw list torpedo = Torpedo(position, [v_x, v_y], angle) self.__torpedos.append(torpedo) # Make screen aware of torpedo self.__screen.register_torpedo(torpedo)
def user_action(self): """ This function does different actions depending on the request of the user """ if self._screen.is_left_pressed(): self.ship.set_heading(self.ship.get_heading() + TURN_LEFT) if self._screen.is_right_pressed(): self.ship.set_heading(self.ship.get_heading() + TURN_RIGHT) if self._screen.is_up_pressed(): self.ship.set_speed_x( self.ship.get_speed_x() + math.cos(math.radians(self.ship.get_heading()))) self.ship.set_speed_y( self.ship.get_speed_y() + math.sin(math.radians(self.ship.get_heading()))) if self._screen.is_space_pressed(): # Launching a torpedo if len(self.torpedos_list) < MAX_TORPEDO: torpedo_speed_x = self.ship.get_speed_x() + \ Torpedo.SPEED_FACTOR * \ math.cos(math.radians( self.ship.get_heading())) torpedo_speed_y = self.ship.get_speed_y() + \ Torpedo.SPEED_FACTOR * \ math.sin(math.radians( self.ship.get_heading())) # Create the new torpedo torpedo = Torpedo(self.ship.get_x(), self.ship.get_y(), torpedo_speed_x, torpedo_speed_y, self.ship.get_heading()) self._screen.register_torpedo(torpedo) self.torpedos_list.append(torpedo)
def move_torpedo(self): """ moves the torpedo in the direction it was fire and update the game score when we hit an asteroid """ if self._screen.is_space_pressed() and len(self.torpedo_lst) <= 15: torpedo = Torpedo(self.ship) torpedo.set_dx() torpedo.set_dy() self._screen.register_torpedo(torpedo) self.torpedo_lst.append(torpedo) dead_torpedo_lst = [] for i, torpedo in enumerate(self.torpedo_lst): if torpedo.get_life() > 0: self.move(torpedo) self._screen.draw_torpedo(torpedo, torpedo.get_lx(), torpedo.get_ly(), torpedo.get_heading()) torpedo.life_update() else: dead_torpedo_lst.append(i) for j, ast in enumerate(self.asteroid_lst): if ast.has_intersection(torpedo): self.score_update(ast.get_size()) self.torpedo_hit(ast, torpedo, i, j) dead_torpedo_lst.append(i) new_torpedo_list = [] for i in range(len(self.torpedo_lst)): if i not in dead_torpedo_lst: new_torpedo_list.append(self.torpedo_lst[i]) if i in dead_torpedo_lst: self._screen.unregister_torpedo(self.torpedo_lst[i]) self.torpedo_lst = new_torpedo_list
def fire_torpedo(self, screen): """Fires a new torpedo.""" # There's a set max number of torpedoes we can have out at a time. if len(self._torpedoes) < MAX_TORPEDOES: torpedo = Torpedo(self._location, self.calc_torpedo_speed(), self._heading) self._torpedoes.append(torpedo) screen.register_torpedo(torpedo)
def __try_generate_torpedo(self): """ generate new torpedo object if space was pressed, only if max capacity hasn't filled yet. """ if self.__screen.is_space_pressed() and (len(self.__torpedos) <= \ GameRunner.MAX_TORPEDO_NUM): self.__torpedos.append(Torpedo(self.__screen, self.__ship))
def add_torpedo_to_game(self): """This function adds torpedoes to the game.""" if len(self.__torpedo_list) < MAX_POSSIBLE_TORPEDO: ship_direction = self.__ship.get_direction() ship_location = self.__ship.get_location() torpedo_speed = self.calculate_torpedo_speed() torpedo = (Torpedo(ship_location, ship_direction, torpedo_speed)) self._screen.register_torpedo(torpedo) self.__torpedo_list.append(torpedo)
def new_torpedo(self): """this function create a new torpedo (only if there is not more then 15 torpedoes in the game!) to the game by using class Torpedo. the function register and add the new torpedo to the torpedoes list.""" if len(self.__torpedos) < TORPEDOS_MAX_NUMBER: torpedo = Torpedo(self.__ship) self._screen.register_torpedo(torpedo) self.__torpedos.append(torpedo)
def create_torpedo(self): """create the torpedo and register it""" if len(self.__torpedo_lst) < MAX_TORPEDO: torpedo = Torpedo(self.__ship.get_x(), self.__ship.get_y(), self.__ship.get_degrees(), self.__ship.get_speedx(), self.__ship.get_speedy()) self.__screen.register_torpedo(torpedo) self.__torpedo_lst.append(torpedo)
def fire_torpedo(self): """ makes a torpedo at the tip of the ship (meaning it has the ship speed,position and heading) :return: """ torpedo = Torpedo(*self.get_position(), *self.get_speed(), self.get_heading()) return torpedo
def _launch_torpedo(self, special): """ creates an torpedo and register it. appends the new torpedo to the total list of the torpedoes. """ new_torpedo = Torpedo(current_ship=self.__current_ship, special=special) self.__screen.register_torpedo(torpedo=new_torpedo) self.__torpedo_list.append(new_torpedo)
def create_torpedo(self): torp = Torpedo(self.__ship_dict["ship"].get_location(X), self.__ship_dict["ship"].get_location(Y), self.__ship_dict["ship"].get_speed(X), self.__ship_dict["ship"].get_speed(Y), self.__ship_dict["ship"].get_heading()) self.__screen.register_torpedo(torp) self.__screen.draw_torpedo(torp, torp.get_location(X), torp.get_location(Y), torp.get_heading()) self.__torpedo_dict[self.__torpedo_counter] = torp
def __shoot_torpedo(self): if len(self.__torpedos_lst) < DEFAULT_TORPEDOS_NUM: if self.__screen.is_space_pressed(): tor_speed_x, tor_speed_y = calc_speed_of_tor_from_obj( self.__ship, 2) torpedo = Torpedo(self.__ship.get_loc_x(), self.__ship.get_loc_y(), tor_speed_x, tor_speed_y, self.__ship.get_direction()) self.__torpedos_lst.append(torpedo) self.__screen.register_torpedo(torpedo)
def __add_torpedo(self): """The function handles the creation of torpedo throughout the game""" if len(self.__torpedo_list) < TORPEDO_LIMIT: # Creates every time a new torpedo object torpedo = Torpedo( (self.__ship.get_velocity()[0], self.__ship.get_velocity()[1]), (self.__ship.get_location()[0], self.__ship.get_location()[1]), self.__ship.get_heading(), 0) # there can be only 10 torpedoes on screen at the same time self.__shoot_torpedo(torpedo) # Process the shootout of the # torpedo self.__torpedo_list.append(torpedo) # To make sure there
def add_torpedo(self): """ initial heading and location are those of the ship, and initial velocity as described. """ x_coordinate = self.__screen_ship.get_x_coordinate() y_coordinate = self.__screen_ship.get_y_coordinate() heading = self.__screen_ship.get_heading_in_radians() new_x_velocity = self.__screen_ship.get_x_velocity() + ACCELERATION_COEFFICIENT * math.cos(heading) new_y_velocity = self.__screen_ship.get_y_velocity() + ACCELERATION_COEFFICIENT * math.sin(heading) new_torpedo = Torpedo(x_coordinate, y_coordinate, new_x_velocity, new_y_velocity, heading) self.__screen.register_torpedo(new_torpedo) self.__screen_torpedoes.append(new_torpedo)
def create_torpedo(self, ship, x, y, x_speed, y_speed, heading): """ if user pressed space, a torpedo is added to the game (unless max torpedo was reached). """ if ship.get_torpedo_count() == self.MAX_TORPEDOS: return torpedo = Torpedo(x, y, x_speed, y_speed, heading) self.__torpedo_list.append(torpedo) self.__screen.register_torpedo(torpedo) ship.change_torpedos(self.TORPEDO_SHOT) x1, y1, x_speed1, y_speed1, heading1 = self._obj_prams(torpedo) self.__screen.draw_torpedo(torpedo, x1, y1, heading1)
def _special_shoot(self): """ The main function of the special shoot, create it and Responsible to the changes of the special shoot in each loop """ if self.__screen.is_special_pressed() and \ len(self.special_shoot_list) < 5: torpedo1 = Torpedo(self._special_shoot_speed(0), self.__ship.get_x_y_loc(), self.__ship.get_dir()) torpedo2 = Torpedo(self._special_shoot_speed(-30), self.__ship.get_x_y_loc(), self.__ship.get_dir() - 30) torpedo3 = Torpedo(self._special_shoot_speed(30), self.__ship.get_x_y_loc(), self.__ship.get_dir() + 30) one_shoot = [torpedo1, torpedo2, torpedo3] self.special_shoot_list.append(one_shoot) for torpedo in one_shoot: self.__screen.register_torpedo(torpedo) self._special_shoot_life() self._draw_special_shoot() self._special_shoot_intersection()
def add_special_torpedo(self): # it's a torpedo with an endless lifespan """ the torpedoes velocity remains constant initial heading and location are those of the ship """ x_coordinate = self.__screen_ship.get_x_coordinate() y_coordinate = self.__screen_ship.get_y_coordinate() heading = self.__screen_ship.get_heading_in_radians() new_x_velocity = self.__screen_ship.get_x_velocity() + 2 * math.cos(heading) # 2 is the acceleration coefficient new_y_velocity = self.__screen_ship.get_y_velocity() + 2 * math.sin(heading) new_torpedo = Torpedo(x_coordinate, y_coordinate, new_x_velocity, new_y_velocity, heading) self.__screen_special_torpedoes.append(new_torpedo) self.__screen.register_torpedo(new_torpedo)
def create_torpedo(self): """ Creates a new torpedo if no. of torpedoes is smaller than max value """ if len(self.__torpedoes_list) < MAX_TORPEDO: new_torpedo = Torpedo(self.ship.get_angle(), self.ship.get_pos_x(), self.ship.get_pos_y(), self.ship.get_speed_x(), self.ship.get_speed_y(), self.screen_max_x, self.screen_max_y, self.screen_min_x, self.screen_min_y) self.__torpedoes_list.append(new_torpedo) self._screen.register_torpedo(new_torpedo)
def fire_torpedo(self): ''' Registers a new torpedo and adds to torpedo list of game. Torpedo created receives the position, velocity and direction of ship. ''' pos_vel_x = self.__ship.get_pos_vel(HORZ) pos_vel_y = self.__ship.get_pos_vel(VERT) direc = self.__ship.get_direction() radian = self.__ship.deg_to_rad() new_torpedo = Torpedo(pos_vel_x, pos_vel_y, direc, radian) self.__torpedo_list.append(new_torpedo) self._screen.register_torpedo(new_torpedo)
def shoot(self, ship): space_pressed = self.__screen.is_space_pressed() if space_pressed: ship_dir = math.radians(ship.get_dir()) x_speed = ship.get_speed()[0] + 2 * math.cos(ship_dir) y_speed = ship.get_speed()[1] + 2 * math.sin(ship_dir) torpedo = Torpedo(ship.getx(), ship.gety(), x_speed, y_speed, ship.get_dir()) self.__screen.register_torpedo(torpedo) self.__screen.draw_torpedo(torpedo, ship.getx(), ship.gety(), ship.get_dir()) self.__torpedo_ls.append(torpedo)
def __create_torpedo(self): """ This method creates one torpedo of the game. Here formula for the speed, location and heading are these of the ship in the moment of striking. """ speed_x = self.ship.get_speed_x() + self.ACCELERATION_FACTOR * \ m.cos(m.radians(self.ship.get_heading())) speed_y = self.ship.get_speed_y() + self.ACCELERATION_FACTOR * \ m.sin(m.radians(self.ship.get_heading())) torp = Torpedo(self.ship.get_loc_x(), speed_x, self.ship.get_loc_y(), speed_y, self.ship.get_heading()) self._screen.register_torpedo(torp) self.torp_lst.append(torp)
def _add_torpedo(self): """ this function adds one torpedo object if there are less than 15 torpedos in the game board. it will add it to the torpedo list and register it to the screen. :return: None. """ if len(self.__torpedos) < MAX_TORPEDO_NUM: # if there are less than 15 torpedos at once, add a torpedo. torpedo = Torpedo(self.__ship.get_position(), self.__ship.get_direction(), self.__ship.get_speed()) self._screen.register_torpedo(torpedo) self.__torpedos.append(torpedo)
def may_create_torpedo(self): """ Create new torpedo, according to the user input (if space pressed)""" if self.__screen.is_space_pressed() and \ len(self.torpedo_list) < MAX_TORPEDO: torpedo = Torpedo(self._torpedo_speed(), self.__ship.get_x_y_loc(), self.__ship.get_dir()) self.torpedo_list.append(torpedo) self.__screen.register_torpedo(torpedo) self.__screen.draw_torpedo(torpedo, torpedo.get_x_y_loc()[0], torpedo.get_x_y_loc()[1], torpedo.get_dir())
def shoot(self, ship): """Function is responsible for shooting normal torpedos. Pressing space will initialize a torpedo launch.""" space_pressed = self.__screen.is_space_pressed() if space_pressed and len(self.__torpedo_ls) <= 10: ship_dir = math.radians(ship.get_dir()) x_speed = ship.get_speed()[0] + 2 * math.cos(ship_dir) y_speed = ship.get_speed()[1] + 2 * math.sin(ship_dir) torpedo = Torpedo(ship.getx(), ship.gety(), x_speed, y_speed, ship.get_dir(), 0) self.__screen.register_torpedo(torpedo) self.__screen.draw_torpedo(torpedo, ship.getx(), ship.gety(), ship.get_dir()) self.__torpedo_ls.append(torpedo)
def create_torpedo(self): """ creates new torpedo in game according to conditions """ if self.__screen.is_space_pressed(): if len(self.__torpedos) < 10: speed_x = self.__ship.get_speed()[0] + 2 * math.cos( math.radians(self.__ship.get_heading())) speed_y = self.__ship.get_speed()[1] + 2 * math.sin( math.radians(self.__ship.get_heading())) torpedo = Torpedo(self.__ship.get_coor()[0], self.__ship.get_coor()[1], speed_x, speed_y, self.__ship.get_heading()) self.__screen.register_torpedo(torpedo) self.__torpedos.append(torpedo)
def __init__(self, asteroids_amnt): self._screen = Screen() self._ship = Ship() self.screen_max_x = Screen.SCREEN_MAX_X self.screen_max_y = Screen.SCREEN_MAX_Y self.screen_min_x = Screen.SCREEN_MIN_X self.screen_min_y = Screen.SCREEN_MIN_Y self._asteroids = list() self._torpedoes = list() for i in range(asteroids_amnt): asteroid = Asteroid() self._screen.register_asteroid(asteroid, asteroid.get_size()) self._asteroids.append(asteroid) for i in range(TORPEDO_AMNT): self._torpedoes.append(Torpedo(self._ship))
def add_torpedo(self): """ this function returns a new torpedo object, with attributes depending on the ship current location, speed and heading. :return: torpedo type object """ x_speed = self.ship.x_speed + 2 * math.cos( math.radians(self.ship.heading)) y_speed = self.ship.y_speed + 2 * math.sin( math.radians(self.ship.heading)) x_location = self.ship.x_location y_location = self.ship.y_location heading = self.ship.heading new_torpedo = Torpedo(x_location, x_speed, y_location, y_speed, heading) return new_torpedo