0
为了提高我的对象技能,我试图在python中制作一个游戏来学习如何处理对象,其中我有一个名为Player
的精灵,它显示从一个名为使用这个类image.gif
:Pygame:加载图像的精灵
class Player(pygame.sprite.Sprite):
def __init__(self, width, height):
super().__init__()
self.rect = pygame.Rect(width, height)
self.image = pygame.Surface([width, height])
self.image = pygame.image.load("image.gif").convert_alpha()
# More sprites will be added.
然后我用的是类下面的代码:
import Pysprites as pys
pygame.init()
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GRAY = (255,255,255)
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Game")
all_sprites_list = pygame.sprite.Group()
player = pys.Player(44,22)
all_sprites_list.add(player)
carryOn = True
clock=pygame.time.Clock()
while carryOn:
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
all_sprites_list.update()
clock.tick(60)
all_sprites_list.draw(screen)
pygame.display.flip()
pygame.display.update()
pygame.quit()
,并得到了错误:
Traceback (most recent call last):
File "Game1.py", line 14, in <module>
player = pys.Player(44,22)
File "Pysprites.py", line 9, in __init__
self.rect = pygame.Rect(width, height)
TypeError: Argument must be rect style object
我在做什么错?我怎样才能让用图像的精灵,而不会遇到这个错误?
您似乎只用2个参数调用Rect,它应该有4. – jgritty
https://www.pygame.org/docs/ref/rect.html – jgritty
Rect(left,top,width,height) - >矩形 或 RECT((左,上),(宽度,高度)) - >矩形 – jgritty