def spaceship_init(x, y, scale): '''Creates and returns a list of graphics objects to make up a spaceship''' shapes = [] r = gr.Rectangle(gr.Point(x - scale * 10, y), gr.Point(x + scale * 10, y - scale * 80)) r.setFill(gr.color_rgb(185, 150, 185)) shapes.append(r) p = gr.Polygon(gr.Point(x - scale * 10, y - scale * 80), gr.Point(x, y - scale * 100), gr.Point(x + scale * 10, y - scale * 80)) p.setFill(gr.color_rgb(150, 170, 200)) shapes.append(p) p = gr.Polygon(gr.Point(x - scale * 10, y), gr.Point(x - scale * 10, y - scale * 20), gr.Point(x - scale * 25, y)) p.setFill(gr.color_rgb(200, 170, 150)) shapes.append(p) p = gr.Polygon(gr.Point(x + scale * 10, y), gr.Point(x + scale * 10, y - scale * 20), gr.Point(x + scale * 25, y)) p.setFill(gr.color_rgb(200, 170, 150)) shapes.append(p) return shapes
def potion_init(x, y, scale): '''Creates and returns a list of graphics objects to make up a potion''' shapes = [] p = gr.Polygon(gr.Point(x + scale * 30, y + scale * 80), gr.Point(x + scale * 40, y + scale * 60), gr.Point(x + scale * 40, y + scale * 40), gr.Point(x + scale * 50, y + scale * 40), gr.Point(x + scale * 50, y + scale * 60), gr.Point(x + scale * 60, y + scale * 80)) p.setFill(gr.color_rgb(198, 226, 255)) shapes.append(p) p = gr.Polygon(gr.Point(x + scale * 30, y + scale * 80), gr.Point(x + scale * 40, y + scale * 60), gr.Point(x + scale * 40, y + scale * 50), gr.Point(x + scale * 50, y + scale * 50), gr.Point(x + scale * 50, y + scale * 60), gr.Point(x + scale * 60, y + scale * 80)) p.setFill(gr.color_rgb(255, 62, 150)) shapes.append(p) r = gr.Rectangle(gr.Point(x + scale * 42, y + scale * 35), gr.Point(x + scale * 48, y + scale * 40)) r.setFill(gr.color_rgb(139, 71, 38)) shapes.append(r) return shapes
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 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 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 fire2_init(x, y, scale): '''Creates and returns a list of graphics objects to make up a fire''' shapes = [] p = gr.Polygon(gr.Point(x + scale * 30, y + scale * 63), gr.Point(x + scale * 30, y + scale * 45), gr.Point(x + scale * 20, y + scale * 63)) p.setFill(gr.color_rgb(255, 165, 0)) shapes.append(p) p = gr.Polygon(gr.Point(x + scale * 15, y + scale * 63), gr.Point(x + scale * 10, y + scale * 45), gr.Point(x + scale * 10, y + scale * 63)) p.setFill(gr.color_rgb(255, 165, 0)) shapes.append(p) p = gr.Polygon(gr.Point(x, y + scale * 63), gr.Point(x - scale * 5, y + scale * 45), gr.Point(x - scale * 10, y + scale * 63)) p.setFill(gr.color_rgb(255, 165, 0)) shapes.append(p) p = gr.Polygon(gr.Point(x - scale * 20, y + scale * 63), gr.Point(x - scale * 30, y + scale * 45), gr.Point(x - scale * 30, y + scale * 63)) p.setFill(gr.color_rgb(255, 165, 0)) shapes.append(p) return shapes
def explosion_animation_frame(shapes4, frame_num, win): ''' After bullet has reached vehicle there is a huge explosion ''' for item in shapes4: orangeexp = gr.color_rgb(255, 165, 0) item.setFill(orangeexp) yellowexp = gr.color_rgb(255, 255, 0) item.setFill(yellowexp)
def background_animation_frame(shapes,frame_num,win): ''' Animates the background by having the color of the sun's rays alternate between orange and yellow. ''' for i in range(3,7): for j in range(7,11): if frame_num % 2 == 1: shapes[i].setFill(gr.color_rgb(255,150,0)) shapes[j].setFill(gr.color_rgb(255,255,0)) else: shapes[i].setFill(gr.color_rgb(255,255,0)) shapes[j].setFill(gr.color_rgb(255,150,0))
def main(): # create a window and tell it not to auto-update win = gr.GraphWin('whales', 1000, 1000, False) # whale is a list of graphics objects whale = citywhale(500, 500, 2.0) whale2 = citywhale(200, 800, 4.0) # draw each graphics object into the window #for thing in whale: # thing.draw( win ) draw(whale, win) draw(whale2, win) while True: # check for a keystroke key = win.checkKey() if key == 'q': break # exit the loop # move the whales left move(whale, -1, 0) move(whale2, -2, 0) # using the whale's eye to check it's horizontal location p1 = whale[2].getCenter() # if the eye goes out of bounds, move it right if p1.x < 0: move(whale, win.getWidth(), 0) # change the color when it shifts whale[0].setFill( gr.color_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) # same check for whale 2 p2 = whale2[2].getCenter() if p2.x < 0: move(whale2, win.getWidth(), 0) whale2[0].setFill( gr.color_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))) # need to update each time for smooth motion win.update() # wait for a mouse click win.getMouse() win.close()
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 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 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 swaprb(src): rows = src.getHeight() cols = src.getWidth() for row in range(rows): for col in range(cols): # get the value of the pixel at (col, row) r, g, b = src.getPixel(col, row) # this function returns 3 values src.setPixel(col, row, gr.color_rgb(b, g, r))
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 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 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 reduceRed(src): rows = src.getHeight() cols = src.getWidth() for row in range(rows): for col in range(cols): # get the value of the pixel at (col, row) r, g, b = src.getPixel(col, row) # this function returns 3 values # option1: rgb = red value is rgb[0], green value is rgb[1], blue value is rgb[2] # option2: r is red value, g is green value, b is the blue value # set the value of the pixel at (col, row) to halve the red value (integer division) src.setPixel(col, row, gr.color_rgb(r // 2, g, b))
def swaprg(src): rows = src.getHeight() cols = src.getWidth() for row in range(rows): for col in range(cols): # get the value of the pixel at (col, row) r, g, b = src.getPixel(col, row) # this function returns 3 values # option1: rgb = red value is rgb[0], green value is rgb[1], blue value is rgb[2] # option2: r is red value, g is green value, b is the blue value # set the value of the pixel at (col, row) to (g, r, b) src.setPixel(col, row, gr.color_rgb(g, r, b))
def whitecover(src): rows = src.getHeight() cols = src.getWidth() for row in range(rows): for col in range(cols): # get the value of the pixel at (col, row) r, g, b = src.getPixel(col, row) # this function returns 3 values r = min(r + 150, 255) g = min(g + 150, 255) b = min(b + 150, 255) src.setPixel(col, row, gr.color_rgb(r, g, b))
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 init_blocks(x, y, size, number, win): bricks = [] # each brick is one across and two supporting for i in range(number): b = pho.Block( win, size, size/8, [x, i*(size/3) + y], [0, 0], [0, 0], gr.color_rgb( 240, 220, 190 ) ) bricks.append( b ) # return the list of bricks and boards return bricks
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 steam_init(x,y,scale): ''' Creates the list of objects needed to draw a steam plant at position (x,y) with the given scale. ''' shapes = [] #steam plant r = gr.Rectangle(gr.Point(x,y),gr.Point(x+scale*100,y-scale*30)) r.setFill(gr.color_rgb(185,185,185)) shapes.append(r) #roof r = gr.Rectangle(gr.Point(x-scale*1,y-scale*30),gr.Point(x+scale*101,y-scale*40)) r.setFill(gr.color_rgb(176,133,85)) shapes.append(r) #smokestack r = gr.Rectangle(gr.Point(x,y),gr.Point(x+scale*10,y-scale*100)) r.setFill(gr.color_rgb(136,96,90)) shapes.insert(0,r) return shapes
def tank_init(x, y, scale): ''' Creates list of objects needed to draw a tank at position (x,y) with given scale ''' shapes1 = [] base = gr.Rectangle(gr.Point(x, y), gr.Point(x + scale * 100, y - scale * 30)) green = gr.color_rgb(0, 150, 0) base.setFill(green) shapes1.append(base) cannonhouse = gr.Rectangle(gr.Point(x + scale * 1.1, y - scale * 30), gr.Point(x + scale * 95, y - scale * 50)) darkergreen = gr.color_rgb(0, 120, 0) cannonhouse.setFill(darkergreen) shapes1.append(cannonhouse) cannon = gr.Rectangle(gr.Point(x + scale * 95, y - scale * 37), gr.Point(x + scale * 120, y - scale * 45)) darkestgreen = gr.color_rgb(0, 90, 0) cannon.setFill(darkestgreen) shapes1.append(cannon) return shapes1
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 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 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 background_init(x, y, scale): '''Creates and returns a list of graphics objects to make up a background''' shapes = [] r = gr.Rectangle(gr.Point(x, y), gr.Point(x + scale * 500, y + scale * 400)) r.setFill(gr.color_rgb(205, 170, 125)) shapes.append(r) r = gr.Rectangle(gr.Point(x, y + scale * 300), gr.Point(x + scale * 500, y + scale * 400)) r.setFill(gr.color_rgb(139, 71, 38)) shapes.append(r) #shelf r = gr.Rectangle(gr.Point(x + scale * 20, y + scale * 80), gr.Point(x + scale * 120, y + scale * 90)) r.setFill(gr.color_rgb(139, 71, 38)) shapes.append(r) r = gr.Rectangle(gr.Point(x + scale * 120, y + scale * 100), gr.Point(x + scale * 250, y + scale * 110)) r.setFill(gr.color_rgb(139, 71, 38)) shapes.append(r) r = gr.Rectangle(gr.Point(x + scale * 250, y + scale * 130), gr.Point(x + scale * 340, y + scale * 140)) r.setFill(gr.color_rgb(139, 71, 38)) shapes.append(r) r = gr.Rectangle(gr.Point(x + scale * 340, y + scale * 170), gr.Point(x + scale * 450, y + scale * 180)) r.setFill(gr.color_rgb(139, 71, 38)) shapes.append(r) #CrystalBallholder p = gr.Polygon(gr.Point(x + scale * 210, y + scale * 100), gr.Point(x + scale * 220, y + scale * 90), gr.Point(x + scale * 240, y + scale * 90), gr.Point(x + scale * 250, y + scale * 100)) p.setFill(gr.color_rgb(108, 123, 139)) shapes.append(p) return shapes
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()
def plant_init(x, y, scale): '''Creates and returns a list of graphics objects to make up a plant''' shapes = [] #pot r = gr.Rectangle(gr.Point(x + scale * 70, y + scale * 50), gr.Point(x + scale * 100, y + scale * 60)) r.setFill(gr.color_rgb(255, 130, 71)) shapes.append(r) p = gr.Polygon(gr.Point(x + scale * 80, y + scale * 80), gr.Point(x + scale * 75, y + scale * 60), gr.Point(x + scale * 95, y + scale * 60), gr.Point(x + scale * 90, y + scale * 80)) p.setFill(gr.color_rgb(255, 130, 71)) shapes.append(p) #leaves p = gr.Polygon(gr.Point(x + scale * 70, y + scale * 50), gr.Point(x + scale * 60, y + scale * 30), gr.Point(x + scale * 80, y + scale * 50)) p.setFill(gr.color_rgb(152, 251, 152)) shapes.append(p) p = gr.Polygon(gr.Point(x + scale * 75, y + scale * 50), gr.Point(x + scale * 80, y + scale * 20), gr.Point(x + scale * 85, y + scale * 50)) p.setFill(gr.color_rgb(152, 251, 152)) shapes.append(p) p = gr.Polygon(gr.Point(x + scale * 85, y + scale * 50), gr.Point(x + scale * 90, y + scale * 20), gr.Point(x + scale * 100, y + scale * 50)) p.setFill(gr.color_rgb(152, 251, 152)) shapes.append(p) p = gr.Polygon(gr.Point(x + scale * 90, y + scale * 50), gr.Point(x + scale * 110, y + scale * 30), gr.Point(x + scale * 100, y + scale * 50)) p.setFill(gr.color_rgb(152, 251, 152)) shapes.append(p) return shapes