1 of 47

Schulich Ignite Flare

Session 7

2 of 47

Session overview

  • Intuitive understanding of machine learning
  • Game agents
  • Large language models
  • Game development time :D

3 of 47

Machine

Learning

4 of 47

What is AI?

  • When computers can perform tasks often thought of as requiring human intelligence
    • Examples
      • bot behaviour in games (hard-coded)
      • pathfinding (hard-coded)
      • computer vision (machine learning)
  • Today we’re interested in the machine learning component of AI

5 of 47

What is machine learning?

Input

Output

Rules to return an output for each given input

Input

Output

Machine learning

Traditional coding

Rules to return an output for each given input

Model

6 of 47

When to use machine learning

  • Don’t use
    • For anything that can be determined with a consistent rules
    • Examples
      • Doing arithmetic
      • Simple bot behaviours like following a player (use a pathfinding algorithm)
  • Use
    • For anything where it is hard to manually come up with a strict set of rules
    • Examples
      • Image classification
      • Writing a story
      • Detecting disease from medical data

7 of 47

Deep Learning

  • A form of machine learning used in many of the more impressive applications of AI
  • For example
      • Large language models
      • Image recognition
      • Self-driving cars
      • Generative AI
  • Primarily used in applications where inputs and outputs have complex relationships, and there are often many more possible inputs and outputs

8 of 47

Reinforced Learning - The baby analogy

  • A system that tries to mimic the way a human brain works to solve problems
  • Think of a neural network as a toddler who learns as they are repeatedly shown what to do (output) in a given situation (input)
  • When they do well they get treats, when they don’t they get lemons as well as showing them how they can adjust their actions to get better.

9 of 47

Neural Networks - The Real Deal

  • A complex mathematical function
  • For example, here’s a graph representing whether or not students were accepted at a given university. Blue = accepted, red = rejected

grades

bribes

10 of 47

Neural Networks - How does it learn?

  • Error function measures how off the model’s output is from the expected output. We want to tune the weights of our model to minimize it

  • We look at how much each weight contributes to the error
    • For example
      • If we incorrectly assumed that bribes decrease one’s chance of getting into our example university, it’d result in a lot of false predictions.
      • We can use math to trace the errors back to this negative weighting on bribes

11 of 47

Neural Networks - Layers

  • Each layer is responsible for figuring out some level of detail
    • For example, in image recognition
      • Earlier layers detect things like edges
      • Then shapes…
      • Then parts of a whole, like noses, eyes, mouth
      • Then the final object

12 of 47

13 of 47

Game Agents

14 of 47

Supervised Learning

  • Train network to predict what a pro player would do in a given game state

15 of 47

What if we don’t have data?

  • Otherwise teach network to evaluate a position, look at possible actions and select the move that leads to the best position

  • But since training a network to return best moves is faster than searching and evaluating every possible position (think in terms of how many times a network is queried), we use the network’s games as training data.

16 of 47

Large Language Models

17 of 47

How Large Language models work

  • Input: A piece of text
  • Output: Predicting the next word
  • After learning to predict the next word in a text, we fine tune the model by giving it examples of how to respond helpfully
  • To make the model even smarter, we can let it “think” using language like a human would. The process of thinking can be fine tuned with feedback on how to “think” more effectively.

18 of 47

Inside a large language model

  • Embedding words into vectors which encode their meanings
  • Attention blocks in a language model use the context around a word to enrich its meaning
  • Multilayer perceptron blocks store facts and add them to word to enrich their meaning

19 of 47

Questions?

20 of 47

Bonus Topic #1

Enemies

21 of 47

Set up your enemy sprite

Setting up a sprite for our enemy is the same format as the sprite we had made two classes ago. Image here

import os

import pygame

class Enemy(pygame.sprite.Sprite):

def __init__(self):

super().__init__()

image_location = os.path.join("assets", "enemy.png")

self.image = pygame.image.load(image_location).convert_alpha()

self.rect = self.image.get_rect()

# Set the x and y values of the image

self.rect.x = 300

self.rect.y = 300

22 of 47

Implementing enemy sprite

# Create an enemy from our Enemy class

enemy = Enemy()

# Create a Sprite group

enemies = pygame.sprite.Group()

# Add enemy to Sprite group

enemies.add(enemy)

while True:

"""

UPDATE Section

"""

enemies.update()

"""

DRAW Section

"""

enemies.draw(screen)

23 of 47

Making your enemy walk

# Inside of Enemy class

def update(self):

# Move the enemy to the right

self.rect.x += self.x_speed

24 of 47

What’s wrong with the walk?

What is the gif doing that most enemies don’t?

(Hint: What happens when we hit the end of the platform?)

# Inside of Enemy class

def update(self):

# Move the enemy to the right

self.rect.x += self.x_speed

How do we make the enemy walk back to the left?

25 of 47

Pygame image transformations

Pygame has useful ways of editing your images

self.image = pygame.transform.scale(self.image, (75, 100))

self.image = pygame.transform.flip(self.image, True, False)

75

100

Flip along x-axis, y-axis?

With scaling

With flipping

26 of 47

Using Pygame image transformations

# example_player.py

def update(self):

mouse_buttons = pygame.mouse.get_pressed()

if mouse_buttons[0]: # left button pressed

self.image = pygame.transform.flip(self.image, True, False)

27 of 47

Exercise #2 - Walking to my own beat

Your turn!

Make your enemy walk left and right every 2 seconds.

Hint: Use pygame.time.get_ticks()

Bonus: Moonwalking is cool, but try to make him turn around!

28 of 47

Enemy collisions

  • What if enemy hits player from the side?
    • We want to kill the player!
  • What if player jumps on top of enemy?
    • We want to kill the enemy!

When a collision happens, we know:

  • Who collided
  • Where they are (during the collision)

That’s it! We’ll need some creativity to decide what happens

29 of 47

From the side - Getting squashed

Let’s start with when an enemy kills the player.

We want to make it so that when the enemy collides with the player, the player will be “killed”.

# Inside the update section

if pygame.sprite.collide_rect(player, enemy)

player.kill() # Kill the player

30 of 47

From the top - Squash ‘em

What if player jumps on top of enemy?

  • We want to kill the enemy!

To get started, what is different about these collisions?

31 of 47

From the top - Squash ‘Em

  • In the frame of the collision, we can check where the player is contacting the enemy.

  • What code can we write to check if the player is “on top” of the enemy?

32 of 47

From the top - Squash ‘Em

Check if the player jumps on the enemy

# Inside the UPDATE section

if pygame.sprite.collide_rect(player, enemy):

if player.rect.y + player.rect.height <= enemy.rect.y + 10:

# Player hits enemy from above, kill enemy

enemy.kill()

else:

# Player hits enemy from below or side, kill player

player.kill()

33 of 47

Level design

We can use a Level class to simplify our code:

import pygame

from platform import Platform

from enemy import Enemy

class Level:

def __init__(self, platforms_list, enemies_list):

self.platforms = pygame.sprite.Group(platforms_list)

self.enemies = pygame.sprite.Group(enemies_list)

34 of 47

Bonus Topic #2

Levels

35 of 47

Setting up the levels variable

  • Use a list to store all the levels
  • Use the level variable to store the current level

levels = [

Level( [Platform(200, 300, 400, 500)], [Enemy(100, 200)] ),

Level( [Platform(100, 340, 250, 600)], [Enemy(500, 600)] )

]

level = levels[0]

36 of 47

Changing levels - The function

  • Use levels.index(level) function to calculate our level number
  • Use the new_level variable to store the next level
  • Return the next level
  • Update the level using level = next_level(level, levels)

def next_level(level, levels):

new_level_index = levels.index(level) + 1

new_level = levels[new_level_index]

return new_level

37 of 47

Changing levels - Calling the function

When do we call next_level()?

if len(level.enemies) == 0:

level = next_level(level, levels)

  • When all the enemies are defeated in a level, we start the next level

38 of 47

Bonus Topic #3

Bullets

39 of 47

Bullets - Sprite & movement

A basic Bullet has:

  • Visuals (Shape, Size & Image/color)
  • Movement (Speed & Direction)
  • Collision with other objects

This means we can copy and paste the code from enemy.py and make a few changes to create a basic bullet.

Bullet image: bullet.png

40 of 47

Bullets - Sprite & movement

class Bullet(pygame.sprite.Sprite):

def __init__(self, x, y, x_speed, enemies):

super().__init__()

image_location = os.path.join("assets", "bullet.png")

self.image = pygame.image.load(image_location).convert_alpha()

self.image = pygame.transform.scale(self.image, (80, 50))

self.rect = self.image.get_rect()

self.rect.x = x

self.rect.y = y

self.x_speed = x_speed

self.enemies = enemies

def update(self):

self.move(self.x_speed, 0) # Same move() as Player class

We move the bullet by calling update()

Download bullet image here

41 of 47

Bullets - Collisions

We can use the pygame.sprite.collide_rect function to check for bullet collisions!

class Bullet(pygame.sprite.Sprite):

# __init__ and move methods here

def update(self):

self.move(self.x_speed, 0)

# Remove any enemies hit by this bullet

for enemy in enemies:

if pygame.sprite.collide_rect(self, enemy):

enemy.kill()

42 of 47

Bullets - Organized code

Let’s apply our code organization skills

  • Move our new code to a method

How does this organized code help us?

class Bullet(pygame.sprite.Sprite):

# __init__ and move methods here

def update(self):

self.move(self.x_speed, 0)

self.handle_collisions(self.enemies)

def handle_collisions(self, enemies):

# Remove any enemies hit by this bullet

for enemy in enemies:

if pygame.sprite.collide_rect(self, enemy):

enemy.kill()

43 of 47

Shooting

We add to Player:

  • bullets member var
  • create_new_bullet() method

Then, when we press the spacebar, we can simply call the create_new_bullet() method to shoot a bullet!

while True:

"""EVENTS section"""

keys_pressed = pygame.key.get_pressed()

if keys_pressed[pygame.K_x]:

player.create_new_bullet()

class Player(pygame.sprite.Sprite):

def __init__(self, x, y, enemies):

# Many other __init__ steps here...

self.bullets = pygame.sprite.Group()

self.enemies = enemies

def update(self):

# Many other update steps here...

self.bullets.update()

def create_new_bullet(self):

new_bullet = Bullet(self.rect.x, self.rect.y,

5, self.enemies)

self.bullets.add(new_bullet)

44 of 47

Curtain fire shooter? We already covered it!

The Curtain Fire (aka manic shooter, bullet hell) genre is

  • Filled with enemy bullets
  • Trying not to get hit
  • Top-down

We already have all the pieces for this!

  • Just remove gravity from Session 5 code

45 of 47

Game

Development Time!

46 of 47

Project Rubric

The top 3 projects will score +1000 Ignite Points. To be eligible, your project must:

  • Be written in Pygame
  • Not allowed to use code you have written in the past. All code must have be from when Ignite sessions started!
  • Not plagiarize any other work (using public libraries is fine)

Our mentors will be looking for projects that:�

  • Are creative
  • Have good design and use OOP
  • Are easy for a user to pick up

47 of 47

Gala Event Reminder (TBD April MST)

  • Celebrate your completion of Schulich Ignite!
  • Attend in person at the University of Calgary or online through Zoom!
  • Interact with industry speakers!
  • Showcase your projects to industry speakers, mentors and the rest of the class
    • This is optional but highly encouraged!
    • Chance to win Best Presentation Award!

  • Registration link: Registration