Moving Rectangle Bounces Left-to-Right
def pong():
width = 10
rate = 10
canvasWidth = 640
canvasHeight = 480
turnNumber = canvasWidth/rate
for frameNumber in range(199):
canvas = makeEmptyPicture(canvasWidth, canvasHeight)
x = (frameNumber%turnNumber)*rate
if isGoingRightAt(frameNumber, turnNumber):
addRectFilled(canvas, x, getHeight(canvas)/2, width, width, orange)
else:
addRectFilled(canvas, canvasWidth-x, getHeight(canvas)/2, width, width, orange)
writeFrame(canvas, frameNumber)
def isGoingRightAt(frame, turnAt):
bounces = frame / turnAt # Integer division, so integer quotient
return (bounces%2==1) # Returns whether bounces is an odd number
def writeFrame(pic, fn):
numerals = str(fn)
if fn < 100:
numerals = "0" + numerals
if fn < 10:
numerals = "0" + numerals
fileName = "pong" + numerals + ".jpg"
writePictureTo(pic, getMediaPath(fileName))
Links to this Page