Schulich Ignite Flare
Session 7
Session overview
Machine
Learning
What is AI?
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
When to use machine learning
Deep Learning
Reinforced Learning - The baby analogy
Neural Networks - The Real Deal
grades
bribes
Neural Networks - How does it learn?
Neural Networks - Layers
Game Agents
Supervised Learning
What if we don’t have data?
Large Language Models
How Large Language models work
Inside a large language model
Questions?
Bonus Topic #1
Enemies
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
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)
Making your enemy walk
# Inside of Enemy class
def update(self):
# Move the enemy to the right
self.rect.x += self.x_speed
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?
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
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)
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!
Enemy collisions
When a collision happens, we know:
That’s it! We’ll need some creativity to decide what happens
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
From the top - Squash ‘em
What if player jumps on top of enemy?
To get started, what is different about these collisions?
From the top - Squash ‘Em
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()
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)
Bonus Topic #2
Levels
Setting up the levels variable
levels = [
Level( [Platform(200, 300, 400, 500)], [Enemy(100, 200)] ),
Level( [Platform(100, 340, 250, 600)], [Enemy(500, 600)] )
]
level = levels[0]
Changing levels - The function
def next_level(level, levels):
new_level_index = levels.index(level) + 1
new_level = levels[new_level_index]
return new_level
Changing levels - Calling the function
When do we call next_level()?
if len(level.enemies) == 0:
level = next_level(level, levels)
Bonus Topic #3
Bullets
Bullets - Sprite & movement
A basic Bullet has:
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
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
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()
Bullets - Organized code
Let’s apply our code organization skills
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()
Shooting
We add to Player:
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)
Curtain fire shooter? We already covered it!
The Curtain Fire (aka manic shooter, bullet hell) genre is
We already have all the pieces for this!
Game
Development Time!
Project Rubric
The top 3 projects will score +1000 Ignite Points. To be eligible, your project must:
Our mentors will be looking for projects that:�
Gala Event Reminder (TBD April MST)