def __init__(self, input_state, game_stats: SessionStats, starfield=None): super().__init__(input_state) self.stats = game_stats self.font = pygame.font.SysFont(None, 48) self.prompt_group = pygame.sprite.Group() self.high_score_state = HighScore(self.input_state) self.starfield = starfield or Starfield() new_high_score = self.font.render("New high score!", True, config.text_color) new_high_score = StaticAnimation(new_high_score) new_high_score.rect.centerx = config.screen_rect.centerx new_high_score.rect.top = 50 self.prompt_group.add(new_high_score) self.entered_name_image = None self.entered_name_rect = None self.entered_name = "" self._update_name_image() self.entered_name_rect.centerx = new_high_score.rect.centerx self.entered_name_rect.top = new_high_score.rect.bottom + 10 # if this is not a new high score, this state will just transition directly to displaying the # high score list self.done = not self.high_score_state.is_new_high_score( game_stats.score)
def setup(self): # Create the background and star field. self.background = Starfield(self.width, self.height) # Create the player ship. self.create_ship('player') # Create an enemy ship. self.create_ship('enemy')
def main(): WINSIZE = 640, 480 NUMBER_OF_STARS = 250 STARTING_ANGLE = 180 CLOSEST_STAR_COLOR = (255, 255, 255) STAR_SIZE_IN_PIXELS = 1 #_V_ = 1 pygame.init() pygame.key.set_repeat(1, 1) screen = pygame.display.set_mode(WINSIZE, pygame.SWSURFACE) stars = Starfield(screen, screen.get_rect(), NUMBER_OF_STARS, STARTING_ANGLE, (40, 4), STAR_SIZE_IN_PIXELS, CLOSEST_STAR_COLOR) ship1 = Ship(screen) done = False while not done: screen.fill(THECOLORS["black"]) stars.update() ship1.draw() ship1.resetSprt() pygame.display.update() events = pygame.event.get() for e in events: if (e.type == QUIT): done = True break elif (e.type == KEYDOWN): if (e.key == K_ESCAPE): done = True break #Test for movement dx, dy = 0, 0 if (e.key == K_DOWN): dy += 1 if (e.key == K_UP): dy -= 1 if (e.key == K_LEFT): dx -= 1 if (e.key == K_RIGHT): dx += 1 ship1.move(dx, dy) #if(e.key == K_PLUS): #stars.set_speed(( print "Exiting!" return
def __init__(self, depth=3): self.starfield = Starfield() self.textures = [] mul = 0.125 / 2.0 for _ in range(depth): texture = pygame.Surface((512, screen_height * 2)) texture.set_colorkey((255, 0, 255)) texture.fill((255, 0, 255)) heightmap = heightmap_1d(9) for x in range(512): min_y = int((heightmap[x] * mul + 1.0) / 2.0 * screen_height) for y in range(min_y, screen_height * 2): noise = random.uniform(0.9, 1.0) color = tuple(255.0 * noise * mul for _ in range(3)) texture.set_at((x, y), color) self.textures.append(texture) mul *= 2
def __init__(self, input_state, starfield=None): super().__init__(input_state) self.font = pygame.font.SysFont(None, 48) self.high_scores = [] self.starfield = starfield or Starfield() self._load_high_scores() # create high score text self.high_score_image = self.font.render("High Scores", True, config.text_color) self.high_score_image = self.high_score_image.convert_alpha( pygame.display.get_surface()) self.high_score_rect = self.high_score_image.get_rect() self.high_score_rect.center = config.screen_rect.center self.high_score_rect.top = 50 self.score_group = pygame.sprite.Group() self._update_high_score_list() self.done = False
def main(): pygame.init() scale = 4 screen = pygame.display.set_mode((256 * scale, 240 * scale)) starfield = Starfield(scale, screen) player = Player(scale) running = True while running: pressed = pygame.key.get_pressed() player.keys(pressed) for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_F4 and pygame.key.get_mods( ) & pygame.KMOD_SHIFT: running = False if event.type == pygame.QUIT: running = False screen.fill((0, 0, 0)) starfield.draw() player.draw(screen) pygame.display.update() pygame.quit() sys.exit()
def __init__(self, input_state, starfield=None): super().__init__(input_state) self.font = pygame.font.SysFont(None, Menu.TitleSize) self.starfield = starfield or Starfield() self.title = Group() # create "Space Invaders" logo # green "SPACE" space_title = StaticAnimation( self.font.render("SPACE", True, config.green_color)) space_title.rect.centerx = config.screen_rect.centerx space_title.rect.centery = config.screen_height // 8 # white "INVADERS" self.font = pygame.font.SysFont(None, Menu.TitleSize // 2) invaders_title = StaticAnimation( self.font.render("INVADERS", True, pygame.Color('white'))) invaders_title.rect.left = space_title.rect.left + space_title.rect.width // 8 invaders_title.rect.top = space_title.rect.bottom + 10 self.title.add(space_title, invaders_title) last_y = config.screen_height - config.screen_height // 3 self.options = Group() self.font = pygame.font.SysFont(None, Menu.MenuItemSize) # create options for option in [("Play Space Invaders", self._play_game), ("High Scores", self._view_high_scores), ("Quit", self._quit)]: option_sprite = StaticAnimation( self.font.render(option[0], True, config.text_color)) option_sprite.callback = option[1] option_sprite.rect.centerx = config.screen_width // 2 option_sprite.rect.top = last_y last_y = option_sprite.rect.bottom + Menu.MenuItemSpacing self.options.add(option_sprite) # create a small sprite to use as menu item selector left_selector = config.atlas.load_static("selector") # need to flip the arrow... right_selector = StaticAnimation( pygame.transform.flip(left_selector.image, True, False)) right_selector.rect.left = config.screen_width self.selectors = Group(left_selector, right_selector) # point values for aliens self.aliens = Group() y_pos = invaders_title.rect.bottom + 50 for alien_stats in config.alien_stats: alien = config.atlas.load_animation( alien_stats.sprite_name).frames[0] spr = self._create_point_sprite(alien, alien_stats.points, y_pos) self.aliens.add(spr) y_pos = spr.rect.bottom + 10 ufo = config.atlas.load_animation("ufo").frames[0] spr = self._create_point_sprite(ufo, "???", y_pos) self.aliens.add(spr) # finish creating state values self.next_state = None self._set_selected(0) pygame.mouse.set_visible(True) sounds.play_music(sounds.menu_music_name)