2017-06-13 7 views
-1

属性の実行に関するpygameのは、次のように私はplayer.pyモジュールをインポートするmain.pyとpygameのプログラムを持っているエラー

#Create walls that the player can run into 
import pygame 
import random 
from player import Player 
from collectable import Collectable 
from walls import Wall 

pygame.init() 
BLACK=(0,0,0) 
WHITE=(255,255,255) 
RED=(255,0,0) 
GREEN =(0,255,0) 

BLUE=(0,0,255) 
GOLD=(255,215,0) 
WIDTH=500 
HEIGHT=500 
size= (WIDTH,HEIGHT) 
screen=pygame.display.set_mode(size) 
pygame.display.set_caption("The Life Game") 


done = False 
clock=pygame.time.Clock() 
wall_list=pygame.sprite.Group() 
all_sprites = pygame.sprite.Group() 
enemy_list = pygame.sprite.Group() 
player=Player() 
player.walls=wall_list 

all_sprites.add(player) 


for i in range(random.randrange(100,200)): 
     whiteStar = Collectable(WHITE, 3, 3, "White Star", "Rect") 
     whiteStar.rect.x = random.randrange(size[0]) 
     whiteStar.rect.y = random.randrange(size[1]) 
     all_sprites.add(whiteStar) 

for i in range(50): 

    enemy = Collectable(RED,6, 6,"Enemy","Ellipse") 
    enemy.rect.x = random.randrange(300) 
    enemy.rect.y = random.randrange(300) 
    enemy_list.add(enemy) 
    all_sprites.add(enemy) 

coin1 = Collectable(GOLD,50,50,"Coin","Ellipse") 
coin1.rect.x=440 
coin1.rect.y=0 
all_sprites.add(coin1) 

coin2 = Collectable(GOLD,50,50,"Coin","Ellipse") 
coin2.rect.x=0 
coin2.rect.y=440 
all_sprites.add(coin2) 

enemy = Collectable(RED,100,100,"Enemy","Ellipse") 
enemy.rect.x=70 
enemy.rect.y=230 
all_sprites.add(enemy) 

#Make the walls (x_pos,y_pos, width, height,colour) 



wall=Wall(0,0,10,600,GREEN) 
wall_list.add(wall) 
all_sprites.add(wall_list) 

wall = Wall(50, 300, 400, 10,RED) 
wall_list.add(wall) 
all_sprites.add(wall_list) 

wall = Wall(10, 200, 100, 10,BLUE) 
wall_list.add(wall) 
all_sprites.add(wall_list) 


score=0 
health=100 

#- - - - - - - - - - - - - -Main Program Loop - - - - - - - - - - - - - - - - 
def main(): 
     done=False 
     score=0 
     health=100 
     while not done: 


        #- - - - - - Main event loop (this is where code for handling keyboard and mouse clicks will go) 
     #Loop until the user clicks the 'x' button (to close program) 
        for event in pygame.event.get(): #User does something 
          if event.type == pygame.QUIT: #If the user clicked close 
             done = True #set the done flag to 'true' to exit the loop 

        keys = pygame.key.get_pressed() #checking pressed keys 
        if keys[pygame.K_LEFT]: 
        player.moveLeft(5) 
        if keys[pygame.K_RIGHT]: 
        player.moveRight(5) 
        if keys[pygame.K_UP]: 
        player.moveUp(5) 
        if keys[pygame.K_DOWN]: 
        player.moveDown(5)     


       #>>----------DRAW SECTION ----------------------------------- 
        #Clear the screen to BLACK. Any drawing commands should be put BELOW this or they will be reased with this command 
        screen.fill(BLACK) 



        #Select the font to be used (size, bold, italics, etc) 
        font_score = pygame.font.SysFont('Calibri',20,True,False) 
        font_health = pygame.font.SysFont('Calibri',20,True,False) 
       #Printing a variable (score or health) to the screen involves converting the score (if integer) to a string first.score_label = font_score.render("Score: " + str(score),True,BLACK) 
        health_label = font_health.render("Health: "+str(health),True,WHITE) 
        score_label = font_score.render("Score: " + str(score),True, WHITE) 
       #Now we can use this line of code to put the image of the text on the screen at a given position 
        screen.blit(score_label,[100,480]) 
        screen.blit(health_label,[190,480]) 


        #>>---------UPDATE SECTION/Put the logic of your game here (i.e. how objects move, when to fire them, etc) 


        all_sprites.update() 

        if coin1.collision_with(player): 
         score=score+1 
         coin1.kill() 
         coin1.rect.x=-20 
         coin1.rect.y=-330 

        if coin2.collision_with(player): 
         score=score+1 
         coin2.kill() 
         coin2.rect.x=-20 
         coin2.rect.y=-330 

        if enemy.collision_with(player): 
         health=health-25 
         enemy.kill() 
         enemy.rect.x=-20 
         enemy.rect.y=-330 

        enemy.update() 





     #-------------PRINTING VARIABLES LIKE SCORE TO SCREEN 
        #Any drawing/graphics code should go here 
        all_sprites.draw(screen) 

        #Update the screen to show whatever you have drawn 
        pygame.display.flip() 

        #Set the frames per second (e.g. 30, 60 etc) 
        clock.tick(120) 

main() 

このプログラムは、次のエラーがアップします:

Traceback (most recent call last): 
    File "N:\pygame\working_on_30\main.py", line 164, in <module> 
    main() 
    File "N:\pygame\working_on_30\main.py", line 128, in main 
    all_sprites.update() 
    File "C:\Python 3.6\lib\site-packages\pygame\sprite.py", line 462, in update 
    s.update(*args) 
    File "N:\pygame\working_on_30\player.py", line 49, in update 
    if self.change_x > 0: 
AttributeError: 'Player' object has no attribute 'change_x' 

私はいろいろなことを変更しようとしましたが、それは仕事を得ることができません。誰か a)は

答えて

0

TLを修正するソリューションを提供します)エラー Bを説明してもらえ; DRは - プレーヤーのクラスであなたの__init__self.change_x = 0self.change_y = 0を追加します。

プレイヤークラスには、さまざまな時点で割り当てますが、initでは割り当てない属性player.change_xがあります。例えばplayer.settopleft()を呼び出す前にplayer.update()に電話をした場合、update()メソッドがif self.change_x > 0:という行を行うときには、self.change_xが定義されていないため、エラーとなります。

self.change_x = 0をinitに追加する場合は、変数が常にPlayerのインスタンスに設定されていることを確認してください。

要求によって明確化

:あなたはplayer.pyでクラスのプレーヤーを定義

。クラスメソッドの1つは__init__です。あなたは、クラスPlayerのオブジェクトを作成するときは、main.pyに持っているとして、あなたのコードはplayer=Player()のような何かをするとき(すなわち、これはあなたのすべてのオブジェクトが正しく初期化されることを保証するために使用することができます__init__メソッドを呼び出します。

。あなたは、このエラーが発生したことがないことを確認するためにあなたのクラスの__init__でそれを定義する必要がありますので、あなたのエラーは、私が示唆し、self.change_xが未定義であることによって引き起こされる:

class Player(pygame.sprite.Sprite): 
    #-------------------Define Variables here 
    speed=0 
    #------------------Initialise Constructor 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.image=pygame.image.load("player.png") 
     self.rect = self.image.get_rect() 

     # - CHANGE HERE! - sets initial speed to 0 
     self.change_x = 0 
     self.change_y = 0 
     # - END OF CHANGE 

     #--------------Fetch the rectangle object that has the dimensions of the image 
     self.rect =self.image.get_rect() 
     #---------------Define movement 
+0

二つの質問に1.「TL; DR」とは何ですか。あなたの答えの始めに?2.あなたは、プレーヤーのclaの__init__にself.change_x = 0を含めて、そのコード行の例を投稿できますか? ss。私はその構造には自信がない。私はその後、質問、回答のステータスを授与することができます!大変ありがとうございます。 – MissComputing

+0

haha​​、tl; drはインターネットが長すぎます。読んだことはありませんでした」 - 基本的には論文の要約と同じです。私は自分の投稿を編集します。 – Stael

+0

@MissComputing - アップデートしましたか? – Stael

関連する問題