def title(): """ This shows the title screen before the game starts """ screen.blit(graphics.img_title_screen, (0, 0)) video.update() sounds.music_play('title_music.ogg') while True: ev = pg.event.poll() if ev.type == pg.KEYDOWN and ev.key == pg.K_RETURN: sounds.startgame() break sounds.music_stop() screen.fill(colors.Black) pg.time.delay(1000) letter_by_letter('I came to eat apples') pg.time.wait(1000) letter_by_letter('and chew bubblegum...') pg.time.wait(3000) sounds.begin() letter_by_letter('And I\'m all out of gum.') screen.fill(colors.Black) pg.time.delay(2000)
def gameloop(replay): """ Everything that makes the game work :param replay: Only show title screen at first start of game loop, else False """ global start_level global score global apples_eaten global timer global time_left _Running = True _GameOver = False _ShowTitle = replay """ Create instances of all game objects """ player = Player(constants.display_width / 2, constants.display_height / 2, move_x=constants.block_size, move_y=0) apple = Apple(random_x(graphics.apple_size), random_y(graphics.apple_size)) bombs = pg.sprite.Group() for i in range(5): bombs.add(Bomb()) bullets = pg.sprite.Group() while _Running: while _ShowTitle: title() pg.time.wait(1000) sounds.music_stop() start_level = True """ Initializes background music """ sounds.music_play('the_reckoning.ogg') _ShowTitle = False while _GameOver: sounds.music_stop() player.x = 1 player.y = 1 screen.fill(colors.White) message_to_screen("GAME OVER", colors.Red, 0, -20) show_score = font.render("Your final score was {}".format(score), True, colors.Black) screen.blit(show_score, [(constants.display_width / 2) - (show_score.get_width() / 2), 240]) """ Add up and display the number of apples you ate after dying """ if apples_eaten > 0: for ate in range(apples_eaten + 1): screen.fill(colors.White) message_to_screen("GAME OVER", colors.Red, 0, -20) show_score = font.render( "Your final score was {}".format(score), True, colors.Black) screen.blit(show_score, [(constants.display_width / 2) - (show_score.get_width() / 2), 240]) apple_large = pg.transform.scale2x(graphics.img_apple) screen.blit(apple_large, [(constants.display_width / 2) - 70, 280]) eaten_count = font.render(" x " + str(ate), True, colors.Black) screen.blit(eaten_count, [(constants.display_width / 2), 290]) video.flip() sounds.bling() pg.time.delay(100) video.flip() else: pass video.update() sounds.game_over() pg.time.delay(5000) screen.fill(colors.White) message_to_screen("Play again? Y or N", colors.Black, 0, -20) video.update() while True: for event in pg.event.get(): if event.type == pg.KEYDOWN: if event.key == pg.K_y: constants.snake_length = 3 player.direction = "right" constants.FPS = 15 apples_eaten = 0 score = 0 time_left = 150 sounds.music_play('the_reckoning.ogg') start_level = True gameloop(False) elif event.key == pg.K_n: screen.fill(colors.Black) video.update() pg.time.wait(1000) quit() _GameOver = False """ Movement controls """ for event in pg.event.get(): if event.type == pg.QUIT: _Running = False if event.type == pg.KEYDOWN and not event.type == pg.KEYUP: if event.key == pg.K_LEFT and player.direction != "right": player.moveLeft() elif event.key == pg.K_RIGHT and player.direction != "left": player.moveRight() elif event.key == pg.K_UP and player.direction != "down": player.moveUp() elif event.key == pg.K_DOWN and player.direction != "up": player.moveDown() elif event.key == pg.K_ESCAPE: _Running = False elif event.key == pg.K_SPACE: b_x = player.x b_y = player.y bullet = Bullet((b_x, b_y), player.direction) sounds.shoot() bullets.add(bullet) """ Display which stage you are on """ if start_level: while True: level_marquee(1) pg.time.delay(100) start_level = False break """ Update the sprite locations """ bullets.update() bombs.update() """ Makes sure snake continues moving in current direction """ player.x += player.move_x player.y += player.move_y """ Create background """ screen.blit(graphics.img_background, (0, 0)) """ Draw apples and bombs """ apple.render(screen) bombs.draw(screen) """ Draw player head and body """ snake_head = [player.x, player.y] player.body.append(snake_head) player.render(player.body) """ Keeps snake the same length while moving around level """ if len(player.body) > constants.snake_length: del player.body[0] """ Draw Health Meter """ healthbar(player.health) """ Draw score counter to screen """ show_text("Score: {}".format(score), colors.White, (constants.display_width - 160), 5) """ PEW PEW PEW """ bullets.draw(screen) """ Time left - 1 second """ timer += 1 / constants.FPS if timer > timer % 1: score += 1 timer = 0 time_left -= 1 """ Display time left """ m, s = divmod(time_left, 60) show_text("Time left - {}:{:02}".format(m, s), colors.White, 260, 5) # ============== LOGIC RELATED STUFF =================== # """ Game Over when player runs into self """ for segment in player.body[:-1]: if segment == snake_head: sounds.xplode() flash_screen() _GameOver = True """ Game Over if player leaves area """ if player.x == 0 or player.x >= constants.level_w or player.y == 40 or player.y >= constants.level_h + 40: _GameOver = True sounds.xplode() flash_screen() """ Game Over when health hits zero """ if player.health == 0: sounds.xplode() flash_screen() _GameOver = True """ Player dies when time runs out """ if time_left == 0: sounds.music_stop() sounds.times_up() screen.fill(colors.Black) video.flip() pg.time.delay(50) screen.blit(graphics.img_time_up, (0, 0)) video.flip() pg.time.wait(4000) _GameOver = True """ Handles collision detection between snake and apples/bombs """ if apple.x <= player.x < apple.x + apple.size[0]: if apple.y <= player.y < apple.y + apple.size[1]: eat_apple() apple.x = random_x(apple.size) apple.y = random_y(apple.size) score += constants.block_size bombs.empty() for i in range(5): bombs.remove(i) bombs.add(Bomb()) if (apples_eaten % constants.block_size == 0) and (player.health <= 3): player.health += 1 """ Destroy bombs when you shoot them """ for target in pg.sprite.groupcollide(bombs, bullets, True, True): target.is_hit(screen) video.update() bombs.remove(target) score += 5 """ Damage player when they hit a bomb """ for target in bombs: if target.rect.x <= player.x < target.rect.x + target.size[0]: if target.rect.y <= player.y < target.rect.y + target.size[1]: target.is_hit(screen) sounds.scream() bombs.remove(target) video.update() player.health -= 1 video.update() clock.tick(constants.FPS)
def gameloop(replay): """ Everything that makes the game work :param replay: Only show title screen at first start of game loop, else False """ global start_level global score global apples_eaten global timer global time_left _Running = True _GameOver = False _ShowTitle = replay """ Create instances of all game objects """ player = Player(constants.display_width / 2, constants.display_height / 2, move_x=constants.block_size, move_y=0) apple = Apple(random_x(graphics.apple_size), random_y(graphics.apple_size)) bombs = pg.sprite.Group() for i in range(5): bombs.add(Bomb()) bullets = pg.sprite.Group() while _Running: while _ShowTitle: title() pg.time.wait(1000) sounds.music_stop() start_level = True """ Initializes background music """ sounds.music_play('the_reckoning.ogg') _ShowTitle = False while _GameOver: sounds.music_stop() player.x = 1 player.y = 1 screen.fill(colors.White) message_to_screen("GAME OVER", colors.Red, 0, -20) show_score = font.render("Your final score was {}".format(score), True, colors.Black) screen.blit(show_score, [(constants.display_width / 2) - (show_score.get_width() / 2), 240]) """ Add up and display the number of apples you ate after dying """ if apples_eaten > 0: for ate in range(apples_eaten + 1): screen.fill(colors.White) message_to_screen("GAME OVER", colors.Red, 0, -20) show_score = font.render("Your final score was {}".format(score), True, colors.Black) screen.blit(show_score, [(constants.display_width / 2) - (show_score.get_width() / 2), 240]) apple_large = pg.transform.scale2x(graphics.img_apple) screen.blit(apple_large, [(constants.display_width / 2) - 70, 280]) eaten_count = font.render(" x " + str(ate), True, colors.Black) screen.blit(eaten_count, [(constants.display_width / 2), 290]) video.flip() sounds.bling() pg.time.delay(100) video.flip() else: pass video.update() sounds.game_over() pg.time.delay(5000) screen.fill(colors.White) message_to_screen("Play again? Y or N", colors.Black, 0, -20) video.update() while True: for event in pg.event.get(): if event.type == pg.KEYDOWN: if event.key == pg.K_y: constants.snake_length = 3 player.direction = "right" constants.FPS = 15 apples_eaten = 0 score = 0 time_left = 150 sounds.music_play('the_reckoning.ogg') start_level = True gameloop(False) elif event.key == pg.K_n: screen.fill(colors.Black) video.update() pg.time.wait(1000) quit() _GameOver = False """ Movement controls """ for event in pg.event.get(): if event.type == pg.QUIT: _Running = False if event.type == pg.KEYDOWN and not event.type == pg.KEYUP: if event.key == pg.K_LEFT and player.direction != "right": player.moveLeft() elif event.key == pg.K_RIGHT and player.direction != "left": player.moveRight() elif event.key == pg.K_UP and player.direction != "down": player.moveUp() elif event.key == pg.K_DOWN and player.direction != "up": player.moveDown() elif event.key == pg.K_ESCAPE: _Running = False elif event.key == pg.K_SPACE: b_x = player.x b_y = player.y bullet = Bullet((b_x, b_y), player.direction) sounds.shoot() bullets.add(bullet) """ Display which stage you are on """ if start_level: while True: level_marquee(1) pg.time.delay(100) start_level = False break """ Update the sprite locations """ bullets.update() bombs.update() """ Makes sure snake continues moving in current direction """ player.x += player.move_x player.y += player.move_y """ Create background """ screen.blit(graphics.img_background, (0, 0)) """ Draw apples and bombs """ apple.render(screen) bombs.draw(screen) """ Draw player head and body """ snake_head = [player.x, player.y] player.body.append(snake_head) player.render(player.body) """ Keeps snake the same length while moving around level """ if len(player.body) > constants.snake_length: del player.body[0] """ Draw Health Meter """ healthbar(player.health) """ Draw score counter to screen """ show_text("Score: {}".format(score), colors.White, (constants.display_width - 160), 5) """ PEW PEW PEW """ bullets.draw(screen) """ Time left - 1 second """ timer += 1 / constants.FPS if timer > timer % 1: score += 1 timer = 0 time_left -= 1 """ Display time left """ m, s = divmod(time_left, 60) show_text("Time left - {}:{:02}".format(m, s), colors.White, 260, 5) # ============== LOGIC RELATED STUFF =================== # """ Game Over when player runs into self """ for segment in player.body[:-1]: if segment == snake_head: sounds.xplode() flash_screen() _GameOver = True """ Game Over if player leaves area """ if player.x == 0 or player.x >= constants.level_w or player.y == 40 or player.y >= constants.level_h + 40: _GameOver = True sounds.xplode() flash_screen() """ Game Over when health hits zero """ if player.health == 0: sounds.xplode() flash_screen() _GameOver = True """ Player dies when time runs out """ if time_left == 0: sounds.music_stop() sounds.times_up() screen.fill(colors.Black) video.flip() pg.time.delay(50) screen.blit(graphics.img_time_up, (0, 0)) video.flip() pg.time.wait(4000) _GameOver = True """ Handles collision detection between snake and apples/bombs """ if apple.x <= player.x < apple.x + apple.size[0]: if apple.y <= player.y < apple.y + apple.size[1]: eat_apple() apple.x = random_x(apple.size) apple.y = random_y(apple.size) score += constants.block_size bombs.empty() for i in range(5): bombs.remove(i) bombs.add(Bomb()) if (apples_eaten % constants.block_size == 0) and (player.health <= 3): player.health += 1 """ Destroy bombs when you shoot them """ for target in pg.sprite.groupcollide(bombs, bullets, True, True): target.is_hit(screen) video.update() bombs.remove(target) score += 5 """ Damage player when they hit a bomb """ for target in bombs: if target.rect.x <= player.x < target.rect.x + target.size[0]: if target.rect.y <= player.y < target.rect.y + target.size[1]: target.is_hit(screen) sounds.scream() bombs.remove(target) video.update() player.health -= 1 video.update() clock.tick(constants.FPS)