python-3.x 为什么我的上烟斗没有出现在PyGame中?

dwbf0jvd  于 2023-03-20  发布在  Python
关注(0)|答案(1)|浏览(141)

我只是想在PyGame里做一个Flappy Bird的克隆,但是上面的管道由于某种原因没有出现。我更新了屏幕,没有什么东西只是出现了。它在第一次到达一个小于0的x后变得不可见,并且它重置了位置,或者我们可以说它在第一次重置后变得不可见。请有人帮帮我

import pygame
import random

WIDTH, HEIGHT = 600, 700
BG_COLOR = (100,255,255)

PLAYER_WIDTH = PLAYER_HEIGHT = 50
PLAYER_COLOR = (255, 255, 50)
PLAYER_SPEED = 1

PIPE_COLOR = (100, 255, 100)
PIPE_WIDTH, PIPE_HEIGHT = 50, 300

velocity = 0

WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Flat Bird")


def draw(player, pipe_up, pipe_down):
    pygame.draw.rect(WIN, PLAYER_COLOR, player)
    pygame.draw.rect(WIN, PIPE_COLOR, pipe_up)
    pygame.draw.rect(WIN, PIPE_COLOR, pipe_down)
    pygame.display.update()


def move_player(player,speed,velocity):
    keys=pygame.key.get_pressed()
    velocity -= speed
    if keys[pygame.K_SPACE]:
        velocity = 1
    player.y -= velocity
    print(velocity)


def generate_pipe_pos(pipe_up, pipe_down):
    offset = random.randint(-40, 40)
    pipe_up.height = -40
    pipe_down.height = 440
    pipe_up.x = 600
    pipe_down.x = 600



def main():
    player = pygame.Rect(100, 350, PLAYER_WIDTH, PLAYER_HEIGHT)
    pipe_up = pygame.Rect(600, -40, PIPE_WIDTH, PIPE_HEIGHT)
    pipe_down = pygame.Rect(600, 440, PIPE_WIDTH, PIPE_HEIGHT)

    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        move_player(player, PLAYER_SPEED, velocity)
        pipe_up.x -= PLAYER_SPEED
        pipe_down.x -= PLAYER_SPEED
        if pipe_up.x < 0:
                generate_pipe_pos()
        draw(player, pipe_up, pipe_down)
        WIN.fill(BG_COLOR)
    pygame.quit()

main()

我试着用和下管一样的方法来做,我期望它能正常工作并表现出来。

hrirmatl

hrirmatl1#

如果将管道放置在起点,则将管道的高度更改为-40。您可能希望设置y坐标而不是height

def generate_pipe_pos(pipe_up, pipe_down):
    offset = random.randint(-40, 40)
    pipe_up.y = -40
    pipe_down.y = 440
    pipe_up.x = 600
    pipe_down.x = 600

相关问题