pygame屏幕没有显示任何内容

2024-10-05 22:04:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用pygame库用Python编写一个spaceshooter程序,但屏幕上没有显示我编程要显示的任何内容。代码似乎没有问题。这是主文件

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循环如果我只提供了有限的信息,请告诉我


Tags: fromimport程序settingsupdatescreensettingpygame
1条回答
网友
1楼 · 发布于 2024-10-05 22:04:10

您需要更新显示。 您实际上是在^{}对象上绘制的。如果在与PyGame显示屏关联的表面上绘制,则不会立即在显示屏上看到。当使用^{}^{}更新显示时,更改将变为visibel

^{}

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()    # < -

相关问题 更多 >