def split_asteroid(self, astr, torp): """ This function will split a given asteroid after an encounter with a torpedo. it will register both new smaller asteroids to the screen and unregister the old one which go hit. :param astr: an Astroid object :param torp: an Torpeo object :return: a tuple of two new asteroid type objects. """ # makes sure the new asteroid are smaller new_size = astr.get_size() - 1 new_x_speed, new_y_speed = self.new_astr_speed(astr, torp) new_astr1 = asteroid.Asteroid(self.board_size, new_size) new_astr2 = asteroid.Asteroid(self.board_size, new_size) # set the new asteroid position new_astr1.set_x_pos(astr.get_x_pos()) new_astr1.set_y_pos(astr.get_y_pos()) new_astr2.set_x_pos(astr.get_x_pos()) new_astr2.set_y_pos(astr.get_y_pos()) # set the new asteroid speed new_astr1.set_x_speed(new_x_speed) new_astr1.set_y_speed(new_y_speed) new_astr2.set_x_speed(new_x_speed * (-1)) new_astr2.set_y_speed(new_y_speed * (-1)) # register and un register to the Screen object self._screen.register_asteroid(new_astr1, new_size) self._screen.register_asteroid(new_astr2, new_size) if astr.is_register(): self._screen.unregister_asteroid(astr) astr.un_register() return new_astr1, new_astr2
def split_asteroid(self, rock, torp): """ When a larger asteroid is hit by a torpedo the asteroid is split into 2 smaller ones. The new speed of each is given by a specific formula. And is dependant on the speed of the torpedo. :param rock: an Asteroid object :param torp: a Torpedo object """ new_pos = rock.get_pos() new_size = rock.get_size() - 1 cur_x_speed_pow = rock.get_x_speed()**2 cur_y_speed_pow = rock.get_y_speed()**2 divisor = math.sqrt(cur_x_speed_pow + cur_y_speed_pow) new_x_speed = (torp.get_x_speed() + rock.get_x_speed()) / divisor new_y_speed = (torp.get_y_speed() + rock.get_y_speed()) / divisor new_speed_i = [new_x_speed, new_y_speed] new_speed_ii = [-new_x_speed, -new_y_speed] rock_i = asteroid.Asteroid(new_pos, new_size, new_speed_i) rock_ii = asteroid.Asteroid(new_pos, new_size, new_speed_ii) self.asteroids += [rock_i, rock_ii] # new asteroids need to be drawn immediately. # waiting until the next draw_all() causes # unexpected behaviour in Screen. self._screen.register_asteroid(rock_i, rock_i.get_size()) self._screen.draw_asteroid(rock_i, *rock_i.get_pos()) self._screen.register_asteroid(rock_ii, rock_ii.get_size()) self._screen.draw_asteroid(rock_ii, *rock_ii.get_pos())
def __init__(self, asteroids_amnt): """ Initialize a new Game object. :param asteroids_amnt (int): the amount of asteroids in the start of the game. The function will set a new screen object. The function will set a new ship object. The function will create asrteroids and will make sure that the starting point is different that the ship's. :return: A new Game object. """ 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 board_size = (self.screen_max_x, self.screen_max_y, self.screen_min_x, self.screen_min_y) self.board_size = board_size self.ship = ship.Ship(board_size) self.score = STARTING_SCORE self.torpedos = [] self.asteroids = [] for i in range(asteroids_amnt): new_asteroid = asteroid.Asteroid(board_size) #making sure asteroids aren't created on the ship while new_asteroid.get_x_pos() == self.ship.get_x_pos() and \ new_asteroid.get_y_pos() == self.ship.get_y_pos(): new_asteroid = asteroid.Asteroid(board_size) self.asteroids.append(new_asteroid) self._screen.register_asteroid(self.asteroids[i], self.asteroids[i].get_size())
def play_game(self): self.start_screen() while not self.world.quit: self.level = 1 self.world.reset() self.world.particle.starfield() if self.settings.video_cap: self.video = video.VidCapture(self.surface, self.settings.video_cap_rate, self.settings.video_out) else: self.video = video.DummyCap() while not self.world.quit: self.level_start() self.world.add_player(self.player) for i in range(self.level * 2): asteroid.Asteroid(self.world, random.randint(75, 100), 0.5 + self.level / 4.0) self.play_level() if not self.world.player: break self.level += 1 self.game_over() self.video.publish() self.epilogue()
def __init__(self, asteroids_amnt): """ A constructor for a gamerunner object :param asteroids_amnt = the amount of asteroids the game will start with. """ 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 self._ship = ship.Ship() self._screen.draw_ship(self._ship.x(), self._ship.y(), self._ship.get_direction()) self.asteroid_list = [] for i in range(asteroids_amnt): self.asteroid_list.append(asteroid.Asteroid(self._ship)) registered_asteroid_list = [] for an_asteroid in self.asteroid_list: self._screen.register_asteroid(an_asteroid, an_asteroid.size) self._screen.draw_asteroid(an_asteroid, an_asteroid.x(), an_asteroid.y()) self.torpedo_list = [] self.score = 0
def play_game(self): self.start_screen() while not self.world.quit: self.level = 1 self.world.reset() self.world.particle.starfield() while not self.world.quit: self.level_start() self.world.add_player() for i in range(self.level * 2): asteroid.Asteroid(self.world, random.randint(75, 100), 0.5 + self.level / 4.0) self.play_level() if not self.world.player: break self.level += 1 self.game_over() self.epilogue()
def impact(self, other): if isinstance(other, alien.Alien): other.kill = True self.kill = True self.world.score += 1000 self.world.particle.explosion(20, other.position, other.velocity) elif isinstance(other, asteroid.Asteroid): other.kill = True self.kill = True self.world.score += other.scale self.world.n_asteroids -= 1 self.world.particle.explosion(other.scale / 3, other.position, other.velocity) if other.scale > 15: n = random.randint(2, max(2, min(5, other.scale / 5))) for i in range(n): new_asteroid = asteroid.Asteroid(self.world, other.scale / n, 1) new_asteroid.position[0] = other.position[0] new_asteroid.position[1] = other.position[1] new_asteroid.velocity[0] += other.velocity[0] new_asteroid.velocity[1] += other.velocity[1] return False
def spawnAsteroidsEveryX(self): # generate x and y positions for asteroids and add them to list asteroidX = random.uniform(-2, 2) asteroidY = random.uniform(-2, 2) asteroidZ = 100 ast = asteroid.Asteroid(asteroidX, asteroidY, asteroidZ) # very hacky but it works...lol x = "astr" + str(random.randint(0, 100000)) while x in self.dictOfAsteroids: x = "astr" + str(random.randint(0, 100000)) self.dictOfAsteroids[x] = ast ''' WEIRD BEHAVIOR WHEN SPAWNING ASTEROIDS pub_asteroid = rospy.Publisher("/particle_shooter", Pose, queue_size=10) msg = Pose() msg.position.x = 0 msg.position.y = 0 msg.position.z = 10 msg.orientation.z = -100 print(msg) pub_asteroid.publish(msg) ''' print("LOG: Spawned Asteroids Succesfully")
def level_start(self): start_animation_frames = 100 start_animation_time = start_animation_frames while not self.world.quit: if start_animation_time == 0: break self.world.update() if self.world.spawn: asteroid.Asteroid(self.world, random.randint(75, 100), self.level) self.surface.fill(util.BLACK) self.draw_hud() self.draw_info() start_animation_time -= 1 t = float(start_animation_time) / start_animation_frames text.draw_string(self.surface, "LEVEL START", util.WHITE, t * 150, [self.width / 2, self.height / 2], centre = True, angle = t * 200.0) self.world.draw() self.clock.tick(60) pygame.display.flip() self.video.capture()
def spawnAsteroid(self): # Want a: # random position, # random direction towards the center-ish # random speed # random rotation angle # 0 = left side # 1 = top side # 2 = right side # 3 = bottom side side = randint(0,3) x,y = 0,0 ang = 0 # Some numbers to make the math look easier. lowbound = math.pi / 16 upbound = math.pi / 2 - math.pi / 16 onebound = math.pi / 2 if side == 0: x = -assets.SIZE[0] y = randint(0, assets.SCREEN_SIZE[1]) if y < assets.HALF_SCREEN[1]: ang = uniform(lowbound, upbound) else: ang = uniform(-lowbound, -upbound) elif side == 1: x = randint(0, assets.SCREEN_SIZE[1]) y = -assets.SIZE[1] if x < assets.HALF_SCREEN[1]: ang = uniform(lowbound, upbound) else: ang = uniform(onebound + lowbound, onebound + upbound) elif side == 2: x = assets.SIZE[0] + assets.SCREEN_SIZE[0] y = randint(0, assets.SCREEN_SIZE[1]) if y < assets.HALF_SCREEN[1]: ang = uniform(onebound + lowbound, onebound + upbound) else: ang = uniform(-(onebound + lowbound), -(onebound + upbound)) else: x = randint(0, assets.SCREEN_SIZE[1]) y = assets.SIZE[1] + assets.SCREEN_SIZE[1] if x < assets.HALF_SCREEN[1]: ang = uniform(-lowbound, -upbound) else: ang = uniform(-(onebound + lowbound), -(onebound + upbound)) spd = randint(1,8) rot = randint(-5,5) self.asteroids.append(asteroid.Asteroid( (x, y), spd, ang, rot))
def __init__(self, num_asteroids, radius_range_min, radius_range_max): self._asteroid_list = [] for i in range(0, num_asteroids, 1): radius = random.randint(radius_range_min, radius_range_max) circumference = 2 * math.pi * radius position_gen = asteroid.Vector3D(random.randint(0, 99), random.randint(0, 99), random.randint(0, 99)) velocity_gen = asteroid.Vector3D(random.randint(-5, 5), random.randint(-5, 5), random.randint(-5, 5)) self._asteroid_list.append(asteroid.Asteroid(circumference, position_gen, velocity_gen))
def split_asteroid(self, index, an_asteroid, a_torpedo, a_ship): """ if an asteroid is hit with a torpedo it splits to two smaller asteroids. this function creates two new smaller asteroids and removes the one that got hit. :param an_asteroid: a asteroid that was hit by a torpedo :param a_torpedo: the torpedo that hit the asteroid :param a_ship: the game's ship """ size = an_asteroid.get_size() self._screen.unregister_asteroid(an_asteroid) self.asteroid_list[index] = None if size != 1: # the new astroids are smaller than the original, by one: new_asteroids = [ asteroid.Asteroid(a_ship, size - 1), asteroid.Asteroid(a_ship, size - 1) ] # calculating the new velocity according to # the following function (for each axis separately: # (torpedo velocity + the old asteroid's velocity) # / ((the old asteroid's velocity in the x axis)**2) + # (the old asteroid's velocity in the y axis)**2)) ** (1/2) new_x_velocity_o = (a_torpedo.get_x_velocity() + an_asteroid.get_x_velocity())/ \ (an_asteroid.get_x_velocity()**2 + an_asteroid.get_y_velocity()**2)\ **(1/2) new_asteroids[0].set_x_velocity(new_x_velocity_o) new_x_velocity_1 = -new_x_velocity_o new_asteroids[1].set_x_velocity(new_x_velocity_1) new_y_velocity_o = (a_torpedo.get_y_velocity() + an_asteroid.get_y_velocity()) \ / (an_asteroid.get_x_velocity()**2 + an_asteroid.get_y_velocity()**2)\ ** (1/2) new_asteroids[0].set_y_velocity(new_y_velocity_o) new_y_velocity_1 = -new_y_velocity_o new_asteroids[1].set_y_velocity(new_y_velocity_1) for a_new_asteroid in new_asteroids: a_new_asteroid.set_pos(an_asteroid.get_pos()) self._screen.register_asteroid(a_new_asteroid, a_new_asteroid.get_size()) self.asteroid_list.append(a_new_asteroid)
def spawn_asteroid(type): new_asteroid = asteroid.Asteroid( width, random.randint(0, height - img_asteroids[type].get_height()), img_asteroids[type].get_width(), img_asteroids[type].get_height(), img_asteroids[type]) asteroids.append(new_asteroid)
def addRandomAsteroid(xrel, yrel, zrel): xran = (xrel + 40, xrel + 100) yran = (yrel - 50, yrel + 50) zran = (zrel - 50, zrel + 50) x = random.randint(xran[0], xran[1]) y = random.randint(yran[0], yran[1]) z = random.randint(zran[0], zran[1]) return asteroid.Asteroid((x, y, z), (1.6, 1.6))
def create_new_ast(old_ast, torp_speed, order="first"): new_speed = calc_new_ast_speed(torp_speed, old_ast.get_speed()) if order == "second": new_speed = (-new_speed[X_AXIS], - new_speed[Y_AXIS]) new_ast = asteroid.Asteroid(old_ast.get_location()[X_AXIS], new_speed[X_AXIS], old_ast.get_location()[Y_AXIS], new_speed[Y_AXIS], old_ast.get_size() - 1) return new_ast
def add_asteroid(self): """ Creates a new asteroid in a random position (not the ship's), adds it to the asteroid list, and registers it so it would appear on the sceen """ new_pos = self._random_pos() # loop until the position is not identical to the ship's. while new_pos == self.ship.get_pos(): new_pos = self._random_pos() new_rock = asteroid.Asteroid(new_pos) self.asteroids.append(new_rock) self._screen.register_asteroid(new_rock, new_rock.get_size())
def randomAsteroids(self): if randint(1, self.levelAsteroidFrequency[self.level]) == 1: astLevel = random.choice(self.asteroidProbability[self.level]) newasteroid = asteroid.Asteroid(astLevel) randx = randint(0, self.screen.get_width() - 50) randy = newasteroid.image.get_height() * -1 newasteroid.rect.x, newasteroid.rect.y = randx, randy newasteroid.speedx = randint(-2, 2) newasteroid.speedy = randint(1, 2) self.asteroids.add(newasteroid)
def asteroids(num_asteroids, player_position, batch=None): asteroids = [] for i in range(num_asteroids): asteroid_x, asteroid_y = player_position while util.distance((asteroid_x, asteroid_y), player_position) < 100: asteroid_x = random.randint(0, 800) asteroid_y = random.randint(0, 600) new_asteroid = asteroid.Asteroid(x=asteroid_x, y=asteroid_y, batch=batch) new_asteroid.rotation = random.randint(0, 360) new_asteroid.velocity_x = random.random() * 40 * random.choice([-1, 1]) new_asteroid.velocity_y = random.random() * 40 * random.choice([-1, 1]) asteroids.append(new_asteroid) return asteroids
def add_asteroid(n=1): for i in range(n): speed = 10 + ship.score / 1000 ship_radius = 150 pos1 = random.randint(0, screen_size[0]) pos2 = random.randint(0, screen_size[1]) while abs(pos1 - ship.position[0]) < ship_radius \ and abs(pos2 - ship.position[1]) < ship_radius: pos1 = random.randint(0, screen_size[0]) pos2 = random.randint(0, screen_size[1]) asteroids.append(asteroid.Asteroid([pos1, pos2], speed))
def next_level(): global asteroids, bullets, level, level_text_brightness level += 1 player1.level_up(level) asteroids = [] for i in range(2 * level): asteroids.append( asteroid.Asteroid([ random.randint(0, screen_size[0]), random.randint(0, screen_size[1]) ])) level_text_brightness = 1.0
def asteroids(num_asteroids, player_position, batch=None): """Generate asteroid objects with random positions and velocities, not close to the player""" asteroids = [] for i in range(num_asteroids): asteroid_x, asteroid_y = player_position while util.distance((asteroid_x, asteroid_y), player_position) < 100: asteroid_x = random.randint(0, 800) asteroid_y = random.randint(0, 600) new_asteroid = asteroid.Asteroid(x=asteroid_x, y=asteroid_y, batch=batch) new_asteroid.rotation = random.randint(0, 360) new_asteroid.velocity_x, new_asteroid.velocity_y = random.random( ) * 40, random.random() * 40 asteroids.append(new_asteroid) return asteroids
def __init__(self, num_asteroids): """ Asteroids are stored in a list. Asteroids have randomly generated attributes. Asteroids have random radius of 1 to 4. Asteroids have random velocity of 0 to 5 metres per second. :param num_asteroids: an int for total number of asteroids :precondition: num_asteroids must be an int greater than 0 """ self._list_of_asteroids = [] for x in range(num_asteroids): radius = random.randint(1, 4) circumference = self.__calculate_circumference(radius) start_pos = vector.Vector.random_init(99, 99, 99) start_velocity = vector.Vector.random_init(5, 5, 5) new_asteroid = asteroid.Asteroid(circumference, start_pos, start_velocity) self._list_of_asteroids.append(new_asteroid)
def asteroids(num_asteroids, player_position, batch=None): asteroids = [] #asteroid_types = [resources.asteroid_big_image, resources.asteroid_small_image] for i in range(num_asteroids): asteroid_x, asteroid_y = player_position while util.distance((asteroid_x, asteroid_y), player_position) < 100: asteroid_x = randint(0, 800) asteroid_y = randint(0, 800) #asteroid_size = randint(0,1) new_asteroid = asteroid.Asteroid(x=asteroid_x, y=asteroid_y, batch=batch) new_asteroid.rotation = randint(0, 360) new_asteroid.velocity_x = random.random() * 40 * randint(-1, 1) new_asteroid.velocity_y = random.random() * 40 * randint(-1, 1) asteroids.append(new_asteroid) return asteroids
def start_screen(self): self.world.add_text('ARGH ITS THE ASTEROIDS', scale = 20) self.world.add_text('PRESS ESC TO QUIT') self.world.add_text('PRESS ENTER TO START', scale = 20) for i in range(4): asteroid.Asteroid(self.world, random.randint(50, 100), 1) self.world.particle.starfield() while not self.world.quit and not self.world.enter: self.world.update() self.surface.fill(util.BLACK) self.draw_info() self.world.draw() # set the limit very high, we can use the start screen as a # benchmark self.clock.tick(200) pygame.display.flip()
def start_screen(self): self.world.add_text('ARGH ITS THE ASTEROIDS', scale=20) self.world.add_text('PRESS ESC TO QUIT') self.world.add_text('PRESS LEFT AND RIGHT TO ROTATE') self.world.add_text('PRESS UP FOR THRUST') self.world.add_text('PRESS SPACE FOR FIRE') self.world.add_text('OR USE MOUSE CONTROLS') self.world.add_text('WATCH OUT FOR ALLEN THE ALIEN') self.world.add_text('PRESS ENTER TO START', scale=20) for i in range(4): asteroid.Asteroid(self.world, random.randint(50, 100), 1) self.world.particle.starfield() while not self.world.quit and not self.world.enter: self.world.update() self.surface.fill(util.BLACK) self.draw_info() self.world.draw() # set the limit very high, we can use the start screen as a # benchmark self.clock.tick(200) pygame.display.flip()
def __init__(self, asteroids_amnt=3): 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 self.score = INITIAL_GAME_SCORE self.torpedoes = [] self.ship = ship.Ship(self.get_x_y_random_values()) self.asteroids = [] for i in range(asteroids_amnt): # Create a good asteroid asteroid_location = self.get_x_y_random_values() while asteroid_location == self.ship.get_position(): asteroid_location = self.get_x_y_random_values() asteroid_velocity = self.get_velocity_x_y_random() new_asteroid = asteroid.Asteroid(asteroid_location[X], asteroid_location[Y], asteroid_velocity[X], asteroid_velocity[Y]) self.asteroids.append(new_asteroid) self._screen.register_asteroid(new_asteroid, new_asteroid.get_size())
def __init__(self, asteroids_amount): 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 self.__start_x_ship = random.randint(self.__screen_min_x, self.__screen_max_x) self.__start_y_ship = random.randint(self.__screen_min_y, self.__screen_max_y) self.__main_ship = ship.Ship(self.__start_x_ship, 0, self.__start_y_ship, 0, 0, 3) self.__torpedos_list = [] self.__asteroids_lst = [] self.__score = 0 while len(self.__asteroids_lst) < asteroids_amount: start_x_asteroid = random.randint(self.__screen_min_x, self.__screen_max_x) start_y_asteroid = random.randint(self.__screen_min_y, self.__screen_max_y) start_x_speed = random.choice([-4, -3, -2, -1, 1, 2, 3, 4]) start_y_speed = random.choice([-4, -3, -2, -1, 1, 2, 3, 4]) new_asteroid = asteroid.Asteroid(start_x_asteroid, start_x_speed, start_y_asteroid, start_y_speed, START_ASTEROID_SIZE) if not new_asteroid.has_intersection(self.__main_ship): # only if there is no intersection with the ship, the asteroid is added to the game self.__asteroids_lst.append(new_asteroid) self.__screen.register_asteroid(new_asteroid, START_ASTEROID_SIZE)
def run_kwargs(params): asteroids = [asteroid.Asteroid(**kwargs) for kwargs in params['asteroids']] in_bounds = bounds.BoundsRectangle(**params['in_bounds']) goal_bounds = bounds.BoundsRectangle(**params['goal_bounds']) # TODO: rename to margin? min_dist = params['min_dist'] ret = { 'field': asteroid.AsteroidField(asteroids=asteroids), 'craft_state': craft.CraftState(**(params['initial_craft_state'])), 'in_bounds': in_bounds, 'goal_bounds': goal_bounds, 'noise_sigma': params['noise_sigma'], 'min_dist': min_dist, 'pilot': pilot.Pilot(min_dist=min_dist, in_bounds=in_bounds), # TODO: remove magic number 'nsteps': 1000 } return ret
def mainLoop(self): while True: ##THIS SECTION IS USED TO DRAW THE MAIN MENU## tColor = (255, 255, 0) while self.menu: title = self.titleFont.render("STEVEN LANDER", 1, tColor) names = self.mainFont.render("By: Hayden, Jack, and Dillon", 1, tColor) choiceEasy = self.mainFont.render("1. Easy", 1, tColor) choiceMid = self.mainFont.render("2. Hard", 1, tColor) choiceHard = self.mainFont.render("3. Impossible", 1, tColor) choiceQuit = self.mainFont.render("4. Quit", 1, tColor) self.window.blit(title, (250, 150)) self.window.blit(names, (250, 200)) self.window.blit(choiceEasy, (250, 250)) self.window.blit(choiceMid, (250, 300)) self.window.blit(choiceHard, (250, 350)) self.window.blit(choiceQuit, (250, 400)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYUP: if (event.key == pygame.K_1): self.ship = ship.Ship(500) self.sprites.add(self.ship) self.spawnRate = 2 self.difficulty = 1 self.menu = False elif (event.key == pygame.K_2): self.ship = ship.Ship(200) self.sprites.add(self.ship) self.spawnRate = 5 self.difficulty = 5 self.menu = False elif (event.key == pygame.K_3): self.ship = ship.Ship(100) self.sprites.add(self.ship) self.spawnRate = 10 self.difficulty = 15 self.menu = False elif (event.key == pygame.K_4): pygame.quit() pygame.display.flip() self.window.blit(self.background, (0, 0)) ################################################## ##HANDLES DRAWING LABELS ON THE SCREEN## if (self.win and not self.died): self.enableInput = False winLab = self.titleFont.render("YOU WIN!", 1, tColor) resetLab = self.mainFont.render( "Press Space to return to menu", 1, tColor) self.window.blit(resetLab, (350, 100)) self.window.blit(winLab, (300, 50)) if (self.died and not self.win): self.enableInput = False deadLab = self.titleFont.render("YOU CRASHED!", 1, tColor) resetLab = self.mainFont.render( "Press Space to return to menu", 1, tColor) self.window.blit(resetLab, (350, 100)) self.window.blit(deadLab, (350, 50)) pygame.key.set_repeat(1, 50) self.background.fill((0, 0, 0)) if (self.ship.velMagnitude() > 0): self.score = round( self.ship.fuel / self.ship.velMagnitude() * self.difficulty, 2) scoreFile = open("score.txt", "r") self.highscore = float(scoreFile.readline()) scoreFile.close() fuelLab = self.mainFont.render("Fuel: " + str(self.ship.fuel), 1, tColor) velocityLab = self.mainFont.render( "Velocity: " + str(round(self.ship.velMagnitude(), 2)), 1, tColor) scoreLab = self.mainFont.render("Score: " + str(self.score), 1, tColor) hscoreLab = self.mainFont.render( "Highscore: " + str(self.highscore), 1, tColor) self.window.blit(fuelLab, (0, 50)) self.window.blit(velocityLab, (0, 100)) self.window.blit(scoreLab, (0, 150)) self.window.blit(hscoreLab, (0, 200)) ######################### #ASTEROID SPAWNING if (random.randrange(1, 500) <= self.spawnRate): ast = asteroid.Asteroid((770, random.randrange(0, 400))) self.asteroids.append(ast) self.sprites.add(ast) #FUEL SPAWNING if (random.randrange(1, 2000) <= self.spawnRate): fuelObj = fuelpod.FuelPod((770, random.randrange(0, 400))) self.fuelpods.append(fuelObj) self.sprites.add(fuelObj) #APPLIES A CONSTANT GRAVITY self.ship.accelerate((0, self.planet.gravity), "world") ##DRAWS THE TERRAIN self.terrain = pygame.draw.lines(self.window, (255, 255, 255), False, self.planet.nodes) self.platform = pygame.draw.line(self.window, (255, 0, 0), self.planet.plat[0], self.planet.plat[1], 10) pygame.draw.line(self.window, (255, 0, 0), self.planet.platStand[0], self.planet.platStand[1]) ######################### ##HANDLE POSITION UPDATES HERE## self.sprites.update() #Checks to make sure the ship is still on the screen if (self.ship.position[0] > self.width - self.ship.rect.width): self.ship.position = (self.width - self.ship.rect.width, self.ship.position[1]) self.ship.velocity = (0, self.ship.velocity[1]) if (self.ship.position[0] < self.ship.rect.width): self.ship.position = (self.ship.rect.width, self.ship.position[1]) self.ship.velocity = (0, self.ship.velocity[1]) if (self.ship.position[1] < 0): self.ship.position = (self.ship.position[0], 0) self.ship.velocity = (self.ship.velocity[0], 0) self.sprites.draw(self.window) ######################### ##CHECK FOR COLLISIONS## for i in self.asteroids: if (self.ship.rect.colliderect(i) and not self.win): self.died = True self.sprites.remove(self.ship) for i in self.fuelpods: if (self.ship.rect.colliderect(i)): self.ship.fuel += 50 self.sprites.remove(i) self.fuelpods.remove(i) del i if (self.ship.rect.colliderect(self.terrain) and not self.win): self.died = True self.ship.velocity = (0, 0) if (self.ship.rect.colliderect(self.platform)): if (self.ship.velMagnitude() > .7 and not self.win): self.died = True if (self.ship.rotation != 0 and not self.win): self.died = True elif (not self.died and not self.win): self.win = True self.checkHighscore() self.ship.velocity = (0, 0) ######################### #WAY TO HANDLE SEVERAL INPUTS HELD DOWN keys = pygame.key.get_pressed() #checking pressed keys if (self.enableInput ): #Makes sure the ship hasn't crashed or landed if keys[pygame.K_w]: self.ship.fuel -= 1 x = (self.ship.rotation / 90) if (self.ship.rotation != 0): y = 0 else: y = -1 if (self.ship.fuel > 0): self.ship.accelerate((x, y), "world") curSprite = random.choice(["yellow", "red"]) else: curSprite = "normal" else: curSprite = "normal" if keys[pygame.K_d]: self.ship.rotation = 90 #self.ship.image = customRotate(self.ship.image,-1) curSprite += "90" elif keys[pygame.K_a]: #self.ship.turn(-1) self.ship.rotation = -90 #self.ship.image = customRotate(self.ship.image,1) curSprite += "-90" else: self.ship.rotation = 0 #changes the ship sprite to the desired throttle state and rotation self.ship.image = pygame.image.load(curSprite + ".png").convert_alpha() ######################### ##NON GAMEPLAY INPUT## for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYUP: if (event.key == pygame.K_SPACE): if (self.died or self.win): self.menu = True self.sprites.remove(self.ship) self.planet = planet.Planet(.1, 20) self.enableInput = True self.died = False self.win = False for i in self.asteroids: self.sprites.remove(i) for i in self.fuelpods: self.sprites.remove(i) self.fuelpods = [] self.asteroids = [] ######################### pygame.display.flip() self.window.blit(self.background, (0, 0)) self.clock.tick(50) pygame.quit()
highscore = 0 with open('highscore.txt', 'r') as highscoreFile: highscore = highscoreFile.read() # Colors white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) yellow = (255, 255, 0) purple = (255, 0, 255) # Asteroids ast_list = [asteroid.Asteroid(200, window_width), asteroid.Asteroid(550, window_width), asteroid.Asteroid(900, window_width), asteroid.Asteroid(1250, window_width), asteroid.Asteroid(1600, window_width)] # Rocket rocket_ = rocket.Rocket(window_width * 0.44, window_height * 0.7, car_width, car_width) music = media.music_load() pygame.mixer.music.set_volume(.4) pygame.mixer.music.play(-1) # ---------------------------------------------------------- def rocket(exploded): screen.blit(rocket_.sprite, (rocket_.x, rocket_.y))