Python 精灵不显示pygame和元组无属性
我研究这个问题已经一个星期了。通过这里和其他来源进行研究。。。还是解决不了。将显示曲面,但不会显示精灵 代码是:Python 精灵不显示pygame和元组无属性,python,python-2.7,pygame,Python,Python 2.7,Pygame,我研究这个问题已经一个星期了。通过这里和其他来源进行研究。。。还是解决不了。将显示曲面,但不会显示精灵 代码是: import sys from pygame.locals import * import pygame class teemoShows(pygame.sprite.Sprite): def __init__(self, position): pygame.sprite.Sprite.__init__(self) teemoFile = "
import sys
from pygame.locals import *
import pygame
class teemoShows(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
teemoFile = "teemoX.png"
self.image = pygame.image.load(teemoFile).convert()
self.rect = self.image.get_rect()
self.rect.x = position[0]
self.rect.y = position[1]
def update(self):
#update sprites .. is this correct?
#self.rect = self.rect.move([0, yposition - self.rect.y])
pygame.update(self)
pygame.init()
screen = pygame.display.set_mode((500, 400))
character = teemoShows([screen.get_rect().x, screen.get_rect().y])
# create sprite group and add character
teemoFile = pygame.sprite.Group()
teemoFile.add(character)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# main loop
pygame.display.flip()
screen.fill((0,0,0))
teemoFile.draw(screen)
teemoFile.update()
它给我的错误是:
line 31, in <module>
character = teemoShows([screen.get_rect().x, screen.get_rect().y])
File "C:\Python27\arminProject_two\textBased_adv.py", line 21, in __init__
self.rect.x = position[0]
NameError: module object has no attribute 'update.'
line character=screen.get_rect.x,screen.get_rect.y将变量字符分配给一个元组,而实际上您正试图使其成为teemoShows类的实例,这就是为什么会出现错误,因为元组没有更新方法
以下是正确的方法:
character = teemoShows([screen.get_rect().x, screen.get_rect().y])
#In your main-loop
character.update()
character.draw(screen)
通过这种方式,您可以创建一个teemoShows实例,并将元组作为position的参数提供请设置intendation,然后熟悉和。哇,正因为如此,我收到了一个-1……我们这里的社区太棒了。意图是正确的。在代码中,字符是元组而不是矩形对象。如果要使用更新和绘制方法,应使用矩形对象。方法get_rect**kwargs返回一个新的矩形,这是您希望使用的吗?我可以将图像作为矩形对象传递并能够使用方法get_rect吗?我应该将character.update和.drawscreen放在哪里?主循环是否与character=teemoShows[]位置相同?在循环中,因为要重新绘制精灵的每个循环,您通常会覆盖更新方法,以按照您喜欢的方式移动精灵更改,现在它说“teemoShows”没有定义。在您发布的代码中,teemoShows类中有一些代码不应该缩进哪个代码?我可以尝试缩进和不缩进代码片段,以查看错误所在的位置,但如果您看到它。指出这一点会更快。