I Know Where Your Cat Lives
Graphics Module
- So far we have created graphical programs using turtle
- But turtle isn’t very good for creating animations
- The underlying graphical system that turtle is built on is difficult to use
- So I created a module to make creating animations easier
- Using module that is not built-in requires an extra step
- Before importing it, you have to put the module’s .py file in the same directory and the .py file that is using it.
- The graphics module’s documentation has a link to the graphics.py file you can use during lab
To draw an oval use the
draw_oval
function, need to specify the x, y, width, and heightimport graphics graphics.draw_oval(0, 0, 100, 100)
- Note the coordinate system is different than turtle, (0, 0) is in the upper left and positive y is down
Can also change the outline, fill, and thickness
import graphics graphics.draw_oval(100, 100, 100, 200, 'red', 'blue', 5)
To create an animation, need to draw multiple times
import graphics for i in range(100) graphics.draw_oval(i, i, 100, 100)
- Add a loop to draw multiple ovals, but they all get drawn very quickly
To add a delay, use the
wait
functionimport graphics for i in range(100) graphics.draw_oval(i, i, 100, 100) graphics.wait()
To remove the trail, need to clear the screen
import graphics for i in range(100) graphics.clear() graphics.draw_oval(i, i, 100, 100) graphics.wait()
Drawing Images
- For the lab today you will need to draw images using the grahpics module
- The graphics module can only draw gif images
- To find a gif do a google image search and
filetype:gif
to your search terms - Put the image in the same directory and you .py file and the graphic.py file
Draw an image with the
draw_image
functiongraphics.draw_image('ball.gif', 0, 0)
- The first argument is the name of the image file, don’t for get the quotes
The second and third arguments are the x and y coordinates of the center of the image