Skip to main content
 首页 » 编程设计

python之Sprite 不工作/不显示(Python/Pygame)

2024年12月31日8lyhabc

请帮助我,我正在开始游戏,但我的 Sprite 没有显示在屏幕上。看一下,我正在使用两个文件,其中包括 pygame 和 classes。我希望这些信息足够了。

冒险.py --

import pygame, random 
pygame.init() 
BROWN = (205,192,176) 
DEEPBROWN = (139,131,120) 
CL = (156,102,31) 
 
from LittleMan import LittleMan 
playerSprite = LittleMan(CL, 200, 300) 
 
size = (1000, 600) 
screen = pygame.display.set_mode(size) 
pygame.display.set_caption("Adventure") 
 
all_sprites_list = pygame.sprite.Group() 
 
playerSprite.rect.x = 200 
playerSprite.rect.y = 300 
 
 
carryOn = True 
 
clock = pygame.time.Clock() 
 
while carryOn: 
 
    for event in pygame.event.get(): 
        screen.fill(BROWN) 
        pygame.draw.rect(screen, DEEPBROWN, [55, 250, 900, 70],0) 
        all_sprites_list.draw(screen) 
        all_sprites_list.add() 
        all_sprites_list.update() 
        pygame.display.flip() 
 
        clock.tick(60) 
 
        if event.type == pygame.QUIT: 
 
              carryOn = False 
 
        if event.type==pygame.KEYDOWN: 
            if event.key==pygame.K_x: #Pressing the x Key will quit the game 
                    carryOn=False 
 
keys = pygame.key.get_pressed() 
if keys[pygame.K_LEFT]: 
    LittleMan.moveLeft(5) 
if keys[pygame.K_RIGHT]: 
    LittleMan.moveRight(5) 

LitlleMan.py --

import pygame 
 
CL = (156,102,31) 
WHITE = (255,255,255)    
 
class LittleMan (pygame.sprite.Sprite): 
 
    def __init__(self, color, width, height): 
 
        super().__init__() 
 
        self.image = pygame.Surface([50, 75]) 
        self.image.fill(CL) 
        self.image.set_colorkey(WHITE) 
 
        pygame.draw.rect(self.image, CL, [0, 0, width, height]) 
 
        self.rect = self.image.get_rect() 
 
        def moveRight(self, pixels): 
            self.rect.x += pixels 
 
        def moveLeft(self, pixels): 
            self.rect.x -= pixels 

有人知道为什么会这样吗?我到处都看过,但我在两个文件中完成了它,但似乎没有人对此有答案,如果有合适的答案,请链接它。谢谢。

请您参考如下方法:

我认为问题的真正症结在于代码没有将 playerSprite 添加到 all_sprites_list 中。如果 Sprite 不在此列表中,则 Sprite 更新和绘制调用不包括它。起初我以为 Sprite 的初始位置可能在屏幕外,所以我参数化了屏幕尺寸,并将 Sprite 定位在中间。

问题的代码中还有很多其他缩进问题,但我认为这些可能是由于将问题粘贴到 SO 中造成的。

我清理并重新组织了代码,它似乎可以运行,按向左/向右移动棕色框。

抱歉,我将两个文件合并在一起以使调试更容易。

import pygame, random 
 
pygame.init() 
BROWN     = (205,192,176) 
DEEPBROWN = (139,131,120) 
CL        = (156,102,31) 
WHITE     = (255,255,255) 
 
WINDOW_WIDTH=500 
WINDOW_HEIGHT=500 
 
# Setup the pyGame window 
size = (WINDOW_WIDTH, WINDOW_HEIGHT) 
screen = pygame.display.set_mode(size) 
pygame.display.set_caption("Adventure") 
 
class LittleMan (pygame.sprite.Sprite): 
 
    def __init__(self, color, width, height): 
        super().__init__() 
        self.image = pygame.Surface([50, 75]) 
        self.image.fill(CL) 
        self.image.set_colorkey(WHITE) 
        self.rect = self.image.get_rect() 
        self.rect.center = ( WINDOW_WIDTH//2 , WINDOW_HEIGHT//2 ) 
 
    def moveRight(self, pixels): 
        self.rect.x += pixels 
 
    def moveLeft(self, pixels): 
        self.rect.x -= pixels 
 
# Create the player sprite 
playerSprite = LittleMan(CL, 200, 300) 
 
# Add user sprite into PyGame sprites list 
all_sprites_list = pygame.sprite.Group() 
all_sprites_list.add(playerSprite); 
 
clock = pygame.time.Clock() 
carryOn = True 
while carryOn: 
 
    # Handle user input 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            carryOn = False 
 
        if event.type==pygame.KEYDOWN: 
            if event.key==pygame.K_x: #Pressing the x Key will quit the game 
                carryOn=False 
            keys = pygame.key.get_pressed() 
            if keys[pygame.K_LEFT]: 
                playerSprite.moveLeft(5) 
            if keys[pygame.K_RIGHT]: 
                playerSprite.moveRight(5) 
 
    # Update and Reapint the screen 
    screen.fill(BROWN) 
    pygame.draw.rect(screen, DEEPBROWN, [55, 250, 900, 70],0) 
    all_sprites_list.update() 
    all_sprites_list.draw(screen) 
    pygame.display.flip() 
    clock.tick(60) 

LittleMan 类不包含 update() 函数,all_sprites_list.update() 通常会调用该函数。我想您只是还不需要这部分内容。

编辑:关于 sprite update() 函数的更多注释 ~

Sprite 的 update() 函数在 all_sprites_list.update() 函数期间由 pygame 调用。所以这意味着添加到该组的任何 Sprite 都会自动更新。理想情况下,所有 Sprite 都具有更新功能,它处理 Sprite 的外观、位置和碰撞(等)。

此函数背后的想法是对 Sprite 进行任何更新。如果你有一个正在移动的 Sprite ,这个函数会计算下一个位置,并设置 Sprite 的 self.rect。或者该 Sprite 可能是动画的 - 更新函数会根据时间将 Sprite 的 image 设置为动画的下一帧。

显然,所有这些工作都可以在更新功能之外执行。但它为 Sprite 机制提供了一种简单且干净的编程机制。