CompClub Internals Presents:
Intro to PyGame!
Aims:
Learn how to use PyGame
Create an amazing game!!
Runsheet
Time | Topic | Presenter |
10:00 - 10:30 | Introduction and setup of coding environment + terminal commands | |
10:30 - 11:30 | Introduction of PyGame + runthrough of what we’ll be making today + map setup | |
(TBC) | | |
| | |
12:30 | LUNCH | |
| | |
| | |
| | |
Basics of Pygame
1
What is Pygame ?
Pygame is a library, or a toolkit, that specializes in all things game development in Python.
Web Development
Apps
Game Development
AI
Data Science
Python
What is Pygame ?
Pygame is a library, or a toolkit, that specializes in all things game development in Python.
Game Development
What is Pygame ?
It helps you deal with all the complex stuff to do with games- like handling audio, visuals, graphics and inputs - by giving easy-to-use instructions.
What is Pygame ?
You can think of it like a chef’s kitchen, it has all the equipment and ingredients you need, so that you can focus entirely on the cooking the food - or in this case, creating your game!
To know all the things Pygame can do, we go to their docs - Pygame Front Page — pygame v2.6.0 documentation - it has all of the functionalities! However, it is a bit difficult to understand at times, so feel free to ask for help from any of the mentors :)
Setting up the environment
2
Hello, world! 🗣️🗣️🤖🤖🤖
Terminal commands
The terminal is another way you can interact with your computer using commands instead of clicking things. Some useful commands are:
cd - change directory, lets you move between directories (folders)
ls - ‘list’, lists files in your current directory
code - opens up Visual Studio Code, where we’ll be writing our code :)
Set Up
-----------------------------------------------------------------------------------------
OR
pip install pygame
What are we creating?
3
GAME DEMO!!
Coding games
4
Looking at the structure of common games
Minecraft’s structure
Player: Your player (and your friends if your playing multiplayer)
Mobs: Creeper, Zombie, Cow, Sheep, etc…
Blocks: Grass, Glowstone, Oak Wood block, etc…
They can be group into the basic of Mobs, Blocks or Player…
Each group (e.g. Mobs and Blocks) have very unique difference, but each item inside the groups has similarities, and difference that can be specified.
E.g. Cow and Creepers both have the ability to move independently, but a Creeper has the ability to inflict damage whilst a Cow cannot
Core Components
What do all of these have in common?
That brings us to… �Classes!!!🤯🤯🤯🤓🤓
Class: a user defined object or data type.
What is a class?
An example would be: Mob
Using Classes!!!
Instance: Using the class
When we create an instance of class, we are telling the program that this object now exists in the current space.
We have our class, what do we do with it??
Children Classes!!!
Parent Class: a broad umbrella class
Children Class: a class which branches off of the parent class.
E.g. Mob is the parent class, and Passive and Aggressive would be children classes
What is a class?
What do we have in our project? 👀👀👀
What do we have here? 👀
Let’s see what our directory looks like:
The directory has been set up where we have main.py which handles the runtime logic of the game.
While inside of src folder which contains all of the code which assist in other pieces of logic for game which we have extracted and moved to separate place.
Let’s gloss over these quickly… 🤔
Breaking down the working directory:
Sprites
Customer
Player
Ingredients
Mood icons..
main.py
5
How does it work? (1/3)
What is a tick?:
A tick is how often any game refreshes its information.
How does it work? (2/3)
main.py can be broken down into 2 parts: setup, and the main gameplay loop.
Setup:
How does it work? (3/3)
Main gameplay loop:
Inside the “main gameplay loop” is where the main runtime logic of the game and navigation menu is stored within a while loop.
Motive:
We want a while loop becauses it’s how we track if the game is still running. It continuously runs the game so that we can simulate time and events happening in real time.
entity.py 😶🌫️😶🌫️👽👽👽
Every subclass (eg customers, ingredients, the player) are all children of this class, which is the entity class. An entity is only really defined by two things - its hitbox and position!
vec2d.py (1/2)
This file contains logic written to make a custom type which represents a two 2d vector. You can do basic arithmetic on it like, adding, multiplying, subtracting.
Examples:
Creating a vec2d:
vec2d.py (2/2)
get_distance()
This is a function we have made for you to make your life easier :D
ingredient.py
The code inside of here is responsible for describing the type of ingredient it’s given by the user.
Do NOT confuse the name you give the ingredient as the data type
So how do we use it?
Warning!!⚠️⚠️⚠️💢💢
Setting up the game world!
At any given time, we can have 5 Customers max to serve simultaneously. The Customers have orders which need to be completed which we get from 6 baskets at the back of the kitchen which will spawn ingredients to be brought to our Customer.
Motive:
To make sure everything appears in the right place and that nothing spawns in the wrong locations. We want to make sure that we got our locations down exactly for where everything should appear at.
Spawn locations to keep track of for:
Exported constants
clarifications.py
1. You don’t need to implement any sprite updating this is done by us already
2.
The customer class
6
Overview of customer.py
what’s inside ?
In this file, we have all of our pre-written methods for our customers! Let’s look at how some of them are called.
customer.py
This file contains all the logic for the the customer behaves.
How do we add a customer to the game world?
customer.py
How do we add a customer to the game world?
The player class
7
WASDWASDWASDWASDWASDWASD
Overview of player.py
What’s inside ?
Handles:
TASK
To implement these methods/functions:
IT’S TIME TO COOK !!!! 🔥🔥🔥
Accessing methods of a class
All classes can have methods
Methods are functions / procedures built into the class that allow you to manipulate it or its data.
To access a class’ method, do className.methodName(inputs).
This will be relevant for the Player class :)
Function to Implement!
Let’s implement a function related to using the methods of a class�
This function already exists in the code for you, you just have to write the code that goes inside them ❗❗
From here on in, the file we are modifying and where these functions can be found is player.py, in src/entities/player.py
get_entities_distance()
get_entities_distance(entity1: Entity, entity2: Entity):
For this method, you want to return the distance between entity1 and entity2.
The methods .get_position() and .get_distance() will be helpful :^)
Attributes of a class
All classes can also have attributes
Attributes are like variables associated with each instance of a class.
For all instances of a class, their attributes are unique to that instance.
To access an attribute, do className.attribute
In this example, self refers to the class itself and is often used within the class itself to modify its own attributes.
Methods to Implement!
Let’s implement some methods related to using the attributes of a class !!�
These methods will belong to the Player class in player.py.
interact_nearest()
interact_nearest(self, entities):
For this method, you want the player to interact with the nearest entity to it, and perform certain behaviours depending on what the nearest entity is and its attributes.
Specifically:
If the nearest entity is an Ingredient
If it is a Customer
A quick detour - surface.blit
We have to draw to the screen somehow
The pygame method for the pygame Surface class .blit draws one image (Surface) onto another image (Surface).
We’ll be using this to draw the player sprite onto the game map and any ingredients it is holding.
Another quick detour - hitbox
why is your hitbox so small ?? that’s unfair 🐷
The Player class has another attribute - hitbox.
This hitbox represents the hitbox of the ingredient the Player carries, and sits at the top left of the player (self.hitbox.topleft).
For example, SurfaceClassName.blit(imageToDraw, positionOfImage).
draw()
draw(self, screen: pygame.Surface):
For this method, you want to draw the sprite of the player onto the game map (which is given to the method as screen).
You also want to draw any other associated sprites with the Player when certain conditions are met
To get the ingredient sprite, INGREDIENTS[self.food_retrieved.name]
Last things to implement
Two things left to implement, first a helper function (that doesn’t utilise attributes or methods in its code) and the other a method that utilises attributes
get_nearest_entity()
get_nearest_entity(entity: Entity, entities: list[Entity]) -> Entity:
From the list ‘entities’, find the closest one to ‘entity’.
Hint: you may find get_entities_distance() useful.
update()
update(self, state):
Move the character, and upon pressing the space key, interact with the nearest entity.
Hints:
If you’re stuck, look at how the move() method gets a list of keys pressed, then if keys[key_name]: to check the key.
What previous written method for Player might help with interaction? What input should you give it? (what variable haven’t you used that is given to update() )?
Wrapping up
8
YOU’VE MADE A LIL PYGAME YAY
Further learning
Some small ideas for other games you could do :
If you want to learn more about pygame, a good start are the tutorials on the pygame wiki - https://www.pygame.org/wiki/tutorials�
THANKS FOR COMING !!
FEEDBACK FORM + Discord
https://discord.gg/HJKshWf7