繁体   English   中英

精灵不工作/不显示(Python/Pygame)

[英]Sprite not working/not being displayed (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)

小人.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 如果精灵不在此列表中,精灵更新和绘制调用不包括它。 一开始我以为精灵的初始位置可能在屏幕外,所以我参数化了屏幕尺寸,并将精灵定位在中间。

问题代码中也有很多其他缩进问题,但我认为这些问题可能是将问题粘贴到 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()函数在all_sprites_list.update()函数期间由 pygame 调用。 这意味着添加到该组的任何精灵都会准自动运行其更新。 理想情况下,所有精灵都有一个更新功能,它处理精灵的外观、位置和碰撞(等)。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM