import pygame
pygame.init()
class PlayerNinja(pygame.sprite.Sprite):
x = 0
y = 0
width = 112
height = 102
def __init__(self, x, y):
self.x = x
self.y = y
super(PlayerNinja, self).__init__()
self.image = pygame.image.load("player.png")
.
.
.
###########################################################################
def main():
from PlayerNinja import *
print "Testing"
player = PlayerNinja(10, 10)
a = player.getX()
print "X: " + str(a)
player.setX(50)
b = player.getX()
print "new X: " + str(b)
player.setY(50)
c = player.getY()
print "Y: " + str(c)
screenWidth = 800
screenHeight = 700
screen = pygame.display.set_mode((screenWidth, screenHeight))
white = (255,255,255)
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(white)
screen.blit(player, (b, c))
pygame.display.flip()
if __name__ == "__main__": main()
当我运行main时,我收到错误
screen.blit(player, b, c)
TypeError: argument 1 must be pygame.Surface, not PlayerNinja"
我尝试用pygame.Surface
替换部分行。当我使用具有不同字符的相同格式(screen.blit(player, (x,y))
)时它起作用...我认为这是因为在这种情况下我将图像加载到类的构造函数中,而另一次我不是#&使用一个班级。我不得不查看super().__init__()
- 部分,所以也许是它的一部分?
答案 0 :(得分:0)
您需要编码
screen.blit(player.image, (b, c))
因为.blit
方法需要 pyagme.Surface
- 对象(加载的图像也是PyGame中的表面)作为第一个参数。您需要将曲面实例传递给.blit()
,PlayerNinja
存储在player
精灵对象(" self.image
")中作为属性super().__init__()
。
我认为其他部分({{1}})很好。 :)