将精灵添加到组

2024-04-16 23:05:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在屏幕上显示一组精灵,但是,我下面的代码显示一个空白屏幕。我已经做了一段时间了,我不知道为什么这不起作用,逻辑似乎是正确的。主要问题发生在sprite方法中,我试图在一个随机位置添加sprite。然后在gameLoop方法中绘制精灵。在

有什么建议吗?在

import pygame, sys, random
pygame.init()
troll = pygame.image.load("image.png")
black = (0, 0, 0)

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 400
sprite_width = 5
sprite_height = 5
spriteCount = 5


class Sprite(pygame.sprite.Sprite):
    pygame.init()
    sprite = pygame.sprite.Group()
    clock = pygame.time.Clock()

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

    def __init__(self, Image, pos):
        pygame.sprite.Sprite.__init__(self)
        self.image =pygame.Surface([0, 0])
        self.rect = self.image.get_rect()
        self.rect.topleft = pos

    @classmethod  
    def sprite(self):
       for i in range(spriteCount):
            tmp_x = random.randrange(0, SCREEN_WIDTH)
            tmp_y = random.randrange(0, SCREEN_HEIGHT)
            # all you have to do is add new sprites to the sprite group
            self.sprite.add(Sprite(troll, [tmp_x, tmp_y]))

    def update(self):
        self.rect.x += 1
        self.rect.y += 2
        if self.rect.y > SCREEN_HEIGHT:
            self.rect.y = -1 * sprite_height
        if self.rect.x > SCREEN_WIDTH:
            self.rect.x = -1 * sprite_width

    @classmethod
    def setImage(self,Image):
        self.image=pygame.image.load(Image)

    @classmethod       
    def gameLoop(self):
        screen = pygame.display.set_mode([640, 400])
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
            screen.fill(black)

            # to update or blitting just call the groups update or draw
            # notice there is no for loop
            # this will automatically call the individual sprites update method

            self.actor.update()
            self.actor.draw(screen)

            pygame.display.update()
            self.clock.tick(20)

Sprite.sprite()
Sprite.setImage("image.png")
Sprite.gameLoop()

Tags: rectimageselfinitdefupdaterandomwidth
1条回答
网友
1楼 · 发布于 2024-04-16 23:05:56
@classmethod
def setImage(self,Image):
    self.image=pygame.image.load(Image)

这里,您应该将第一个参数命名为cls,而不是{},因为它是一个类方法,而不是实例方法。另外,参数名应该是小写(image而不是Image)。在


^{pr2}$

这里也一样。另外,我不知道为什么你的游戏循环是你的Sprite类的一部分。另外,您不应该首先将您的类命名为Sprite,因为它只会与pygame的Sprite类混淆。在


class Sprite(pygame.sprite.Sprite):
    ...
    sprite = pygame.sprite.Group()
    ...

    @classmethod  
    def sprite(self):
        ...

这里有一个名为sprite的字段和一个类级函数sprite。这将不起作用,因为方法将覆盖字段。因此,每当您想访问字段sprite,您将访问函数sprite。在


@classmethod       
def gameLoop(self):
    ...
    self.actor.update()

Sprite.actor不退出。你从来没有创造过这样的领域。在


def __init__(self, Image, pos):
    pygame.sprite.Sprite.__init__(self)
    self.image =pygame.Surface([0, 0])
    self.rect = self.image.get_rect()
    self.rect.topleft = pos

这里,传递一个名为Image的参数。但是,您不使用它,而是使用空的Surface。在


以下是您的代码的运行版本:

import pygame, sys, random

pygame.init()
troll = pygame.image.load("image.png")
black = pygame.color.Color('black')
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 400
spriteCount = 5


class Sprite(pygame.sprite.Sprite):
    actors = pygame.sprite.Group()
    clock = pygame.time.Clock()

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

    def __init__(self, image, pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.topleft = pos

    @classmethod  
    def init(self):
       for i in range(spriteCount):
            tmp_x = random.randrange(0, SCREEN_WIDTH)
            tmp_y = random.randrange(0, SCREEN_HEIGHT)
            self.actors.add(Sprite(troll, [tmp_x, tmp_y]))

    def update(self):
        self.rect.x += 1
        self.rect.y += 2
        if self.rect.y > SCREEN_HEIGHT:
            self.rect.y = -1 * self.rect.height
        if self.rect.x > SCREEN_WIDTH:
            self.rect.x = -1 * self.rect.width

    @classmethod       
    def gameLoop(self):
        screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
            screen.fill(black)

            self.actors.update()
            self.actors.draw(screen)

            pygame.display.update()
            self.clock.tick(20)

Sprite.init()
Sprite.gameLoop()

相关问题 更多 >