Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from graphics import *
- import math
- import random
- def centerOfWheels(win, w1, w2, w3):
- # gets the center of each wheel
- w1Center = w1.getCenter()
- w2Center = w2.getCenter()
- w3Center = w3.getCenter()
- return w1Center, w2Center, w3Center
- def isClickInSpin(p):
- spinCenter = Point(325, 150) # center of the spin button
- # finds the distance of the circle
- dist = math.sqrt(abs((p.getX() - spinCenter.getX()) ** 2 -
- (p.getY() - spinCenter.getY()) ** 2))
- if dist > 55: # distance is outside the radius of the circle
- return False
- else:
- return True # distance is within the radius of the circle
- def spinButton(p, win):
- while (not (isClickInSpin(p))): # user clicked outside of spin button
- p = win.getMouse()
- return False
- if isClickInSpin(p): # user clicked inside the spin button
- return True
- def drawBackground(win):
- # draws the spin button
- spin = Rectangle(Point(365, 160), Point(275,130))
- spin.draw(win)
- spinText = Text(Point(320, 145), "Spin!")
- spinText.draw(win)
- # draws the 3 wheels of the slot machine. w1, w2, and w3, the first, second
- # and third wheel, respectively
- w1 = Rectangle(Point(105, 340), Point(248, 200))
- w1.draw(win)
- w2 = Rectangle(Point(252, 340), Point(390, 200))
- w2.draw(win)
- w3 = Rectangle(Point(394, 340), Point(530, 200))
- w3.draw(win)
- return w1, w2, w3
- def createShapes(win, w1Center, w2Center, w3Center):
- # first wheel's square, circle, and triangle
- w11 = Rectangle(Point(145, 299), Point(204, 241))
- w11.setFill("blue")
- w12 = Circle(w1Center, 35)
- w12.setFill("red")
- w13 = Polygon(Point(171, 299), Point(145, 241), Point(204, 241))
- w13.setFill("cyan")
- # second wheel's square, circle, and triangle
- w21 = Rectangle(Point(288, 299), Point(345, 241))
- w21.setFill("blue")
- w22 = Circle(w2Center, 35)
- w22.setFill("red")
- w23 = Polygon(Point(315, 299), Point(287, 241), Point(345, 241))
- w23.setFill("cyan")
- # third wheel's square, circle, and triangle
- w31 = Rectangle(Point(430, 299), Point(489, 241))
- w31.setFill("blue")
- w32 = Circle(w3Center, 35)
- w32.setFill("red")
- w33 = Polygon(Point(456, 299), Point(430, 241), Point(489, 241))
- w33.setFill("cyan")
- return w11, w12, w13, w21, w22, w23, w31, w32, w33
- def pickW1Shape(win, w11, w12, w13):
- # generates a random number for the shape, w1ShapeNum, from 0 to 2
- w1ShapeNum = random.randrange(3)
- if (w1ShapeNum == 0):
- w1shape = w11 # wheel is a square
- if (w1ShapeNum == 1):
- w1shape = w12 # wheel is a circle
- if (w1ShapeNum == 2):
- w1shape = w13 # wheel is a triangle
- return w1shape # was badly indented
- def pickW2Shape(win, w21, w22, w23):
- # generates a random number for the shape, w2ShapeNum, from 0 to 2
- w2ShapeNum = random.randrange(3)
- if (w2ShapeNum == 0):
- w2shape = w21 # wheel is a square
- if (w2ShapeNum == 1):
- w2shape = w22 # wheel is a circle
- if (w2ShapeNum == 2):
- w2shape = w23 # wheel is a triangle
- return w2shape # was badly indented
- def pickW3Shape(win, w31, w32, w33):
- # generates a random number for the shape, w3ShapeNum, from 0 to 2
- w3ShapeNum = random.randrange(3)
- if (w3ShapeNum == 0):
- w3shape = w31 # wheel is a square
- if (w3ShapeNum == 1):
- w3shape = w32 # wheel is a circle
- if (w3ShapeNum == 2):
- w3shape = w33 # wheel is a triangle
- return w3shape # was badly indented
- def undrawShapes(win, w1shape, w2shape, w3shape):
- w1shape = w1shape.undraw()
- w2shape = w2shape.undraw()
- w3shape = w3shape.undraw()
- return w1shape, w2shape, w3shape
- def drawShapes(win, w1shape, w2shape, w3shape):
- w1shape = w1shape.draw(win)
- w2shape = w2shape.draw(win)
- w3shape = w3shape.draw(win)
- return w1shape, w2shape, w3shape
- def main():
- # draws the main display window
- win = GraphWin("Jackpot!", 550, 450)
- win.setCoords(0, 0, 650, 450)
- w1, w2, w3 = drawBackground(win)
- w1Center, w2Center, w3Center = centerOfWheels(win, w1, w2, w3)
- w11, w12, w13, w21, w22, w23, w31, w32, w33 = createShapes(win, w1Center, w2Center, w3Center)
- # pick random shapes
- w1shape = pickW1Shape(win, w11, w12, w13)
- w2shape = pickW2Shape(win, w21, w22, w23)
- w3shape = pickW3Shape(win, w31, w32, w31)
- # draw picked shapes
- drawShapes(win, w1shape, w2shape, w3shape)
- # lines below here badly indented
- while (1):
- p = win.getMouse() # player's mouse click
- if spinButton(p, win): # checks whether the click is valid
- undrawShapes(win, w1shape, w2shape, w3shape)
- w1shape = pickW1Shape(win, w11, w12, w13)
- w2shape = pickW2Shape(win, w21, w22, w23)
- w3shape = pickW3Shape(win, w31, w32, w31)
- drawShapes(win, w1shape, w2shape, w3shape)
- win.getMouse() # wait for user click
- # lines above here badly indented
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement