我在pygame终端屏幕上遇到了一个难题,我的代码中没有出现错误,但由于某种原因,它重新调整为false

问题描述

我需要帮助,我的脚本返回假并触发结束屏幕,但它仅应触发 当我在屏幕底部或顶部以及管道中达到某个坐标时,我正在遵循本教程指南 https://www.youtube.com/watch?v=UZg49z76cLw&list=TLPQMjAwOTIwMjDNZAgyx5jIqA&index=1

import pygame,sys,random


def draw_floor():
    screen.blit(floor_surface,(floor_animation,400))
    screen.blit(floor_surface,(floor_animation + 275,400))

def create_pipe():
    random_pipe_pos = random.choice(pipe_height)
    bottom_pipe = pipe_surface.get_rect(midtop = (350,random_pipe_pos))
    top_pipe = pipe_surface.get_rect(midbottom = (350,random_pipe_pos - 150))
    return bottom_pipe,top_pipe
SPAWNPIPE: command and gets stored in the pipe list

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

    return  pipes


def draw_pipes(pipes):
    for pipe in pipes:
        if pipe.bottom >= 512:
            screen.blit(pipe_surface,pipe)
        else:
            flip_pipe = pygame.transform.flip(pipe_surface,False,True)
            
            screen.blit(flip_pipe,pipe)

def check_collision(pipes):
    for pipe in pipes:
        if bird_rect.colliderect(pipe):
            
            return False
            

    if bird_rect.top <= -150 or bird_rect.bottom >= 400:
        return False

    return True



pygame.init()

screen = pygame.display.set_mode((275,512))
clock = pygame.time.Clock()
variables
gravity = 0.25
bird_movement = 0
game_active = True

bg_surface = pygame.image.load('C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/sprites/background-day.png').convert()
floor_surface = pygame.image.load('C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/sprites/base.png').convert()
floor_animation = 0

bird_surface = pygame.image.load('C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/sprites/bluebird-midflap.png').convert()
bird_rect = bird_surface.get_rect(center = (100,256))

pipe_surface = pygame.image.load('C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/sprites/pipe-green.png').convert()
pipe_list = []
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE,1200)
pipe_height = [200,300,350]


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 -= 7
        if event.type == SPAWNPIPE:
            pipe_list.extend(create_pipe())



    screen.blit(bg_surface,(0,0))
    if game_active:
        #bird
        bird_movement += gravity
        bird_rect.centery += bird_movement
        screen.blit(bird_surface,bird_rect)
        game_active = check_collision(pipe_list)
        
        #pipes
        pipe_list = move_pipes(pipe_list)
        draw_pipes(pipe_list)

    #floor
    floor_animation -= 3
    draw_floor()
    if floor_animation <= -275:
        floor_animation = 0
    screen.blit(floor_surface,400))

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)