Github Classroom Link: https://classroom.github.com/a/Bb83mHVX
This is a problem set of 3 problems, all involving graphics operations.
draw_image
draw_pyramid
clicky_box
draw_image
Write a function that, when run, uses PGL to draw something you find beautiful, or at least as close as you can manage on short notice. You must use at least one each of:
GRect
GOval
GLine
GLabel
How I solved it.
def draw_image():
# Creating the window
gw = GWindow(WIDTH, HEIGHT)
w = WIDTH # just to save typing
#red box
r = GRect(w//2,w)
r.set_filled(True)
r.set_fill_color("red")
gw.add(r)
# green circle
o = GOval(w//2,w//2)
o.set_filled(True)
o.set_fill_color("green")
gw.add(o)
# line for left side middle to rightside middle
gw.add(GLine(0,w//2,w,w//2))
# that's half height, half width - the middle.
gw.add(GLabel("hello world", w//2, w//2))
draw_image
Write a function that, when run, uses PGL to draw something you find beautiful, or at least as close as you can manage on short notice. You must use at least one each of:
GRect
GOval
GLine
GLabel
How I solved it.
draw_pyramid
Write a function that, when run, uses PGL to display a pyramid of rectangles on the graphics window.
The pyramid consists of bricks arranged in horizontal rows, arranged so that the number of bricks decreases by one as you move upward, as shown in the demo. The pyramid should be centered perfectly in the window both horizontally and vertically. You should be able to change the following constants in your program and have it react appropriately by drawing the altered pyramid centered still within the window.
BRICK_WIDTH
The width of each brick
BRICK_HEIGHT
The height of each brick
BRICKS_IN_BASE
The number of bricks in the base (bottom) row
How I solved it. I wrote an "n_bricks" function that made a horizontal row of bricks and then used a similar loop to the '*' pyramid, but multipled "height" and "width" rather than spaces.
WIDTH = 400 # for the demo, must be changeable in your code
HEIGHT = 400 # for the demo, must be changeable in your code
BRICK_WIDTH =
BRICK_HEIGHT =
BRICKS_IN_BASE =
clicky_box
The function clicky_box
will
Firstly, add a colored and filled square of size SQUARE_SIZE
to the center of the window. You can choose the color of the square. Ensure your square is displaying centered in the screen when you run your program before continuing.
Optionally, add a listener to your window which will listen for when the user presses down the mouse button, and calls the on_mouse_down
function when that occurs. Run your program and ensure that, now, when you click the mouse anywhere in the window, a message prints to the terminal saying as much!
Next, add/update on_mouse_down
function so that if (and only if) you click inside the colored square, the square moves to a new random position that causes it to be entirely within the window bounds, that is, fully visible within the graphic window. Make sure that nothing happens if you click outside the square: it should only move if you clicked within the square boundry.
There are several ways you can check to see if the mouse was clicked within the confines of the square, some easier and some harder. You may want to look here to refresh your memory about some functions/methods that may be useful.
Lastly maintain a "score" SCORE_DX
from the left wall and SCORE_DY
up from the bottom. Every time that you click inside the square, you want to add one to the score, and every time you click outside the square, set the score to 0.
There are a few ways you could handle this. One would be to keep track of the score purely inside aGLabel
, getthing and setting the text of the label as necessary. Another would be to create a variable which you would increment or reset as needed, and then update theGLabel
from the variable. Just be aware that if you go the variable route, you will need to add that variable as an attribute to the GWindow, or else you won’t be able to globally set its value within the callback function.
clicky_box
Here is demo. The label is a bit goofy looking due to web-embedding.
This code snippet defines how to calculate a random integer between 0 and SOME_VAL
import random # at top of file, by other imports
def get_r():
return random.randint(0, SOME_VAL)
This code snippet defines how to configure a graphics window to perform a function after a click. Without this, 'on_mouse_down' will never run.
gw.add_event_listener("click", on_mouse_down)