def divide_asteroid(self, asteroid, torpedo): """ The following is responsible for dividing an astroid into 2 astroid, it Generates 2 new astroid in a special speed, in addition, if it was called with astroid with size 1 - it's returning, nothing to divide. :param asteroid: a given astroid :param torpedo: The torpedo that hitted the astroid :return: None """ if asteroid.get_size() == 1: return new_x = asteroid.get_position().get_x() new_size = asteroid.get_size() - 1 new_y = asteroid.get_position().get_y() divide_factor = math.sqrt(asteroid.get_speed().get_x_speed()**2 + asteroid.get_speed().get_y_speed()**2) new_speed_x = (torpedo.get_speed().get_x_speed() + asteroid.get_speed().get_x_speed()) / divide_factor new_speed_y = (torpedo.get_speed().get_y_speed() + asteroid.get_speed().get_y_speed()) / divide_factor asteroid_one = Asteroid(asteroid.get_position(), Speed2D(new_speed_y, new_speed_x), new_size) asteroid_two = Asteroid(asteroid.get_position(), Speed2D(-1 * new_speed_y, -1 * new_speed_x), new_size) self.__screen.register_asteroid(asteroid_one, new_size) self.__screen.register_asteroid(asteroid_two, new_size) self.__asteroids.append(asteroid_one) self.__asteroids.append(asteroid_two)
def update(self, delta_time : float) -> int: if random() < Asteroid.spawn_chance * 0.01 or len(self.asteroids) < self.number_asteroids: self.asteroids.append(Asteroid(SCREEN_WIDTH * choice([0, 1]), SCREEN_HEIGHT * choice([0, 1]), Asteroid.largest_radius)) self.player.update(delta_time) for asteroid in self.asteroids: if self.player.check_collision(asteroid): return 1 for bullet in self.player.bullets: if asteroid.check_collision(bullet): if asteroid.radius > Asteroid.smallest_radius: self.asteroids.append(Asteroid(asteroid.position.x, asteroid.position.y, asteroid.radius / 2)) self.asteroids.append(Asteroid(asteroid.position.x, asteroid.position.y, asteroid.radius / 2)) else: self.score += 1 EXPLOSION_SOUND.play() self.asteroids.remove(asteroid) self.player.bullets.remove(bullet) for asteroid in self.asteroids: asteroid.update(delta_time) for bullet in self.player.bullets: bullet.update(delta_time) if bullet.is_off_screen: self.player.bullets.remove(bullet)
def load_level2(app): '''(App) -> NoneType ...''' BLUE = (0, 0, 255) MOON1_ORBIT, MOON1_RADIUS = 1, 15 MOON2_ORBIT, MOON2_RADIUS = 3, 20 ASTEROID1_STARTX, ASTEROID1_STARTY, ASTEROID1_RADIUS = 100, 0, 12 ASTEROID2_STARTX, ASTEROID2_STARTY, ASTEROID2_RADIUS = 780, 800, 12 ASTEROID3_STARTX, ASTEROID3_STARTY, ASTEROID3_RADIUS = 780, 400, 12 PLANET_RADIUS = 50 app.num_orbits = 3 app.planet = Planet(BLUE, app.level_center, PLANET_RADIUS) app.moons.append(Moon(MOON1_ORBIT, MOON1_RADIUS)) app.moons.append(Moon(MOON2_ORBIT, MOON2_RADIUS)) app.asteroids.append( Asteroid(ASTEROID1_STARTX, ASTEROID1_STARTY, ASTEROID1_RADIUS)) app.asteroids.append( Asteroid(ASTEROID2_STARTX, ASTEROID2_STARTY, ASTEROID2_RADIUS)) app.asteroids.append( Asteroid(ASTEROID3_STARTX, ASTEROID3_STARTY, ASTEROID3_RADIUS)) app.asteroids_ingame = app.asteroids.copy()
def asteroid_burst(self, asteroid, torpedo): """This function divide an asteroid to smaller asteroids with opposite velocity and register them""" for i in range(ASTEROID_DIVIDE): # if we can divide the asteroid, it's mane the asteroid size larger # than the smaller size if asteroid.get_size() > SMALL_SIZE: norm = math.sqrt(asteroid.get_velocity_x()**2 + asteroid.get_velocity_y()**2) * (-1)**i new_velocity_x = (torpedo.get_velocity_x() + asteroid.get_velocity_x()) / norm new_velocity_y = (torpedo.get_velocity_y() + asteroid.get_velocity_y()) / norm # the size of the asteroid decrease new_size = asteroid.get_size() - ASTEROID_DIVIDE_SIZE new_asteroid = Asteroid(asteroid.get_position_x(), asteroid.get_position_y(), new_velocity_x, new_velocity_y, new_size) # add the asteroid to the register self.asteroids.append(new_asteroid) self._screen.register_asteroid(new_asteroid, new_asteroid.get_size())
def split_asteroid(self, asteroid, asteroid_speed, torpedo_speed, asteroid_location): """This function splits an asteroid when a torpedo hits it.""" smaller_asteroid_speed_x = self.new_asteroid_x_speed( asteroid_speed, torpedo_speed) smaller_asteroid_speed_y = self.new_asteroid_y_speed( asteroid_speed, torpedo_speed) if asteroid.get_size() == BIG_ASTEROID_SIZE: new_asteroid_size = MEDIUM_ASTEROID_SIZE elif asteroid.get_size() == MEDIUM_ASTEROID_SIZE: new_asteroid_size = SMALL_ASTEROID_SIZE else: new_asteroid_size = ASTEROID_DESTROYED self.delete_asteroid(asteroid) if new_asteroid_size != ASTEROID_DESTROYED: new_asteroid_1 = Asteroid(asteroid_location, (smaller_asteroid_speed_x * OPPOSITE_DIRECTION_ASTEROID_MOVEMENT, smaller_asteroid_speed_y * OPPOSITE_DIRECTION_ASTEROID_MOVEMENT), new_asteroid_size) new_asteroid_2 = Asteroid( asteroid_location, (smaller_asteroid_speed_x, smaller_asteroid_speed_y), new_asteroid_size) self.__asteroids_list.extend([new_asteroid_1, new_asteroid_2]) self._screen.register_asteroid(new_asteroid_1, new_asteroid_size) self._screen.register_asteroid(new_asteroid_2, new_asteroid_size)
def tick(self): self.player.tick() for asteroid in self.asteroids: if not asteroid.tick(): self.asteroids.remove(asteroid) for projectile in self.projectiles: if not projectile.tick(): self.projectiles.remove(projectile) for asteroid in self.asteroids: if self.running: self.running = not asteroid.collision1(self.player) for asteroid in self.asteroids: for projectile in self.projectiles: if asteroid.collision2(projectile): if asteroid.size >= 30: self.score += 1 self.asteroids.append( Asteroid(self, 1, False, Vector2(asteroid.pos), asteroid.size / 2)) self.asteroids.append( Asteroid(self, 1, False, Vector2(asteroid.pos), asteroid.size / 2)) self.asteroids.remove(asteroid) self.projectiles.remove(projectile) print(len(self.asteroids))
def splitting_asteroid(self, torpedo, asteroid): """A function that creates new asteroids after an intersect with a torpedo""" if asteroid.get_size() == self.SIZE3: new_size = self.SIZE2 elif asteroid.get_size() == self.SIZE2: new_size = self.SIZE1 elif asteroid.get_size() == self.SIZE1: self.remove_asteroid(asteroid) self.__screen.unregister_asteroid(asteroid) return new_speed_x1, new_speed_y1 = self.calculate_new_speed_for_split(torpedo, asteroid) new_speed_x2 = new_speed_x1 * self.CHANGE_DIRECTION new_speed_y2 = new_speed_y1 * self.CHANGE_DIRECTION asteroid1 = Asteroid(asteroid.get_x(), asteroid.get_y(), new_speed_x1, new_speed_y1, new_size) asteroid2 = Asteroid(asteroid.get_x(), asteroid.get_y(), new_speed_x2, new_speed_y2, new_size) self.__screen.register_asteroid(asteroid1, new_size) self.__screen.register_asteroid(asteroid2, new_size) self.__screen.draw_asteroid(asteroid1, asteroid.get_x(), asteroid.get_y()) self.__screen.draw_asteroid(asteroid2, asteroid.get_x(), asteroid.get_y()) self.__asteroid_lst.append(asteroid1) self.__asteroid_lst.append(asteroid2) self.remove_asteroid(asteroid) self.__screen.unregister_asteroid(asteroid)
def blow_up_asteroid(self, i, j): '''decide what is going happend after asteroid hit by laser beam!''' if self.asteroids[j].asize == 'Large': self.asteroids.append( Asteroid(self.SPACE, 'Med', self.laser_beams[i].x, self.laser_beams[i].y, self.laser_beams[i].x_vel, -self.laser_beams[i].y_vel, 0, 0.0)) self.asteroids.append( Asteroid(self.SPACE, 'Med', self.laser_beams[i].x, self.laser_beams[i].y, -self.laser_beams[i].x_vel, self.laser_beams[i].y_vel, 0, 0.0)) elif self.asteroids[j].asize == 'Med': self.asteroids.append( Asteroid(self.SPACE, 'Small', self.laser_beams[i].x, self.laser_beams[i].y, self.laser_beams[i].x_vel, -self.laser_beams[i].y_vel, 0, 0.0)) self.asteroids.append( Asteroid(self.SPACE, 'Small', self.laser_beams[i].x, self.laser_beams[i].y, -self.laser_beams[i].x_vel, self.laser_beams[i].y_vel, 0, 0.0)) self.asteroids.append( Asteroid(self.SPACE, 'Small', self.laser_beams[i].x, self.laser_beams[i].y, self.laser_beams[i].x_vel, self.laser_beams[i].y_vel, 0, 0.0)) elif self.asteroids[j].asize == 'Small': self.asteroids[j].asize = ' ' self.asteroids.remove(self.asteroids[j]) self.asteroid_blow_up = True if len(self.asteroids) == 0: self.asteroid_destroyed = True
def test_constructor(): SPACE = {'w': 100, 'h': 200} # Test minimal required constructor args a = Asteroid(SPACE) assert a.SPACE['w'] == 100 and \ a.SPACE['h'] == 200 and \ hasattr(a, "x") and \ hasattr(a, "y") and \ hasattr(a, "x_vel") and \ hasattr(a, "y_vel") and \ hasattr(a, "rotation") and \ hasattr(a, "rot_vel") and \ a.asize == 'Large' # Test with optional size value b = Asteroid(SPACE, 'Small') assert b.asize == 'Small' # Test with insufficient arguments try: c = Asteroid() except TypeError: failedWithTypeError = True assert failedWithTypeError
def asteroid_split(self, asteroid, torpedo): """ this method checks whether the size of the asteroid is more than 1, and if it is it will split it into 2 smaller asteroids :param asteroid: an Asteroid object :param torpedo: a torpedo object :return: None """ if asteroid.size == 1 and asteroid in self.asteroids: self.asteroids.remove(asteroid) self.__screen.unregister_asteroid(asteroid) else: new_speed = self.calculate_new_speed(asteroid, torpedo) x_speed = new_speed[0] y_speed = new_speed[1] if asteroid.size == 2 or asteroid.size == 3: asteroid_1 = Asteroid(asteroid.x_location, x_speed, asteroid.y_location, -y_speed, asteroid.size - 1) asteroid_2 = Asteroid(asteroid.x_location, -x_speed, asteroid.y_location, y_speed, asteroid.size - 1) self.add_asteroid(asteroid_1) self.add_asteroid(asteroid_2) self.__screen.unregister_asteroid(asteroid) self.asteroids.remove(asteroid) self.__screen.unregister_torpedo(torpedo) self.torpedoes.remove(torpedo)
def __init__(self, asteroids_amnt): """ Constructor :param asteroids_amnt: The amount of asteroids the game will start with """ self.__screen = Screen() # every game has one screen object he works with 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.__ship = Ship(self.__screen, self.get_rand_location()) # every game contains only one ship object # initiation of asteroids objects self.__asteroids = [] for asteroid in range(asteroids_amnt): while True: # this loop makes sure that the asteroid is not generated # at the current location of the ship asteroid = Asteroid(self.__screen, self.get_rand_location(), GameRunner.START_AST_SIZE) if not asteroid.has_intersection(self.__ship): break self.__asteroids.append(asteroid) self.__torpedos = [] self.__score = 0
def __generate_asteroid(self): """This method generate a random asteroid. the method gets nothing and returns an asteroid.""" x = random.randrange(self.__screen_min_x, self.__screen_max_x) y = random.randrange(self.__screen_min_y, self.__screen_max_y) if x != self.__ship.get_location_x( ) and y != self.__ship.get_location_y(): speed_x = random.randrange(1, 5) * random.choice( [-1, 1]) # random speed between -4 to 4, zero not included speed_y = random.randrange(1, 5) * random.choice( [-1, 1]) # random speed between -4 to 4, zero not included ast = Asteroid(x, y, speed_x, speed_y) else: while x == self.__ship.get_location_x( ) and y == self.__ship.get_location_y(): x = random.randrange(self.__screen_min_x, self.__screen_max_x) y = random.randrange(self.__screen_min_y, self.__screen_max_y) if x != self.__ship.get_location_x( ) and y != self.__ship.get_location_y(): speed_x = random.randrange(1, 5) * random.choice([ -1, 1 ]) # random speed between -4 to 4, zero not included speed_y = random.randrange(1, 5) * random.choice([ -1, 1 ]) # random speed between -4 to 4, zero not included ast = Asteroid(x, y, speed_x, speed_y) return ast
def asteroids_maker(self, asteroids_amnt): """ this function will create the asteroids for us. the function runs in range of given amount of asteroids the user wants and creates an asteroid that will be in a different random position than the ship's random position, a different speed from the ship's speed and a default size. asteroids_amnt DEFAULT is 5 AST_INIT_SIZE is 3 (asteroid initializing size) """ for asteroid_num in range(asteroids_amnt): new_ast = Asteroid() # creating a new asteroid object # this makes sure the position of the asteroid is different than # the ship's position and that their speed is also different while (new_ast.x_coord == self.ship.x_coord and new_ast.y_coord == self.ship.y_coord) \ or (new_ast.x_speed == self.ship.x_speed and new_ast.y_speed == self.ship.y_speed): # initializing a new coordinate for the new asteroid(see # Asteroid class) new_ast._init_random_coord() self.asteroids.append(new_ast) # apeending to our asteroid list # registering it to the GUI so it'll be displayed self._screen.register_asteroid(new_ast, AST_INIT_SIZE)
def __init__(self, num): self._ast_list = [] for i in range(num): # Asteroid def __init__(self, radius, position, velocity, timestamp) temp = Asteroid(Asteroid.rand_radius(), Asteroid.rand_pos(), Asteroid.rand_velocity(), time.time()) self._ast_list.append(temp)
def __split_asteroid(self, asteroid): """The function handles the split of an asteroid upon hit""" location = asteroid.get_location() # T velocity = asteroid.get_velocity() new_size = asteroid.get_size() - 1 if asteroid.get_size() > 1: # As long as the size is greater than 1 if asteroid.get_size() == 3: # Case for asteroid of size 3 (MAX) self.__points += 20 # adds 20 points self.__screen.set_score(self.__points) # points to screen elif asteroid.get_size() == 2: # Case for asteroid of size 2 self.__points += 50 # adds 50 points self.__screen.set_score(self.__points) # points to screen # Here we begin the process of splitting the asteroid self.__screen.unregister_asteroid(asteroid) self.__asteroid_list.remove(asteroid) new_asteroid1 = Asteroid(location, velocity, new_size) negative_velocity_tup = ((-1) * velocity[0], (-1) * velocity[1]) new_asteroid2 = Asteroid(location, negative_velocity_tup, new_size) self.__asteroid_list.append(new_asteroid1) self.__asteroid_list.append(new_asteroid2) self.__screen.register_asteroid(new_asteroid1, new_asteroid1.get_size()) self.__screen.register_asteroid(new_asteroid2, new_asteroid1.get_size()) elif asteroid.get_size() == 1: # Case for asteroid of size 1 self.__points += 100 # adds 100 points self.__screen.set_score(self.__points) # Update the screen # Remove the asteroid as the Radius of the Asteroid is less than 1 self.__screen.unregister_asteroid(asteroid) self.__asteroid_list.remove(asteroid)
def reset_game(self): """Initialise start of game state (reset ship position, score, and asteroids).""" self.ship.reset() Asteroid.initiate_game() self.death = False self.spawn_speed = constants.INITIAL_SPAWN_FREQUENCY self.next_spawn = pyxel.frame_count + self.spawn_speed
def _split_asteroid(self, asteroid, asteroid_hit_speed, new_asteroids_pos, torpedo_speed): """ this function creates two asteroids from the one that got hit by a torpedo. the new speed will be calculated according to a formula, and in an opposite directions. the new position will be the old asteroid's position. the size of them will be reduced by one size from the old asteroid. the function will then add the asteroids to the game. :param asteroid: asteroid object that was hit by torpedo (asteroid). :param asteroid_hit_speed: the current speed of asteroid (tuple). :param new_asteroids_pos: the position of the new asteroids (tuple). :param torpedo_speed: the current speed of torpedo (tuple). :return: None. """ new_x_speed = self._asteroid_new_axis_speed(X_AXIS, asteroid_hit_speed, torpedo_speed) new_y_speed = self._asteroid_new_axis_speed(Y_AXIS, asteroid_hit_speed, torpedo_speed) if asteroid.get_size() == Asteroid.BIGGEST_SIZE_ASTEROID: new_asteroids_size = Asteroid.MEDIUM_SIZE_ASTEROID # asteroid size is decreased to medium. else: # if the size of the asteroid is medium. new_asteroids_size = Asteroid.SMALLEST_SIZE_ASTEROID # asteroid size is decreased to small. asteroid_1 = Asteroid(new_asteroids_pos, (new_x_speed, new_y_speed), new_asteroids_size) asteroid_2 = Asteroid(new_asteroids_pos, (OPPOSITE_DIRECTION * new_x_speed, OPPOSITE_DIRECTION * new_y_speed), new_asteroids_size) # create two asteroids with the new size, same position as the # old asteroid, and opposite speed. add them to the game. self.__asteroids.extend([asteroid_1, asteroid_2]) self._screen.register_asteroid(asteroid_1, new_asteroids_size) self._screen.register_asteroid(asteroid_2, new_asteroids_size)
def pooled_process_velocity(args): """ Evaluate wander based on input list of initial velocity values. :param args: (list) List containing initial conditions of the form (v_x, v_y, r_lagrange_point). Each item is to be evaluated and have its wander calculated. :return: (list) List of calculated ranges of wander in the same order as the input list. """ # Iterate through each item in args, calculate wander and append to r_max_list r_max_list = [] for item in args: # Save start time of function time_start = time() # Unpack item and set initial conditions x, y, r_lagrange_point = item r_a_initial = [r_lagrange_point[0], r_lagrange_point[1], 0] v_a_initial = [x, y, 0] # Simulate asteroid for 100 orbits asteroid = Asteroid(r_a_initial, v_a_initial) t, r_a, v_a = asteroid.solve_orbit(100) # Calculate r_max, the maximum distance of the asteroid from the Lagrange point r_max = np.amax(np.sqrt((r_a[0] - r_lagrange_point[0]) ** 2 + (r_a[1] - r_lagrange_point[1]) ** 2)) r_max_list.append(r_max) # Output runtime for each loop to provide feedback during long calculation print(f"Took {time()-time_start}s") return r_max_list
def plot_orbit(self, r_a_initial, v_a_initial, n_orbits=100): """ Plotting routine for a standard, two picture, orbital plot. :param r_a_initial: (list) Initial position vector of the asteroid. :param v_a_initial: (list) Initial velocity vector of the asteroid :param n_orbits: (int) Number of orbits of Jupiter to plot. :return: None """ # Define asteroid and simulate its trajectory for n_orbits orbits of Jupiter asteroid = Asteroid(r_a_initial, v_a_initial) t, r_a, v_a = asteroid.solve_orbit(n_orbits) # Plot overview of orbit (axis 1) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=[3.0 * 4, 2.8 * 2]) ax1.set_xlim((-7, 7)) ax1.set_ylim((-7, 7)) ax1.plot(r_a[0], r_a[1]) self.plot_extras(ax1) ax1.set_xlabel("x /AU") ax1.set_ylabel("y /AU") # Plot zoomed in view of asteroid orbit (axis 2) ax2.plot(r_a[0], r_a[1]) ax2.set_xlabel("x /AU") ax2.set_ylabel("y /AU") # Plot starting position on axis 2 ax2.plot(r_a[0][0], r_a[1][0], 'r+') # Save figure plt.savefig("fig.png")
def create_asteroids(self, amount, asteroid=None, new_size=None, torpedo=None): """ first, init asteroids and draw them on screen. if asteroid was sent, func splits it into 2 new asteroids and draw them on screen (orig asteroid is deleted). """ if asteroid: # split given asteroid x_old, y_old, x_speed_old, y_speed_old, size_old = self._obj_prams( asteroid) x_speed_new1, y_speed_new1, x_speed_new2, y_speed_new2 = self.new_asteroid_speed( x_speed_old, y_speed_old, torpedo) asteroid1 = Asteroid(x_old, y_old, x_speed_new1, y_speed_new1, new_size) asteroid2 = Asteroid(x_old, y_old, x_speed_new2, y_speed_new2, new_size) self.update_game_asteroids(asteroid, asteroid1, asteroid2) self.draw_asteroid(asteroid1, new_size, x_old, y_old) self.draw_asteroid(asteroid2, new_size, x_old, y_old) # init asteroids for i in range(amount): x, y = self._random_coor() x_speed, y_speed = self._random_speed() asteroid = Asteroid(x, y, x_speed, y_speed) size = asteroid.get_size() self.update_game_asteroids(asteroid) self.draw_asteroid(asteroid, size, x, y)
def load_level(self, levelfile): p1 = Planet(self, V2(300, 220) + self.game.game_offset, 7, Resources(70, 20, 10)) p1.change_owner(self.player_civ) p1.hide_warnings = True self.homeworld = p1 self.game_group.add(p1) p2 = Planet(self, V2(400, 120) + self.game.game_offset, 3, Resources(100, 0, 0)) p2.change_owner(self.enemy.civ) p2.flag.kill() p2.visible = False p2.selectable = False p2.population = 0 self.game_group.add(p2) for i in range(6): theta = i * -0.65 pos = helper.from_angle(theta) * ( 50 + random.random() * 80) + self.homeworld.pos a = Asteroid(self, pos, Resources(40, 0, 0)) a.health = 10 self.game_group.add(a) self.objgrid.generate_grid(self.get_objects_initial()) self.player_civ.bonus_supply = 99
def __init__(self, asteroids_amount): # Calls the screen and sets its borders self.__screen = Screen() 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 # Randomizes coordinates for the ship in each of the axis random_x = random.randint(self.__screen_min_x, self.__screen_max_x) random_y = random.randint(self.__screen_min_y, self.__screen_max_y) self.__ship = Ship((random_x, random_y), (0, 0), 0) # Our ship self.__asteroid_list = [] # The list of asteroids the are in the game # Handles the initialization of the asteroids when the game starts for current_asteroid_i in range(asteroids_amount): location = random.randint(self.__screen_min_x, self.__screen_max_x), random.randint( self.__screen_min_x, self.__screen_max_x) velocity = random.choice(SPEED_RANGE), random.choice(SPEED_RANGE) asteroid = Asteroid(location, velocity, INITIAL_AS_SIZE) while asteroid.has_intersection(self.__ship): asteroid = Asteroid(location, velocity, INITIAL_AS_SIZE) self.__asteroid_list.append(asteroid) for asteroid in self.__asteroid_list: self.__screen.register_asteroid(asteroid, INITIAL_AS_SIZE) self.__torpedo_list = [] # The list which stores shouted torpedoes self.__points = 0 # stores the points of the player self.__screen.set_score(self.__points) # Register the intialized
def add_asteroid(self): position = (random() * self.arena.width, random() * self.arena.height) mass = random() * (self.max_mass - self.min_mass) + self.min_mass velocity = [0, 0] density = MAX_DENSITY * 0.1 + random() * MAX_DENSITY * 0.9 a = Asteroid(position, mass, velocity, density, self.arena) a.apply_force(10000 * (random() - 0.5), 10000 * (random() - 0.5))
def split_asteroid(self): ''' this function its to move torpedo's and check if the torpedo touched the asteroid and split the asteroid to asteroid smaller than before and delete the torpedo that touched the asteroid and the torpedo that his life ends ! ==200''' for tor in self.torp_lst: self.move_obj(tor) tor.set_life(tor.get_life() + 1) if tor.get_life() != TORPEDO_LIFE_LIMIT: self._screen.draw_torpedo(tor, tor.get_place_x(), tor.get_place_y(), tor.get_direction()) for ast in self.ast_lst: if ast.has_intersection(tor): if ast.get_size() == ASTEROID_MIN_SIZE: self.__value += MAX_POINTS self._screen.unregister_asteroid(ast) self.ast_lst.remove(ast) self._screen.unregister_torpedo(tor) self.torp_lst.remove(tor) self._screen.set_score(self.__value) break elif ast.get_size() == ASTEROID_MID_SIZE: self.__value += MID_POINTS for i in range(0, 2): new_ast = Asteroid(ast.get_place_x(), ast.get_place_y(), ASTEROID_MIN_SIZE) self.ast_lst.append(new_ast) self._screen.register_asteroid( new_ast, ASTEROID_MIN_SIZE) self.set_speed_tor(tor, ast, i) self._screen.unregister_asteroid(ast) self.ast_lst.remove(ast) self._screen.unregister_torpedo(tor) self.torp_lst.remove(tor) self._screen.set_score(self.__value) break elif ast.get_size() == ASTEROID_MAX_SIZE: self.__value += MIN_POINTS for i in range(0, 2): new_ast = Asteroid(ast.get_place_x(), ast.get_place_y(), ASTEROID_MID_SIZE) self.ast_lst.append(new_ast) self._screen.register_asteroid( new_ast, ASTEROID_MID_SIZE) self.set_speed_tor(tor, ast, i) self._screen.unregister_asteroid(ast) self.ast_lst.remove(ast) self._screen.unregister_torpedo(tor) self.torp_lst.remove(tor) self._screen.set_score(self.__value) break else: self._screen.unregister_torpedo(tor) self.torp_lst.remove(tor)
def _init_asteroid_start(self, asteroids_amount): """ launch new asteroid. :param asteroids_amount: """ for i in range(asteroids_amount): new_asteroid = Asteroid() self.__asteroid_list.append(new_asteroid) self.__screen.register_asteroid(asteroid=new_asteroid, size=new_asteroid.get_size())
def init_asteroids(self): for i in range(self.max_asteroids): asteroid = Asteroid(self.sp_asteroids, self.sp_effects, 0, randrange(3, 5), randrange(0, self.screen.get_width() - 1), (0 - randrange(100, 200))) asteroid.set_images(self.asteroid_images[randrange(1, 6)]) self.asteroids.append(asteroid) self.sp_asteroids.add(self.asteroids)
def reset_game(self): """Initialise start of game state (reset ship position, score, and asteroids).""" # send connection message to game lettin em know we out here self.child_con.send("reset") self.ship.reset() Asteroid.initiate_game() self.death = False self.spawn_speed = constants.INITIAL_SPAWN_FREQUENCY self.next_spawn = pyxel.frame_count + self.spawn_speed
def create_asteroid(ai_settings, screen, asteroids, asteroid_number, row_number): """Create a single aseroid and put it in a row""" # Create an asteroid and place it in the row asteroid = Asteroid(ai_settings, screen) asteroid.width = asteroid.rect.width asteroid.height = asteroid.rect.height asteroid.x = asteroid_width + 2 * asteroid_width * asteroid_number asteroid.rect.x = asteroid.x asteroid.rect.y - asteroid.rect.height + 2 * asteroid.rect.height * row_number asteroids.add(asteroid)
def __init__(self): self.player = Player() self.asteroid = Asteroid() self.menu = Menu() self.score = 0 self.TIMER_START = 60 self.timer = self.TIMER_START self.start_ticks = pygame.time.get_ticks() self.cur_ticks = self.start_ticks self.previous_score = 0 self.high_score = 0
def create_asteroids(self, num_of_asteroids_to_create, location_vectors_list=None, speed_vectors_list=None, size_list=None): """ :param num_of_asteroids_to_create: default number of 5 as given in the exercise :param location_vectors_list: list of the asteroids random locations :param speed_vectors_list: list of the asteroids random speeds :param size_list: list of the asteroids sizes (the size is 3 at the beginning) The function creates 5 asteroids with the information in the lists :return: list of 5 asteroids """ asteroids_added = [] # starts an empty list which the new asteroids is going to be if location_vectors_list is None: # uses create_random_location_vectors_for_asteroids to # create list of random locations for the new asteroids location_vectors_list = self.create_random_location_vectors_for_asteroids( num_of_asteroids_to_create) if speed_vectors_list is None: # uses create_random_speed_vectors_for_asteroids # to create list of random speed locations for the new asteroids speed_vectors_list = self.create_random_speed_vectors_for_asteroids( num_of_asteroids_to_create) if size_list is None: # uses create_size_list_for_asteroids # to create list of sizes for the new asteroids size_list = self.create_size_list_for_asteroids( num_of_asteroids_to_create) # the for loop creates the asteroids with the data in lists which # created in the 3 if's above append them to the # list of asteroids and screen them for i in range(num_of_asteroids_to_create): new_asteroid = Asteroid(location_vectors_list[i], speed_vectors_list[i], size_list[i]) asteroids_added.append(new_asteroid) self._screen.register_asteroid(new_asteroid, new_asteroid.get_size()) self.draw_asteroids(asteroids_added) return asteroids_added
def __init__(self): """ Creates 100 asteroids and stores them into a list """ for i in range(1, 101): circumference = Asteroid.calc_circumference(random.randint(1, 4)) temp_asteroid = Asteroid(circumference, Controller.starting_position, (random.randint(1, 5), random.randint( 1, 5), random.randint(1, 5))) Controller.asteroid_list.append(temp_asteroid)
def __init__(self, width, height, n_asteroids, min_mass=800, max_mass=1000): self.min_mass = min_mass self.max_mass = max_mass self.schedule = Schedule('move', 'random', 'collision_check', 'collision_response') Asteroid.activate(self.schedule) Ship.activate(self.schedule) Bullet.activate(self.schedule) CollisionChecker.activate(self.schedule) self._tick = 0 self.logger = logging.getLogger("Model") self.arena = Arena(width, height) for i in range(n_asteroids): self.add_asteroid() centre = [self.arena.width / 2, self.arena.height / 2] self.ship = Ship(centre, 1500, 150, 1500, self.arena)
def asteroids(num_asteroids, player_position, batch=None): asteroids = [] for i in range(num_asteroids): asteroid_x, asteroid_y = player_position while utils.distance((asteroid_x, asteroid_y), player_position) < 100: asteroid_x = random.randint(0, WINDOW_WIDTH) asteroid_y = random.randint(0, WINDOW_HEIGHT) new_asteroid = Asteroid(name="Asteroid #" + str(i + 1), img=resources.asteroid_image, x=asteroid_x, y=asteroid_y, batch=batch) new_asteroid.rotation = random.randint(0, 360) new_asteroid.velocity_x = random.random() * 40 new_asteroid.velocity_y = random.random() * 40 new_asteroid.rotation_speed = random.random() * 60 asteroids.append(new_asteroid) return asteroids