#---------------------------------------------------------------------- # Introduction to JES Graphics # # Copyright (C) September 27-29, 2017 -- Dr. William T. Verts #---------------------------------------------------------------------- import time #---------------------------------------------------------------------- # Graphics helper functions to translate what we really want into the # appropriate calls to JES graphics routines. # # Box and Block use corner coordinates of the rectangles, instead of # upper-left location, width, and height as required by JES functions # addRect and addRectFilled. # # addCircle and addCircle filled use center coordinate and radius, # instead of upper-left location, width, and height as required by JES # functions addOval and addOvalFilled. #---------------------------------------------------------------------- def addBox (Canvas,X1,Y1,X2,Y2,NewColor): # Want: to addRect(Canvas,X1,Y1,X2-X1,Y2-Y1,NewColor) # Need: X,Y,Width,Height return def addBlock(Canvas,X1,Y1,X2,Y2,NewColor): # Want: to addRectFilled(Canvas,X1,Y1,X2-X1,Y2-Y1,NewColor) # Need: X,Y,Width,Height return def addCircle(Canvas,X,Y,Radius,NewColor): # Want: , Radius addOval(Canvas,X-Radius,Y-Radius,2*Radius,2*Radius,NewColor) # Need: X,Y,Width,Height return def addCircleFilled(Canvas,X,Y,Radius,NewColor): # Want: , Radius addOvalFilled(Canvas,X-Radius,Y-Radius,2*Radius,2*Radius,NewColor) # Need: X,Y,Width,Height return #---------------------------------------------------------------------- # Bounce a ball around screen #---------------------------------------------------------------------- def Main(): Canvas = makeEmptyPicture(600,400,red) show(Canvas) # Initialize position and direction variables X = 300 # X location of center Y = 200 # Y location of center ChangeX = 1 # Direction of motion in X ChangeY = 1 # Direction of motion in Y Radius = 40 # Size of Ball while (True): # Fill the screen with the background color #addRectFilled(Canvas,0,0,getWidth(Canvas),getHeight(Canvas),red) setAllPixelsToAColor(Canvas,red) # Paint the new geometry on screen addCircleFilled(Canvas, X, Y, Radius, green) # Make the updated screen visible repaint(Canvas) # Wait to eliminate flicker time.sleep(0.01) # Update position and direction variables # Bounce ball off of its edge instead of its center. X = X + ChangeX Y = Y + ChangeY if X > getWidth(Canvas) - Radius: # Hit right wall ChangeX = -1 if X < Radius: # Hit left wall ChangeX = 1 if Y > getHeight(Canvas) - Radius: # Hit bottom wall ChangeY = -1 if Y < Radius: # Hit top wall ChangeY = 1 return