所以我试图使用图形绘制三个单独的骰子,虽然我终于让它们出现了,但我现在得到了这个错误
>>> Game()
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
Game()
File "C:UsersBrandonDesktopFinalProject3.py", line 19, in Game
Green.draw(Gamewindow)
AttributeError: 'DieViewGreen' object has no attribute 'draw'
这对我来说毫无意义,因为图形应该导入它。
我的代码中缺少什么吗?
from graphics import *
class DieViewGreen:
""" DieView is a widget that displays a graphical representation
of a standard six-sided die."""
def __init__(self, win, center, value):
"""Create a view of a die, e.g.:
d1 = GDie(myWin, Point(40,50), 20)
creates a die centered at (40,50) having sides
of length 20."""
# first define some standard values
self.win = win
#self.background = Color color of die face
#self.foreground = Color2 # color of the pips
# create a square for the face
if value==0:
x, y = center.getX(), center.getY()
p1 = Point(x-25, y-25)
p2 = Point(x+25, y+25)
rect = Rectangle(p1,p2)
rect.draw(win)
rect.setFill('green')
""" Set this die to display value."""
if value == 1:
x, y = center.getX(), center.getY()
p1 = Point(x-25, y-25)
p2 = Point(x+25, y+25)
rect = Rectangle(p1,p2)
rect.draw(win)
rect.setFill('green')
self.Brain=Text(Point(40,75),'B')
self.Brain.draw(self.win)
elif value == 2:
x, y = center.getX(), center.getY()
p1 = Point(x-25, y-25)
p2 = Point(x+25, y+25)
rect = Rectangle(p1,p2)
rect.draw(win)
rect.setFill('green')
self.Brain=Text(Point(40,75),'B')
self.Brain.draw(self.win)
elif value == 3:
x, y = center.getX(), center.getY()
p1 = Point(x-25, y-25)
p2 = Point(x+25, y+25)
rect = Rectangle(p1,p2)
rect.draw(win)
rect.setFill('green')
self.Brain=Text(Point(40,75),'B')
self.Brain.draw(self.win)
elif value == 4:
x, y = center.getX(), center.getY()
p1 = Point(x-25, y-25)
p2 = Point(x+25, y+25)
rect = Rectangle(p1,p2)
rect.draw(win)
rect.setFill('green')
self.Foot=Text(Point(40,75),'F')
self.Foot.draw(self.win)
elif value == 5:
x, y = center.getX(), center.getY()
p1 = Point(x-25, y-25)
p2 = Point(x+25, y+25)
rect = Rectangle(p1,p2)
rect.draw(win)
rect.setFill('green')
self.Foot=Text(Point(40,75),'F')
self.Foot.draw(self.win)
else:
x, y = center.getX(), center.getY()
p1 = Point(x-25, y-25)
p2 = Point(x+25, y+25)
rect = Rectangle(p1,p2)
rect.draw(win)
rect.setFill('green')
self.Shotgun=Text(Point(40,75),'S')
self.Shotgun.draw(self.win)
我运行类的函数是这样的,它调用中的dieview类(有三个类似于上面的),并用它来绘制窗口
from graphics import *
from DieViewYellow import *
from DieViewGreen import *
from DieViewRed import *
from Button import *
import random
def Game():
"""Runs the game"""
Gamewindow = GraphWin('Game', 200, 200)
Green = DieViewGreen(Gamewindow, Point(40,75),20)
Yellow = DieViewYellow(Gamewindow, Point(95,75),20)
Red = DieViewRed(Gamewindow, Point(150,75),20)
Green.draw(Gamewindow)
Roll = Button(Gamewindow, Point(100,130), 160, 20, "Roll Dice")
Continue = Button(Gamewindow, Point(55,170), 70, 20, "Continue")
while True:
pt=Gamewindow.getMouse()
if roll.clicked(pt):
DieViewGreen(Gamewindow, Point(40,75),3)
else:
continue
#else:
# Exitbutton = "Exit"
# Stopexit = Button(Gamewindow, Point(145,170), 70, 20, Exitbutton)
# Buttons = [Roll, Continue, Stopexit]
# for i in range(len(Buttons)):
# Buttons[i].activate()
# n = Gamewindow.getMouse()
该错误的简单含义是您的DieViewGreen
类中没有draw
函数。您的程序希望有一个。
问题在我的实际游戏代码中,而不是我的die视图中,因为我在不需要的时候使用了Green.draw