pygame 屏幕不显示任何内容

pygame screen not showing anything

我正在使用 pygame library.But 在 Python 中编写一个 space 射击程序 library.But 显示器没有显示我编程显示的任何内容。代码似乎没有任何问题。这是主文件。

import sys
import pygame
import bullet
from settings import Setting
from ship import Ship
from pygame.sprite import Group
import functions as gf

def run_game():
    pygame.init()
    gmSet = Setting()
    screen = pygame.display.set_mode((gmSet.screen_w, gmSet.screen_h))
    pygame.display.set_caption("Alien Invasion")
    bg_color = gmSet.bg_color

    ship = Ship(screen)
    bullets = Group()

    while True:
        gf.check_events(ship, gmSet, screen, bullets)
        bullets.update()
        for bullet in bullets.copy():
            if bullet.rect.bottom <= 0:
                bullets.remove(bullet)
        print(len(bullets))
        gf.update_screen(gmSet, screen, ship, bullets)
        
        
run_game()

这是 update_screen 函数。

def update_screen(settings, screen, ship, bullets):
    screen.fill(settings.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.place() 

Python 在运行时没有给出任何错误,我可以说程序也没有陷入 while 循环。 如果我只提供了有限的信息,请告诉我

您需要更新显示。 您实际上是在 Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip().

上绘图

pygame.display.flip():

This will update the contents of the entire display.

update_screen 函数中或在应用程序循环结束时调用 pygame.display.flip()

while True:
    gf.check_events(ship, gmSet, screen, bullets)
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)
    print(len(bullets))
    gf.update_screen(gmSet, screen, ship, bullets)
        
    pygame.display.flip()    # <---