Skip to content

An interactive, simple tutorial to make a game with Python using pygame

Notifications You must be signed in to change notification settings

CSE-Club-ISU/pygame-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 

Repository files navigation

pygame-workshop

An interactive, simple tutorial to make a game with Python using pygame

Table of Contents

Environment Setup

Install Python

Setup your development environment

  1. Open a new project in your IDE (like VSCode)

  2. Open a terminal in your project directory

    A reminder that the following are all terminals

    • Command Prompt or Git Bash for Windows
    • Terminal for MacOS
    • The terminal (ctrl+shift+~ on VSCode) built into your IDE

    Also, some basic terminal commands for navigation. <thing> notates an argument named thing which is passed to the command

    • cd <directory> to Change Directory or "move" around in your file system. e.g. cd / or cd /Users/alex or cd \Users\alex
    • ls, which stsands for list, will list everything* in the directory you are currently in
      • *not true without certain a flag, but don't worry about that
    • pwd, which stands for Print Working Directory, will print your working directory to the terminal

    Hopefully these will get you where you need to go 👍

  3. Setup a virtual environment

    IMPORTANT NOTE: Your IDE (PyCharm, Intellij) may already do this for you. If, when creating a project via a 'new project' or similar button, you see any inputs about creating a new 'Virtual environment' or 'venv', skip this step.

    1. In your terminal, while in the project directroy, run python -m venv venv.

      • This will create a new folder called venv in your project directory
      • This folder will contain the python packages, like pygame, you install for the project
      • If you don't use a virtual environment (venv), you will end up installing everything globally and potentially run into conflicts and other issues

      If you get an error, try running python3 -m venv venv

    2. Then, activate the virtual environment by running the activation script

      • MacOS: source venv/bin/activate
      • Windows: venv\Scripts\activate

      If you get an error regarding running the script, try running the command (in the same directory) in Command Prompt.

  4. Install pygame by running pip install pygame

    • pip is a package manager for python; use it to install packages you need for your project
  5. Create the python file for the game!

Basic Pygame Setup

Copy and paste the following code into your main .py file to get started. There are comments which explain the jist of the code.

# imports
import sys

import pygame

# initialize pygame
pygame.init()

# variables for the window size
WIDTH, HEIGHT = 800, 600
# init a pygame window. the set_mode function takes in a tuple (similar to a list, but immutable) for the window size
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# initialize the clock
clock = pygame.time.Clock()

# game loop. when you run this python file, the program will spend most of its time in this loop
run = True
while  run:
    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            # if the user closes the window, exit the game loop
            pygame.quit()
            sys.exit()

    # game logic will go here!

    # draw logic will go here!
    ## clear the screen every frame before re-drawing
    screen.fill((0, 0, 0))
    ## custom drawing logic goes here
    ## ...
    ## update the display with the newly drawn frame
    pygame.display.update()

    # update the clock. `clock.tick()` takes one argument, framerate
    # this needs to be called once per frame
    # if you are curious to how this works, look up pygame.time.Clock documentation,
    # then look for the tick() function
    clock.tick(60)

Useful Pygame Functions

Drawing

# draw a rectangle
# PARAMS:
# screen is the window
# color is the color of the rectangle. simplest is to pass a tuple of RGB values, e.g. (255, 0, 0)
# rect is the rectangle, a tuple of (x, y, width, height)
    # x is left to right position of top left corner of the rectangle
    # y is top to bottom position of top left corner of the rectangle
    # width is the width of the rectangle (left to right)
    # height is the height of the rectangle (top to bottom)
pygame.draw.rect(screen, color, rect)

# draw a circle
# PARAMS:
# center is a point (a tuple of (x, y) coordinates)
pygame.draw.circle(screen, color, center, radius)

# draw a line
# PARAMS:
# start and end are points
pygame.draw.line(screen, color, start, end)

Input Events

# get the current state of the keys
keys = pygame.key.get_pressed()
# if 'a' key is pressed
if keys[pygame.K_a]:
    # do something

The official pygame documentation can be found here, including basic examples.

Your IDE (more specifically, the language server which your IDE manages) can also be very helpful when navigating external libraries.

Don't forget the Python Cheatsheet and Learn X in Y Minutes (Python Edition) for Python things too.

About

An interactive, simple tutorial to make a game with Python using pygame

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages