AttributeError:'function'对象没有属性'CANVAS_WIDTH'

时间:2016-03-07 07:09:10

标签: python python-3.x attributes

我正试图制作一个突破性的游戏,我有一个错误,我不知道该怎么做,下面我已经附上主要代码和边界代码,如果你愿意可以看看,说什么错这将是美好的

这是主文件:

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)

这是border类:

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\]

我不知道为什么我有错误,某人?

1 个答案:

答案 0 :(得分:0)

您已将borders定义为函数。你不能拥有它的属性。

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

也许你的意思是borders(CANVAS_WIDTH)