%pip install -q otter-grader
# 1. Download the file using gdown (which handles Google Drive links perfectly)
# We pass the file ID directly to it.
!gdown "1pj2DSQsRkv6yR9AkOm6nQS0ITCtUJPr7" -O assignment.zip
# 2. Unzip the file silently into the current Colab directory
!unzip -q -o assignment.zip
# 3. (Optional) Verify the tests folder is there
!ls -l tests/
# Initialize Otter
import otter
grader = otter.Notebook()
# Validation: Check Python version
import sys
SUPPORTED_VERSIONS = ["3.12", "3.13"]
current_version = f"{sys.version_info.major}.{sys.version_info.minor}"
if current_version not in SUPPORTED_VERSIONS:
raise EnvironmentError(
f"Python version mismatch!\n"
f"Supported: {', '.join(SUPPORTED_VERSIONS)}\n"
f"Found: {current_version}\n"
f"Please use Python 3.12 or 3.13 for this course."
)
print(f"Python version: {current_version}")
Flappy Sebastian¶
Goal: Today we are building a real, playable game using Python. You will set up the game's settings using variables, write two small functions, and then launch a Flappy Bird game starring Sebastian the Ibis right here in this notebook.
Everything you have learned so far — variables, types, print statements, and functions — is enough to make this happen. Let's go!
The Game Engine¶
The cell below contains the game engine. You do not need to understand this code — just run the cell so the game is loaded into memory. You will launch the game later after configuring your settings.
# --- GAME ENGINE (DO NOT EDIT) ---
# This cell defines the game. Run it, then move on to configure your settings.
from IPython.display import HTML
def build_game_html(player_name, bird_speed, pipe_gap, gravity):
"""Build the Flappy Sebastian game HTML. Students do not edit this."""
return f"""
<div style="display:flex;justify-content:center;">
<canvas id="flappyCanvas" width="400" height="600"
style="border:2px solid #005030; border-radius:8px; cursor:pointer;"
tabindex="1"></canvas>
</div>
<script>
(function() {{
const canvas = document.getElementById('flappyCanvas');
const ctx = canvas.getContext('2d');
const W = canvas.width, H = canvas.height;
// --- Configuration from Python ---
const PLAYER_NAME = "{player_name}";
const PIPE_SPEED = {bird_speed};
const GAP = {pipe_gap};
const GRAVITY = {gravity};
const FLAP = -8;
const BIRD_X = 80;
const PIPE_WIDTH = 52;
const GROUND_H = 60;
let bird, pipes, score, gameState;
function reset() {{
bird = {{ y: H / 2, vel: 0, size: 24 }};
pipes = [];
score = 0;
gameState = 'waiting';
}}
// --- Draw Sebastian the Ibis (pixel art) ---
function drawBird() {{
const x = BIRD_X, y = bird.y, s = 3;
// Body (orange)
ctx.fillStyle = '#F47321';
ctx.fillRect(x, y, s*8, s*6);
// Chest (white)
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(x + s*1, y + s*2, s*4, s*3);
// Wing (green)
ctx.fillStyle = '#005030';
ctx.fillRect(x - s*1, y + s*1, s*3, s*2);
// Eye (black)
ctx.fillStyle = '#000000';
ctx.fillRect(x + s*6, y + s*1, s*1, s*1);
// Beak (orange-red)
ctx.fillStyle = '#FF6600';
ctx.fillRect(x + s*8, y + s*2, s*3, s*2);
// Legs (orange)
ctx.fillStyle = '#F47321';
ctx.fillRect(x + s*2, y + s*6, s*1, s*2);
ctx.fillRect(x + s*5, y + s*6, s*1, s*2);
}}
// --- Draw background ---
function drawBg() {{
const grad = ctx.createLinearGradient(0, 0, 0, H - GROUND_H);
grad.addColorStop(0, '#87CEEB');
grad.addColorStop(1, '#E0F0FF');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, W, H - GROUND_H);
// Ground
ctx.fillStyle = '#005030';
ctx.fillRect(0, H - GROUND_H, W, GROUND_H);
ctx.fillStyle = '#006040';
ctx.fillRect(0, H - GROUND_H, W, 4);
}}
// --- Draw pipes ---
function drawPipes() {{
for (const p of pipes) {{
// Top pipe (orange)
ctx.fillStyle = '#F47321';
ctx.fillRect(p.x, 0, PIPE_WIDTH, p.topH);
ctx.fillStyle = '#D45A10';
ctx.fillRect(p.x, p.topH - 20, PIPE_WIDTH + 4, 20);
// Bottom pipe (green)
const botY = p.topH + GAP;
ctx.fillStyle = '#005030';
ctx.fillRect(p.x, botY, PIPE_WIDTH, H - botY - GROUND_H);
ctx.fillStyle = '#003820';
ctx.fillRect(p.x - 2, botY, PIPE_WIDTH + 4, 20);
}}
}}
// --- Draw score ---
function drawScore() {{
ctx.fillStyle = '#F47321';
ctx.font = 'bold 36px sans-serif';
ctx.textAlign = 'center';
ctx.fillText(score, W / 2, 50);
}}
// --- Draw start screen ---
function drawStartScreen() {{
ctx.fillStyle = 'rgba(0,0,0,0.3)';
ctx.fillRect(0, 0, W, H);
ctx.fillStyle = '#FFFFFF';
ctx.font = 'bold 28px sans-serif';
ctx.textAlign = 'center';
ctx.fillText('Flappy Sebastian', W/2, H/2 - 60);
ctx.font = '20px sans-serif';
ctx.fillText('Click or press Space to start!', W/2, H/2);
ctx.font = '16px sans-serif';
ctx.fillStyle = '#F47321';
ctx.fillText('Player: ' + PLAYER_NAME, W/2, H/2 + 40);
}}
// --- Draw game over ---
function drawGameOver() {{
ctx.fillStyle = 'rgba(0,0,0,0.5)';
ctx.fillRect(0, 0, W, H);
ctx.fillStyle = '#F47321';
ctx.font = 'bold 32px sans-serif';
ctx.textAlign = 'center';
ctx.fillText('Game Over, ' + PLAYER_NAME + '!', W/2, H/2 - 40);
ctx.fillStyle = '#FFFFFF';
ctx.font = '24px sans-serif';
ctx.fillText('Score: ' + score, W/2, H/2 + 10);
ctx.font = '18px sans-serif';
ctx.fillText('Click or press Space to restart', W/2, H/2 + 50);
}}
// --- Collision detection ---
function checkCollision() {{
const bTop = bird.y;
const bBot = bird.y + bird.size;
const bLeft = BIRD_X;
const bRight = BIRD_X + bird.size;
if (bBot >= H - GROUND_H || bTop <= 0) return true;
for (const p of pipes) {{
if (bRight > p.x && bLeft < p.x + PIPE_WIDTH) {{
if (bTop < p.topH || bBot > p.topH + GAP) return true;
}}
}}
return false;
}}
// --- Spawn pipe ---
function spawnPipe() {{
const minTop = 60;
const maxTop = H - GROUND_H - GAP - 60;
const topH = Math.floor(Math.random() * (maxTop - minTop)) + minTop;
pipes.push({{ x: W, topH: topH, scored: false }});
}}
// --- Game loop ---
let pipeTimer = 0;
function update() {{
if (gameState !== 'playing') return;
bird.vel += GRAVITY;
bird.y += bird.vel;
pipeTimer++;
if (pipeTimer > 90) {{
spawnPipe();
pipeTimer = 0;
}}
for (const p of pipes) {{
p.x -= PIPE_SPEED;
if (!p.scored && p.x + PIPE_WIDTH < BIRD_X) {{
p.scored = true;
score++;
}}
}}
while (pipes.length > 0 && pipes[0].x < -PIPE_WIDTH) {{
pipes.shift();
}}
if (checkCollision()) {{
gameState = 'dead';
}}
}}
function draw() {{
drawBg();
drawPipes();
drawBird();
drawScore();
if (gameState === 'waiting') drawStartScreen();
if (gameState === 'dead') drawGameOver();
}}
function loop() {{
update();
draw();
requestAnimationFrame(loop);
}}
// --- Input handling ---
function flap() {{
if (gameState === 'waiting') {{
gameState = 'playing';
bird.vel = FLAP;
pipeTimer = 0;
}} else if (gameState === 'playing') {{
bird.vel = FLAP;
}} else if (gameState === 'dead') {{
reset();
gameState = 'playing';
bird.vel = FLAP;
pipeTimer = 0;
}}
}}
canvas.addEventListener('click', flap);
canvas.addEventListener('keydown', function(e) {{
if (e.code === 'Space') {{ e.preventDefault(); flap(); }}
}});
// --- Start ---
reset();
canvas.focus();
loop();
}})();
</script>
"""
print("Game engine loaded! Move on to the next section to configure your settings.")
Configure Your Bird¶
Before you can play, you need to set up your game. Each variable below controls a different part of the game. Read the comments carefully, then set each variable to a value you want to try.
| Variable | What it controls | Type | Try values in this range |
|---|---|---|---|
player_name |
Your name on the game-over screen | string | Any name you want |
bird_speed |
How fast the pipes move toward you | integer | 2 to 5 |
pipe_gap |
The space between the top and bottom pipes (bigger = easier) | integer | 100 to 200 |
gravity |
How fast Sebastian falls (bigger = harder) | float | 0.3 to 0.8 |
# ✏️ YOUR ANSWER HERE
# Set your player name (a string)
player_name = ...
# Set how fast pipes scroll toward you (an integer, try 2-5)
bird_speed = ...
# Set the gap between pipes in pixels (an integer, try 100-200)
pipe_gap = ...
# Set how strong gravity pulls the bird down (a float, try 0.3-0.8)
gravity = ...
grader.check("q0_config")
Your First Game Function: Scoring¶
Every game needs a way to keep score. Your job is to write a function that calculates the player's final score.
The score is calculated by multiplying the number of pipes the player flew through by a difficulty level. For example, if you passed 5 pipes on difficulty 2, your score would be 10.
Complete the function below.
# ✏️ YOUR ANSWER HERE
def calculate_score(pipes_passed, difficulty):
# pipes_passed: an integer, how many pipes you flew through
# difficulty: an integer, the difficulty multiplier
# Return the score (pipes_passed times difficulty)
score = ...
...
grader.check("q1_calculate_score")
Your Second Game Function: Game Over Message¶
When the game ends, we want to show a message to the player. Your job is to write a function that creates this message.
The function should return a string in this exact format:
Game Over, Sebastian! You scored 10 points!
Where "Sebastian" is the player's name and "10" is their score. Use an f-string to build the message.
# ✏️ YOUR ANSWER HERE
def game_over_message(player_name, score):
# player_name: a string, the player's name
# score: an integer, the final score
# Return a string: "Game Over, [player_name]! You scored [score] points!"
message = ...
...
grader.check("q2_game_over_message")
Launch the Game!¶
You have configured your settings and written your game functions. Now it is time to play!
Run the cell below to launch Flappy Sebastian. Click on the game or press the spacebar to flap.
Controls:
- Click or Spacebar = Flap (make Sebastian fly up)
- Avoid the pipes and the ground!
If you want to change your settings, go back to the "Configure Your Bird" section, change your variables, re-run that cell, then re-run the cell below.
# Launch Flappy Sebastian!
# This cell uses your variables to build and display the game.
game_html = build_game_html(player_name, bird_speed, pipe_gap, gravity)
HTML(game_html)
Experiment!¶
Now that your game is running, try going back and changing your settings to see what happens. Here are some challenges:
- Hard mode: Set
gravity = 1.0andpipe_gap = 80— can you even score 1 point? - Easy mode: Set
gravity = 0.3andpipe_gap = 200— how high can you score? - Chaos mode: Set
bird_speed = 6— everything moves fast! - Personalize it: Change
player_nameto your name or your favorite nickname
After changing your variables, re-run the configuration cell and then re-run the launch cell to see your changes.
What You Just Did (in Python!)¶
You just built and played a real game. Here is how everything you did connects to Python:
| What You Did | Python Concept |
|---|---|
Set player_name = "Sebastian" |
Variable assignment, strings |
Set pipe_gap = 150 |
Variable assignment, integers |
Set gravity = 0.5 |
Variable assignment, floats |
Wrote calculate_score(...) |
Functions, parameters, return values |
Wrote game_over_message(...) |
f-strings, string formatting |
| Changed values and relaunched | Experimentation and iteration |
These are the same building blocks behind every Python program — from games to websites to data analysis to AI. The only difference is scale.
Submitting to Gradescope¶
This file is saved on your computer — you do not need to download it from Jupyter.
Find the file on your computer:
- Mac: Open Finder →
Documents/CSC115/lecture_notebooks/99_lecture_python_games/99_lecture_python_games_colab.ipynb - Windows: Open File Explorer →
Documents\CSC115\lecture_notebooks\99_lecture_python_games\99_lecture_python_games_colab.ipynb
- Mac: Open Finder →
Upload to Gradescope:
- Go to gradescope.com and open this assignment
- Click "Submit" and upload the
.ipynbfile - The filename must be exactly
99_lecture_python_games_colab.ipynb
Do NOT use File > Download in Jupyter — this can rename the file and cause submission errors.
You have completed the Flappy Sebastian notebook.
To double-check your work, the cell below will rerun all of the autograder tests.
grader.check_all()