def instructionLoop(): global GAME_MODE print("Instructions Loop") bg = resources.getBackground('instructions') darkness = 100 mode = 'fade in' goto = GAME_MENU tick = 0 menuButton = sprites.Button(resources.getSprite('button-menu')) menuButton.rect.center = (GAME_WIDTH // 4, GAME_HEIGHT * 3 // 4) while GAME_MODE == GAME_INFO: for event in pygame.event.get(): if event.type == pygame.QUIT: GAME_MODE = GAME_QUIT if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: goto = GAME_MENU mode = 'fade out' if mode == 'fade in': darkness -= fadeSpeed if darkness <= 0: mode = 'still' elif mode == 'fade out': darkness += fadeSpeed if darkness >= 100: tick = 60 mode = 'wait' if tick > 0: tick -= 1 if tick == 0: GAME_MODE = goto if mode == 'still': if menuButton.clicked(): goto = GAME_MENU mode = 'fade out' gameDisplay.blit(bg, (0, 0)) gameDisplay.blit(menuButton.image, menuButton.rect) dimDisplay(darkness) pygame.display.update() clock.tick(60)
def logoLoop(): global GAME_MODE print("Logo Loop") bg = resources.getBackground('logo') tick = 0 darkness = 100 mode = 'fade in' while GAME_MODE == GAME_LOGO: for event in pygame.event.get(): if event.type == pygame.QUIT: GAME_MODE = GAME_QUIT if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: GAME_MODE = GAME_QUIT if event.key == pygame.K_RETURN: mode = 'fade out' if tick > 0: tick -= 1 if mode == 'fade in': darkness -= fadeSpeed elif mode == 'fade out': darkness += fadeSpeed elif tick <= 0 and mode == 'still': mode = 'fade out' elif tick <= 0 and mode == 'wait': GAME_MODE = GAME_MENU if darkness <= 0 and mode == 'fade in': mode = 'still' tick = 60 elif darkness >= 100 and mode == 'fade out': mode = 'wait' tick = 60 gameDisplay.blit(bg, (0, 0)) dimDisplay(darkness) pygame.display.update() clock.tick(60)
def gameOverLoop(): global GAME_MODE print("Game Over Loop") print("Game Over!\nScore:", str(score)) darkness = 100 tick = 0 mode = 'fade in' bg = resources.getBackground('red').copy() drawText('Game Over!', GAME_WIDTH // 2, GAME_HEIGHT // 4, color=(0, 200, 0), text_align='center', surf=bg) drawText('Score:', GAME_WIDTH // 2, GAME_HEIGHT // 4 + 100, color=(0, 200, 0), text_align='center', surf=bg) drawText(str(score), GAME_WIDTH // 2, GAME_HEIGHT // 4 + 130, color=(0, 200, 0), text_align='center', surf=bg) playButton = sprites.Button(resources.getSprite('button-replay')) menuButton = sprites.Button(resources.getSprite('button-menu')) playButton.rect.topleft = (GAME_WIDTH, GAME_HEIGHT // 4) menuButton.rect.topleft = (GAME_WIDTH, GAME_HEIGHT // 4 + 100) goto = GAME_MENU while GAME_MODE == GAME_OVER: for event in pygame.event.get(): if event.type == pygame.QUIT: GAME_MODE = GAME_QUIT if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: goto = GAME_MENU mode = 'fade out' if event.key == pygame.K_RETURN: goto = GAME_MENU mode = 'fade out' if mode == 'fade in': darkness -= fadeSpeed elif mode == 'fade out': darkness += fadeSpeed if darkness <= 0 and mode == 'fade in': mode = 'still' elif darkness >= 100 and mode == 'fade out': mode = 'wait' tick = 60 if mode == 'fade in': darkness -= fadeSpeed if GAME_WIDTH - playButton.rect.left < 160: playButton.rect.left -= 2 menuButton.rect.left -= 2 if darkness <= 0: mode = 'still' elif mode == 'fade out': darkness += fadeSpeed if GAME_WIDTH - playButton.rect.left > 0: playButton.rect.left += 2 menuButton.rect.left += 2 if darkness >= 100: tick = 60 if tick > 0: tick -= 1 if tick == 0: GAME_MODE = goto if mode == 'still': if playButton.hovering(): playButton.rect.left = GAME_WIDTH - 165 if playButton.clicked(): mode = 'fade out' goto = GAME_PLAY else: playButton.rect.left = GAME_WIDTH - 160 if menuButton.hovering(): menuButton.rect.left = GAME_WIDTH - 165 if menuButton.clicked(): mode = 'fade out' goto = GAME_MENU else: menuButton.rect.left = GAME_WIDTH - 160 gameDisplay.blit(bg, (0, 0)) gameDisplay.blit(playButton.image, playButton.rect) gameDisplay.blit(menuButton.image, menuButton.rect) dimDisplay(darkness) pygame.display.update() clock.tick(60)
def menuLoop(): print("Menu Loop") global GAME_MODE mode = 'fade in' goto = GAME_LOGO tick = 0 darkness = 100 bg = resources.getBackground('menu') buttons = pygame.sprite.RenderPlain() playButton = sprites.Button(resources.getSprite('button-play')) ruleButton = sprites.Button(resources.getSprite('button-rules')) buttons.add(playButton, ruleButton) playButton.rect.right, ruleButton.rect.right = 0, 0 playButton.rect.y = GAME_HEIGHT / 2 ruleButton.rect.y = GAME_HEIGHT / 2 + 100 while GAME_MODE == GAME_MENU: for event in pygame.event.get(): if event.type == pygame.QUIT: GAME_MODE = GAME_QUIT if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: GAME_MODE = GAME_QUIT if event.key == pygame.K_RETURN: goto = GAME_PLAY mode = 'fade out' if tick > 0: tick -= 1 if mode == 'fade in': darkness -= fadeSpeed elif mode == 'fade out': darkness += fadeSpeed if playButton.rect.right > 0: playButton.rect.right -= 3 ruleButton.rect.right -= 3 elif tick == 0 and mode == 'wait': GAME_MODE = goto elif mode == 'buttons': playButton.rect.right += 3 ruleButton.rect.right += 3 if playButton.rect.right >= 100: mode = 'still' if darkness <= 0 and mode == 'fade in': mode = 'buttons' elif darkness >= 100 and mode == 'fade out': mode = 'wait' tick = 60 if mode == 'still': if playButton.hovering(): playButton.rect.right = 110 else: playButton.rect.right = 100 if ruleButton.hovering(): ruleButton.rect.right = 110 else: ruleButton.rect.right = 100 if playButton.clicked(): goto = GAME_PLAY mode = 'fade out' if ruleButton.clicked(): goto = GAME_INFO mode = 'fade out' gameDisplay.blit(bg, (0, 0)) buttons.draw(gameDisplay) dimDisplay(darkness) clock.tick(60) pygame.display.update()
def gameLoop(): global blocks, playerGroup, enemies, newLife, obstacles global hud, score print("Game Loop") global GAME_MODE blocks = pygame.sprite.RenderPlain() playerGroup = pygame.sprite.GroupSingle() enemies = pygame.sprite.Group() newLife = pygame.sprite.GroupSingle() obstacles = pygame.sprite.Group() bg = resources.getBackground('game') hud = sprites.HUD(GAME_HEIGHT) player = sprites.PlayerBlock(enemies, newLife, lives=5) player.rect.x = GAME_WIDTH // 2 player.rect.y = GAME_HEIGHT // 2 playerGroup.add(player) blocks.add(player) waveNum, untilNext, enemiesLeft = getNextWave(0) enemyTick = 60 tick = 0 score = 0 mode = 'fade in' darkness = 100 nextObstacle = 720 goto = GAME_MENU while GAME_MODE == GAME_PLAY: for event in pygame.event.get(): if event.type == pygame.QUIT: GAME_MODE = GAME_QUIT if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: goto = GAME_OVER mode = 'fade out' if mode == 'play': keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] or keys[pygame.K_a]: player.x_speed -= player.speed if keys[pygame.K_RIGHT] or keys[pygame.K_d]: player.x_speed += player.speed if keys[pygame.K_UP] or keys[pygame.K_w]: player.y_speed -= player.speed if keys[pygame.K_DOWN] or keys[pygame.K_s]: player.y_speed += player.speed for block in blocks: block.check_boundaries(GAME_WIDTH, hud.rect.top) if mode == 'play': tick += 1 untilNext -= 1 if untilNext <= 0: waveNum, untilNext, enemiesLeft = getNextWave(waveNum) if untilNext % 10 == 0: score += len(enemies.sprites()) * 5 if enemiesLeft > 0: enemyTick -= 1 if enemyTick <= 0: enemiesLeft -= 1 spawnEnemy() enemyTick = 60 if player.lives <= 0: goto = GAME_OVER mode = 'fade out' if tick % 3600 == 0: spawnNewLife() if tick == nextObstacle: spawnObstacle() nextObstacle = random.randint(3600, 7200) + tick blocks.update() if mode == 'fade in': darkness -= fadeSpeed elif mode == 'fade out': darkness += fadeSpeed if darkness <= 0: mode = 'play' elif darkness >= 100: GAME_MODE = goto if pygame.sprite.spritecollide(player, obstacles, False): if player.x_speed > player.y_speed: player.bounce(True, False) else: player.bounce(False, True) gameDisplay.blit(bg, (0, 0)) blocks.draw(gameDisplay) hud.clear() drawHUD() dimDisplay(darkness) clock.tick(60) pygame.display.update()