ICS 32 Winter 2022
Exercise Set 7

Due date and time: Tuesday, March 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)

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 problem1.pdf, which contains your answer to this question.


Problem 2 (4 points)

In the Inputs and Sounds lecture, we saw that PyGame provides two different ways of reacting to keypresses on the keyboard:

It is suggested in that lecture that you only actually need the first of these techniques; while the second technique is convenient, the theory advanced in the lecture is that you could build it yourself using the first. Let's prove that theory by actually doing it!

Download the file problem2.py below, which contains the basic skeleton of a PyGame-based "game" that displays a string of text on its display. (It's a simple enough game that we won't bother separating the model from the view, because we're exploring a particular PyGame technique.) Try it and make sure you understand why it does what it does, as a starting point.

Now, modify the game so that it does something a little different: Instead of always printing the string 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ, it prints a string consisting of whichever of those characters (only digits or letters) are keys on the keyboard that are currently being held down (i.e., they've been pressed but not released). If more than one key is currently being held down, show all of them, sorted so that the digits appear before the letters, and so that digits are sorted numerically and the letters are sorted alphabetically. (So, for example, if the user is holding down the keys B, 1, 3, and ], we'd display 13B.) Don't worry about the effect of "modifier" keys, such as Shift or Ctrl. They're not relevant for this problem; if the key is down, it's down.

But there's a twist! You can't call any of the functions in the pygame.key module, but instead need to implement this by reacting only to input events that come from pygame.event.get(). (If we can do that, then we've proven that we only need the first of the two techniques, which is what we're actually setting out to do here.)

What to submit

Submit the one Python script named problem2.py that we provided, with your updates that comprise your solution to this problem.


Problem 3 (2 points)

We've now seen two different libraries for writing programs that have graphical user interfaces: PyGame and Tkinter. In exploring these two libraries, we've seen that there are substantial similarities between them, but also substantial differences; both the similarities and the differences are instructive.

  1. When we wrote Tkinter-based user interfaces and we needed to specify what would happen when meaningful input events arose, we tended to write the names of functions, but didn't follow them with parentheses and didn't pass arguments to them, which might have struck you as odd at first. What is it about Tkinter's underlying design that made this the correct choice? What would have happened if we had put in the parentheses and passed the arguments instead?
  2. When we wrote PyGame-based games and we needed to specify what would happen when meaningful input events arose, we instead wrote calls to functions (i.e., we included the parentheses and any applicable arguments). What's different about PyGame's design that made that the right choice, when it's not the right choice in Tkinter?

What to submit

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


Problem 4 (2 points)

Suppose that you decided to implement a graphical user interface in Tkinter's general style — buttons, menus, and so on — but you opted to build it using PyGame instead of Tkinter. If you already knew everything there was to know about PyGame, would you expect this be possible? (In other words, could someone who's already learned all of PyGame solve this problem, even if you're relatively new to it?)

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, 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, and we won't be able to offer any credit on exercises that you forgot to submit.

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, so it's not a disaster if you miss one of them along the way.