This sheet describes briefly all the graphics things (i.e., drawing pictures) you can easily do with the gasp module for Python. (You can actually do lots of other complicated graphics things with Python, but they’re, errrm, complicated. We’ve made a bunch of useful things easy using the gasp module, and these are what this sheet describes.)
There’s an introduction to graphics in Sheet 3 (Pretty Pictures), so this sheet is more a reference than an introduction. Some of it is very terse; ask a teacher if it doesn’t make any sense. (If you are a teacher or you are studying this on your own and it still doesn’t make any sense, feel free to send an email to email@example.com .)
(0, 0) is at the bottom left of the window. The window is 640 pixels by 480, by default. (You can make it a different size if you want to.) Coordinates are given in units of one pixel.
All functions that take coordinates take them as a tuple (x, y).
Circle((300, 200), 10) # :) This is good Circle(300, 200, 10) # :( This is bad
To access the color module GASP has to offer. Call color.* where * is the color you wish to call. For example: ` color.BLACK ` This is the color black. Check out the gasp color refrence chart to see all of the availble color options.
from gasp import * begin_graphics() ... # all of your code end_graphics()
These are the essentials. ` from gasp import * ` imports the gasp module, begin_graphics() starts the graphics window, and end_graphics() quits the graphics window and ends the program. It’s dead simple, but also dead necessary.
begin_graphics(width=800, height=600, title="My Game", background=color.YELLOW)
This creates a graphics window with the dimensions 800x600, a title of My Game , and a background color of yellow. With no arguments you get a white 640x480 graphics window titled Gasp .
Ends a graphics window.
Clears everything off of the graphics window. It looks like a new graphcs window as if you just called begin_graphics().
removes those objects from the screen
The objects that you will be displayed in your graphics window. You can manipulate these objects using the screen object methods
Plot(pos, color=color.black, size=1)
It puts a dot on the screen.
Line(start, end, color=color.black)
Creates a line on the screen.
Box(center, width, height, filled=False, color=color.black, thickness=1)
This creates a Box on the screen
Polygon(points, filled=False, color=color.black, thickness=1)
Creates a polygon on the screen
Circle(center, radius, filled=False, color=color.black, thickness=1)
Draws a circle, its center is a set of coordinates, and the radius is in pixels. It defaults to not being filled and the color black.
Arc(center, radius, start_angle, end_angle, filled=False, color=color.black, thickness=1)
Creates an arc on the screen.
Oval(center, width, height, filled=False, color=color.black, thickness=1)
Puts an oval on the screen wherever you want.
Image(file_path, center, width=None, height=None):
Uploads an image onto the screen. If you only pass width and not a height it automatically scales the height to fit the width you passed it. It behaves likewise when you pass just a height.
The methods that manipulates screen objects
Move a screen object to a pos
move_by(obj, dx, dy)
Move a screen object relative to it’s position
Rotate an object to an angle
Rotate an object a certain degree.
Text(text, pos, color=color.black, size=12)
Puts text on the screen
All of the functions that manipulate sound.
Creates a sound object
Plays a sound object
stop_sound(obj, finish=False, fade=True)
It stops a sound object that was playing