2016-03-07 85 views
2

我想做一個突破遊戲,並且我有一個錯誤,我不知道該怎麼處理,下面我附上了主代碼和邊框代碼,如果你願意可以去看看,說什麼是錯這將是美妙的AttributeError:'function'對象沒有屬性'CANVAS_WIDTH'

這是主要的文件:

from pedalClass import Pedal 
from ballClass import Ball 
import bordersClass 
from turtle import * 
import random 
import time 
import turtle 

canvas=getcanvas() 
CANVAS_WIDTH = canvas.winfo_width() 
CANVAS_HEIGHT = canvas.winfo_height() 

turtle.penup() 
turtle.setup(800,500) 
turtle.ht() 
turtle.bgpic('breakout.gif') 
TIME_STEP = 30 
SQUARE_SIZE=20 


ball1=Ball(1, 0.5, 0, 0) 
mypad=Pedal(0.5, 0, -230, 5, 1) 

def borders(cells): 
    global mypad 
    width=get_screen_width() 
    height=get_screen_height() 
    x=mypad.xcor() 
    y=mypad.ycor() 


if (mypad.xcor() > width): 
    mypad.set_dx(-mypad.get_dx()) 
if (cell.ycor() > height): 
    cell.set_dy(-cell.get_dy()) 
if (mypad.xcor() < -width): 
    mypad.set_dx(-mypad.get_dx()) 
if (cell.ycor() < -height): 
    cell.set_dy(-cell.get_dy()) 

def move_pedal(event): 
    global mypad 
    global CANVAS_WIDTH 
    global CANVAS_HEIGHT 
    mypad.goto(event.x-canvas.winfo_width()/2, mypad.ycor()) 
    CANVAS_WIDTH = canvas.winfo_width() 
    CANVAS_HEIGHT = canvas.winfo_height() 


def move_left(event): 
    global mypad 
    mypad.goto(event.x-1, mypad.ycor()) 


def move_right(event): 
    global mypad 
    mypad.goto(event.x+1, mypad.ycor()) 


canvas.bind("<Motion>", move_pedal) 
canvas.bind("<Left>", move_left) 
canvas.bind("<Right>", move_right) 
getscreen().listen() 


Playing=True 
while Playing: 
    print(borders.CANVAS_WIDTH) 
    ball1.move() 
    mypad.borders() 
    mypad.move_pedal() 
time.sleep(3) 

,這是邊界類:

from turtle import * 
canvas=getcanvas() # the canvas is the area that the turtle is moving (the white background) 
CANVAS_WIDTH = canvas.winfo_width() # here we get canvas(screen) width 
CANVAS_HEIGHT = canvas.winfo_height() # here we get the canvas(screen)height 
def get_screen_width(): 
    global CANVAS_WIDTH 
    return int(CANVAS_WIDTH/2-10) 

# This function returns the height of the screen 
def get_screen_height(): 
    global CANVAS_HEIGHT 
    return int(CANVAS_HEIGHT/2-5) 

,這是錯誤:

Traceback (most recent call last): 
    File "C:\Users\User\Desktop\MEET-YL1-master\breakout\BreakoutNew.py", line 70, in <module> 
    print(borders.CANVAS_WIDTH) 
AttributeError: 'function' object has no attribute 'CANVAS_WIDTH' 
[Finished in 2.4s with exit code 1] 
[shell_cmd: python -u "C:\Users\User\Desktop\MEET-YL1-master\breakout\BreakoutNew.py"] 
[dir: C:\Users\User\Desktop\MEET-YL1-master\breakout] 
[path: C:\ProgramData\Oracle\Java\javapath;C:\Python34;C:\Users\User\AppData\Local\Programs\Python\Python35-32;C:\Program Files\Skype\Phone\] 

我不知道爲什麼我有一個錯誤,有人嗎?

+0

我認爲它應該是'print(bordersClass.CANVAS_WIDTH)' – JRodDynamite

回答

0

您已將borders定義爲函數。你不能擁有它的屬性。

def borders(cells): 
    global mypad 
    width=get_screen_width() 
    height=get_screen_height() 
    x=mypad.xcor() 
    y=mypad.ycor() 

也許你的意思是borders(CANVAS_WIDTH)

+0

謝謝你,你幫了我很多 –