2016-04-25 5 views
0

Я новичок в программировании, и я продолжаю получать эту ошибку в своем коде. Вот мой полный код: import sys, pygame, math; из pygame.locals импорт *; из SYS импорта выхода pygame.init()Ошибка атрибута: объект 'module' не имеет атрибута - Больше не нужна помощь

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 на «mouse.get_pos()», где ошибка BG будет следовать за мышью игрока.

+0

Я не вижу 'pygame.Shrek_Rect' определено в любом месте, и Pygame не имеет какого-либо атрибута' Shrek_Rect' на мой взгляд из документация. Вы смутили его с помощью 'pygame.Rect'? –

+0

@ Александр Хушаг определяется в строке 11 :) – Crocatortoise

+0

А не видел этого. Ну, это решает мою проблему: D –

ответ

0

Ваша проблема одна с пространствами имен. Shrek_Rect не определен в пределах pygame, он определен в локальном пространстве имен.

Попробуйте изменить:

pos = pygame.Shrek_Rect 

To:

pos = Shrek_Rect 
+0

Большое вам спасибо! Это сработало. Я очень ценю ответ. :) – Crocatortoise

 Смежные вопросы

  • Нет связанных вопросов^_^