-1

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.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Same way as you would in ms paint: flood fill with the background – Mad Physicist Nov 22 '19 at 04:34
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) and [While Loop Locks Application](https://stackoverflow.com/questions/28639228/python-while-loop-locks-application) – stovfl Nov 22 '19 at 08:12

1 Answers1

0

It's simply a matter of keeping a list of all the shapes you allocate on a give spin. You can use this list to both draw() and undraw() the shapes:

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
cdlane
  • 40,441
  • 5
  • 32
  • 81