2016-04-25 22 views
0

我是編程新手,我不斷收到我的代碼中的這個錯誤。這裏是我的完整代碼: import sys,pygame,math;來自pygame.locals的* import *; 從SYS進口出口 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,那麼BG將跟隨播放器鼠標。

+0

我沒有看到'pygame.Shrek_Rect'任何地方定義,pygame的沒有任何屬性'Shrek_Rect'在我的一瞥文檔。你把它與'pygame.Rect'混淆了嗎? –

+0

@AlexanderHuszagh它在第11行定義:) – Crocatortoise

+0

啊沒有看到。那麼,這解決了我的問題:D –

回答

0

你的問題是一個與命名空間。 Shrek_Rect未在pygame中定義,它在本地名稱空間內定義。

嘗試改變:

pos = pygame.Shrek_Rect 

要:

pos = Shrek_Rect 
+0

非常感謝!有效。我真的很感謝答案。 :) – Crocatortoise

相關問題