Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. from graphics import *
  2. import math
  3. import random
  4.  
  5. def centerOfWheels(win, w1, w2, w3):
  6. # gets the center of each wheel
  7. w1Center = w1.getCenter()
  8. w2Center = w2.getCenter()
  9. w3Center = w3.getCenter()
  10. return w1Center, w2Center, w3Center
  11.  
  12.  
  13. def isClickInSpin(p):
  14. spinCenter = Point(325, 150) # center of the spin button
  15.  
  16. # finds the distance of the circle
  17. dist = math.sqrt(abs((p.getX() - spinCenter.getX()) ** 2 -
  18. (p.getY() - spinCenter.getY()) ** 2))
  19.  
  20. if dist > 55: # distance is outside the radius of the circle
  21. return False
  22. else:
  23. return True # distance is within the radius of the circle
  24.  
  25.  
  26. def spinButton(p, win):
  27. while (not (isClickInSpin(p))): # user clicked outside of spin button
  28. p = win.getMouse()
  29. return False
  30. if isClickInSpin(p): # user clicked inside the spin button
  31. return True
  32.  
  33. def drawBackground(win):
  34. # draws the spin button
  35. spin = Rectangle(Point(365, 160), Point(275,130))
  36. spin.draw(win)
  37. spinText = Text(Point(320, 145), "Spin!")
  38. spinText.draw(win)
  39.  
  40. # draws the 3 wheels of the slot machine. w1, w2, and w3, the first, second
  41. # and third wheel, respectively
  42. w1 = Rectangle(Point(105, 340), Point(248, 200))
  43. w1.draw(win)
  44. w2 = Rectangle(Point(252, 340), Point(390, 200))
  45. w2.draw(win)
  46. w3 = Rectangle(Point(394, 340), Point(530, 200))
  47. w3.draw(win)
  48.  
  49. return w1, w2, w3
  50.  
  51. def createShapes(win, w1Center, w2Center, w3Center):
  52. # first wheel's square, circle, and triangle
  53. w11 = Rectangle(Point(145, 299), Point(204, 241))
  54. w11.setFill("blue")
  55. w12 = Circle(w1Center, 35)
  56. w12.setFill("red")
  57. w13 = Polygon(Point(171, 299), Point(145, 241), Point(204, 241))
  58. w13.setFill("cyan")
  59.  
  60. # second wheel's square, circle, and triangle
  61. w21 = Rectangle(Point(288, 299), Point(345, 241))
  62. w21.setFill("blue")
  63. w22 = Circle(w2Center, 35)
  64. w22.setFill("red")
  65. w23 = Polygon(Point(315, 299), Point(287, 241), Point(345, 241))
  66. w23.setFill("cyan")
  67.  
  68. # third wheel's square, circle, and triangle
  69. w31 = Rectangle(Point(430, 299), Point(489, 241))
  70. w31.setFill("blue")
  71. w32 = Circle(w3Center, 35)
  72. w32.setFill("red")
  73. w33 = Polygon(Point(456, 299), Point(430, 241), Point(489, 241))
  74. w33.setFill("cyan")
  75. return w11, w12, w13, w21, w22, w23, w31, w32, w33
  76.  
  77. def pickW1Shape(win, w11, w12, w13):
  78. # generates a random number for the shape, w1ShapeNum, from 0 to 2
  79. w1ShapeNum = random.randrange(3)
  80. if (w1ShapeNum == 0):
  81. w1shape = w11 # wheel is a square
  82. if (w1ShapeNum == 1):
  83. w1shape = w12 # wheel is a circle
  84. if (w1ShapeNum == 2):
  85. w1shape = w13 # wheel is a triangle
  86.  
  87. return w1shape # was badly indented
  88.  
  89.  
  90. def pickW2Shape(win, w21, w22, w23):
  91. # generates a random number for the shape, w2ShapeNum, from 0 to 2
  92. w2ShapeNum = random.randrange(3)
  93. if (w2ShapeNum == 0):
  94. w2shape = w21 # wheel is a square
  95. if (w2ShapeNum == 1):
  96. w2shape = w22 # wheel is a circle
  97. if (w2ShapeNum == 2):
  98. w2shape = w23 # wheel is a triangle
  99.  
  100. return w2shape # was badly indented
  101.  
  102. def pickW3Shape(win, w31, w32, w33):
  103. # generates a random number for the shape, w3ShapeNum, from 0 to 2
  104. w3ShapeNum = random.randrange(3)
  105. if (w3ShapeNum == 0):
  106. w3shape = w31 # wheel is a square
  107. if (w3ShapeNum == 1):
  108. w3shape = w32 # wheel is a circle
  109. if (w3ShapeNum == 2):
  110. w3shape = w33 # wheel is a triangle
  111.  
  112. return w3shape # was badly indented
  113.  
  114. def undrawShapes(win, w1shape, w2shape, w3shape):
  115. w1shape = w1shape.undraw()
  116. w2shape = w2shape.undraw()
  117. w3shape = w3shape.undraw()
  118. return w1shape, w2shape, w3shape
  119.  
  120. def drawShapes(win, w1shape, w2shape, w3shape):
  121. w1shape = w1shape.draw(win)
  122. w2shape = w2shape.draw(win)
  123. w3shape = w3shape.draw(win)
  124. return w1shape, w2shape, w3shape
  125.  
  126. def main():
  127. # draws the main display window
  128. win = GraphWin("Jackpot!", 550, 450)
  129. win.setCoords(0, 0, 650, 450)
  130.  
  131. w1, w2, w3 = drawBackground(win)
  132. w1Center, w2Center, w3Center = centerOfWheels(win, w1, w2, w3)
  133. w11, w12, w13, w21, w22, w23, w31, w32, w33 = createShapes(win, w1Center, w2Center, w3Center)
  134.  
  135. # pick random shapes
  136. w1shape = pickW1Shape(win, w11, w12, w13)
  137. w2shape = pickW2Shape(win, w21, w22, w23)
  138. w3shape = pickW3Shape(win, w31, w32, w31)
  139.  
  140. # draw picked shapes
  141. drawShapes(win, w1shape, w2shape, w3shape)
  142.  
  143. # lines below here badly indented
  144. while (1):
  145. p = win.getMouse() # player's mouse click
  146.  
  147. if spinButton(p, win): # checks whether the click is valid
  148. undrawShapes(win, w1shape, w2shape, w3shape)
  149. w1shape = pickW1Shape(win, w11, w12, w13)
  150. w2shape = pickW2Shape(win, w21, w22, w23)
  151. w3shape = pickW3Shape(win, w31, w32, w31)
  152.  
  153. drawShapes(win, w1shape, w2shape, w3shape)
  154. win.getMouse() # wait for user click
  155. # lines above here badly indented
  156.  
  157. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement