Anyone can make a video game

And that includes YOU!

Game-making may seem like a daunting task, but I super-duper-pinky-promise that you have what it takes. Games are a wonderful opportunity for creativity to run wild, to express yourself, and to connect with others, and today we’re gonna learn to make a simple platformer game about a hungry monkey together.

hungrymonkey.gif

Getting Started

If you’ve already got Python and PyGame installed on your computer, you can skip this section.

First, we’re gonna need to make sure Python is installed on your computer. To download it, follow this link: https://www.python.org/downloads/. After it’s downloaded, click on it and follow the instructions to get it fully installed.

Make sure to download the version of PyGame that is compatible with your OS (Windows, macOS, etc)! Then, in either your PowerShell/Command Prompt (for Windows) or Terminal (for macOS), type the following command to install PyGame (the library we’ll be using to make our game):

# For Windows
py -m pip install pygame --user

# For macOS:
python3 -m pip install pygame --user

Game Foundations

Every game needs three basic components:

  1. A window (to display the game on)
  2. A game loop (to run and update the game frame by frame)
  3. Event handling (to check for things like key strokes, mouse clicks, etc).

To implement these basic features, create a file in VS Code and name it hungry_monkey.py , or any name that ends in .py. Now simply copy and paste the following code into your VS Code. As we go through this workshop, if you’re every curious about how a handful of specific lines work, there are comment explaining what most little chunks are doing. However, you don’t need to read every comment or line of code by any means.

# Importing libraries that we'll be using
import pygame
import sys
import random

# Initializing pygame
pygame.init()

# These set us up to be able to use text and timers later
font = pygame.font.SysFont(None, 36)
clock = pygame.time.Clock()

# Defining the window we will display our game on (in terms of pixels)
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Hungry Monkey')

def game_loop():
	"""This function runs our main game loop, yippie!"""
	
	running = True
	while running == True:
	
		# Here is an instance of event handling, checking if the user wants to exit
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				running = False
				pygame.quit()
				sys.exit()
				
		# Rest of game loop goes here
		
game_loop()

Now let’s try running our code. If you are using VS Code, there should be a play button that’ll let you run the file. Otherwise, type python -m <your_file_name> into your terminal.

You should see a black box appear on your screen that’ll stay there until you hit the exit button. Hurray!

If you don’t see this, try entering the following code into your VS Code terminal:


#For Windows:
pip install pygame

#For MacOS
pip3 install pygame