Home / Expert Answers / Computer Science / simple-graphics-module-as-an-introduction-to-programming-with-graphics-we-will-be-using-the-simple-pa719

(Solved): Simple Graphics Module As an introduction to programming with graphics, we will be using the simple ...



Simple Graphics Module
As an introduction to programming with graphics, we will be using the simple graphics module graphics.Simple Graphics Module
As an introduction to programming with graphics, we will be using the simple graphics module graphics.As an introduction to programming with graphics, we will be using the simple graphics module graphics.py developed by John M.Discussion: What does the code mean?
from graphics import \( * \)
win = GraphWin(My second window!, 400, 600)
num_clicks \(The GraphWin methods getMouse ( ) and checkMouse () are two different ways to get input from the user (there are also correspGraphics objects.
Discussion: What does the code mean?
from graphics import \( * \)
win = GraphWin(My first graphics!, 400,Creating buttons or other objects that users can interact with is a matter combining Graphics objects and the GraphWin mousebutton_click \( = \) False
while not button_click :
click \( = \) win. checkMouse ()
if click \( == \) None :
pass
elif clickLanguages that are designed for programming graphical user interfaces, like JavaScript, typically have more built-in mouse evwhile counter \( <5 \) :
counter_text.setText (\{\}. format (counter))
if mallet. showing \( == \) False:
click \( = \) winFinal Project: Whack-a-Mole!
Complete the whack-a-mole game with the following features
- Moles pop out of holes at random
-Simple Graphics Module
As an introduction to programming with graphics, we will be using the simple graphics module graphics.
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 . 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 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 . 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 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 . 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 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 while num_clicks : click = win.checkMouse () \#print (click) if click == None : pass else : num_clicks print(click) win.close() from graphics import * win GraphWin("My second window!", 400, 600) num_clicks while num_clicks < 5 : click = win. checkMouse () \#print(click) if click None : pass else : num_clicks 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 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 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 , "yellow", color_rgb(255,0,0)] for i in range(3): circle = Circle(Point (200,450-i*150),75) circle.setFill(colors circle.draw(win) win.getMouse() circle.undraw() win.getMouse() win. close() from graphics import win = GraphWin("My first graphics!", 400, 600) colors , "yellow", for in range(3) : circle Circle 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 win = GraphWin("My first button!", 400, 600) win.setBackground("orange") button_h button_w 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 win = GraphWin("My first button!", 400, 600) win. setBackground("orange") button_h button_w quit_button = Rectangle -button_w/2,300+button_h/2), +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 , Point counter_disp.draw(win) counter counter_text . format (counter counter_text,setSize(30) counter_text.draw(win) mallet Image Point , "smallhammer. png" mallet. showing False mallet. length mallet.start mallet.end while counter : counter_text, setText ("\{\}", format (counter)) if mallet. showing False: while counter : counter_text.setText ("\{\}". format (counter)) if mallet. showing False: click win. checkMouse () else: click = None if not click == None: mallet. click.getX( mallet. click.getY () mallet.move(mallet.x, mallet.y) mallet, draw(win) mallet, showing True mallet.start if mallet. showing True: mallet.end time( if mallet.end - mallet.start > mallet. length : mallet. undraw( ) mallet.move (-mallet. mallet.y) mallet. showing False counter 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 . Discussion: What does the code mean? from graphics import win = GraphWin("My first window!", 400, 600) for in range(5) : click = win.getMouse() print(click) win. close() from graphics import win = GraphWin("My first window!", 400, 600) for in range(5) : click = win. getMouse () print(click) win.close ( ) Discussion: What does the code mean? from graphics import


We have an Answer from Expert

View Expert Answer

Expert Answer





The code you provided is written in Python and use...
We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe