# Game Development:Intro to Computer Science

## CSC 105Union CollegeFall 2009

Exercises about dictionaries and to practice for the final are on Codelab.

Helpdesk in Olin 102, Sun-Thu 7-9pm.

### Take Home Exam 1 - due Wednesday, 9/30 before class

Note: This is a take home exam. That means that (differently from the homework assignments) you have to work on this on your own. Do not talk to class members or any one else about the questions or your solutions. Do not use any resources other than a) the text book (Gaddis, Starting out with Python), b) the online text book Think Python, c) the python documentation, and d) the pygame documentation.

#### 1) Chameleon ball

Objectives: understand the game loop, practice the use of if-statements

Download chameleon_ball_v0.py. Run it, and study the code to make sure that you understand how it works.

This program lets a navy ball bounce around in front of a light blue and light purple background. Change the code so that the ball changes colors every time it (or its center) crosses over from one background color into the other. That is, if the center of the ball is in the light blue area, the ball should be drawn in navy; if the center of the ball is in the light purple area, the ball should be drawn in dark magenta. Here is a video that shows what the result should look like.

##### Part 1: algorithm description

Explain in English what you are going to do to make this happen. Point out where in the program you need to make changes; and describe in English what these changes will do. (Consult the criteria for writing algorithm descriptions.)

#### 2) Reacting to Events

Objectives: reinforce understanding of how we make games that react to user actions

Study the game that reacts to mouse clicks into the ball by playing a pop-sound. That's the program you wrote in class last Thursday. Here is my version of this program: pop_the_ball.py. (In case you need the wav file for the popping sound: pop.wav.

Explain in English what changes would be necessary to make the program play the popping sound if the user clicks into the ball and a new "awh" sound if the user clicks outside of the ball.

#### 3) Steering a Character

Objectives: practice using for loops, deepen your understanding of working with events, learn about another type of player input

Download steering_v0.py. Run it, and study the code to make sure that you understand how it works. All it does is draw a stationary blue circle; but notice that we still have variables for the x-speed and the y-speed - they just happen to be set to 0.

The goal for this problem is to make the circle move using the keyboard. More specifically, the circle should move up as long as the "w" key is pressed, down as long as the "s" key is pressed, right as long as the "d" key is pressed, and left as long as the "a" key is pressed. That means: if the user presses "w", the y-speed of the ball should be set to something smaller than 0 (to make the ball move upward). When the user releases the "w" key, the y-speed should be set to 0, so that the ball stops moving upward. And similar things should happen for the other keys.

We will tackle this problem in several steps:

1. make sure that we can detect key presses and releases
2. make sure that if a key was pressed, we can determine correctly which key was pressed
3. "translate" the key presses and releases into movement of the circle; that is, set the x-speed and y-speed to appropriate values

Remeber what is going on in this part of the code:

```for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
```
The function pygame.event.get() returns a list of all events that happened (since the last time we checked). The for-loops goes through that list and looks at each event individually. At the moment, we ignore all events except for events of type pygame.QUIT.

To be able to control the circle using the keyboard, we also need to do something for events of type `pygame.KEYDOWN` (i.e., if the user pressed a key down) and for events of type `pygame.KEYUP` (i.e., if the user released a key).

##### Step 1: detecting key presses and releases

Add code so that each time an event of type `pygame.KEYDOWN` happens, a message gets printed. And do the same for events of type `pygame.KEYUP`. Try it out. Each time you press a key down, something should get printed and each time you release a key, something else should get printed.

##### Step 2: determining which key was pressed/released

Each event that is of either type `pygame.KEYDOWN` or `pygame.KEYUP` is associated with a name for the specific key that was pressed. Let's say that the variable event refers to an event of those types, then you can retrieve the name for the key using the following function

```pygame.key.name(event.key)
```
This returns a string. For example, if the key that was pressed or released was the "w" key, this function will return the string "w". So, you can use the line
```key = pygame.key.name(event.key)
```
to associate the variable key with a string specifying which key was pressed or released.

For both the `KEYDOWN` and the `KEYUP` events, add some code that checks which key was pressed/released and prints out a message indicating which key was pressed/released.

##### Step 3: making the ball move

If key "w" gets pressed we want to have the ball moving up - that means, we want the y-speed of the ball to become something negative. If the "w" key gets released again, we want the ball to stop again - that means, the y-speed should become 0 again.

Add code that changes the y-speed as described in the previous paragraph, whenever the "w" key gets pressed/released.

Try it out to make sure it works, then add code to deal with the other three keys "a", "s", "d".

#### 4) For extra credit: steer a bug

Take the program from the previous problem and change it such that instead of moving a simple circle around, we are moving around a bug made from several shapes. Here is a video showing what I have in mind.

Keep your code as concise as possible. In particular, you should not need to make any changes to code inside the event loop (the for loop going through all events and reacting to them).

#### Submission

Submit everything on Blackboard.

The material that you submit should consist of at least 4 parts:

1. the algorithm description for "Chameleon Ball
2. the Python code for "Chameleon Ball
3. the algorithm description for Reacting to Events
4. the Python code for "Steering a Character"
and should follow the guidlines described here.