首页 > 解决方案 > Sprite 不工作/不显示(Python/Pygame)

问题描述

请帮助我,我正在开始游戏,但我的精灵没有显示在屏幕上。看一下,我正在使用两个文件,其中包括 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)

LittleMan.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

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

标签: pythonpygamesprite

解决方案


我认为问题的真正症结在于代码没有添加playerSpriteall_sprites_list. 如果 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()函数在函数期间由 pygameall_sprites_list.update()调用。这意味着任何添加到该组的精灵,其更新都会准自动运行。理想情况下,所有精灵都有更新功能,它处理精灵的外观、位置和碰撞(等)。

这个函数背后的想法是对精灵进行任何更新。所以你们中有一个正在移动的精灵,这个函数将计算下一个位置,并设置精灵的self.rect. 或者也许那个精灵是动画的——更新函数会根据image时间将精灵设置为动画的下一帧。

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


推荐阅读