【问题标题】:Having trouble using sprites in pygame在 pygame 中使用精灵时遇到问题
【发布时间】:2023-03-05 17:12:01
【问题描述】:

我一直在尝试学习 pygame,尤其是如何在 pygame 中使用精灵。然而,经过一些实验,下面的代码是我最终得到的,我想知道为什么我的播放器类没有出现。

from pygame.locals import *
import pygame

pygame.init()
size = width, height = 500, 700
screen = pygame.display.set_mode(size)
plr_g = pygame.sprite.Group()

blue = (0,206,209)

class player(pygame.sprite.Sprite):
    def __init__(self, s_x, s_y):
        pygame.sprite.Sprite.__init__(self, plr_g)
        self.s_x = 300
        self.s_y = 300
        self.image.fill(blue)
        #self.plr_img = pygame.image.load("Xpic.png")
        plr_g.add(self)

        self.image = pygame.screen([s_x, s_y])

        self.rect = self.image.get_rect()

plr_g.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()


    plr_g.draw(screen)

    screen.fill((50, 50, 50))

    #player.draw(screen)
    plr_g.draw(screen)

    pygame.display.flip()

【问题讨论】:

  • 不确定这是答案还是错字,但您实际上并没有创建类的实例(即 p = player(300,300)),然后将其添加到您的 plr_g 中。另外作为奖励提示,请查看 PEP 0008;除其他外,类应该在 CapWords 中命名......所以你会有类 Player(然后可能有一个名为 player 的实例)

标签: python class pygame


【解决方案1】:

首先,您并没有正确使用 sprite 类。当你定义一个新的精灵时,你至少需要两件事:一个图像和一个矩形。图像将被显示,矩形定义了它在屏幕上绘制时的放置位置。

对于图像,要使用简单的矩形,只需创建一个 pygame Surface 并填充它。然后只需使用内置的 get_rect() 命令来定义矩形:

self.image = pygame.Surface([s_x, s_y])
self.image.fill(blue)
self.rect = self.image.get_rect()

然后,您实际上需要创建对象的实例。 Python 标准是用大写字母命名类,以区别于变量:

class Player(pygame.sprite.Sprite):

然后你像这样创建一个玩家精灵的实例:

player = Player(50, 50)

在您的游戏循环中,您在精灵组上调用 draw 两次,一次在填充屏幕之前,一次在填充屏幕之后 - 您只需执行一次,反正第一个被绘制。您也可以在您的组上调用 update(),尽管在您为精灵定义更新方法之前它不会做任何事情。

这是一些注释/清理过的代码:

from pygame.locals import *
import pygame

pygame.init()
# use caps to indicate constants (Python convention)
SIZE = WIDTH, HEIGHT = 500, 700
screen = pygame.display.set_mode(SIZE)
# consider naming your group something more friendly
plr_g = pygame.sprite.Group()

BLUE = (0, 206, 209)

class Player(pygame.sprite.Sprite):
    # assuming s_x and s_y are supposed to be size. What about using width/height?
    def __init__(self, width, height):
        pygame.sprite.Sprite.__init__(self, plr_g)
        # what are these for?
        # self.s_x = 300
        # self.s_y = 300
        self.image = pygame.Surface([width, height])
        self.image.fill(BLUE)
        self.rect = self.image.get_rect()
        # this isn't necessary, since you're adding to the group in the __super__ call
        # plr_g.add(self)
        # set your location via the rect:
        self.rect.center = (WIDTH / 2, HEIGHT / 2)


player = Player(50, 50)
while True:
    for event in pygame.event.get():
        # don't put multiple statements on a single line
        if event.type == pygame.QUIT:
            sys.exit()

    # update sprites in group
    plr_g.update()

    # fill screen, then draw
    screen.fill((50, 50, 50))
    plr_g.draw(screen)
    pygame.display.flip()

现在,尝试在你的精灵中定义一个更新方法,通过改变矩形坐标使其移动,例如

self.rect.x += 1  

您还应该查看http://www.pygame.org/docs/ref/time.html#pygame.time.Clock 以了解如何设置帧速率。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多