CSC 111: Intro to Computer Science through Programming

Homework 7

Due: Tuesday, Apr. 4 at 11:59pm on Moodle

The goal of this homework is to practice while loops in the context of animated graphics. This homework builds on the idea of "falling snow" we discussed in class. We will also integrate other important concepts including return, working with objects, and nested control structures. For this assignment, you are welcome to discuss your work with other students, but all code you submit should be your own original work. It should be produced and understood by you, without looking at anyone else's assignment.

Quiz 7

Please take the short Week 7 quiz on Moodle (also due by Tuesday night). This is to help me understand what areas need work, and also help you understand what concepts might need extra practice. It is not a large part of the overall homework grade. Just for the quiz part, do not discuss the questions with other students, TAs, etc. (Note: quiz usually posted Wednesday afternoon.)

Animated Graphics Game

First I'll describe the high-level goal of this homework, then get into the specific steps and details. The idea is to create an animated game where some type of object is moving randomly on the window, and the user is trying to click on it. If the user successfully clicks on the object, the object disappears and something else is left in its place. If the user does not click on the object by the time the object gets to the edge of the window, the object stays and a new randomly placed object appears.

In the example below, the objects are "shooting stars". If the user clicks on one (in the very center), then it disappears and leaves a "glowing ring". There are 30 stars total, and some of them make it to the edge. (You can see that I am not very good at this game, even though I created it.) In your game, you can choose the objects and the details of the game play.

The following steps will take you through the process of creating your own game.

Step by Step

  1. The first step is to create a helper function that will create and return a graphics shape, but doesn't draw it on the window. Since we'll be moving the shape later on, it should be a single graphics object (not made up of several shapes). It must be a Polygon instance with at least 4 sides, but other than that it is completely up to you. Feel free to modify the fill color, outline color, and outline width.

    The arguments should be two integers which specify the center of the Polygon. All the other points of the Polygon will be based on this center. Finally, document your function using a docstring (see Day 21). Here is an example - note that I didn't call it draw_star(..) because it doesn't draw the shape.

    helper
    .
    .
    .
    helper

  2. Now in main, set up a graphics window and call your helper function. After assigning the return value to a variable, draw your shape on the window. Now change the arguments to randomly chosen integers within the width / height of the window, so that the shape appears at a random position (any window size is fine).

  3. Next we will make the shape move in a random direction. Choose a "dx" and a "dy" to specify the random direction (any direction should be possible). Then using a for-loop, make the shape move in that direction for a few steps.

  4. When you are successfully able to move the shape in a random direction, change your for-loop to a while loop that will stop the shape when it is close to the edge (it doesn't have to be too precise, but the shape should be visible after it stops moving). For this we'll need to be able to obtain the updated center of the shape. To do this, we can used the method getPoints(), which returns a list of the points of the Polygon. From the first point in this list (or any convenient point), obtain the x and y values close to the center of the shape. Use these values to modify the condition in your while loop.

  5. Put all the code to create and move the shape inside a for-loop, which will iteratively make a fixed number of shapes (here I used 30, but feel free to use more/less). Make sure to keep testing your code to see everything working properly.

  6. Now we are ready for the interactive part. Instead of using win.getMouse(), which will wait until the mouse is clicked, now we'll use win.checkMouse(), which will return a Point but not pause the program. Inside your while loop, check if the user's click is close to the center of the shape. If it is, used the undraw() method to remove the shape. In it's place, put another shape of your choice.

  7. There is one caveat to the step above, which is that if the mouse wasn't clicked, there will not be a Point to return (the return type will be None). So when you're checking to see if the click was close, first check that the mouse click Point is not equal to None.

Extensions

Optional variations:

  1. Create your own version of Flappy Birds, where the motion of the object (bird) is downward, but clicks can move it upward to avoid some other fixed objects such as pipes.

  2. Create your own version of Fruit Ninja, where some objects are thrown up (and come down via gravity), and the user has to click ("slice") them.

If you do a variation, still submit the version described in the main assignment. Then submit your variation to me over email (if it's possible to do a video of the game play, that would be ideal!)

Submit

You do not have to submit a transcript for this homework. Instead, take a screenshot of the game after it has been played (like the last frame of the video above) and submit that with your code. Make sure that you've thought about variable names and code structure, and that your code is well-commented. Include a docstring for each helper function describing the parameters, return type, and what the function does. It's okay to have a few hard-coded numbers in this assignment, but if you find yourself using the same ones over and over again, create a variable.

TO SUBMIT ON MOODLE:

  • hw7.py

  • game.png