def witchArms_init(x, y, scale): '''Creates and returns a list of graphics objects to make up a witcharms and stick''' shapes = [] #witch arm r = gr.Rectangle(gr.Point(x - scale * 15, y - scale * 25), gr.Point(x - scale * 2, y - scale * 20)) r.setFill(gr.color_rgb(0, 205, 0)) shapes.append(r) r = gr.Rectangle(gr.Point(x + scale * 15, y - scale * 25), gr.Point(x + scale * 2, y - scale * 20)) r.setFill(gr.color_rgb(0, 205, 0)) shapes.append(r) #sleeves r = gr.Rectangle(gr.Point(x - scale * 15, y - scale * 25), gr.Point(x - scale * 5, y - scale * 20)) r.setFill(gr.color_rgb(128, 0, 128)) shapes.append(r) r = gr.Rectangle(gr.Point(x + scale * 15, y - scale * 25), gr.Point(x + scale * 5, y - scale * 20)) r.setFill(gr.color_rgb(128, 0, 128)) shapes.append(r) #spoon r = gr.Rectangle(gr.Point(x - scale * 2, y - scale * 35), gr.Point(x + scale * 2, y + scale * 10)) r.setFill(gr.color_rgb(139, 62, 47)) shapes.append(r) return shapes
def bullet_init(x, y, scale): shapes3 = [] bullet = gr.Rectangle(gr.Point(x + scale * 100, y - scale * 39), gr.Point(x + scale * 115, y - scale * 44)) yellowbullet = gr.color_rgb(200, 200, 0) bullet.setFill(yellowbullet) shapes3.append(bullet) return shapes3
def catArm_init(x, y, scale): '''Creates and returns a list of graphics objects to make up a cat arm''' shapes = [] r = gr.Rectangle(gr.Point(x + scale * 170, y + scale * 68), gr.Point(x + scale * 200, y + scale * 73)) r.setFill(gr.color_rgb(0, 0, 0)) shapes.append(r) return shapes
def init_man(x, y, scale): # define the man shapes and put them into a list # Head: Create a circle with the color brown head = gr.Circle(gr.Point(x + scale * 10, y - scale * 50), 20 * scale) head.setFill('brown') # arms : Create a rectangle with the color blue for the arms arms = gr.Rectangle(gr.Point(x + scale * 110, y), gr.Point(x - scale * 80, y - scale * 30)) arms.setFill('blue') # stomach : Create a rectangle with the color blue for the sh stomach = gr.Rectangle(gr.Point(x + scale * 70, y + scale * 60), gr.Point(x - scale * 50, y)) stomach.setFill('blue') # # leg2 : Create a rectangle with the color red for pants leg1 = gr.Rectangle(gr.Point(x - scale * 20, y + scale * 60), gr.Point(x - scale * 40, y + scale * 180)) leg1.setFill('red') # # leg2 : Create a rectangle with the color red for pants leg2 = gr.Rectangle(gr.Point(x + scale * 60, y + scale * 60), gr.Point(x + scale * 40, y + scale * 180)) leg2.setFill('red') # man creates the list of all the basic shapes created by zelle man = [head, arms, leg1, leg2, stomach] return man
def grass(x, y, scale): # define the grass cc1 = g.Circle( g.Point(x + 250 * (random.uniform(0.2, 3)) * scale, y + 150 * (random.uniform(1, 1.5)) * scale), random.uniform(5, 10)) # random grass generate cc1.setFill("green") cc2 = g.Circle( g.Point(x + 250 * (random.uniform(0.2, 3)) * scale, y + 150 * (random.uniform(1, 1.5)) * scale), random.uniform(5, 10)) # random grass generate cc2.setFill("green") test3 = [cc1, cc2] return test3
def init_sun(x, y, scale): # create a circle with the sun inner_sun = gr.Circle(gr.Point(x - scale * 260, y - scale * 200), 120 * scale) inner_sun.setFill('orange') outer_sun = gr.Circle(gr.Point(x - scale * 260, y - scale * 200), 150 * scale) outer_sun.setFill('yellow') # leg1 = gr.Rectangle( gr.Point ( x-scale*20, y+scale*60), gr.Point(x-scale*40, y+scale*180 )) # leg1.setFill('red') sun = [outer_sun, inner_sun] return sun
def cauldron_init(x, y, scale): '''Creates and returns a list of graphics objects to make up a cauldron''' shapes = [] o = gr.Oval(gr.Point(x - scale * 35, y + scale * 13), gr.Point(x + scale * 35, y + scale * 63)) o.setFill(gr.color_rgb(56, 56, 56)) shapes.append(o) r = gr.Rectangle(gr.Point(x - scale * 25, y + scale * 10), gr.Point(x + scale * 25, y + scale * 20)) r.setFill(gr.color_rgb(56, 56, 56)) a(r) return shapes
def init_pool(x, y, scale): # # diving board : Create a rectangle with the color brown diving_board = gr.Rectangle(gr.Point(x + scale * 180, y + scale * 180), gr.Point(x - scale * 260, y + scale * 250)) diving_board.setFill('brown') # create a swiming pool with the color skyblue water = gr.Rectangle(gr.Point(x + scale * 780, y + scale * 880), gr.Point(x - scale * 260, y + scale * 250)) water.setFill('skyblue') # create a list of the complex shapes pool = [diving_board, water] return pool
def steam_animation_frame(shapes,frame_num,win): ''' Draws one frame of a steam plant animation. The animation will involve smoke rising out of the chimney. shapes is a list containing the graphics objects needed to draw the steam plant. frame_num is a number indicating which frame of animation it is. win is the GraphWin object containing the scene. This animates by creating up to 20 steam circles. Each circle creeps up the screen until it gets to the top, when it is brought back down to the smokestack so it can be used again. ''' p1 = shapes[0].getP1() p2 = shapes[0].getP2() dx = p2.getX() - p1.getX() newx = (p2.getX() + p1.getX())*0.5 newy = p2.getY() - dx*0.5 if frame_num % 2 == 0 and len(shapes) < 20: c = gr.Circle(gr.Point(newx,newy),0.4*dx) c.setFill(gr.color_rgb(150,150,150)) c.draw(win) shapes.append(c) for thing in shapes[3:]: thing.move(0,-10) center = thing.getCenter() if center.y < 0: mx = newx - center.getX() my = newy - center.getY() thing.move(mx,my)
def main(argv): if len(argv) < 2: print("usage: python3 image.py <image filename>") return # read in the image from the filename specified on the command filename = argv[1] image = gr.Image(gr.Point(0, 0), filename) # create a window that is the same size as the image rows = image.getHeight() cols = image.getWidth() win = gr.GraphWin(filename, cols, rows) # move the image so it is centered in the window image.move(cols / 2, rows / 2) # call the filter function before the image is drawn into a window # swaprb( image ) # reduceGreen( image ) whitecover(image) image.draw(win) win.getMouse() win.close()
def warhol(filename, listOfEffects): # read the image from the file original = gr.Image(gr.Point(0, 0), filename) rows = original.getHeight() cols = original.getWidth() panels = len(listOfEffects) # create a window based on the image and how many copies win = gr.GraphWin('Warhol', cols * panels, rows) allimages = [] # for each effect for i in range(panels): # clone the original image clone = original.clone() # duplicates the Image object # apply the filter to the clone if listOfEffects[i] == 'swaprb': filter.swaprb(clone) elif listOfEffects[i] == 'reduceGreen': filter.reduceGreen(clone) elif listOfEffects[i] == 'whitecover': filter.whitecover(clone) # move the image to the right location clone.move(cols / 2 + i * cols, rows / 2) # draw the image into the window clone.draw(win) allimages.append(clone) # return the window return win, allimages
def explosion_init(x, y, scale): shapes4 = [] explosion = gr.Circle(gr.Point(x + 35 * scale, y - scale * 9), 100) redexp = gr.color_rgb(255, 0, 0) explosion.setFill(redexp) shapes4.append(explosion) return shapes4
def icecream_init(x,y,scale): ''' Creates the list of objects needed to draw an ice cream cone at position (x,y) with the given scale. ''' shapes = [] #cone IDX 0 t = gr.Polygon(gr.Point(x,y),gr.Point(x+5*scale,y-15*scale), gr.Point(x-5*scale,y-15*scale)) t.setFill(gr.color_rgb(222,184,135)) shapes.append(t) #scoops IDX 1-4 c = gr.Circle(gr.Point(x,y-20*scale),6*scale) c.setFill(gr.color_rgb(255,0,127)) shapes.append(c) c = gr.Circle(gr.Point(x,y-30*scale),6*scale) c.setFill(gr.color_rgb(0,255,128)) shapes.append(c) c = gr.Circle(gr.Point(x,y-40*scale),6*scale) c.setFill(gr.color_rgb(153,255,255)) shapes.append(c) c = gr.Circle(gr.Point(x,y-50*scale),6*scale) c.setFill(gr.color_rgb(255,153,153)) shapes.append(c) return shapes
def warhol(filename, listOfEffects): # read the image from the file use original=Image... # and get rows with Height, cols with Width... original = gr.Image(gr.Point(0, 0), filename) rows = original.getHeight() cols = original.getWidth() panels = len(listOfEffects) # create a window based on the image and how many copies which from the len(listOfEffects) win = gr.GraphWin('Warhol', cols * panels, rows) allimages = [] # for each effect from the filter file swaprb, reduceGreen, whitecover for i in range(panels): # clone the original image clone = original.clone() # duplicates the Image object # apply the filter to the clone, which filter to apply? if listOfEffects[i] == 'swaprb': filter.swaprb(clone) elif listOfEffects[i] == 'reduceGreen': filter.reduceGreen(clone) elif listOfEffects[i] == 'whitecover': filter.whitecover(clone) # move the image to the right location with their owner cols values clone.move(cols / 2 + i * cols, rows / 2) # draw the image into the window and append each copy into the images clone.draw(win) allimages.append(clone) # create some text at the last image center so the Points should be cols*2.5 and rows/2 sometext = gr.Text(gr.Point(cols * 2.5, rows / 2), "hello world") sometext.setFill('red') sometext.setSize(20) sometext.draw(win) # create a red circle outline the text so the circle center is the same as above Points patch = gr.Circle(gr.Point(cols * 2.5, rows / 2), 80) patch.setOutline("red") patch.draw(win) # return the window and return all the images return win, allimages
def crystalBall_init(x, y, scale): '''Creates and returns a list of graphics objects to make up a crystal Ball''' shapes = [] c = gr.Circle(gr.Point(x + scale * 230, y + scale * 70), scale * 20) c.setFill(gr.color_rgb(198, 226, 255)) shapes.append(c) return shapes
def init_rocket(x, y, scale): # define the rocket shapes here and put them in a list #Body: Create a Rectangle using points (x-scale*10, y) and (x+scale*10, y-scale*80). #Set the Rectangle fill color to be a grey color (185, 185, 185). body = gr.Rectangle(gr.Point(x - scale * 10, y), gr.Point(x + scale * 10, y - scale * 80)) body.setFill(gr.color_rgb(185, 185, 185)) #Nose: Create a Polygon, with points (x-scale*10, y-scale*80), (x, y-scale*100 ), and (x+scale*10, y-scale*80 ). #Set the Polygon fill color to be a grey-blue color (150, 170, 200 ). #L Fin: Create a Polygon, with points (x-scale*10, y), (x-scale*10, y-scale*20 ), and (x- scale*25, y+scale*5 ). #Set the Polygon fill color to be a grey-red color (200, 170, 150 ). #R Fin: Create a Polygon, with points (x+scale*10, y), (x+scale*10, y-scale*20 ), and (x+scale*25, y+scale*5 ). #Set the Polygon fill color to be a grey-red color (200, 170, 150 ). return [body]
def warhol( filename, listOfEffects ): # read the image from the file original = gr.Image( gr.Point(0, 0), filename ) rows = original.getHeight() cols = original.getWidth() panels = len(listOfEffects) # create a window based on the image and how many copies win = gr.GraphWin( 'Warhol', cols*panels, rows ) allimages = [] # for each effect for i in range(panels): # clone the original image clone = original.clone() # duplicates the Image object # apply the filter to the clone, which filter to apply? if listOfEffects[i] == 'swaprg': image.swaprg( clone ) elif listOfEffects[i] == 'reduceRed': image.reduceRed( clone ) # implied else don't do anything # move the image to the right location clone.move( cols/2 + i*cols , rows/2 ) # draw the image into the window clone.draw(win) allimages.append( clone ) # create some text sometext = gr.Text( gr.Point( cols/2, rows/2+30 ), "Aaaaargh" ) sometext.setFill('red') sometext.setSize( 20 ) sometext.draw(win) # create an eye patch patch = gr.Circle( gr.Point( cols/2-15, rows/2-30 ), 8 ) patch.setFill( 'black' ) patch.draw(win) # return the window return win, allimages
def interactive(): win = gr.GraphWin( 'interactive demo', 500, 500, False ) r = gr.Rectangle( gr.Point( 220, 260 ), gr.Point( 280, 240 ) ) r.setOutline( 'purple' ) r.setFill( 'pink' ) r.setWidth( 3 ) r.draw( win ) counter = 0 falling = None # begin event loop while True: # infinite loop if counter % 100 == 0: if falling != None: falling.undraw() falling = gr.Circle( gr.Point( random.randint(0, 500), 0 ), 10 ) falling.setFill( 'blue' ) falling.draw(win) if falling != None: falling.move( random.randint(-3, 3), 5 ) counter += 1 key = win.checkKey() if key == 'q': break elif key == 'a': # move left r.move( -20, 0 ) elif key == 'w': # move up r.move( 0, -20 ) elif key == 's': # move down r.move( 0, 20 ) elif key == 'd': # move right r.move( 20, 0 ) win.update() # manually update the window print("Terminating") win.close()
def vehicle_init(x, y, scale): ''' Creates list of objects needed to draw a tan armored vehicle at posiiton (x,y) with given scale ''' shapes2 = [] vehicle = gr.Rectangle(gr.Point(x * scale + 100, y + scale * 30), gr.Point(x + scale * 30, y - scale * 1)) tan = gr.color_rgb(255, 239, 219) vehicle.setFill(tan) shapes2.append(vehicle) leftwheel = gr.Circle(gr.Point(x + scale * 44, y + scale * 40), 9 * scale) black = gr.color_rgb(0, 0, 0) leftwheel.setFill(black) shapes2.append(leftwheel) rightwheel = gr.Circle(gr.Point(x + scale * 88, y + scale * 40), 9 * scale) rightwheel.setFill(black) shapes2.append(rightwheel) return shapes2
def main(argv): win=gr.GraphWin("My window",500,500) c=gr.Circle(gr.Point(250, 250),10) c.draw(win) win.getMouse() c.setFill("green") win.getMouse() c.move(50,-40) win.getMouse() win.close() return
def steam_init(x, y, scale): ''' Creates a list of objets needed to draw a steam plant at position (x,y) with given scale ''' shapes = [] plant = gr.Rectangle(gr.Point(x, y), gr.Point(x + scale * 100, y - scale * 30)) grey = gr.color_rgb(185, 185, 185) plant.setFill(grey) shapes.append(plant) roof = gr.Rectangle(gr.Point(x - scale * 1, y - scale * 30), gr.Point(x + scale * 101, y - scale * 40)) ltbrown = gr.color_rgb(176, 133, 85) roof.setFill(ltbrown) shapes.append(roof) smokestack = gr.Rectangle(gr.Point(x, y), gr.Point(x + scale * 10, y - scale * 100)) rstbrwn = gr.color_rgb(136, 96, 90) smokestack.setFill(rstbrwn) shapes.insert(0, smokestack) return shapes
def main(): # assign to win a GraphWin object made with title, width, height, and the value False win = gr.GraphWin("rocketship!", 600, 600, False) # (note the final parameter is not one we have used before. It makes it so the # window doesn't attempt to redraw after every change). # assign to shapes an empty list shapes = [] # assign to c a new Circle object at (250, 250) with radius 10 c = gr.Circle(gr.Point(250, 250), 10) # call the draw function of the circle object stored in c c.draw(win) # append the variable c to the list shapes shapes.append(c) # while True while True: # call time.sleep with a half-second delay (0.5) time.sleep(0.5) # for each thing in shapes for thing in shapes: # assign to dx a random integer between -10 and 10 dx = random.randint(-10, 10) # assign to dy a random integer between -10 and 10 dy = random.randint(-10, 10) # call the move method of the object referred to by thing, passing in dx and dy thing.move(dx, dy) #color r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) color = gr.color_rgb(r, g, b) thing.setFill(color) #clone if random.random() < 0.2: oldthing = random.choice(shapes) newthing = oldthing.clone() newthing.draw(win) shapes.append(newthing) # tell the window to update its display (call win.update()) win.update() # if win.checkMouse() is not None if win.checkMouse() is not None: break # break out of the while loop # close the window win.close()
def spaceship_animate(shapes, frame_num, win): '''given the spaceship list, a frame number, and a window, it draws the spaceship in the window for the given frame number''' p1 = shapes[0].getP1() p2 = shapes[0].getP2() dx = p2.getX() - p1.getX() newx = (p1.getX() + p2.getX()) / 2 newy = p1.getY() for i in range(2): c = gr.Circle(gr.Point(newx, newy), 0.4 * dx) r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) color = gr.color_rgb(r, g, b) c.setFill(color) c.draw(win) shapes.append(c) for item in shapes[:4]: item.move(0, -dx * 0.25) count = 4 for item in shapes[4:]: c = item.getCenter() if c.getY() < newy + 5 * dx: if count % 2 == 0: item.move(random.randint(-5, -1), dx * 0.5) else: item.move(random.randint(1, 5), dx * 0.5) else: item.undraw() shapes.pop(count) count = count - 1 count = count + 1
def main(): win=g.GraphWin("My window", 1000, 600) x=0 y=0 scale=1 for i in range(30): cc1 = g.Circle(g.Point(x + 260 * (random.uniform(1,3)) * scale, y + 250 * (random.uniform(1,1.2)) * scale), random.uniform(5,10)) cc1.setFill("green") cc2 = g.Circle(g.Point(x + 260 * random.uniform(1, 2) * scale, y - 250 * random.uniform(1, 2) * scale), random.uniform(10, 20)) cc2.setFill("green") test4 = [cc1, cc2] for thing in test4: thing.draw(win) cc1.draw(win) win.getMouse() win.close()
def init_card(win): cards = [] #create Rectangle objects for cards and exit button for i in range(6): for j in range(7): card = gr.Rectangle(gr.Point(60 + 100 * j, 50 + 120 * i), gr.Point(140 + 100 * j, 150 + 120 * i)) cards.append(card) exit = gr.Rectangle(gr.Point(600, 10), gr.Point(650, 30)) #draw cards for card in cards: card.setFill("white") card.draw(win) #show text for exit button exit.setFill("white") exit.draw(win) text = gr.Text(gr.Point(625, 20), "Exit") text.setFill("black") text.draw(win) return cards, exit
def main(): win = gr.GraphWin( 'balls colliding', 500, 500, False ) ball1 = pho.Ball( win, 20 ) ball2 = pho.Ball( win, 20 ) ball1.setPosition( [200, 200] ) ball2.setPosition( [300, 200] ) # set up velocity and acceleration so they collide ball1.setVelocity( [200, 200] ) ball2.setVelocity( [-200, 200] ) ball1.setAcceleration( [0, -200] ) ball2.setAcceleration( [0, -200] ) ball1.draw() ball2.draw() txt = gr.Text( gr.Point( 250, 50), "Click to continue" ) txt.setSize( 16 ) txt.draw(win) win.getMouse() # loop for some time and check for collisions dt = 0.01 for frame in range(120): if not coll.collision_ball_ball( ball1, ball2, dt ): ball1.update(dt) else: txt.setText( "Boom!" ) if not coll.collision_ball_ball( ball2, ball1, dt ): ball2.update(dt) win.update() time.sleep(0.5*dt) if win.checkMouse() != None: break txt.setText('Click to quit') win.getMouse() txt.setText('See ya') win.update() time.sleep(0.2) win.close()
def main(argv): # create a window win = gr.GraphWin("My window", 500, 500) print("argument1 ", argv[1]) # create a circle c = gr.Circle(gr.Point(int(argv[2]), int(argv[3])), int(argv[1])) c.setFill(gr.color_rgb(100, 30, 100)) # draw the circle inot the window c.draw(win) # pause until user gets mouse win.getMouse() win.close() return
def main( argv ): # create a window win = gr.GraphWin( "My window", 500, 500 ) # create a circle c = gr.Circle( gr.Point( 250, 250 ), 10 ) # draw the circle into the window c.move( 50, -40 ) # relative move to a new location c.draw( win ) # draw into the window win.getMouse() # pause c.setFill( "green" ) # make the circle green win.getMouse() # pause c.undraw() # undraw the circle # pause until the user clicks the mouse win.getMouse() win.close() # close window return
def main(): win = gr.GraphWin("Press a key", 400, 400, False) shapes = [] c = gr.Circle(gr.Point(250, 250, 10)) c.draw(win) shapes.append(c) while True time.sleep(0.5) for thing in shapes: dx=random.randInt(-10,10) dy = random.randInt(-10,10) thing.move(dx,dy) win.update() if win.checkMouse() break win.close()
def main(): ''' Draws circles with radius 10 in random colors and in random places in the window of given title and size until mouse is clicked in the window. ''' win = gr.GraphWin('Press a key', 400, 400, False) shapes = [] c = gr.Circle(gr.Point(250, 250), 10) c.draw(win) shapes.append(c) while True: time.sleep(0.5) for thing in shapes: r = random.randrange(0, 255) g = random.randrange(0, 255) b = random.randrange(0, 255) color = gr.color_rgb(r, g, b) thing.setFill(color) dx = random.randint(-10, 10) dy = random.randint(-10, 10) thing.move(dx, dy) if random.random() < 0.2: oldthing = random.choice(shapes) newthing = oldthing.clone() newthing.draw(win) shapes.append(newthing) win.update() if win.checkMouse(): break win.close()