首页 > 解决方案 > 雪碧没有出现在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()

标签: pygamegame-development

解决方案


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

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

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


推荐阅读