Question 1 on this problem set is all about PGL objects and placement. Question 2 is about working with coordinate positions in PGL, while Question 3 introduces you to mouse-driven events in PGL. Files are provided in the starting template linked before for all three questions. Make sure you fill out the top metadata on each problem please!
Accept AssignmentProblem 1
While printing content or inputting content from the terminal is functional, often times you want to have more control over graphical elements in your program. To accomplish such tasks, we are using the PGL library in class this semester. To get you started in a very simple manner, in this problem you will just need to draw a pretty picture of whatever you might like. A few qualifications though to get full points:
-
draw_tree(gw, 100, 300)or as
draw_tree(100, 300)to draw a tree in the
gwwindow at the location x=100, y=300.
If you need a list of named colors in PGL, it is the same as the named CSS colors from web development, which you can find here. Or you can use something like a color picker to get the hexadecimal string for a color (which should start with a # symbol), and provide that string directly to the set_color method.
As a bit of an example, below is my creation, which uses purely lines, circles, rectangles, and a label:
Problem 2
Your task here is to complete the template given in Prob2.py to display a pyramid of rectangles centered 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. While it feels very similar to the console pyramid problem, the approach to solve it is actually quite different. As a series of possible 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 stacked 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 3
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 it 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_downfunction 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_downand 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 ever be 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 boundary. 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, getting 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 theGLabelfrom 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_DXfrom the left wall andSCORE_DYup 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.