Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Show a ball bouncing off the sides of the window.
- '''
- from graphics import *
- import time, math
- HEIGHT=400
- WIDTH=400
- SIDE=200
- VSIDE=140 # visible length of side
- AMPLITUDE=20
- CORNERS=[]
- SHOWCORNERS=False
- CSIDE = 60 # corner side
- def animate(win, hline1, hline2, vline1, vline2, rate):
- ''' Animate a shape moving in jumps (dx, dy), bouncing when
- its center reaches the low and high x and y coordinates.
- '''
- global SHOWCORNERS
- oldsin=0 # for horizontal lines, moving vertically
- oldcos=1 # for vertical lines, moving horizontally
- vsum=0
- delay = .05
- for t in range(1, 6000):
- newsin = math.sin(rate*t)
- newcos = math.cos(rate*t)
- vchange= AMPLITUDE*(newcos - oldcos)
- hchange= AMPLITUDE*(newsin - oldsin)
- hline1.move(0, vchange)
- hline2.move(0, vchange)
- vline1.move(hchange, 0)
- vline2.move(hchange, 0)
- oldsin = newsin
- oldcos = newcos
- #vsum += vchange
- #if t % 20 == 0: print('vsum={}'.format(vsum))
- p = win.checkMouse()
- if p != None:
- SHOWCORNERS = not SHOWCORNERS
- corners(SHOWCORNERS, win)
- time.sleep(delay)
- def getRandomPoint(xLow, xHigh, yLow, yHigh):
- '''Return a random Point with coordinates in the range specified.'''
- x = random.randrange(xLow, xHigh+1)
- y = random.randrange(yLow, yHigh+1)
- return Point(x, y)
- def makesquare(x,y, r):
- #x = center.getX()
- #y = center.getY()
- sqr = Rectangle(Point(x-r, y-r), Point(x+r, y+r))
- sqr.setFill("green")
- return sqr
- def corners(makeVisible, win):
- if makeVisible:
- for s in CORNERS: s.draw(win)
- else:
- for s in CORNERS: s.undraw()
- def main(rate):
- '''create lines and start animation'''
- global CORNERS
- win = GraphWin('Illusion', WIDTH, HEIGHT)
- midX = WIDTH/2
- midY = HEIGHT/2
- hs = SIDE/2 # half side
- hv = VSIDE/2 # half visible side
- DEFAULT_CONFIG["width"] = 3
- hlineupper = Line(Point(midX - hv,midY-hs), Point(midX + hv, midY -hs))
- hlinelower = Line(Point(midX - hv,midY+hs), Point(midX + hv, midY +hs))
- vlineleft = Line(Point(midX - hs,midY-hv), Point(midX - hs, midY+hv))
- vlineright = Line(Point(midX + hs,midY-hv), Point(midX + hs, midY+hv))
- hlineupper.move(0,AMPLITUDE)
- hlinelower.move(0,AMPLITUDE)
- hlineupper.draw(win)
- hlinelower.draw(win)
- vlineleft.draw(win)
- vlineright.draw(win)
- CORNERS.append(makesquare(midX-hs, midY-hs, CSIDE/2))
- CORNERS.append(makesquare(midX+hs, midY-hs, CSIDE/2))
- CORNERS.append(makesquare(midX+hs, midY+hs, CSIDE/2))
- CORNERS.append(makesquare(midX-hs, midY+hs, CSIDE/2))
- animate(win, hlineupper, hlinelower, vlineleft, vlineright,rate)
- #pld win.close()
- main(0.16)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement