Danqing's CS151 Project 6
Skip to end of metadata
Go to start of metadata

In this project we are supposed to learn how to use graphics objects to draw pictures and make animations. Also, we understand how to write more effective codes. 
The first required picture is each complex object we build in complex_shape.
My first object is a stoplight. When it is animated, it can shift color from green to yellow and red. 

The second object is a marine. It is a character from Starcraft. When it is animated it can fall from the sky.
The third thing is a plane which can fly through the window when animated.

The fourth thing is a building. When animated the windows on the building can shift color from yellow to black randomly. 
my code to make it is shown below.
 for i in range (50):
        time.sleep(0.8)
        for item in m:
            if random.random()<0.6:
                item.setFill("yellow")
            else:
                item.setFill("black")
(m is a list of those windows)

Next object is a monster. When it is animated it can grow larger. I show my code below.
m=monster_init(x,y,size)
    draw(m,win)
    for i in range (40):
        time.sleep(0.01)
        undraw(m)
        size=size+0.03
        m=monster_init(x,y,size)
        draw(m,win)
m=monster_init(x,y,size)
    draw(m,win)
    for i in range (40):
        time.sleep(0.01)
        undraw(m)
        size=size+0.03
        m=monster_init(x,y,size)
        draw(m,win)
(size is the origin size of the monster. I undraw the old monster and redraw a new larger monster at same location for every loop)
 
Here is the movie with all of my work in it. Specially I used thread module to create subprocess for each function so that they can process at same time. 
Here is one example:
for i in range(6):
        thread.start_new_thread(complex_shape.air_drop_marine,(30+i*200,70,1,5,win))
        time.sleep(0.8)
In this way I can air drop another marine when the first marine haven't reach the ground. 
I write all my codes and make the gif on my pc so the picture may looks messy. I used a online gif tool to make this gif.

For extension:
1. I draw 5 things totally so it is 3 more than required.
2. Set up a system that creates a scene out of complex objects based on a list that gives the name, location, and scale of each complex object in the scene.

I still want to call my functions one by one so that I can set up some arguments. However, if I wish I can also use list to store name location etc. of objects.
clist=[[complex_shape.fly_plane,20,30,1.5,6,win],[complex_shape.stoplight,1000,30,0.5,win]]
and then if my functions have same number of arguments I can use 
for i in clist:
eval(i[0])(i[1],i[2],i[3]...)
to draw these pictures. This way can save some times. But my functions have different arguments so this time I can't use it in my main function. 

Conclusion:

In this object, I learned how to use graphics module to make animations. The use of time.sleep function in loops is new and interesting.

Including shapes in a list to process a whole picture as a unit is also an efficient way. Another thing is the use to thread module. 

I wanted to run two function at the same time so I learned thread module online by myself. Sub-processing is very useful in the future study. 

Labels