2016-04-25 51 views
0

私はプログラミングが初めてで、私のコードでこのエラーが発生しています。ここに私のフルコードがあります: import sys、pygame、math; からpygame.locals import *; 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

@AlexanderHuszaghそれは11行目で定義されています:) – Crocatortoise

+0

ああ、それを見ませんでした。さて、それは私の問題を解決します:D –

答えて

0

あなたの問題は名前空間の問題です。 Shrek_Rectpygameに定義されていません。ローカルの名前空間内で定義されています。

変更してみてください:

pos = pygame.Shrek_Rect 

へ:

pos = Shrek_Rect 
+0

ありがとう!出来た。私は本当に答えに感謝します。 :) – Crocatortoise

関連する問題