def update(self): gameover = False self.score = len(self.snake) - 4 for index, s in enumerate(self.snake[-1:0:-1]): s[0], s[1] = [ self.snake[len(self.snake) - 2 - index][0], self.snake[len(self.snake) - 2 - index][1] ] self.snake[0][0] += int(math.cos(math.radians(self.direction))) self.snake[0][1] += int(math.sin(math.radians(self.direction))) if self.snake[0] == [self.apple.x, self.apple.y]: self.snake.append([self.snake[-1][0], self.snake[-1][1]]) self.apple.reseed(self.snake) for s in self.snake[1:]: if s == self.snake[0]: gameover = True if 0 <= self.snake[0][0] < 40 and 0 <= self.snake[0][1] < 30: pass else: gameover = True if gameover == True: GameEngine.display_data(320, 220, "Game Over!", GameEngine.font, (255, 255, 255)) super().draw() time.sleep(1) GameEngine.display_data(320, 260 + 20, "Your score was: " + str(self.score), GameEngine.font, (255, 255, 255)) super().draw() time.sleep(2) self.exit() GameEngine.change_state(games.menu.Menu()) super().update(15)
def draw(self): GameEngine.game_display.fill((0, 0, 0)) GameEngine.game_display.blit(utils.rot_center(self.ship.image, self.ship.angle), (self.ship.rect.x, self.ship.rect.y)) for bullet in self.ship.bullets: GameEngine.game_display.blit(bullet.image, (bullet.rect.x, bullet.rect.y)) for asteroid in self.asteroids: GameEngine.game_display.blit( pygame.transform.scale(asteroid.image, (25 * asteroid.size, 25 * asteroid.size)), (asteroid.rect.x, asteroid.rect.y)) GameEngine.display_data(100, 20, self.score, GameEngine.font, (255, 255, 255)) super().draw()
def draw(self): GameEngine.game_display.fill((0, 0, 0)) GameEngine.display_data(400, 125, "Classic Games in Python", GameEngine.font, (255, 255, 255)) GameEngine.display_data(400, 400, "Asteroids", GameEngine.font, (255, 255, 255)) GameEngine.display_data(400, 440, "Snake", GameEngine.font, (255, 255, 255)) super().draw()
def draw(self): GameEngine.game_display.fill((0, 0, 0)) GameEngine.display_data(400, 125, "Game Engine", GameEngine.font, (255, 255, 255)) super().draw()
def update(self): self.ship.rect.x -= self.ship.acceleration * math.sin(math.radians(self.ship.angle)) self.ship.rect.y -= self.ship.acceleration * math.cos(math.radians(self.ship.angle)) self.ship.acceleration = max(self.ship.acceleration - .1, 0) self.ship.warp = max(self.ship.warp - .5, 0) self.ship.firing = max(self.ship.firing - .5, 0) if self.ship.rect.x > GameEngine.display_width: self.ship.rect.x = 0 if self.ship.rect.x < 0: self.ship.rect.x = GameEngine.display_width if self.ship.rect.y > GameEngine.display_height: self.ship.rect.y = 0 if self.ship.rect.y < 0: self.ship.rect.y = GameEngine.display_height if random.randint(0, 100) > 97: if random.random() > 0.5: if random.random() > 0.5: self.asteroids.append( Asteroid(0, random.randint(0, GameEngine.display_height), random.randint(0, 360), 3)) else: self.asteroids.append( Asteroid(GameEngine.display_width, random.randint(0, GameEngine.display_height), random.randint(0, 360), 3)) else: if random.random() > 0.5: self.asteroids.append( Asteroid(random.randint(0, GameEngine.display_width), 0, random.randint(0, 360), 3)) else: self.asteroids.append( Asteroid(random.randint(0, GameEngine.display_width), GameEngine.display_height, random.randint(0, 360), 3)) for bullet in self.ship.bullets: bullet.rect.x -= 9 * math.sin(math.radians(bullet.angle)) bullet.rect.y -= 9 * math.cos(math.radians(bullet.angle)) if bullet.rect.x < 0 or bullet.rect.x > GameEngine.display_width: self.ship.bullets.remove(bullet) elif bullet.rect.y < 0 or bullet.rect.y > GameEngine.display_height: self.ship.bullets.remove(bullet) else: for asteroid in self.asteroids: if abs(bullet.rect.x - asteroid.rect.x) < 12.5 * asteroid.size and abs( bullet.rect.y - asteroid.rect.y) < 12.5 * asteroid.size: self.score = int(self.score + 300 / asteroid.size) try: self.asteroids.remove(asteroid) except ValueError: pass try: self.ship.bullets.remove(bullet) except ValueError: pass if asteroid.size > 1: self.asteroids += [ Asteroid(asteroid.rect.x, asteroid.rect.y, random.randint(0, 360), asteroid.size - 1), Asteroid(asteroid.rect.x, asteroid.rect.y, random.randint(0, 360), asteroid.size - 1)] for asteroid in self.asteroids: asteroid.rect.x -= 4 / asteroid.size * math.sin(math.radians(asteroid.angle)) asteroid.rect.y -= 4 / asteroid.size * math.cos(math.radians(asteroid.angle)) if asteroid.rect.x > GameEngine.display_width: asteroid.rect.x = 0 if asteroid.rect.x < 0: asteroid.rect.x = GameEngine.display_width if asteroid.rect.y > GameEngine.display_height: asteroid.rect.y = 0 if asteroid.rect.y < 0: asteroid.rect.y = GameEngine.display_height if abs(self.ship.rect.x - asteroid.rect.x) < 15 * asteroid.size and abs( self.ship.rect.y - asteroid.rect.y) < 15 * asteroid.size: GameEngine.display_data(GameEngine.display_width / 2, GameEngine.display_height / 2 - 20, "Game Over!", GameEngine.font, (255, 255, 255)) super().draw() time.sleep(1) GameEngine.display_data(GameEngine.display_width / 2, GameEngine.display_height / 2 + 20, "Your score was: " + str(self.score), GameEngine.font, (255, 255, 255)) super().draw() time.sleep(2) self.exit() GameEngine.change_state(games.menu.Menu()) super().update(50)