#file week10/carBounce.py from graphics import * import time def main(): winW,winH = 500,300 win = GraphWin("Moving car",winW,winH) xstart,ystart = 100,100 carW,carH = 90,50 r = Rectangle(Point(xstart,ystart),Point(xstart+carW,ystart+carH)) r.setFill("purple") line = Line(Point(0,ystart+carH),Point(winW,ystart+carH)) r.draw(win) line.draw(win) speed = 1 # 1 from left to right -1 for right to left while True: r.move(speed * 5,0) # dx = 5 dy = 0 time.sleep(0.01) if ( r.getP1().getX()+carW > winW or r.getP1().getX() < 0): speed = speed * -1 main()