Problem Set 4

Due: March 3, 2025

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

While printing content or inputing 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 using the template Prob1.py. A few qualifications though to get full points:

  • It must be a coherent picture. No purely abtract art comprised of random shapes.
  • You must use multiple colors.
  • You must use multiple types of GObjects (ovals, rectangles, lines, etc)
  • You must define at least one function responsible for grouping together several graphical primitives to form a compound object.

For instance, a function to draw a tree, where the function handles creating both the trunk and leaves. Or a function to draw a cloud by grouping together several circles. In order to add the graphical primitives to your window, you’ll need to pass the GWindow object into your function, as well as the desired location of the object. So, for instance, if I defined a function called draw_tree, it should be able to call it as: python draw_tree(gw, 100,300) to draw a tree in the gw window at the location x=100, y=300.

  • You must practice decomposition to group related parts of your image together, and include good documentation describing what part of the image different parts of your code are responsible for.

  • You must use a loop to draw some repeating portion of your image.

  • You must title your masterpiece at the top or bottom using a GLabel centered horizontally within the window. 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 Jed’s creation, which uses purely lines, circles, rectangles, and a label: Example Image

Problem 2

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:

A pyramid of bricks

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:

  1. First writing code to generate a solid grid of bricks
  2. Adjusting one of your loops to get a triangle of bricks, but where they are still stacks on one side (not centered)
  3. Adjust the bricks to get them displaying appropriately centered on the previous row
  4. 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:

  1. 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 the GWindow. make sure your square is displaying centered in the screen when you run your program before continuing.
  2. 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!
  3. 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.
  4. 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 the GLabel 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. your label depicting the score should be placed in the bottom left corner of the window: SCORE_DX from the left wall and SCORE_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.