pygame window 显示黑屏

pygame window showing blank screen

我在 pygame 中做了很多修改,但是每当我 运行 这个程序时,我得到的只是一个空白屏幕(没有错误)。我知道这不是我的代码的问题,因为我从互联网上复制了另一个人的程序,但它甚至没有用。我在 Mac os。大约 1 个月前,我编写了这段代码,当时它可以正常工作,但现在我得到的只是一个空白屏幕。我该如何解决这个问题?(我也查看了其他类似的问题并且 none 的 os 解决方案有效) 无论如何,这是我的代码(使用了带有 tim 教程的技术):

import pygame
import neat
import time
import os
import random

WINDOW_WIDTH = 500
WINDOW_HEIGHT = 800

BIRD_IMGS = [pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bird1.png'))),pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bird2.png'))),pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bird3.png')))]
PIPE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','pipe.png')))
BASE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','base.png')))
BG_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bg.png')))

class Bird:
    IMGS = BIRD_IMGS
    MAX_ROTATION = 25
    ROT_VEL = 20
    ANIMATION_TIME = 5

    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.tilt = 0
        self.tick_count = 0
        self.vel = 0
        self.height = self.y
        self.img_count = 0
        self.img = self.IMGS[0]

    def jump(self):
        self.vel = -10.5
        self.tick_count = 0
        self.height = self.y
    def move(self):
        self.tick_count += 1

        d = self.vel * self.tick_count + 1.5 * self.tick_count**2
        if d >= 16:
            d = 16
        if d < 0:
            d -= 2

        self.y += d

        if d < 0 or self.y < self.y + 50:
            if self.tilt < self.MAX_ROTATION:
                self.tilt = self.MAX_ROTATION
        else:
            if self.tilt > -90:
                self.tilt -= self.ROT_VEL
    def draw(self,win):
        self.img_count += 1

        if self.img_count < self.ANIMATION_TIME:
            self.img = self.IMGS[0]

        elif self.img_count < self.ANIMATION_TIME*2:
            self.img = self.IMGS[1]
        elif self.img_count < self.ANIMATION_TIME*3:
            self.img = self.IMGS[2]
        elif self.img_count < self.ANIMATION_TIME*4:
            self.img = self.IMGS[1]
        elif self.img_count == self.ANIMATION_TIME*4+1:
            self.img = self.IMGS[0]
            self.img_count = 0
        if self.tilt <= 80:
            self.img = self.IMGS[1]
            self.img_count = self.ANIMATION_TIME*2

        rotated_img = pygame.transform.rotate(self.img, self.tilt)
        new_rect = rotated_img.get_rect(center=self.img.get_rect(topleft = (self.x, self.y)).center)
        win.blit(rotated_img, new_rect.topleft)

    def get_mask(self):
        return pygame.mask.from_surface(self.img)

def draw_window(win,bird):
    win.blit(BG_IMG, (0,0))
    bird.draw(win)
    pygame.display.update()

def main():
    bird = Bird(200,200)
    win = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
    clock = pygame.time.Clock()

    running = True
    while running:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        bird.move()
        draw_window(win, bird)
    pygame.quit()
    quit()


if __name__ == '__main__':
    main()

您的代码在主函数中有 win = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))。我通常将 pygame.display.set_mode 放在主函数之外。你也没有添加pygame.init。我认为这是问题所在。

我休息了几个月,认为我应该重新安装最新版本的 pygame。一旦我这样做了,我就在 atom 和 Jupyter notebook 上都试过了,但是 none 它们都工作了。我尝试使用常规 python IDE,结果出人意料地有效。