I've started learning Python (Zelle Graphics) and I'm trying to make a slot machine.
Using a function that I've defined in advance, I've managed to draw the shapes into the slot machine in while
loop. I need to reset (undraw()
) them when the user clicks the "Spin!" button, but since the shapes are randomly chosen, each randomly-generated shape is not assigned to a named variable, so I cannot undraw()
them because I don't know their name.
I've tried a lot to solve this issue, but whenever I tried to make a list of the shapes to remove all of them by using a for
loop, Python rejects it saying that it's not defined. In order to remove the old shapes before the new shapes show up, I have to place the code before the main loop but that causes an error because it's not defined.
What I've been trying to do is kind of like this:
shapes = [cirShape, recShape, triShape, creShape, ovlShape, smfShape, diaShape]
for shape in shapes:
shape.undraw()
but it's not working at all.
The code that and I'm actually struggling with:
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)
Thank you for any opinion or help.