我是编程新手,我在代码中不断出现此错误。这是我的完整代码: import sys,pygame,math; 来自pygame.locals import *; 来自sys import exit 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跟随光标。我坚持这个错误,无法弄明白。
另外,如果用" mouse.get_pos()"替换Shrek_Rect其中错误是BG将遵循播放器鼠标。
答案 0 :(得分:0)
您的问题是名称空间。 Shrek_Rect
未在pygame
中定义,它在本地命名空间中定义。
尝试更改:
pos = pygame.Shrek_Rect
要:
pos = Shrek_Rect