def main(): # Initializes pygame, window pygame.init() window = pygame.display.set_mode( (globals.SCREEN_WIDTH, globals.SCREEN_HEIGHT)) input_params = Inputs() clock = pygame.time.Clock() app = QApplication(sys.argv) param_win = Window() param_win.show() # Polygon to be controlled by arrows polygon = Polygon(np.array([800, 500]), 30, 3, 10, (255, 255, 255)) space = Space(polygon) while globals.RUN: # Drawing shapes space.draw(window) # Adding force to main polygon if input_params.is_left(): polygon.add_force(np.array([-globals.KEY_FORCE, 0.])) elif input_params.is_right(): polygon.add_force(np.array([globals.KEY_FORCE, 0.])) if input_params.is_up(): polygon.add_force(np.array([0., -globals.KEY_FORCE])) elif input_params.is_down(): polygon.add_force(np.array([0., globals.KEY_FORCE])) # Add shape on left click if input_params.mouse_left_click(): space.add_shape(globals.NEW_SHAPE_TYPE, input_params.mouse_pos, globals.NEW_SHAPE_RADIUS, globals.NEW_SHAPE_MASS, globals.NEW_SHAPE_DEGREE) # Remove shape on right click if input_params.mouse_right_click(): space.remove_shape(input_params.mouse_pos) if input_params.is_clear(): space.shapes = [] clock.tick(globals.FPS) elapsed_time = clock.get_time() # Update shapes in space class for movement, rotarion... space.update(window, elapsed_time) # Check for mouse of keyboard click input_params.update() pygame.display.update() pygame.display.set_caption(str(clock.get_fps())) sys.exit(app.exec_())
def main(): #Initialize Pygame pygame.init() #Create the display and caption the window SCREEN = pygame.display.set_mode((800,600)) pygame.display.set_caption("Rocket to the Moon") #Create the clock object and fps clock = pygame.time.Clock() fps = 60 #Initialize phase phase = 0 #Create an addText function def addText(text, pos, size, color): FONT = pygame.font.SysFont('calibri', size) TEXT = FONT.render(text, 5, color) SCREEN.blit(TEXT, pos) #Create the sprite lists allSprites = pygame.sprite.Group() rocket_list = pygame.sprite.Group() # This is a sloppy sprite list made for the intro for the rocket. asteroids = pygame.sprite.Group() #Create an instance of the background space = Space(SCREEN) #Create the rocket rocket = Rocket() allSprites.add(rocket) rocket_list.add(rocket) #Create the lives images life = pygame.image.load("./assets/rocket.png") life = pygame.transform.scale(life, (20, 30)) def showLives(): if rocket.lives == 3: SCREEN.blit(life, (700, 550)) SCREEN.blit(life, (740, 550)) SCREEN.blit(life, (780, 550)) elif rocket.lives == 2: SCREEN.blit(life, (740, 550)) SCREEN.blit(life, (780, 550)) elif rocket.lives == 1: SCREEN.blit(life, (780, 550)) #Create the asteroids for i in range(8): asteroid = Asteroid() allSprites.add(asteroid) asteroids.add(asteroid) asteroid.shuffle() #Create the intro buttons playButton = Button(SCREEN, 'rect', (165, 485), (150, 75), GREEN) quitButton = Button(SCREEN, 'rect', (485, 485), (150, 75), RED) def buttons(): playButton.draw() quitButton.draw() playButton.addText('Play', 'calibri', 40, (0,0,0)) quitButton.addText('Quit', 'calibri', 40, (0,0,0)) #When the mouse touches the play button if playButton.touching(pygame.mouse.get_pos()): playButton.detail = (0,150,0) else: playButton.detail = GREEN playButton.bevel((0,150,0), 5, ['left', 'bottom']) #When the mouse touches the quit button if quitButton.touching(pygame.mouse.get_pos()): quitButton.detail = (150, 0, 0) else: quitButton.detail = RED quitButton.bevel((150,0,0), 5, ['left', 'bottom']) #Create the intro def intro(): addText("Rocket", (270, 100), 100, RED) addText("to the", (230, 200), 60, BLUE) addText("Moon", (350, 175), 120, GREEN) buttons() #Create the startGame def startGame(): rocket.rect.x = 350 rocket.rect.y = 600 def rocketIntro(): rocket_list.draw(SCREEN) if rocket.rect.y != 500: rocket.rect.y -= 2 def play(): rocket.move() for asteroid in asteroids: asteroid.scroll() allSprites.draw(SCREEN) showLives() #Main pygame loop while True: space.draw() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == MOUSEBUTTONDOWN: if phase == 0 and quitButton.touching(pygame.mouse.get_pos()): pygame.quit() sys.exit() if phase == 0 and playButton.touching(pygame.mouse.get_pos()): phase = 1 startGame() if phase == 0: intro() elif phase == 1: rocketIntro() if rocket.rect.y == 500: phase = 2 elif phase == 2: play() collideList = pygame.sprite.spritecollide(rocket, asteroids, False) if collideList: rocket.explode() pygame.display.update() clock.tick(fps)
class Game: """PyFighter - The game of the year.""" ALIEN_DELAY = 2000 ALIEN_DELTA = 200 def __init__(self): """Initialize game engine.""" pygame.init() self.screen = pygame.display.set_mode(Data.RESOLUTION) pygame.display.set_caption('PyFighter [using PyGame {0}]'.format(Data.PYGAME_INFO)) self.clock = pygame.time.Clock() self.space = Space() self.story = _Story() self.audio = _Audio() self.player = pygame.sprite.GroupSingle(Player()) self.aliens = pygame.sprite.Group() self.beams = pygame.sprite.Group() self.timer = 0 def play(self): """Start the game.""" self.story.play_intro(self.screen) self.audio.play_music() self._idle_loop() def _idle_loop(self): """Main loop of the game.""" while True: self._process_events() self._update_game() self._draw_screen() def _process_events(self): """Analyze all proper events from the queue.""" for event in pygame.event.get(): if event.type == QUIT: self._game_over(False) elif event.key == K_SPACE: # Generate the rocket and store it in the list. self.audio.play_laser() self.beams.add(Laser(self.player.sprite.rect)) else: self.player.sprite.handle(event) def _update_game(self): """Calculate all changes.""" self.space.update() self.player.update() self.beams.update() self._check_state() self.aliens.update() # Generate next alien. delay = Game.ALIEN_DELAY - Game.ALIEN_DELTA * Score.levelNo if pygame.time.get_ticks() - self.timer > delay and len(self.aliens) < Score.levelNo: self.aliens.add(Alien()) self.timer = pygame.time.get_ticks() self._check_state() def _check_state(self): """Check all game actions.""" # Check if the player was hit or if alien went past the screen. if pygame.sprite.spritecollideany(self.player.sprite, self.aliens) or Score.livesNo == 0: self._game_over(False) # Check for beams collisions. result = pygame.sprite.groupcollide(self.beams, self.aliens, True, True) for hits in result.values(): Score.score += len(hits) Score.count += len(hits) # Increase level of difficulty if proper no of ships was destroyed if Score.count >= Score.levelNo: Score.count -= Score.levelNo Score.levelNo += 1 # Show good ending in case player reached last level. if Score.levelNo == 7: self._game_over(True) def _draw_screen(self): """Draw everything.""" self.space.draw(self.screen) self.player.draw(self.screen) self.beams.draw(self.screen) self.aliens.draw(self.screen) Score.draw(self.screen) # Show the screen. pygame.display.flip() self.clock.tick(Data.FRAMES_SEC) def _game_over(self, isGood): """Show ending information.""" self.audio.stop_music() self.story.play_outro(self.screen, isGood) self.story.play_game_over(self.screen) pygame.quit() sys.exit()