【发布时间】:2016-08-19 12:16:10
【问题描述】:
我是编程新手,我的代码中不断出现此错误。这是我的完整代码: 导入系统、pygame、数学; 从 pygame.locals 导入 *; 从系统导入退出 pygame.init()
blk = pygame.Color(0,0,0)
BG = ('BG.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
#mousec = pygame.image.load(mouse_c).convert_alpha()
B_G = pygame.image.load(BG).convert_alpha()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
fpsclock = pygame.time.Clock()
Shrek = pygame.image.load('SHORK.png').convert_alpha()
Shrek_Rect = Shrek.get_rect()
class Shork(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('SHORK.png')
screen = pygame.display.get_surface()
self.x = 62
self.y = 50
self.direction = "down"
def Moving(self):
if self.direction == "right":
self.x += 2
elif self.direction == "left":
self.x -= 2
elif self.direction == "down":
self.y += 2
elif self.direction == "up":
self.y -= 2
def Path(self): # Moves the "SHORK" image on the screen
if self.x == 62 and self.y == 538:
self.direction = "right"
if self.x == 246 and self.y == 538:
self.direction = "up"
if self.x == 246 and self.y == 366:
self.direction = "left"
if self.x == 176 and self.y == 366:
self.direction = "up"
if self.x == 176 and self.y == 114:
self.direction = "right"
if self.x == 530 and self.y == 114:
self.direction = "down"
if self.x == 530 and self.y == 366:
self.direction = "left"
if self.x == 460 and self.y == 366:
self.direction = "down"
if self.x == 460 and self.y == 538:
self.direction = "right"
if self.x == 644 and self.y == 538:
self.direction = "up"
if self.y == 0:
sys.exit()
Shork = Shork()
Run = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
print("test1")
elif event.type == MOUSEBUTTONDOWN and event.button == 3:
print("test3")
while Run:
fpsclock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
Run = False
pos = pygame.Shrek_Rect
angle = 360-math.atan2(pos[1]-300,pos[0]-400)*180/math.pi
rotimage = pygame.transform.rotate(B_G,angle)
screen.fill(blk)
Shork.Moving()
Shork.Path()
screen.blit(Shork.image, (Shork.x, Shork.y))
pygame.display.update()
rect = rotimage.get_rect(center=(400,300))
screen.blit(rotimage,rect)
pygame.display.update()
#Rotates the BG image on the screen
这是我的错误发生的地方:
pos = pygame.Shrek_Rect
angle = 360-math.atan2(pos[1]-300,pos[0]-400)*180/math.pi
rotimage = pygame.transform.rotate(B_G,angle)
错误提示:
Traceback (most recent call last):
File "turny.py", line 96, in (module)
pos = pygame.Shork_Rect
AttributeError: 'module' object has no attribute 'Shrek_Rect'
程序的重点是当 BG 图像在屏幕上移动时将其旋转到 SHORK 图像。我从一些旧代码中重新调整了这段代码的用途,这些代码旨在使 BG 跟随光标。我被困在这个错误上,无法弄清楚。
此外,如果您将 Shrek_Rect 替换为错误为 BG 的“mouse.get_pos()”,则会跟随玩家鼠标。
【问题讨论】:
-
我在任何地方都没有看到
pygame.Shrek_Rect定义,并且在我浏览文档时 pygame 没有任何属性Shrek_Rect。你把它和pygame.Rect混淆了吗? -
@AlexanderHuszagh 它是在第 11 行定义的 :)
-
啊没看到。好吧,这解决了我的问题:D
-
@AlexanderHuszagh 比你的答案!
-
如果答案有效并解决了您的问题,请考虑将答案标记为已关闭,以便其他人知道该问题已得到解答。如果我的回答没有回答你的问题,当然,请不要接受。
标签: pygame python-2.5