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 __init__(self, position, velocity, angle, ttl=SPECIAL_LIFE_TIME): """ Creates a torpedo class instance :param position: position vector (x,y) :param velocity: velocity vector (v_x, v_y) :param angle: angle in degrees :return: """ Torpedo.__init__(self, position, velocity, angle, ttl)
def change_torpedo_live(self, torpedo: Torpedo): """this function update the lives of torpedo that has been shoot from the ship. as starting the live of torpedo are 200 and each step he lose one 'live'. if torpedo lose lives the function remove him!""" if torpedo.get_lives() == TORPEDO_OUT_OF_LIFE: self._screen.unregister_torpedo(torpedo) self.__torpedos.remove(torpedo) else: torpedo.set_lives(torpedo.get_lives() - ONE_UNIT_LESS)
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 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 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 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 __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 register_torpedo(self): ''' :return: Generates objects with Class type Torpedo and draws them to the screen ''' if self._screen.is_space_pressed() and len(self._torpedo_list) <= MAX_TORPEDOS: temp_x_speed, temp_y_speed = self._ship.get_speed() new_x_speed = temp_x_speed + ACC_FACTOR * math.cos(math.radians(self._ship.get_heading())) new_y_speed = temp_y_speed + ACC_FACTOR * math.sin(math.radians(self._ship.get_heading())) speed = new_x_speed, new_y_speed temp_torpedo = Torpedo(self._ship.get_location(), speed, self._ship.get_heading(), TORPEDO_RADIUS) if (id(temp_torpedo) not in TORPEDO_LIST_ID): TORPEDO_LIST_ID.append(temp_torpedo) self._screen.register_torpedo(temp_torpedo) x, y = temp_torpedo.get_location() self._screen.draw_torpedo(temp_torpedo, x, y, temp_torpedo.get_heading()) self._torpedo_list.append(temp_torpedo)
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 _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 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 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 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 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 __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 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_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): """ 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 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 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 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 __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 split_asteroid(self, asteroid: Asteroid, torpedo: Torpedo): """if torpedo hit asteroid (in size more then 1) the function split the asteroid to two new smaller asteroids. the new asteroid move in opposite directions and the size of them is one unit less (from the original asteroid). the function remove the original asteroid and the torpedo that hit him!""" # the formula given in ex.pdf sum_speed_x = torpedo.get_speed_x() + asteroid.get_speed_x() sum_speed_y = torpedo.get_speed_y() + asteroid.get_speed_y() current_x_and_y_speed = math.pow(asteroid.get_speed_x(), 2) \ + math.pow(asteroid.get_speed_y(), 2) for i in range(2): # create two new asteroid (split the original asteroid) new_asteroid = Asteroid(asteroid.get_size() - ONE_UNIT_LESS) new_asteroid.set_new_speed_x(sum_speed_x / math.sqrt(current_x_and_y_speed)) new_asteroid.set_new_speed_y(sum_speed_y / math.sqrt(current_x_and_y_speed)) new_asteroid.set_new_pos_in_x(asteroid.get_pos_x()) new_asteroid.set_new_pos_in_y(asteroid.get_pos_y()) # the size of the new asteroids is smaller by one unit self._screen.register_asteroid(new_asteroid, asteroid.get_size() - ONE_UNIT_LESS) self.__asteroids.append(new_asteroid) if i == 1: # change the movement of one of the asteroids to the # opposite direction new_asteroid.set_new_speed_x(new_asteroid.get_speed_x() * OPPOSITE) new_asteroid.set_new_speed_x(new_asteroid.get_speed_y() * OPPOSITE) # remove the original asteroid after we split him self._screen.unregister_asteroid(asteroid) self.__asteroids.remove(asteroid) # remove the torpedo after he hit the asteroid self._screen.unregister_torpedo(torpedo) self.__torpedos.remove(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)