【问题标题】:Pygame refuses to draw objectsPygame 拒绝绘制对象
【发布时间】:2025-12-24 17:05:13
【问题描述】:

所以我正在开发一种平台游戏。您将看到的代码只是它的开始。问题是由于某种原因 pygame 拒绝将我的对象绘制到屏幕上。一切似乎都很好,我什至没有收到任何错误。帮助将不胜感激。 :)

import pygame
from pygame.locals import *
pygame.init()
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
black = (0,0,0)
white = (255,255,255)
width = 1280
height = 720
score = 0
'create window'
screen = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()
'sprite groups'
all_sprites = pygame.sprite.Group()
blocks = pygame.sprite.Group()
class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((32,32))
        self.image.fill(green)
        self.rect = self.image.get_rect()
        self.rect.x = 640
        self.rect.y = 360
        self.speed = 0
    def update(self):
        pass
class Block(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((32,32))
        self.image.fill(blue)
        self.rect = self.image.get_rect()
        self.rect.x = 640
        self.rect.y = 395


p = Player()
all_sprites.add(p)
running = True
while running:
    all_sprites.draw(screen)

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    您需要在循环中包含screen.update(),以便屏幕实际刷新每一帧:

    while running:
        all_sprites.draw(screen)
        screen.update()
    

    【讨论】:

    • 哇哦。很抱歉这个超级明显的错误。感谢您的回答!