Sprite 没有出现在 pygame

Sprite is not showing up in pygame

我目前正在通过教程学习 pygame。所以在下面的代码中,我按照教程中显示的每一步都进行了操作,但结果仍然不同。我的精灵没有出现。一点帮助可能会有用。

import pygame
import random

WIDTH = 720
HEIGHT = 480
FPS = 30

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0 ,0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

class Player(pygame.sprite.Sprite):
    #Sprite for the player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50)) 
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH / 2, HEIGHT / 2)

pygame.init()
pygame.mixer.init()                                 #----------------for sound and music
screen = pygame.display.set_mode((WIDTH, HEIGHT))   #-----------------setting up screen
pygame.display.set_caption("My Game")               #----------------game title
clock = pygame.time.Clock()                         #----------------to check 

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

running = True                                      #--------------variable to control start and stop of game
while(running):                                     #--------------Game loop begins
# Keep loop running at right speed/FPS
    clock.tick(FPS)

# Process Input
    for event in pygame.event.get():
        if event.type == pygame.QUIT:               #--------------Check for closing the window
            running = False

# Update
    all_sprites.update()

# Draw/Render
    all_sprites.draw(screen)
    screen.fill(BLACK)                          
    pygame.display.flip()                           

pygame.quit()

在绘制组中的精灵之前,您必须清除显示:

screen.fill(BLACK)        
all_sprites.draw(screen)                  
pygame.display.flip()

screen.fill(BLACK) 用黑色填充整个 screen 表面。该操作会在所有表面上绘制,并且之前绘制的所有内容都将丢失。
pygame.display.flip() 通过关联表面更新 window。当您之前执行 screen.fill(BLACK) 时,window 将变为全黑。