There are just two questions in this problem set. Question 1 is about working with coordinate positions in PGL, while Question 2 introduces you to mouse-driven events in PGL. Files are provided in the starting template linked before for all two questions. Make sure you fill out the top metadata on each problem please!
Accept Assignment
Problem 1
Complete the template given in Prob2.py
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 following image:
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.
Constant | Description |
---|---|
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 |
The tricky part of this problem is always in getting the position coordinates correct for your bricks. As a series of stepping stones, I might suggest:
- First writing code to generate a solid grid of bricks
- Adjusting one of your loops to get a triangle of bricks, but where they are still stacks on one side (not centered)
- Adjust the bricks to get them displaying appropriately centered on the previous row
- Adjust the initial coordinates to ensure that the entire pyramid is perfectly centered
Problem 2
The goal of this problem is to create a simple clicking-type game that might amuse a 4-year-old, or maybe a cat (or maybe a college-aged individual, I’m not judging!). The program will run by displaying a square on the screen. When the square is clicked, and only when the square is clicked, it will move to a different random part of the screen, and the process can then be repeated. You are welcome to use code from other libraries you might have written, but make sure to upload those libraries along with your code back to GitHub. You have been provided a starting template in Prob3.py
, and I’ll provide for you the following steps:
- Add a colored and filled square to the center of the window. You can choose the color of the square, but iti should have a width and height as determined by the constant
SQUARE_SIZE
. You will only be altering the properties of this object, not reassigning it, so you don’t need to add it as an attribute to theGWindow
. make sure your square is displaying centered in the screen when you run your program before continuing. - 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! - Now, erase the print function inside
on_mouse_down
and add code 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 (no square should even by sticking partly outside the window!). It can be fun to make the color of the square change to a new random color as well, but that is optional. 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 at our Python Summary to refresh your memory about some functions/methods that may be useful. - Lastly, every time that you click inside the square, you want to add a point to the score, and every time you click outside the square, you want to reset the score to 0. There are a few ways you could handle this. One would be to keep track of the score purely inside a
GLabel
, 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 theGWindow
, or else you won’t be able to globally set its value within the callback function. your label depicting the score should be placed in the bottom left corner of the window:SCORE_DX
from the left wall andSCORE_DY
up from the bottom.
When the game is finished, playing it should look something like the animation below! Here I made the background a light gray just so you could see the boundary of the window against the background.