Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. '''
  2. Show a ball bouncing off the sides of the window.
  3. '''
  4.  
  5. from graphics import *
  6. import time, math
  7.  
  8. HEIGHT=400
  9. WIDTH=400
  10. SIDE=200
  11. VSIDE=140 # visible length of side
  12. AMPLITUDE=20
  13. CORNERS=[]
  14. SHOWCORNERS=False
  15. CSIDE = 60 # corner side
  16.  
  17.  
  18. def animate(win, hline1, hline2, vline1, vline2, rate):
  19. ''' Animate a shape moving in jumps (dx, dy), bouncing when
  20. its center reaches the low and high x and y coordinates.
  21. '''
  22. global SHOWCORNERS
  23. oldsin=0 # for horizontal lines, moving vertically
  24. oldcos=1 # for vertical lines, moving horizontally
  25. vsum=0
  26. delay = .05
  27. for t in range(1, 6000):
  28. newsin = math.sin(rate*t)
  29. newcos = math.cos(rate*t)
  30. vchange= AMPLITUDE*(newcos - oldcos)
  31. hchange= AMPLITUDE*(newsin - oldsin)
  32. hline1.move(0, vchange)
  33. hline2.move(0, vchange)
  34. vline1.move(hchange, 0)
  35. vline2.move(hchange, 0)
  36. oldsin = newsin
  37. oldcos = newcos
  38. #vsum += vchange
  39. #if t % 20 == 0: print('vsum={}'.format(vsum))
  40. p = win.checkMouse()
  41. if p != None:
  42. SHOWCORNERS = not SHOWCORNERS
  43. corners(SHOWCORNERS, win)
  44. time.sleep(delay)
  45.  
  46. def getRandomPoint(xLow, xHigh, yLow, yHigh):
  47. '''Return a random Point with coordinates in the range specified.'''
  48. x = random.randrange(xLow, xHigh+1)
  49. y = random.randrange(yLow, yHigh+1)
  50. return Point(x, y)
  51.  
  52. def makesquare(x,y, r):
  53. #x = center.getX()
  54. #y = center.getY()
  55. sqr = Rectangle(Point(x-r, y-r), Point(x+r, y+r))
  56. sqr.setFill("green")
  57. return sqr
  58.  
  59. def corners(makeVisible, win):
  60. if makeVisible:
  61. for s in CORNERS: s.draw(win)
  62. else:
  63. for s in CORNERS: s.undraw()
  64.  
  65. def main(rate):
  66. '''create lines and start animation'''
  67. global CORNERS
  68. win = GraphWin('Illusion', WIDTH, HEIGHT)
  69. midX = WIDTH/2
  70. midY = HEIGHT/2
  71. hs = SIDE/2 # half side
  72. hv = VSIDE/2 # half visible side
  73.  
  74. DEFAULT_CONFIG["width"] = 3
  75.  
  76. hlineupper = Line(Point(midX - hv,midY-hs), Point(midX + hv, midY -hs))
  77. hlinelower = Line(Point(midX - hv,midY+hs), Point(midX + hv, midY +hs))
  78.  
  79. vlineleft = Line(Point(midX - hs,midY-hv), Point(midX - hs, midY+hv))
  80. vlineright = Line(Point(midX + hs,midY-hv), Point(midX + hs, midY+hv))
  81.  
  82. hlineupper.move(0,AMPLITUDE)
  83. hlinelower.move(0,AMPLITUDE)
  84.  
  85. hlineupper.draw(win)
  86. hlinelower.draw(win)
  87. vlineleft.draw(win)
  88. vlineright.draw(win)
  89.  
  90. CORNERS.append(makesquare(midX-hs, midY-hs, CSIDE/2))
  91. CORNERS.append(makesquare(midX+hs, midY-hs, CSIDE/2))
  92. CORNERS.append(makesquare(midX+hs, midY+hs, CSIDE/2))
  93. CORNERS.append(makesquare(midX-hs, midY+hs, CSIDE/2))
  94.  
  95.  
  96. animate(win, hlineupper, hlinelower, vlineleft, vlineright,rate)
  97. #pld win.close()
  98.  
  99.  
  100. main(0.16)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement