简体   繁体   English

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

[英]Sprite not working/not being displayed (Python/Pygame)

Please help me I am starting a game and my sprite is not showing on screen.请帮助我,我正在开始游戏,但我的精灵没有显示在屏幕上。 Take a look, I am using two files, which include pygame and classes.看一看,我使用了两个文件,其中包括 pygame 和 classes。 I hope that's enough information.我希望这是足够的信息。

Adventure.py --冒险.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 --小人.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

Anyone know why this could be?有谁知道为什么会这样? I've looked everywhere but I've done it in two files and no-one seems to have an answer to that and if there is a decent answer please link it.我到处找,但我已经在两个文件中完成了它,似乎没有人对此有答案,如果有合适的答案,请链接它。 Thank you.谢谢你。

I think the real crux of the problem is the code is not adding playerSprite to the all_sprites_list .我认为问题的真正症结在于代码没有将playerSprite添加到all_sprites_list If a sprite is not in this list, the sprite update and paint calls do not include it.如果精灵不在此列表中,精灵更新和绘制调用不包括它。 At first I thought the initial position of the sprite may be off-screen, so I parameterised the screen dimensions, and positioned the sprite in the middle.一开始我以为精灵的初始位置可能在屏幕外,所以我参数化了屏幕尺寸,并将精灵定位在中间。

There's a bunch of other indentation issues in the question's code too, but I think these may be from pasting the question into SO.问题代码中也有很多其他缩进问题,但我认为这些问题可能是将问题粘贴到 SO 中。

I cleaned-up and re-organised the code, it seems to run, and pressing Left/right moves the brown box.我清理并重新组织了代码,它似乎可以运行,然后按左/右移动棕色框。

I merged both files together to make my debugging easier, my apologies.我将这两个文件合并在一起以使我的调试更容易,我很抱歉。

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)

The LittleMan class does not include an update() function, which all_sprites_list.update() would normally call. LittleMan类不包含update()函数, all_sprites_list.update()通常会调用该函数。 I expect you just haven't needed this part yet.我想你只是还没有需要这部分。

EDIT: More notes on the sprite update() function ~编辑:关于精灵更新()函数的更多注释~

The sprite's update() function is called by pygame during the all_sprites_list.update() function. sprite 的update()函数在all_sprites_list.update()函数期间由 pygame 调用。 So that means any sprite added to this group, has its update run quasi-automatically.这意味着添加到该组的任何精灵都会准自动运行其更新。 Ideally all sprites have an update function, and it handles the look, position and collisions (etc.) of the sprite.理想情况下,所有精灵都有一个更新功能,它处理精灵的外观、位置和碰撞(等)。

The idea behind this function is to do any updates to the sprite.这个函数背后的想法是对精灵进行任何更新。 So of you had a sprite that was moving, this function would calculate the next position, and set the sprite's self.rect .所以你有一个正在移动的精灵,这个函数会计算下一个位置,并设置精灵的self.rect Or perhaps that sprite is animated - the update function would set the sprite's image to the next frame of the animation based on the time.或者也许那个精灵是动画的——更新函数会根据时间将精灵的image设置为动画的下一帧。

Obviously all this work can be performed outside of the update function.显然,所有这些工作都可以在更新功能之外执行。 But it provides a simple and clean programming mechanism for sprite mechanics.但它为精灵机制提供了一种简单而干净的编程机制。

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

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