1 of 54

CompClub Internals Presents:

Intro to PyGame!

Aims:

Learn how to use PyGame

Create an amazing game!!

2 of 54

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

3 of 54

Basics of Pygame

1

4 of 54

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

5 of 54

What is Pygame ?

Pygame is a library, or a toolkit, that specializes in all things game development in Python.

Game Development

6 of 54

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.

7 of 54

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 :)

8 of 54

Setting up the environment

2

Hello, world! 🗣️🗣️🤖🤖🤖

9 of 54

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 :)

10 of 54

Set Up

  • Open the Terminal

-----------------------------------------------------------------------------------------

  • Install PyGame by typing:�pip3 install pygame

OR

pip install pygame

  • Type ./game
  • Type code .

11 of 54

What are we creating?

3

12 of 54

GAME DEMO!!

13 of 54

Coding games

4

Looking at the structure of common games

14 of 54

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?

15 of 54

That brings us to… �Classes!!!🤯🤯🤯🤓🤓

Class: a user defined object or data type.

  • Each class has its own defined characteristics: e.g. Mobs health, position
  • Each class has its own defined functions: e.g. moving

What is a class?

An example would be: Mob

16 of 54

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??

17 of 54

Children Classes!!!

Parent Class: a broad umbrella class

Children Class: a class which branches off of the parent class.

  • It contains more specific functions and characteristics to further differentiate between different instances of a class

E.g. Mob is the parent class, and Passive and Aggressive would be children classes

What is a class?

18 of 54

What do we have in our project? 👀👀👀

19 of 54

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:

20 of 54

Sprites

Customer

Player

Ingredients

Mood icons..

21 of 54

main.py

5

22 of 54

How does it work? (1/3)

What is a tick?:

A tick is how often any game refreshes its information.

  • In a game like minecraft, this is some small number like 50 milliseconds.
  • For our project, we will refresh our logic at most 60 times a second! (you can find the tick speed of that as a challenge exercise.

23 of 54

How does it work? (2/3)

main.py can be broken down into 2 parts: setup, and the main gameplay loop.

Setup:

  • Is focused on generating and spawning entities into our game world
  • And then adding them to our “entities” list so that they can be stored in a single place to accessed for later use. I.e updating player sprite when the player moves at every tick.

24 of 54

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.

25 of 54

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!

26 of 54

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:

27 of 54

vec2d.py (2/2)

get_distance()

This is a function we have made for you to make your life easier :D

28 of 54

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!!⚠️⚠️⚠️💢💢

29 of 54

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:

  • Customer
  • Ingredients

30 of 54

Exported constants

31 of 54

clarifications.py

1. You don’t need to implement any sprite updating this is done by us already

2.

Credits: This presentation template was created by Slidesgo, including icons by Flaticon, infographics & images by Freepik.

32 of 54

The customer class

6

33 of 54

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.

34 of 54

customer.py

This file contains all the logic for the the customer behaves.

How do we add a customer to the game world?

35 of 54

customer.py

How do we add a customer to the game world?

36 of 54

The player class

7

WASDWASDWASDWASDWASDWASD

37 of 54

Overview of player.py

What’s inside ?

  • Consists of utility functions and the Player class (and its associated methods)
  • class Player(entity)

Handles:

  • Movement of the player (this is given to you)
  • Interactions the player has

38 of 54

TASK

To implement these methods/functions:

  • get_entities_distance
  • get_nearest_entity
  • interact_nearest
  • draw
  • update

IT’S TIME TO COOK !!!! 🔥🔥🔥

39 of 54

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 :)

40 of 54

Function to Implement!

Let’s implement a function related to using the methods of a class�

  • get_entities_distance(entity1: Entity, entity2: Entity):

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

41 of 54

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 :^)

42 of 54

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.

43 of 54

Methods to Implement!

Let’s implement some methods related to using the attributes of a class !!�

  • interact_nearest(self, entities):
  • draw(self, screen: pygame.Surface):

These methods will belong to the Player class in player.py.

44 of 54

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

  • Pick it up (hint, the .food_retrieved attribute is useful)

If it is a Customer

  • What method did you write before that might be useful here ?
  • How is the .food_retrieved related to the Customer?

45 of 54

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.

46 of 54

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).

47 of 54

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

  • hint, what can the Player carry and when should it appear?

To get the ingredient sprite, INGREDIENTS[self.food_retrieved.name]

48 of 54

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(entity: Entity, entities: list[Entity]) -> Entity
  • update(self, state):

49 of 54

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.

50 of 54

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() )?

51 of 54

Wrapping up

8

YOU’VE MADE A LIL PYGAME YAY

52 of 54

Further learning

Some small ideas for other games you could do :

  • Bullet hell
  • Side scroller / platformer game
  • anything you can think of !!

If you want to learn more about pygame, a good start are the tutorials on the pygame wiki - https://www.pygame.org/wiki/tutorials

53 of 54

THANKS FOR COMING !!

54 of 54

FEEDBACK FORM + Discord

https://discord.gg/HJKshWf7