我已经开始学习Python(Zelle Graphics(,我正在尝试制作老虎机。
使用我预先定义的函数,我设法在循环中将形状绘制到老虎机while
。当用户单击"旋转!"按钮时,我需要重置(undraw()
(它们,但由于形状是随机选择的,因此每个随机生成的形状都不会分配给命名变量,所以我无法undraw()
它们,因为我不知道它们的名字。
我已经尝试了很多来解决这个问题,但是每当我尝试使用for
循环创建形状列表以删除所有形状时,Python 都会拒绝它,说它没有定义。为了在新形状出现之前删除旧形状,我必须将代码放在主循环之前,但这会导致错误,因为它没有定义。
我一直在尝试做的是这样的:
shapes = [cirShape, recShape, triShape, creShape, ovlShape, smfShape, diaShape]
for shape in shapes:
shape.undraw()
但它根本不起作用。
我实际上正在努力的代码:
from graphics import *
import random
win = GraphWin("Slot Machine", 600, 600)
r1 = Rectangle(Point(10,10), Point(190,190))
r1.draw(win)
r2 = Rectangle(Point(210,10), Point(390,190))
r2.draw(win)
r3 = Rectangle(Point(410,10), Point(590,190))
r3.draw(win)
slots = [r1,r2,r3]
r4 = Rectangle(Point(210,220), Point(390,290)).draw(win) # "Spin!" button
label = Text(Point(300,255), "Spin!").draw(win)
colors = ["red", "yellow", "blue", "orange", "green", "indigo", "violet"]
def circle(slot):
c = Circle(slot.getCenter(), 70)
c.setFill(random.choice(colors))
return c
def rectangle(slot):
p1 = slot.getP1()
p2 = slot.getP2()
r = Rectangle(Point(p1.getX() + 30, p1.getY() + 30), Point(p2.getX() - 30, p2.getY() - 30))
r.setFill(random.choice(colors))
return r
def polygon(slot):
p1 = slot.getP1()
p2 = slot.getP2()
t = Polygon(Point(p1.getX() + 30, p2.getY() - 30), Point(p2.getX() - 30, p2.getY() - 30), Point((p2.getX() + p1.getX()) / 2, p1.getY() + 40))
t.setFill(random.choice(colors))
return t
def oval(slot):
p1 = slot.getP1()
p2 = slot.getP2()
o = Oval(Point(p1.getX()+20, p1.getY()+60), Point(p2.getX()-20, p2.getY()-60))
o.setFill(random.choice(colors))
return o
def crescentMoon1(slot):
p1 = slot.getP1()
p2 = slot.getP2()
p3 = slot.getCenter()
cM1 = Circle(slot.getCenter(), 70)
cM1.setFill("yellow")
return cM1
def crescentMoon2(slot):
p1 = slot.getP1()
p2 = slot.getP2()
p3 = slot.getCenter()
cM2 = Circle(Point((p3.getX() + 10), p3.getY()), 60)
cM2.setFill("black")
return cM2
def smileyFace1(slot):
sF1 = Circle(slot.getCenter(), 70)
sF1.setFill("yellow")
return sF1
def smileyFace2(slot):
p1 = slot.getP1()
sF2 = Circle(Point(p1.getX() + 60,p1.getY() + 70), 10)
sF2.setFill("black")
return sF2
def smileyFace3(slot):
p1 = slot.getP1()
sF3 = Circle(Point(p1.getX() + 120,p1.getY() + 70), 10)
sF3.setFill("black")
return sF3
def smileyFace4(slot):
p3 = slot.getCenter()
sF4 = Polygon(Point(p3.getX() - 60, p3.getY()), Point(p3.getX() + 60, p3.getY()), Point(p3.getX(), p3.getY() + 60))
sF4.setFill("black")
return sF4
def diamond1(slot):
p3 = slot.getCenter()
d1 = Polygon(Point(p3.getX() - 50, p3.getY()), Point(p3.getX() + 50, p3.getY()), Point(p3.getX(), p3.getY() + 80))
d1.setFill("black")
return d1
def diamond2(slot):
p3 = slot.getCenter()
d2 = Polygon(Point(p3.getX() - 50, p3.getY()), Point(p3.getX() + 50, p3.getY()), Point(p3.getX(), p3.getY() - 80))
d2.setFill("black")
return d2
count = 0
winner_label = Text(Point(300,450), "Winner!!")
while True:
count = count + 1
p = win.getMouse()
print("You clicked", p.getX(), p.getY())
if 210 <= p.getX() <= 390 and 220 <= p.getY() <= 290:
winner_label.undraw()
cirCount = 0
recCount = 0
triCount = 0
ovlCount = 0
creCount = 0
smfCount = 0
diaCount = 0
for slot in slots:
randomNum = random.randrange(7)
if randomNum == 0:
cirShape = circle(slot)
cirCount = cirCount + 1
cirShape.draw(win)
elif randomNum == 1:
recShape = rectangle(slot)
recCount = recCount + 1
recShape.draw(win)
elif randomNum == 2:
triShape = polygon(slot)
triCount = triCount + 1
triShape.draw(win)
elif randomNum == 3:
ovlShape = oval(slot)
ovlCount = ovlCount + 1
ovlShape.draw(win)
elif randomNum == 4:
creShape1 = crescentMoon1(slot)
creShape2 = crescentMoon2(slot)
creCount = creCount + 1
creShape1.draw(win)
creShape2.draw(win)
elif randomNum == 5:
smfShape1 = smileyFace1(slot)
smfShape2 = smileyFace2(slot)
smfShape3 = smileyFace3(slot)
smfShape4 = smileyFace4(slot)
smfCount = smfCount + 1
smfShape2 = smileyFace2(slot)
smfShape3 = smileyFace3(slot)
smfShape4 = smileyFace4(slot)
smfShape1.draw(win)
smfShape2.draw(win)
smfShape3.draw(win)
smfShape4.draw(win)
elif randomNum == 6:
diaShape1 = diamond1(slot)
diaShape2 = diamond2(slot)
diaCount = diaCount + 1
diaShape1.draw(win)
diaShape2.draw(win)
shapeCounts = [cirCount, recCount, triCount, ovlCount, creCount, smfCount, diaCount]
for shapeCount in shapeCounts:
if shapeCount == 3:
winner_label.draw(win)
感谢您的任何意见或帮助。
只需保留您在给定旋转时分配的所有形状的列表即可。 您可以使用此列表来draw()
和undraw()
形状:
from random import choice
from graphics import *
COLORS = ['red', 'yellow', 'blue', 'orange', 'green', 'indigo', 'violet']
def circle(slot):
c = Circle(slot.getCenter(), 70)
c.setFill(choice(COLORS))
return c
def rectangle(slot):
p1 = slot.getP1()
p2 = slot.getP2()
r = Rectangle(Point(p1.getX() + 30, p1.getY() + 30), Point(p2.getX() - 30, p2.getY() - 30))
r.setFill(choice(COLORS))
return r
def triangle(slot):
p1 = slot.getP1()
p2 = slot.getP2()
t = Polygon(Point(p1.getX() + 30, p2.getY() - 30), Point(p2.getX() - 30, p2.getY() - 30), Point((p2.getX() + p1.getX()) / 2, p1.getY() + 40))
t.setFill(choice(COLORS))
return t
def oval(slot):
p1 = slot.getP1()
p2 = slot.getP2()
o = Oval(Point(p1.getX() + 20, p1.getY() + 60), Point(p2.getX() - 20, p2.getY() - 60))
o.setFill(choice(COLORS))
return o
def crescentMoon1(slot):
cM1 = Circle(slot.getCenter(), 70)
cM1.setFill('yellow')
return cM1
def crescentMoon2(slot):
p = slot.getCenter()
cM2 = Circle(Point(p.getX() + 10, p.getY()), 60)
cM2.setFill('black')
return cM2
def smileyFace1(slot):
sF1 = Circle(slot.getCenter(), 70)
sF1.setFill('yellow')
return sF1
def smileyFace2(slot):
p1 = slot.getP1()
sF2 = Circle(Point(p1.getX() + 60, p1.getY() + 70), 10)
sF2.setFill('black')
return sF2
def smileyFace3(slot):
p1 = slot.getP1()
sF3 = Circle(Point(p1.getX() + 120, p1.getY() + 70), 10)
sF3.setFill('black')
return sF3
def smileyFace4(slot):
p = slot.getCenter()
x, y = p.getX(), p.getY()
sF4 = Polygon(Point(x - 60, y), Point(x + 60, y), Point(x, y + 60))
sF4.setFill('black')
return sF4
def diamond(slot):
p = slot.getCenter()
x, y = p.getX(), p.getY()
d = Polygon(Point(x - 50, y), Point(x, y + 80), Point(x + 50, p.getY()), Point(x, y - 80))
d.setFill('black')
return d
win = GraphWin("Slot Machine", 600, 600)
slots = []
for distance in range(0, 600, 200):
slot = Rectangle(Point(10 + distance, 10), Point(190 + distance, 190))
slot.draw(win)
slots.append(slot)
Text(Point(300, 255), "Spin!").draw(win)
spinButton = Rectangle(Point(210, 220), Point(390, 290)).draw(win) # "Spin!" button
winner_label = Text(Point(300, 450), "Winner!")
shapes = []
while True:
p = win.getMouse()
if spinButton.getP1().getX() <= p.getX() <= spinButton.getP2().getX() and spinButton.getP1().getY() <= p.getY() <= spinButton.getP2().getY():
winner_label.undraw()
for shape in shapes:
shape.undraw()
shapes = []
counts = {'circle': 0, 'rectangle': 0, 'triangle':0, 'oval': 0, 'crescent': 0, 'smiley': 0, 'diamond': 0}
for slot in slots:
randomShape = choice(list(counts))
if randomShape == 'circle':
cirShape = circle(slot)
counts['circle'] += 1
shapes.append(cirShape)
elif randomShape == 'rectangle':
recShape = rectangle(slot)
counts['rectangle'] += 1
shapes.append(recShape)
elif randomShape == 'triangle':
triShape = triangle(slot)
counts['triangle'] += 1
shapes.append(triShape)
elif randomShape == 'oval':
ovlShape = oval(slot)
counts['oval'] += 1
shapes.append(ovlShape)
elif randomShape == 'crescent':
creShape1 = crescentMoon1(slot)
creShape2 = crescentMoon2(slot)
counts['crescent'] += 1
shapes.extend([creShape1, creShape2])
elif randomShape == 'smiley':
smfShape1 = smileyFace1(slot)
smfShape2 = smileyFace2(slot)
smfShape3 = smileyFace3(slot)
smfShape4 = smileyFace4(slot)
counts['smiley'] += 1
shapes.extend([smfShape1, smfShape2, smfShape3, smfShape4])
elif randomShape == 'diamond':
diaShape = diamond(slot)
counts['diamond'] += 1
shapes.append(diaShape)
for shape in shapes:
shape.draw(win)
for count in counts.values():
if count == len(slots):
winner_label.draw(win)
break