Simple Graphics Module As an introduction to programming with graphics, we will be using the simple graphics module graphics.py developed by John M. Zelle to accompany his introductory textbook "Python Programming: An Introduction to Computer Science". The module and reference information are available at https://mcsp.wartburg.edu/zelle/python/ The graphics module will give us a number of objects we can use to create a graphical visual interface (GUI) for program development. These objects include GraphWin objects, the windows that we create as a "canvas" for our GUI, and Graphics objects that we can draw in the window and allow the user to interact with. GraphWin Objects To create a window for a program GUI, we must define a GraphWin object. This class of object allows us to enter a string that will appear on the title bar at the top of the window and define the dimensions (height and width in pixels) of the window. If only one argument (the string) is given, the default window size is 200×200. Discussion: What does the code mean? from graphics import ∗ win = GraphWin("My first window!", 400, 600) for i in range(5) : click = win.getMouse() print(click) win. close() from graphics import ∗ win = GraphWin("My first window!", 400, 600) for i in range(5) : click = win.getMouse ( ) print(click) win.close( ) Discussion: What does the code mean?
Simple Graphics Module As an introduction to programming with graphics, we will be using the simple graphics module graphics.py developed by John M. Zelle to accompany his introductory textbook "Python Programming: An Introduction to Computer Science". The module and reference information are available at https://mcsp.wartburg.edu/zelle/python/ The graphics module will give us a number of objects we can use to create a graphical visual interface (GUI) for program development. These objects include GraphWin objects, the windows that we create as a "canvas" for our GUI, and Graphics objects that we can draw in the window and allow the user to interact with. GraphWin Objects To create a window for a program GUI, we must define a GraphWin object. This class of object allows us to enter a string that will appear on the title bar at the top of the window and define the dimensions (height and width in pixels) of the window. If only one argument (the string) is given, the default window size is 200×200. Discussion: What does the code mean? from graphics import ∗ win = GraphWin("My first window!", 400, 600) for i in range(5) : click = win.getMouse( ) print(click) win. close( ) from graphics import ∗ win = GraphWin("My first window!", 400, 600) for i in range(5) : click = win. getMouse () print(click) win. close () Discussion: What does the code mean?
As an introduction to programming with graphics, we will be using the simple graphics module graphics.py developed by John M. Zelle to accompany his introductory textbook "Python Programming: An Introduction to Computer Science". The module and reference information are available at https://mcsp.wartburg.edu/zelle/python/ The graphics module will give us a number of objects we can use to create a graphical visual interface (GUI) for program development. These objects include GraphWin objects, the windows that we create as a "canvas" for our GUI, and Graphics objects that we can draw in the window and allow the user to interact with. GraphWin Objects To create a window for a program GUI, we must define a GraphWin object. This class of object allows us to enter a string that will appear on the title bar at the top of the window and define the dimensions (height and width in pixels) of the window. If only one argument (the string) is given, the default window size is 200×200. Discussion: What does the code mean? from graphics import ∗ win = GraphWin( "My first window!", 400, 600) for i in range(5) : click = win. getMouse( ) print(click) win.close() from graphics import ∗ win = GraphWin("My first window!", 400, 600) for i in range(5) : click = win.getMouse () print(click) win.close ()
Discussion: What does the code mean? from graphics import ∗ win = GraphWin("My second window!", 400, 600) num_clicks =0 while num_clicks <5 : click = win.checkMouse () \#print (click) if click == None : pass else : num_clicks +=1 print(click) win.close() from graphics import * win = GraphWin("My second window!", 400, 600) num_clicks =0 while num_clicks < 5 : click = win. checkMouse () \#print(click) if click == None : pass else : num_clicks +=1 print(click) win.close ( )
The GraphWin methods getMouse ( ) and checkMouse () are two different ways to get input from the user (there are also corresponding keyboard methods). The method getMouse pauses the program execution and waits until a mouse click has been detected before proceeding. The checkMouse method doesn't pause the execution of the program but simply checks to see if a click has been received, and otherwise has a value of None. Explore: Modify the code above by adding a print statement print ( click) right after click= win. checkMouse ( ) in the while loop. See what happens in the console? What if you replace the checkMouse method with getMouse? Graphics Objects Both of the GraphWin methods taking user input from the mouse return a Graphics object called a point. The Graphics objects included in the graphics module are a handful of simple geometric shapes: points, lines, circles, rectangles, ovals, and polygons, along with images and text. A Point object is defined by an ordered pair of coordinates that corresponds to the position in the GraphWin object that the mouse is clicked. You will notice that the Point objects returned by your mouse in the previous explorations indicate that the location (0,0) in the window is located in the upper left corner and the first coordinate measures horizontal distance (in pixels) positively to the right and vertically the positive direction is down. Point objects can be used to define characteristics (location, size, etc.) of other Graphics objects. Discussion: What does the code mean?
Graphics objects. Discussion: What does the code mean? from graphics import ∗ win = GraphWin("My first graphics!", 400, 600) colors =["#00FF00", "yellow", color_rgb(255,0,0)] for i in range(3): circle = Circle(Point (200,450-i*150),75) circle.setFill(colors [i]) circle.draw(win) win.getMouse() circle.undraw() win.getMouse() win. close() from graphics import ∗ win = GraphWin("My first graphics!", 400, 600) colors =["#00FF00", "yellow", colorrgb(255,0,0)] for i in range(3) : circle = Circle (Point(200,450−i∗150),75) circle.setFill(colors [i]) circle.draw(win) win.getMouse ( ) circle.undraw( ) win.getMouse ( ) win.close( ) The Graphics object Circle will give us a circle centered at a Point with the given radius. We can define the color of the circle by entering a string corresponding to a string with a hex color code, a predefined color (search X11 color names on web), or by defining the color using its RGB representation (you might find the Adobe color wheel at https://color.adobe.com/create/color-wheel/ helpful).
Creating buttons or other objects that users can interact with is a matter combining Graphics objects and the GraphWin mouse methods. We want to have the button trigger a particular action when a mouse click is detected within the boundary of the Graphics object and not trigger the action when a mouse click is not detected within the boundary of the Graphics object. Discussion: What does the code mean? from graphics import ∗ from graphics import Point as P win = GraphWin("My first button!", 400, 600) win.setBackground("orange") button_h =100 button_w =200 quit_button = Rectangle(P(200-button_w/2,300+button_h/2), P(200+button_w/2,310-button_h/2)) quit_button.setFill("purple1") quit_button.setWidth(10) quit_button.set0utline("purple3") quit_button.draw(win) quit_button_text = Text(quit_button.getCenter( ), "Click to Quit") quit_button_text.setSize( 30) quit_button_text.draw(win) button_click = False while not button_click : from graphics import ∗ from graphics import Point as P win = GraphWin("My first button!", 400, 600) win. setBackground("orange") button_h =100 button_w =200 quit_button = Rectangle (P(200-button_w/2,300+button_h/2), P(200 +button_w/2,310-button_h/2)) quit_button._setFill("purplē") quit_button.setWidth(10) quit_button.setOutline( "purple3") quit_button.draw(win) quit_button_text = Text(quit_button.getCenter(), "Click to Quit") quit_button_text.setSize(30) quit_button_text.draw(win) button_click = False while not button_click :
button_click = False while not button_click : click = win. checkMouse () if click == None : pass elif click.getX() > quit_button.getCenter( ).getX( ) - button_w/2 and click.getX ()< quit_button.getCenter () .getX ()+ button_w/2 and click.getY( ) > quit_button.getCenter().getY() - button_h/2 and click.getY( ) < quit_button.getCenter().getY( ) + button_h/2 : button_click = True win.close( )
Languages that are designed for programming graphical user interfaces, like JavaScript, typically have more built-in mouse events such as tracking the location of a mouse in a window at all times, registering a doubleclick, or registering a right-click. The graphics module has only the two GraphWin mouse methods that we have already seen so we are limited to animation controlled by clicks and timing. If we want to do more complicated animations, then we have to import an additional Python module. We are going to animate an Image object of a "mallet" to be used in a whack-a-mole game that you will complete for your next project. Discussion: What does the code mean? from graphics import ∗ from time import time win = GraphWin("My first animation!", 400, 600) counter_disp = Rectangle ( Point (180,50), Point (220,10)) counter_disp.draw(win) counter =0 counter_text =Text(Point(200,30),"{}". format (counter )) counter_text,setSize(30) counter_text.draw(win) mallet = Image ( Point (θ,θ), "smallhammer. png" ) mallet. showing = False mallet. length =0.5 mallet.start =0 mallet.end =0 while counter <5 : counter_text, setText ("\{\}", format (counter)) if mallet. showing == False:
while counter <5 : counter_text.setText ("\{\}". format (counter)) if mallet. showing == False: click = win. checkMouse () else: click = None if not click == None: mallet. x= click.getX( ) mallet. y= click.getY () mallet.move(mallet.x, mallet.y) mallet, draw(win) mallet, showing = True mallet.start =time() if mallet. showing == True: mallet.end = time( ) if mallet.end - mallet.start > mallet. length : mallet. undraw( ) mallet.move (-mallet. x,− mallet.y) mallet. showing = False counter +=1
Final Project: Whack-a-Mole! Complete the whack-a-mole game with the following features - Moles pop out of holes at random - A counter increases every time a mole is whacked - The game ends (the window can just close, but inserting some kind of a pause is probably a good idea) when the target number of moles has been whacked - Include instructions for the game so your user knows what the goal of the game is and how to play (assume the user experience is limited to the graphics window).
Simple Graphics Module As an introduction to programming with graphics, we will be using the simple graphics module graphics.PY developed by John M. Zelle to accompany his introductory textbook "Python Programming: An Introduction to Computer Science". The module and reference information are available at https://mcsp.wartburg.edu/zelle/python/ The graphics module will give us a number of objects we can use to create a graphical visual interface (GUI) for program development. These objects include GraphWin objects, the windows that we create as a "canvas" for our GUI, and Graphics objects that we can draw in the window and allow the user to interact with. GraphWin Objects To create a window for a program GUI, we must define a GraphWin object. This class of object allows us to enter a string that will appear on the title bar at the top of the window and define the dimensions (height and width in pixels) of the window. If only one argument (the string) is given, the default window size is 200×200. Discussion: What does the code mean? from graphics import ∗ win = GraphWin("My first window!", 400, 600) for i in range(5) : click = win.getMouse() print(click) win. close() from graphics import ∗ win = GraphWin("My first window!", 400, 600) for i in range(5) : click = win. getMouse () print(click) win.close ( ) Discussion: What does the code mean? from graphics import ∗