└── motion_parallax.py /motion_parallax.py: -------------------------------------------------------------------------------- 1 | ### 2 | ### Author: Xin Li 3 | ### Description: In this programming assignment, 4 | ### In this programming assignment, you will be 5 | ### building a program that displays a landscape 6 | ### in a graphics canvas and allows the user to 7 | ### control the perspective of the landscape by 8 | ### moving the mouse. The perspective seems to 9 | ### change due to an effect called motion parallax 10 | ### , which you should implement in this program. 11 | ### The above gif shows an example of the user 12 | ### interacting with a completed version of the 13 | ### program. 14 | ### Name of this program is: motion_parallax.py 15 | from graphics import graphics 16 | import random 17 | def main(): 18 | gui = graphics(600, 600, 'motion_parallax') 19 | color_string1, color_string2, color_string3 = random_mountian_color(gui) 20 | x_coord = -600 21 | x_coord_2 = 0 22 | x_coord_3 = 0 23 | while True: 24 | y_coord = 100 25 | y_coord_2 = 50 26 | y_coord_3 = 0 27 | gui.clear() 28 | x = gui.mouse_x // 5 + 200 29 | y = gui.mouse_y // 5 + 200 30 | blue_canvas(gui, x, y) 31 | mountains(gui, x, y, color_string1, color_string2, color_string3) 32 | lake_lawn_and_tree_house(gui, x, y) 33 | the_birds(gui, x_coord, y_coord) 34 | x_coord=change_of_x_coord(x_coord) 35 | the_clouds(gui, x_coord_2, y_coord_2) 36 | x_coord_2=change_of_x_coord_2(x_coord_2) 37 | 38 | gui.update_frame(60) 39 | 40 | def blue_canvas(gui, x, y): 41 | '''Initially, get a program that generates 42 | a square canvas, and fill the canvas with 43 | a blue rectangle. The blue rectangle can serve 44 | as the sky. 45 | ''' 46 | gui.rectangle(-10, -10, 620, 620, 'sky blue') 47 | gui.ellipse(x+150, y-150, 100, 100, 'gold') 48 | 49 | def mountains(gui, x, y, color_string1, color_string2, color_string3): 50 | '''Next, work on getting a static landscape 51 | displayed. At this point, don鈥檛 worry about 52 | the motion parallax, random mountain colors, 53 | or the birds. The shapes will be added the canvas 54 | in the order you add them programatically. 55 | ''' 56 | gui.triangle(x+50, y-100, x-150, y+250, x+250, y+250, color_string1) 57 | gui.triangle(x-150, y-50, x-450, y+250, x+150, y+250, color_string2) 58 | gui.triangle(x+200, y-50, x-50, y+250, x+550, y+250, color_string3) 59 | 60 | def lake_lawn_and_tree_house(gui, x, y): 61 | gui.rectangle(x-450, y+250, 1000, 300, 'springgreen') 62 | i = 0 63 | while i <= 620: 64 | gui.line(i, y+230, i, y+250, 'springgreen') 65 | i += 5 66 | gui.rectangle(x+150, y+230, 20, 50, 'brown') 67 | gui.ellipse(x+160, y+200, 50, 100, 'green') 68 | gui.ellipse(x, y+300, 400, 50, 'blue') 69 | gui.rectangle(x+220, y+250, 100, 100, 'gold4') 70 | gui.triangle(x+270, y+200, x+220, y+250, x+320, y+250, 'orange') 71 | gui.rectangle(x+230, y+275, 40, 40, 'sky blue') 72 | gui.rectangle(x+280, y+275, 30, 60, 'brown') 73 | 74 | def random_mountian_color(gui): 75 | '''Update the program so that it generates three random colors 76 | for the mountains on each program run. 77 | ''' 78 | color_string1 = "blue" 79 | color_string2 = "red" 80 | color_string3 = "green" 81 | red = random.randint(0,255) 82 | green = random.randint(0,255) 83 | blue = random.randint(0,255) 84 | color_string1 = gui.get_color_string(blue, red, green) 85 | color_string2 = gui.get_color_string(red, green, blue) 86 | color_string3 = gui.get_color_string(green, blue, red) 87 | return color_string1, color_string2, color_string3 88 | 89 | 90 | def the_birds (gui, x_coord, y_coord): 91 | '''add the flying birds to the canvas. You should 92 | use a while loop. You are not required to make the 93 | birds move, or to be a part of the parallax. 94 | ''' 95 | i = 0 96 | while i < 5: 97 | gui.line(x_coord+100*i, y_coord+20*i, x_coord+25+100*i, y_coord+10+20*i, 'black', 5) 98 | gui.line(x_coord+25+100*i, y_coord+10+20*i, x_coord+50+100*i, y_coord+20*i, 'black', 5) 99 | i += 1 100 | def change_of_x_coord(x_coord): 101 | x_coord += 10 102 | if x_coord > 620: 103 | x_coord = -600 104 | return x_coord 105 | 106 | def the_clouds (gui, x_coord_2, y_coord_2): 107 | gui.ellipse(x_coord_2, y_coord_2, 150, 50, 'white') 108 | def change_of_x_coord_2(x_coord_2): 109 | x_coord_2 += 5 110 | if x_coord_2 > 700: 111 | x_coord_2 = -200 112 | return x_coord_2 113 | main() 114 | --------------------------------------------------------------------------------