mdinfotech.net  



Unit 4: Game Dev in Python

In class lesson on how to run Python hello world in VSCode. print() and input(), variables, if statements, boolean expressions.

Activity: Watch Python in 1 hour.

Programming Exercise (optional):

Create a program that asks the user to enter their name and age. Print a message addressed to them that tells them the year that they will turn 100 years old.

Resources

Intro in class to Random Numbers, Lists, Loops.

Activity: Finish watching Python in 1 hour.

Tutorials Complete the programming exercises below.

Programming Exercise 1:

Make a Rock-Paper-Scissors game against the computer.

Programming Exercise 2:

Write a program to remove duplicates from a list. Example: if my_list = [ 1, 2, 2, 3, 4, 4, 5, 1, 8, 3] then the result = [ 1, 2, 3, 4, 5, 8] .

100/80/60 Quiz on these topics at the end of class

In class lesson on functions (methods).

Activity: Watch Functions Explained.

Programming Exercise 3:

Write a function that calculates the sum of all numbers in a list. Write a test driver for this function.

In class lesson on dictionaries.

Watch this video: Python Dictionaries

Programming Exercise 4:

Write a program that asks the user to enter a sentence. Your program should count how many times each word appears and store the results in a dictionary. Then, print out each word followed by its count. Use the skeleton code below to get started. Read pass keyword in Python.

sentence = input("Enter a sentence: ")
words = sentence.split()

word_counts = {}

# TODO: Loop through the words and count how many times each appears
for word in words:
    # Fill in the logic to count each word
    pass

# TODO: Print each word and its count
for word in word_counts:
    pass                
                

Pygame is a free, open-source Python programming library designed specifically for creating 2D video games and multimedia applications. It allows beginner developers to easily manage graphical windows, render images, play sound effects, and handle user inputs from keyboards, mice, or joysticks. By shifting focus away from building a game engine from scratch, Pygame provides beginners and hobbyists with a straightforward platform for rapidly prototyping interactive projects.

Activities
  1. Install Pygame using VS Code. Note: when the video runs pip to install pygame, use this command instead: py -m pip install pygame.
  2. Watch Pygame in 10 minutes
Activity
  1. Program along with: Pygame Hello World
  2. Complete the 5 exercises at the end of the video.
  3. Get it marked by Ms. Wear
Resources

Below is a list of common errors students get during this unit, along with what it means and how to fix it.

  1. Anything in pygame is "not initialized": You did not initialize pygame. The fix: Call pygame.init() first in main.
  2. pygame anything does not exist: Meaning - you probably named your program "pygame.py" or "import pygame.py" and it is overriding the actual pygame library. The fix: Rename your file "hello.py" or something else.
  3. Your program was working, but now it says you have an error on line 1 and the console prompt looks like this: >>>: Meaning - Your console is in python command line instead of windows command line.The fix: Type exit() and hit enter.
  4. Something is not being drawn: The fix: First draw the background. Then blit/draw the images and text. Then call pygame.display.update() only once at the end of the game loop. Do not put update in an if or another loop. Never call it more than once.
  5. blah is not callable: Meaning - you are calling blah like it is a function, but it is not. ie. Suppose foo is a function that requires two parameters, a blah and a tuple, but you forgot the comma like this: foo(blah (x,y)). This will cause this error. The fix: use the comma to separate the parameters to foo: foo(blah, (x,y)).
  6. Tuples cannot be reassigned: Example
                        foo = (1, 'hello', 9.2)
                        foo1 = 'world' 
    . Meaning - Tuples are immutable, which means once they have been assigned, their values cannot be modified. The fix: Use a list instead.
                        foo =  1, 'hello', 9.2
                        foo1 = 'world' 

Complete the 4 video lessons on Hangman.

Tutorials
  1. Hangman 1
  2. Hangman 2
  3. Hangman 3
  4. Hangman 4
The Assignment: Themed Hangman Assignment
Choose one of the following projects as your Python Team Project:
  1. Tetris Team Project Description
  2. Pomodoro Timer Team Project