【发布时间】: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 的实例)