def __init__(self, title, screen_width, screen_height): # Initialize Pygame pygame.init() # Set up screen self.SCREEN_WIDTH = screen_width self.SCREEN_HEIGHT = screen_height self.window = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT)) pygame.display.set_caption(title) # Set the window bar title self.screen = pygame.display.get_surface() # This is where images are displayed # Set up font self.font = pygame.font.Font(None, 36) # Generate Battlecruiser self.battlecruiser = Battlecruiser(self.screen, self.SCREEN_WIDTH / 2, self.SCREEN_HEIGHT * 5 / 7) # Generate 10 enemies self.enemies = [] for i in range(10): self.enemies.append(Enemy(self.screen, randint(0, self.SCREEN_WIDTH), randint(0, self.SCREEN_HEIGHT - 300), self.battlecruiser)) # Add all sprites to group self.sprites = pygame.sprite.Group(self.battlecruiser, self.enemies) # Set up clock self.clock = pygame.time.Clock() self.FPS = 30 self.seconds = 0 pygame.time.set_timer(USEREVENT + 1, 1000) # Used to correctly implement seconds
# Constants FPS = 50 SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600 LASER_SPEED = -5 MAX_SPEED = 20 #in x and y directions separatly, not normalized num_enemies = 5 gameover = False # Set the display's dimensions screenDimensions = (SCREEN_WIDTH, SCREEN_HEIGHT) window = pygame.display.set_mode(screenDimensions) pygame.display.set_caption('Battle for Ram Aras') # Set the window bar title screen = pygame.display.get_surface() # This is where images are displayed my_cruiser = Battlecruiser(screen, SCREEN_WIDTH/2, SCREEN_HEIGHT/2) my_cruiser.draw() font = pygame.font.Font(None, 28) score = 0 COUNTER_LOCATION = (10, 10) def input(events): ''' Function to handle events, particularly quitting the program ''' for event in events: if event.type == QUIT: quit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: quit() if event.key == K_SPACE: my_cruiser.create_laser()
score = 0 # Keep track of lasers lasers = [] # Create the enemies; choose starting location and starting speed for each sprite enemies = [] for i in range(NUM_ENEMIES): enemies.append(Enemy(screen, randint(1, (screen.get_size()[0]/2 - 19)), 0, randint(1, MAX_SPEED), randint(1, MAX_SPEED))) # Keep track of events events = {'down': 0, 'up': 0, 'right': 0, 'left': 0} # Create battlecruiser cruiser = Battlecruiser(screen, SCREEN_WIDTH/2, SCREEN_HEIGHT-100, 4) # Create background bg = ScrollingBackground(screen, SCROLL_SPEED) # Game loop while True: if cruiser.active == False: render_game_over(screen, score) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit()
cur_enemies = 10 num_enemies = 10 max_enemies = 100 dist_mod = .4 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) game_over_screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) game_over = False pygame.display.set_caption('simple Sprite Demo') clock = pygame.time.Clock() font = pygame.font.Font(None, 28) counter = 0 score = 0 ship = Battlecruiser(screen, SPRITE_IMAGE, 400, 500) ram_aras = Background(screen, SCROLL_SPEED) end_aras = Background(game_over_screen, SCROLL_SPEED) ship_events = [] swarm = spawn(cur_enemies, dist_mod) #setup Ram Aras while not game_over: time_passed = clock.tick(FPS) text = font.render("Score: "+ str(score),1,(0,200,127)) #spawn_enemies
class Game: DEBUG = False def __init__(self, title, screen_width, screen_height): # Initialize Pygame pygame.init() # Set up screen self.SCREEN_WIDTH = screen_width self.SCREEN_HEIGHT = screen_height self.window = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT)) pygame.display.set_caption(title) # Set the window bar title self.screen = pygame.display.get_surface() # This is where images are displayed # Set up font self.font = pygame.font.Font(None, 36) # Generate Battlecruiser self.battlecruiser = Battlecruiser(self.screen, self.SCREEN_WIDTH / 2, self.SCREEN_HEIGHT * 5 / 7) # Generate 10 enemies self.enemies = [] for i in range(10): self.enemies.append(Enemy(self.screen, randint(0, self.SCREEN_WIDTH), randint(0, self.SCREEN_HEIGHT - 300), self.battlecruiser)) # Add all sprites to group self.sprites = pygame.sprite.Group(self.battlecruiser, self.enemies) # Set up clock self.clock = pygame.time.Clock() self.FPS = 30 self.seconds = 0 pygame.time.set_timer(USEREVENT + 1, 1000) # Used to correctly implement seconds def run(self): while True: # for each frame self.handle_input() for sprite in self.sprites: sprite.update() self.clock.tick(self.FPS) self.screen.fill((255, 255, 255)) for sprite in self.sprites: sprite.draw() score = (11 - len(self.sprites)) * 100 ending_font = self.font.render("Score: {0}".format(score), 1, (0, 0, 255)) self.screen.blit(ending_font, (10, 10)) pygame.display.flip() def handle_input(self): for event in pygame.event.get(): if event.type == QUIT: self.quit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: self.quit() elif event.key == K_LEFT: self.battlecruiser.update("LEFT") elif event.key == K_RIGHT: self.battlecruiser.update("RIGHT") elif event.key == K_UP: self.battlecruiser.update("UP") elif event.key == K_DOWN: self.battlecruiser.update("DOWN") elif event.key == K_SPACE: self.battlecruiser.update("FIRE") elif event.type == USEREVENT + 1: self.seconds += 1 def quit(self): quit()
def main(): # Check if sound and font are supported if not pygame.font: print "Warning, fonts disabled" if not pygame.mixer: print "Warning, sound disabled" # Constants FPS = 50 SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600 BACKGROUND_COLOR = (0, 0, 0) LASER_SPEED = 300 SHIP_SPEED = 300 SCROLL_SPEED = 50 SCORE_POS = (20, 20) ENEMY_RELEASE_DELAY = 750 MIN_SPEED = 100 MAX_SPEED = 350 # Initialize Pygame, the clock, a font, and a screen pygame.init() pygame.display.set_caption("Game.py") screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) clock = pygame.time.Clock() font = pygame.font.Font(None, 36) endfont = pygame.font.Font(None, 60) # Create the sprite group lasers = pygame.sprite.Group() enemies = pygame.sprite.Group() player = Battlecruiser(screen, (SCREEN_WIDTH / 2), -1, SHIP_SPEED) background = ScrollingBackground(screen, SCROLL_SPEED) # Initialize variables score = 0 delay_counter = 0 # Game loop while True: time_passed = clock.tick(FPS) # Event handling here (to quit) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() sys.exit() if event.key == K_SPACE and player.get_state() == "Flying": lasers.add(player.shoot()) if event.key == K_UP: player.up() if event.key == K_DOWN: player.down() if event.key == K_LEFT: player.left() if event.key == K_RIGHT: player.right() if event.key == K_r and player.get_state() == "Dead": enemies.empty() lasers.empty() background.kill() background = ScrollingBackground(screen, SCROLL_SPEED) player.kill() player = Battlecruiser(screen, (SCREEN_WIDTH / 2), -1, SHIP_SPEED) elif event.type == KEYUP: if event.key == K_UP: if player.get_direction()[1] < 0: player.zero_vert() if event.key == K_DOWN: if player.get_direction()[1] > 0: player.zero_vert() if event.key == K_LEFT: if player.get_direction()[0] < 0: player.zero_hort() if event.key == K_RIGHT: if player.get_direction()[0] > 0: player.zero_hort() # Update if player still alive if player.get_state() == "Flying": delay_counter += time_passed if delay_counter >= ENEMY_RELEASE_DELAY: enemies.add( Enemy( screen, randint(1, SCREEN_WIDTH), 0, randint(MIN_SPEED, MAX_SPEED), randint(MAX_SPEED * -1, MAX_SPEED), randint(MIN_SPEED, MAX_SPEED) * -1, ) ) delay_counter = 0 # Redraw the background background.update(time_passed) background.draw() # Draw score fontscore = font.render(str(score), 1, (255, 255, 255)) screen.blit(fontscore, SCORE_POS) # Check for collisions player_collision = pygame.sprite.spritecollideany(player, enemies) if player_collision is not None: if player_collision.get_state() is not "Dead": player.explode() collided = pygame.sprite.groupcollide(enemies, lasers, False, False) for enemy in collided: if enemy.get_state() == "Flying": enemy.explode() score += 100 # Update and redraw all enemies enemies.update(time_passed) for enemy in enemies.sprites(): enemy.draw() # Update and redraw all lasers lasers.update(time_passed) for laser in lasers.sprites(): laser.draw() player.update(time_passed) player.draw() # Allow death to progress else: player.update(time_passed) player.draw() if player.get_active(): endtext = endfont.render("Final Score: " + str(score), 1, (255, 255, 255)) screen.blit(endtext, (240, 180)) else: endtext = endfont.render(("'r' to restart."), 1, (255, 255, 255)) screen.blit(endtext, (240, 250)) # Draw the sprites pygame.display.update()
print "Sound not working." background_music.play(-1) screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Battlecruiser, Reporting!') clock = pygame.time.Clock() font = pygame.font.Font(None, 28) background_image = pygame.image.load(BACKGROUND_IMAGE) background_image = background_image.convert_alpha() y_pos_on_scroll = -1*background_image.get_size()[1] + SCREEN_HEIGHT enemies = [] player = Battlecruiser(screen, BC_IMAGE, LASER_IMAGE, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, BC_MAX_SPEED, LASER_SPEED, BC_X_ACCEL, BC_Y_ACCEL, LASER_SOUND) game_over = False score = 0 counter = 0 while True: time_passed = clock.tick(FPS) #Event handling here (to quit) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_ESCAPE: pygame.quit() sys.exit()