Date

November 3, 2016

Activity 1: Login and Record Attendance

We will be taking roll in lab each week! Please come to your assigned lab to be counted present!

If you are in an Olsson lab, click "Lab Attendance" on the left-hand menu in Collab to register your attendance and keep up with your lab grade.
YOU MUST CLICK THE LARGE GREEN OR YELLOW BUTTON FOR YOUR ATTENDANCE TO COUNT!!

You must do this from a machine in Olsson 001 and not your laptop. If you have trouble, talk to your lab TA. Students in Lab 109 will do attendance via direction from the TA.

Activity 2: Pairing

You are about to start working on a new project! You can work with the same partner or find a new one! Once you have found a partner, ONE partner should fill out this form: Game Project Partner Registration - https://goo.gl/forms/mMdlkCOMQ1bkAIzo1

Activity 3: Starting Out

This week we are going to begin working with gamebox - a library that will aid you in creating your own video game. First you'll need to download gamebox.py and put it in your PyCharm project directory. You will be able to find gamebox and all of our example code at https://cs1110.cs.virginia.edu/code/gamebox. We may add more example code as we move through POTD 16 and the project.

You should also download and look over the gamebox API and introduction - https://cs1110.cs.virginia.edu/code/gamebox/gamebox.pdf

Activity 4: Installing PyGame

You should install PyGame on your laptop before you leave and we encourage you to do this first!

Windows: Download these two files to the same folder on your computer (such as your desktop). This assumes you are running Python 3.5.2 as you were instructed to install at the beginning of class. If this is not the case, a staff member may need to help you.

Open a command window. To do this in Windows 10, click on the Windows icon and type: cmd and hit enter.

You need to be in the same directory in the command window as where you downloaded the files. For example, if you downloaded these to your desktop, type cd Desktop and hit enter. Downloads is the other most common directory where these files may be.

Then copy and paste the following into the command window and run these commands:

python get-pip.py
python -m pip install wheel
python -m pip install pygame-1.9.2a0-cp35-none-win32.whl

Go into PyCharm and verify that it worked by copying https://cs1110.cs.virginia.edu/code/gamebox/gamebox.py and https://cs1110.cs.virginia.edu/code/gamebox/infinitejumper.py into a project and running infinitejumper.py.

Troubleshooting Notes:

  1. There is a chance that this will not work with your python installation if the python command is not recognized. If you get errors, try to change these commands by replacing the python part of the command with the full path to your python install: c:\Users\<your login name>\AppData\Local\Programs\Python\Python35-32\python.exe Note that the install location may be different than this!

  2. Another thing to try if python cannot be found normally is to use py as the command instead of python.

  3. Some browsers are downloading the .whl file as a .zip file. Rename the file first!

  4. If you downloaded 64 bit python instead of 32 bit python (you can verify this by running python on the command line and look at the version), then use this whl file instead http://cs1110.cs.virginia.edu/code/gamebox/pygame-1.9.2a0-cp35-none-win_amd64.whl

Mac: Download and install XQuartz - https://dl.bintray.com/xquartz/downloads/XQuartz-2.7.11.dmg. Then, open a Terminal window (Applications -> Utilities -> Terminal) and paste the following commands in one at a time and run them individually:

xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
alias brew=/usr/local/bin/brew
brew install mercurial 
brew install git
brew install sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi
pip3 install hg+http://bitbucket.org/pygame/pygame

Go into PyCharm and verify that it worked by copying https://cs1110.cs.virginia.edu/code/gamebox/gamebox.py and https://cs1110.cs.virginia.edu/code/gamebox/infinitejumper.py into a project and running infinitejumper.py.

Troubleshooting Notes:

  1. If you have trouble getting fonts / text to work, try opening a terminal window and running fc-list. Let that run to completion and try again.

PLEASE see a TA or professor if you need assistance getting this installed!

Activity 5: Simple Example

Start by creating a new game. Call it game_lab.py. Then, if you haven't already, download https://cs1110.cs.virginia.edu/code/gamebox/gamebox.py and put it in the same PyCharm project.

Every PyGame / gamebox game looks basically the same. Start with the template code below:

# blank game template

import pygame
import gamebox
camera = gamebox.Camera(800,600)

def tick(keys):
    # to check if a key is pressed do, e.g., if pygame.K_LEFT in keys
    # to check the mouse, use camera.mouse (for position) or camera.mouseclick (for buttons)

    # typically here you update the position of the characters
    # then you call camera.draw(box) for each GameBox you made

    # usually camera.display() should be the last line of the tick method
    camera.display()

ticks_per_second = 30

# keep this line the last one in your program
gamebox.timer_loop(ticks_per_second, tick)

Your code will mostly go inside the tick function. This function is called 30 times per second (as you can see from the code). You should typically leave it at 30. You can add other functions and variables outside of tick, but the main part of your game will go here.

Run this game. You should see a blank box.

Activity 6: Adding to the Game

Let's add a small, basic character to our game and give us control over it. Change your code so it looks like this now:

# our first game
import pygame
import gamebox

camera = gamebox.Camera(800,600)
character = gamebox.from_color(50, 50, "red", 30, 60)

def tick(keys):
    if pygame.K_RIGHT in keys:
        character.x += 5
    if pygame.K_LEFT in keys:
        character.x -= 5
    camera.clear("cyan")
    camera.draw(character)
    camera.display()
ticks_per_second = 30

# keep this line the last one in your program
gamebox.timer_loop(ticks_per_second, tick)

Give it a try! See what happens when you use the left and right keys!

Activity 7: Gravity

Let's add some code to make our character obey gravity.

First, after you create the character, give it a yspeed:

character.yspeed = 0

Then, inside tick, you can apply gravity to your character:

character.yspeed += 1
character.y = character.y + character.yspeed

Try out your code!

Activity 8: Making a floor

Well... that was interesting. Let's add a floor so our character doesn't fly off the screen.

ground = gamebox.from_color(-100, 600, "black", 3000, 100)

Then, inside tick but above camera.display() add:

camera.draw(ground)

And... that almost works. We need to handle the collision.

if character.bottom_touches(ground):
    character.yspeed = 0

if character.touches(ground):
    character.move_to_stop_overlapping(ground)

The first if statement makes it so your character stops falling. The second statement ensures it doesn't get stuck inside the floor! This can happen if the character is moving too fast when the collision occurs.

Activity 9: Your Turn! Time for POTD 16!

For the rest of lab, you should start working on your own POTD 16 (separate from your partner!). Take a look at the examples posted on the gamebox website for ideas on how to do some more basic things!

Submission

Each partner should submit one .py file named game_lab.py to the submission system at https://archimedes.cs.virginia.edu/cs1110/. Please put both partners' names and id's in two comments at the top of the file.

You must submit on time! Even if you don't finish, submit what you have.