Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 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. def pickW2Shape(win, w21, w22, w23):
  90. # generates a random number for the shape, w2ShapeNum, from 0 to 2
  91. w2ShapeNum = random.randrange(3)
  92. if (w2ShapeNum == 0):
  93. w2shape = w21 # wheel is a square
  94. if (w2ShapeNum == 1):
  95. w2shape = w22 # wheel is a circle
  96. if (w2ShapeNum == 2):
  97. w2shape = w23 # wheel is a triangle
  98.  
  99. return w2shape # was badly indented
  100.  
  101. def pickW3Shape(win, w31, w32, w33):
  102. # generates a random number for the shape, w3ShapeNum, from 0 to 2
  103. w3ShapeNum = random.randrange(3)
  104. if (w3ShapeNum == 0):
  105. w3shape = w31 # wheel is a square
  106. if (w3ShapeNum == 1):
  107. w3shape = w32 # wheel is a circle
  108. if (w3ShapeNum == 2):
  109. w3shape = w33 # wheel is a triangle
  110.  
  111. return w3shape # was badly indented
  112.  
  113. def undrawShapes(win, w1shape, w2shape, w3shape):
  114. w1shape = w1shape.undraw()
  115. w2shape = w2shape.undraw()
  116. w3shape = w3shape.undraw()
  117. return w1shape, w2shape, w3shape
  118.  
  119. def drawShapes(win, w1shape, w2shape, w3shape):
  120. w1shape = w1shape.draw(win)
  121. w2shape = w2shape.draw(win)
  122. w3shape = w3shape.draw(win)
  123. return w1shape, w2shape, w3shape
  124.  
  125. def main():
  126. # draws the main display window
  127. win = GraphWin("Jackpot!", 550, 450)
  128. win.setCoords(0, 0, 650, 450)
  129.  
  130. w1, w2, w3 = drawBackground(win)
  131. w1Center, w2Center, w3Center = centerOfWheels(win, w1, w2, w3)
  132. w11, w12, w13, w21, w22, w23, w31, w32, w33 = createShapes(win, w1Center, w2Center, w3Center)
  133.  
  134. # pick random shapes
  135. w1shape = pickW1Shape(win, w11, w12, w13)
  136. w2shape = pickW2Shape(win, w21, w22, w23)
  137. w3shape = pickW3Shape(win, w31, w32, w31)
  138.  
  139. # draw picked shapes
  140. drawShapes(win, w1shape, w2shape, w3shape)
  141.  
  142. # lines below here badly indented
  143. while (1):
  144. p = win.getMouse() # player's mouse click
  145.  
  146. if spinButton(p, win): # checks whether the click is valid
  147. undrawShapes(win, w1shape, w2shape, w3shape)
  148. w1shape = pickW1Shape(win, w11, w12, w13)
  149. w2shape = pickW2Shape(win, w21, w22, w23)
  150. w3shape = pickW3Shape(win, w31, w32, w31)
  151.  
  152. drawShapes(win, w1shape, w2shape, w3shape)
  153. # win.getMouse() # wait for user click DOESN'T APPEAR TO BE NEEDED?
  154. # lines above here badly indented
  155.  
  156. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement