John Zelle Python Graphics Module

From Knowledge Kitchen
Jump to navigation Jump to search


Overview

John M. Zelle

The code here plays around with the Graphics module built by John M. Zelle to accompany his book, Python Programming: An Introduction to Computer Science (Franklin, Beedle & Associates). To use this module, will need to download the file graphics.py linked to from his site and include it in the folder where you save your own code.

Zelle's Graphics module provides a set of objects that simplify the creation and drawing of common graphics shapes. The module contains class definitions representing the following common shapes:

  • Lines
  • Circles
  • Rectangles
  • Ovals
  • Polygons
  • Text objects
  • Images

Getting started

As with much object-oriented code, you don't really have to know how these classes work. But you do have to know how to use them.

Zelle's documentation:

Other more sophisticated graphics modules

Advanced Python programmers probably would not use this module. There are a few popular Python modules that provide graphics capabilities:

  • PyGame ' a gaming engine for Python
  • Pyglet ' another 2d graphics library
  • TkInter ' a bare-bones graphical user interface (GUI) toolkit included as part of Python.

Why we are not using these

Unfortunately, these modules have not been fully ported, including documentation, to Python 3.x, which is what we are using. Also, they tend to be more complicated.

So we will stick with Zelle's module here.

Examples

All examples require that the graphics.py module be in the same folder as your own code.

Creating a graphics window

See the official documentation of the GraphWin class

Here is an example of creating a window:

from graphics import *    #import Zelle's graphics module

windowWidth = 100
windowHeight = 100
windowTitle = "My Favorite Window"

# create the window by instantiating the GraphWin class
win = GraphWin(windowTitle, windowWidth, windowHeight)

Creating a graphics window and closing it when the user clicks the mouse

Here is an example of creating a window that waits for the user to click the mouse, and then closes. This extra functionality uses the GraphWin object's getMouse() and close() methods, which you can see documented on the reference website:

from graphics import *    #import Zelle's graphics module

windowWidth = 100
windowHeight = 100
windowTitle = "My Favorite Window"

# create the window by instantiating the GraphWin class
win = GraphWin(windowTitle, windowWidth, windowHeight)

#wait till user clicks the mouse before closing the window
win.getMouse()      # Pause until the user clicks the mouse
win.close()         # Close window when done

Creating a Point

The module provides a class named Point, which can be used to represent a set of x,y coordinates in the window.

See official documentation of the methods encapsulated within the Point class

Here is an example of creating a window and then creating a Point object:

from graphics import *

windowWidth = 100
windowHeight = 100
windowTitle = "My Favorite Window"

win = GraphWin(windowTitle, windowWidth, windowHeight)

#create a Point object
xCoord = 50
yCoord = 25
p = Point(xCoord, yCoord)  # create the Point object by instantiating the Point class

Creating a Circle

see official documentation about the methods encapsulated in the Circle class

Here is an example of creating a window, and then creating and drawing a Circle object:

from graphics import *

windowWidth = 100
windowHeight = 100
windowTitle = "My Favorite Window"

win = GraphWin(windowTitle, windowWidth, windowHeight)

xCoord = 50
yCoord = 25
p = Point(xCoord, yCoord)

r = 10              # the radius of the circle
c = Circle(p, r)    # instantiate a Circle object

c.draw(win)         # draw the Circle object to the window

#wait till user clicks the mouse before closing the window
win.getMouse()      # Pause to view result until the user clicks the mouse
win.close()         # Close window when done

Creating a Rectangle

see official documentation about the methods encapsulated in the Rectangle class

Here is an example of creating a window, and then creating and drawing a Rectangle object:

from graphics import *

windowWidth = 100
windowHeight = 100
windowTitle = "My Favorite Window"

win = GraphWin(windowTitle, windowWidth, windowHeight)

#create the top left and bottom right points for this rectangle
topLeftPoint = Point(10, 10)
bottomRightPoint = Point(20, 20)

r = Rectangle(topLeftPoint, bottomRightPoint)    # instantiate a Rectangle object

r.draw(win)         # draw the object to the window

#wait till user clicks the mouse before closing the window
win.getMouse()      # Pause to view result until the user clicks the mouse
win.close()         # Close window when done

Bouncing rabbit

Save the poor rabbit (to your hard drive)

A rabbit that bounces around the window.

This program relies on the following assumption:

  • You have the graphics module written by John M. Zelle in the same folder as this program.
  • You have a GIF image file named rabbit.gif in the same folder as this program.


from graphics import *
import time

#create a window, specify title, width, height
win = GraphWin("My First Graphics Program!!!", 400, 400)

#create a point by specifying the x and y positions
point1 = Point(200, 200)

#create circle, specify the center point and radius
image1 = Image(point1, "Rabbit.gif")

#make it show up in the window
image1.draw(win)

speedX = 10
speedY = 7

#slide the circle over in the x direction one pixel at a time
while True:


    centerPoint = image1.getAnchor()
    xPos = centerPoint.getX()
    yPos = centerPoint.getY()

    if (xPos + (image1.getWidth() / 2)) > 400 or (xPos- (image1.getWidth() / 2)) < 0:
        speedX = speedX * -1
    if (yPos + (image1.getHeight() / 2)) > 400 or (yPos - (image1.getHeight() / 2)) < 0:
        speedY = speedY * -1

    image1.move(speedX, speedY)
    
    time.sleep(0.05)


Inherited methods

All Graphics Objects, such as Point, Circle, Rectangle, Image, and the others that are listed on the reference site, inherit a few common methods from their parent class.

These common methods are outlined on the Graphics Objects reference page.

So, for example, if we create a Circle object:

c = Circle(p, r)    # instantiate a Circle object
c.draw(win)         # draw the Circle object to the window

we can then move that circle object by calling its inherited move(dx,dy) method:

c.move(5, 10)    # move the circle 5 units to the right, and 10 units down

... and this same move(dx,dy) method can be used on any other graphics object, such as Rectangle and the rest because it is inherited by all Graphics Objects.

Download more examples

Basics

Download the basic examples. Examples include:

  • creating a window
  • detecting a mouse click (watch the output to the Python shell when you click)
  • detecting a mouse click with a counter display
  • drawing a circle


Movement

Download the movement examples. Examples include:

  • example of three bouncing circles
  • drawing a circle that oscillates on the screen
  • drawing a circle that oscillates on the screen and fires bullets when you click the window


Alien examples

Download the alien examples. Examples include:

  • bouncing aliens
  • an oscillating circle that shoot at the bouncing aliens


Jerking cat and mouse examples

Download the jerking cat and mouse examples. Examples include:

  • jerking cat that wraps around if it goes off-screen
  • jerking cat with a smoothing function applied to make it move more smoothly
  • a much more advanced example of a smooth cat chasing a smooth mouse]], done in object-oriented style


Thanksgiving turkey cull example

Download the thanksgiving turkey cull example and click to catch a turkey. Example includes:

  • a turkey that randomly wanders around the screen
  • an oscillating bow-and-arrow
  • when you click on the screen, the bow shoots an arrow
  • if the arrow pierces the turkey, your reward is a roasted turkey


What links here