# Stephanie Taylor # Lecture 18 for CS151 Fall 2012 # Component.py # Module of functions that support components of scenes # A "component" is a list of graphics objects. import basic_shapes as ba import time import graphics as gr # draw a list of graphics object in the given window def draw( objlist, win ): for item in objlist: item.draw( win ) # move a list of graphics object in the given window def move( objlist, dx, dy ): for item in objlist: item.move( dx, dy ) def ball_init( x, y, scale ): shapes = [] b = gr.Circle( gr.Point(x,y), 20*scale ) b.setFill( "blue" ) b.setOutline( 'yellow' ) b.setWidth( 4*scale ) shapes.append(b) b = gr.Circle( gr.Point(x+15*scale,y), 20*scale ) b.setFill( "blue" ) b.setOutline( 'yellow' ) b.setWidth( 4*scale ) shapes.append(b) print "printing shapes" print shapes return shapes def beach_init( x, y, scale ): shapes = [] r = gr.Rectangle( gr.Point(x,y), gr.Point(x+300*scale,y+130*scale) ) r.setFill( gr.color_rgb(210,180,140) ) shapes.append( r ) return shapes def grass_init( x, y, scale ): shapes = [] r = ba.createRectangle( x, y, x+1000*scale, y+300*scale, "green", "green", None ) shapes.append(r) return shapes def car_init( x, y, scale, clr ): shapes = [] r = ba.createRectangle( x, y, x+100*scale, y+50*scale, clr, clr, None ) shapes.append(r) r = ba.createCircle( x+20*scale, y+50*scale, 25*scale, "black", "black", None ) shapes.append(r) r = ba.createCircle( x+(100-20)*scale, y+50*scale, 25*scale, "black", "black", None ) shapes.append(r) return shapes def car_animate( shapes, frame, win, wrap_around_from, wrap_around_to, jump_at, start_at_frame ): if frame < start_at_frame: return p1 = shapes[0].getP1() p2 = shapes[0].getP2() width = p2.getX()-p1.getX() if p1.getX() >= wrap_around_from: move( shapes, wrap_around_to-p1.getX()-width, 0 ) elif p1.getX() >= jump_at: move( shapes, width*0.1, -width*0.05 ) else: move( shapes, width*0.1, 0 ) if __name__ == '__main__': ball = ball_init( 150, 150, 1 ) print "printing ball" print ball