【问题标题】:pipes spawning too fast in flappy bird pygame飞扬的鸟pygame中的管道产卵太快
【发布时间】:2020-11-18 11:57:19
【问题描述】:

我正在编写这个 flappy bird pygame 教程,当我测试代码时,管道生成得太快了。我正在使用教程,因为我对 python 还很陌生,希望能得到任何帮助。谢谢。这是教程网站的链接:https://github.com/clear-code-projects/FlappyBird_Python/blob/master/flappy.py

这是我目前的代码。

import pygame
import sys
import os
from random import randint


def draw_floor():
    screen.blit(floor_surface,(floor_x_pos,750))
    screen.blit(floor_surface,(floor_x_pos + 576,750))


def create_pipe():
    new_pipe = pipe_surface.get_rect(midtop = (288,375))
    return new_pipe

def move_pipes(pipes):
    for pipe in pipes:
        pipe.centerx -= 5
    return pipes

def draw_pipes(pipes):
    for pipe in pipes:
        if pipe.bottom >= 750:
            screen.blit(pipe_surface,pipe)

pygame.init()
screen = pygame.display.set_mode((576,855))
clock = pygame.time.Clock()

# Game variables
gravity = 0.25
bird_movement = 0

bg_surface = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bg.png')).convert_alpha())

floor_surface = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','base.png')).convert_alpha())
floor_x_pos = 0

bird_surface = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bird2.png')).convert_alpha())
bird_rect = bird_surface.get_rect(center = (100,427))


pipe_surface = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','pipe.png')))
pipe_list = []
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE,5000)
pipe_height = [300,500,700]

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bird_movement = 0
                bird_movement -= 12

    if event.type == SPAWNPIPE:
        pipe_list.extend(create_pipe())
        
            
    # bird
    screen.blit(bg_surface,(0,0))

    bird_movement += gravity
    bird_rect.centery += int(bird_movement)
    screen.blit(bird_surface,bird_rect)

    # pipes
    pipe_list = move_pipes(pipe_list)
    draw_pipes(pipe_list)

    
    floor_x_pos -= 1
    draw_floor()
    if floor_x_pos <= -576:
        floor_x_pos = 0


    pygame.display.update()
    clock.tick(120)`

【问题讨论】:

  • 尝试在代码末尾添加睡眠,sleep(50)

标签: python-3.x pygame flappy-bird-clone


【解决方案1】:

问题在于,一旦您获得第一个 SPAWNPIPE 事件,用于添加新管道的 event.type 条件变为 True。

但是,由于缩进不当,每一帧都会重复检查该条件,直到接收到下一个事件。这意味着代码在这段时间内每帧都会不断产生管道。

修复缩进,将管道事件检查带回事件 for 循环中:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bird_movement = 0
                bird_movement -= 12

        if event.type == SPAWNPIPE:                        # <<-- HERE  
            pipe_list.extend(create_pipe())                # <<-- AND HERE

这使得 SPAWNPIPE 检查仅在实际收到事件时执行。

【讨论】:

  • 谢谢你,这对我帮助很大。非常感谢。
【解决方案2】:

缩进 SPAWNPIPE 检查:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bird_movement = 0
                bird_movement -= 12

        if event.type == SPAWNPIPE:    # indent this
             pipe_list.extend(create_pipe())

【讨论】:

  • 非常感谢。这是我的第一个堆栈溢出问题,你们如何快速帮助我让我想更多地使用堆栈溢出。谢谢
  • 请接受答案,以便从“无答案”列表中删除此帖子。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
相关资源
最近更新 更多