ICS 32A Fall 2023
Exercise Set 7

Due date and time: Friday, December 1, 11:59pm


Getting started

First of all, be sure that you've read the Reinforcement Exercises page, which explains everything you'll need to know, generally, about how the reinforcement exercises will work this quarter. Make sure you read that page before you continue with this one.


Problem 1 (2 points)

We started learning recently about the basics of a library called PyGame. Like most libraries, PyGame solves a lot of problems for us, but requires us to adopt its point of view on how best to solve those problems. What we get when we use a library is the benefit of taking a lot of work off of our own plates; what we give up is for that work to be done the way we would have preferred it. That doesn't mean it's not often worth the trade, but it's wise to be cognizant that there's a tradeoff there.

Let's explore some of the design choices that PyGame made by answering a couple of questions about them.

  1. Why do we need to "flip" the display in order to see what we've drawn? Of course, the short answer is "So we can see what we drew!", but that's not really the interesting question. The interesting question is this: Why was PyGame designed so that we have to flip the display before we see what was drawn?
  2. What is the purpose of using PyGame's Clock class? We saw that we can call tick on a Clock object to, for example, establish a frame rate of 30 frames per second. But what is a practical benefit of doing that, when we could instead run at a much higher rate?

What to submit

Submit one PDF file named problem1.pdf, which contains your answers to these two questions.


Problem 2 (2 points)

One of the new techniques we used in the Spots example was what we might call model-view separation, which is to say that we separated the code that manipulated our program's model from the code that manipulated our program's view. This is a fairly common technique in programs that have user interfaces, not just in games written using PyGame, so it's worth stopping to make sure you understand why we did it. Let's explore that by considering some questions about it.

  1. What problem does a model focus on?
  2. What problem does a view focus on?
  3. In no more than a sentence or two each, what are two benefits of separating the code that manipulates the model from the code that manipulates the view?

What to submit

Submit one PDF file named problem2.pdf, which contains your answers to these three questions.


Problem 3 (4 points)

When we built the Spots example in lecture, we used two different coordinate systems to represent points in two-dimensional space: fractional coordinates and pixel coordinates. Fractional and pixel coordinates, though, aren't the only way to represent two-dimensional space, so let's explore another. Mathematics gives us a mechanism called polar coordinates, which represent a point in two dimensional space in terms of two values: a radius (i.e., the distance of that point from the origin) and an angle (i.e., in what direction we'd travel from the origin to get to that point). We can think of the angles as being measured in degrees, with 0° meaning that we'd travel directly to the right, 90° meaning that we'd travel directly upward, and so on. Lots more details on polar coordinates can be found at the Wikipedia link below, though you won't need too much of that detail for our purposes here.

Below is a Python script with an almost complete program written, but with one key piece missing.

When complete, it draws a collection of blips, which are yellow circles that travel around the display in a spiral pattern, beginning from the center. A new blip appears once per second, but each blip moves a little bit every 1/30th of a second. After you've completed this problem and your program has been running for a while, this is what it would look like.

The problem is that the program, as provided, draws all of the blips in the same place — in the top-left corner of the display — no matter how much time has passed. How you'll solve this problem is to rewrite the function _determine_blip_center, which has been provided with an incorrect implementation. Replace its implementation with a correct one — one that performs the appropriate coordination conversion to allow PyGame to know where to draw the blips.

We'll need to agree on one convention for what our polar coordinates mean. We'll say that a radius of 1 means the distance from the center of the display to the edge of the display, so that a point with a radius of 1 and an angle of 0° would be along the rightmost edge of the display (halfway up), a point with a radius of 1 and an angle of 90° would be along the topmost edge of the display (halfway across), and so on. The display is resizable, but this rule applies no matter what the size of the display is.

(Note, too, that this particular program doesn't separate model and view implementations into separate modules, and it has one monolithic run() function that does almost everything in one long loop, and that's fine; our goal here is to explore the one technique you're implementing, but this is hardly a paragon of software design otherwise.)

What to submit

Submit the one Python script named problem3.py that we provided, with your updated _determine_blip_center function, which will be enough to solve the problem.


Problem 4 (2 points)

Suppose that you wanted to use PyGame to build a graphical version of the server-based Connect Four game that you built in Project 2, so that you could play Connect Four against the same AI located on the same server, but using graphics and mouse support to spice up the experience. While that job is too big for us this week, it's worth thinking a little bit about how it's similar to — and how it's different from — the work we've been doing with PyGame thus far.

Suppose that clicking on a column with the mouse is how the user drops a tile into a particular column. At that point, you would need to update the current state of the board, but you would also need to communicate with the server. In no more than a couple of sentences, how does the need to communicate with the server make it more difficult to build a game that works properly?

What to submit

Submit one PDF file named problem4.pdf, which contains your answer to this question.


Deliverables

In Canvas, you'll find a separate submission area for each problem. Submit your solution to each problem into the appropriate submission area. Be sure that you're submitting the correct file into the correct area (i.e., submitting your Problem 1 solution to the area for Problem 1, and so on). Under no circumstances will we offer credit for files submitted in the incorrect area.

Submit each file as-is, without putting it into a Zip file or arranging it in any other way. If we asked for a PDF, for example, all we want is a PDF; no more, no less. If you submit something other than what we asked for (e.g., a text file when we asked for a PDF, even if its filename ends in .pdf), we will not be offering you any credit on the submission. There are no exceptions to this rule.

Of course, you should also be aware that you're responsible for submitting precisely the version of your work that you want graded. We won't regrade an exercise simply because you submitted the wrong version accidentally.

Can I submit after the deadline?

Unlike some of the projects in this course, the reinforcement exercises cannot be submitted after the deadline; there is no late policy for these. Each is worth only 3% of your grade, with the lowest score dropped — see the Reinforcement Exercises page for details — so it's not a disaster if you miss one of them along the way.

You're responsible for making a submission in order to receive credit, which means you'll want to be sure that you've remembered to submit your work and verify in Canvas that it's been received. A later claim of having forgotten to submit your work or having misremembered the due date will not be grounds for a resubmission under any circumstances.

What do I do if Canvas adjusts my filename?

Canvas will sometimes modify your filenames when you submit them (e.g., by adding a numbering scheme like -1 or a long sequence of hexadecimal digits to its name). In general, this is fine; as long as the file you submitted has the correct name prior to submission, we'll be able to obtain it with that same name, even if Canvas adjusts it.