Exemplo n.º 1
0
def reset():
    """Reset the screen and reset entities"""
    screen.blit(background, (0, 0))  # Erase screen
    pygame.display.update()
    global ship, asteroids, bullets, score, asteroid_spawn_count
    if ship:
        ship.reset(vec2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2))
    else:
        ship = Ship(screen, vec2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2))
        ship.setSpawnBullet(
            lambda pos, vel: bullets.append(Bullet(screen, pos, vel)))
    bullets = []
    score = 0
    asteroids = []
    asteroid_spawn_count = MIN_ASTEROIDS
    for i in range(asteroid_spawn_count):
        asteroids.append(Asteroid.genAsteroid(screen))
Exemplo n.º 2
0
def play():
    """Run the main game loop of Asteroids"""
    global ship, asteroids, bullets, currentscoreboard, bestscoreboard, state
    running = True
    clock = pygame.time.Clock()
    while running:
        # Handle events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                state = GameState.QUIT
                return
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_p:
                state = GameState.PAUSE
                return
            else:
                ship.handle_event(event)

        dirty_rects = []  # For updating screen

        # Erase entities froms screen
        if currentscoreboard != None:
            dirty_rects.append(
                screen.blit(background, SCOREBOARD_POS,
                            currentscoreboard.get_rect()))
        if bestscoreboard != None:
            dirty_rects.append(
                screen.blit(background, BESTSCORE_POS,
                            bestscoreboard.get_rect()))

        dirty_rects.append(
            screen.blit(background, ship.getupperleft(), ship.getbounds()))
        for bullet in bullets:
            dirty_rects.append(
                screen.blit(background, bullet.getupperleft(),
                            bullet.getbounds()))
        for ast in asteroids:
            dirty_rects.append(
                screen.blit(background, ast.getupperleft(), ast.getbounds()))

        # Update entities
        dt = clock.tick(FPS_LIM)
        speed = 1 / float(dt)
        ship.update(speed)
        for ast in asteroids:
            ast.update(speed)
        for i in range(len(bullets) - 1, -1, -1):
            if bullets[i].update(speed):
                bullets.remove(bullets[i])

        if running:  # running may be set to false in quit event
            running = checkCollisions()

        # Spawn more Asteroids if too few exist
        if len(asteroids) < asteroid_spawn_count:
            for i in range(asteroid_spawn_count - len(asteroids)):
                asteroids.append(Asteroid.genAsteroid(screen))

        # Show entities
        dirty_rects.append(ship.show())
        for ast in asteroids:
            dirty_rects.append(ast.show())
        for bullet in bullets:
            dirty_rects.append(bullet.show())

        currentscoreboard = scorefont.render(f"Score: {score}", False,
                                             FONT_COLOR)
        bestscoreboard = scorefont.render(f"Best: {maxscore}", False,
                                          FONT_COLOR)
        dirty_rects.append(screen.blit(currentscoreboard, SCOREBOARD_POS))
        dirty_rects.append(screen.blit(bestscoreboard, BESTSCORE_POS))

        pygame.display.update(dirty_rects)
    return False