Slide 1

Slide 1 text

@siloraptor [email protected] https://github.com/rosarior http://robertorosario.com An educational retro game using only Python? Challenge Accepted!

Slide 2

Slide 2 text

Who am I?

Slide 3

Slide 3 text

Who am I?

Slide 4

Slide 4 text

Who am I?

Slide 5

Slide 5 text

Who am I?

Slide 6

Slide 6 text

Who am I? http://pythonlatino.com

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Who am I? http://pythonlatino.com

Slide 12

Slide 12 text

Preamble

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Original Problem

Slide 26

Slide 26 text

Problem

Slide 27

Slide 27 text

Problem http://elitedaily.com

Slide 28

Slide 28 text

Problem http://elitedaily.com

Slide 29

Slide 29 text

Problem

Slide 30

Slide 30 text

Problem

Slide 31

Slide 31 text

Problem

Slide 32

Slide 32 text

Problem

Slide 33

Slide 33 text

Problem

Slide 34

Slide 34 text

Problem

Slide 35

Slide 35 text

Doing bad in school in not such a big deal

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Problem

Slide 38

Slide 38 text

Problem

Slide 39

Slide 39 text

Problem

Slide 40

Slide 40 text

Problem

Slide 41

Slide 41 text

Problem

Slide 42

Slide 42 text

Problem

Slide 43

Slide 43 text

Problem

Slide 44

Slide 44 text

Instant action, storylines are boring

Slide 45

Slide 45 text

Problem

Slide 46

Slide 46 text

The eye candy, pixel wars

Slide 47

Slide 47 text

And then...

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Experiment

Slide 55

Slide 55 text

Experiment

Slide 56

Slide 56 text

Success!

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

One simple truth

Slide 59

Slide 59 text

“Content is king”

Slide 60

Slide 60 text

“A video game is just modern day storytelling, pixel perfect graphics, pitch perfect music are not requirements for that. Tell a story someone will want to spend time experiencing.”

Slide 61

Slide 61 text

Easiest part?

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

Hardest part?

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

Basic Game Elements

Slide 66

Slide 66 text

#1 Title screen #2 Introduction / Storyline #3 Progressive stages #4 Player #5 Bosses #6 Game over #7 Game win

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

#1 Title screen - Setting (static) #2 Introduction / Storyline - Setting (scripted) #3 Progressive stages - Setting (interactive) #4 Player - Entity (controllable) #5 Bosses - Entity (autonomous) #6 Game over - Setting (scripted) #7 Game win - Setting (scripted)

Slide 69

Slide 69 text

#1 Title screen - Setting (static) #2 Introduction / Storyline - Setting (scripted) #3 Progressive stages - Setting (interactive) #6 Game over - Setting (scripted) #7 Game win - Setting (scripted)

Slide 70

Slide 70 text

#4 Player - Entity (controllable) #5 Bosses - Entity (autonomous)

Slide 71

Slide 71 text

#1 Settings = Stages #2 Entities = Actors

Slide 72

Slide 72 text

This looks familiar

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Story?

Slide 75

Slide 75 text

Monomyth or “Hero’s Journey”

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

Simplified monomyth #1 Major crisis #2 Choosen one #3 Behind enemy lines #4 Insurmountable odds #5 Return home victorious

Slide 78

Slide 78 text

Storyboard

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Game mechanics

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

Dismissal

Slide 88

Slide 88 text

GameJam

Slide 89

Slide 89 text

Pygame

Slide 90

Slide 90 text

Scanlines

Slide 91

Slide 91 text

Scanlines Sprites

Slide 92

Slide 92 text

Scanlines Sprites Double buffering

Slide 93

Slide 93 text

Scanlines Sprites Double buffering Main loop

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

The Main Loop

Slide 98

Slide 98 text

The main loop VBlank Interrupt service: ● Process input events ● Update bad guys ● Update good guys ● Update environment ● Paint screen ● Update screen ● Release control

Slide 99

Slide 99 text

The main loop BEGIN: ● Process input events ● Update bad guys ● Update good guys ● Update environment ● Paint screen ● Update screen ● GOTO BEGIN

Slide 100

Slide 100 text

The main loop BEGIN: ● Process input events ● Update bad guys ● Update good guys ● Update environment ● Paint screen ● Update screen ● GOTO BEGIN

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

Actual code

Slide 103

Slide 103 text

The main loop class Game(object): def on_setup(self): # Configure screen, resolution, sound, fonts def on_events(self): # Handle input events def on_update(self): # Update entities, movement, behaviour def on_blit(self): # Paint scene and actor in canvas def on_render(self): # Display canvas on screen

Slide 104

Slide 104 text

The main loop class Game(object): ... def run(self): self.on_setup() while not self.finish: self.on_events() self.on_update() self.on_blit() # Render pygame.display.flip()

Slide 105

Slide 105 text

Setup

Slide 106

Slide 106 text

The main loop def on_setup(self) self.clock = pygame.time.Clock() screen_mode = pygame.HWSURFACE | pygame.DOUBLEBUF if self.fullscreen: screen_mode |= pygame.FULLSCREEN self._screen = pygame.display.set_mode(DEFAULT_SCREENSIZE, screen_mode) self.surface = pygame.Surface(self._screen.get_size()) self.running = True self.font = pygame.font.Font(GAME_FONT, 15) self.pause_sound = pygame.mixer.Sound('assets/sounds/pause.wav') self.player = ActorPlayer(game=self) pygame.display.set_caption(GAME_TITLE) self._current_stage = None self.can_be_paused = self._can_be_paused = False self._exit_prompt = False self.ask_exit_confirm = False self.initial_stage = self.module.initial_stage post_event(event=EVENT_CHANGE_STAGE, stage=self.initial_stage)

Slide 107

Slide 107 text

Events

Slide 108

Slide 108 text

The main loop def on_event(self) for event in pygame.event.get(): try: self.on_event(event) except SwallowEvent: pass else: if self._current_stage: self.module.stages[self._current_stage].on_event(event)

Slide 109

Slide 109 text

The main loop def on_event(self) for event in pygame.event.get(): try: self.on_event(event) except SwallowEvent: pass else: if self._current_stage: self.module.stages[self._current_stage].on_event(event)

Slide 110

Slide 110 text

Updates

Slide 111

Slide 111 text

The main loop class Game(object): ... def run(self): self.on_setup() while not self.finish: self.on_events() self.module.stages[self._current_stage].on_update() self.on_blit() # Render pygame.display.flip()

Slide 112

Slide 112 text

The main loop class Game(object): ... def run(self): self.on_setup() while not self.finish: self.on_events() self.module.stages[self._current_stage].on_update() self.on_blit() # Render pygame.display.flip()

Slide 113

Slide 113 text

Blit

Slide 114

Slide 114 text

The main loop def on_blit(self): self.module.stages[self._current_stage].on_blit() if self._exit_prompt: self.exit_confirmation(self.font) if self.paused: self.display_pause_label(self.font) if self.shake_screen: self._screen.blit(self.surface, (randint(0, 10), randint(0, 10))) else: self._screen.blit(self.surface, (0, 0))

Slide 115

Slide 115 text

Screen refresh

Slide 116

Slide 116 text

The main loop class Game(object): ... def run(self): self.on_setup() while not self.finish: self.on_events() self.module.stages[self._current_stage].on_update() self.on_blit() # Render pygame.display.flip()

Slide 117

Slide 117 text

The main loop class Game(object): ... def run(self): self.on_setup() while not self.finish: self.on_events() self.module.stages[self._current_stage].on_update() self.on_blit() # Render pygame.display.flip()

Slide 118

Slide 118 text

Double buffering

Slide 119

Slide 119 text

Game code Screen

Slide 120

Slide 120 text

Game code Screen

Slide 121

Slide 121 text

Game code Screen

Slide 122

Slide 122 text

Scanlines

Slide 123

Slide 123 text

http://lea.hamradio.si/~s51kq/scan-lines.gif

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

Game code Screen Memory page 1 Memory page 2

Slide 127

Slide 127 text

Game code Screen Memory page 1 Memory page 2 Video generator pointer

Slide 128

Slide 128 text

Game code Screen Memory page 1 Memory page 2 Video generator pointer Draw on inactive page

Slide 129

Slide 129 text

Game code Screen Memory page 1 Memory page 2 Video generator pointer Draw on inactive page

Slide 130

Slide 130 text

Game code Screen Memory page 1 Memory page 2 Switch video generator pointer

Slide 131

Slide 131 text

Game code Screen Memory page 1 Memory page 2 Draw on inactive page

Slide 132

Slide 132 text

Game code Screen Memory page 1 Memory page 2 Switch video generator pointer

Slide 133

Slide 133 text

Stages

Slide 134

Slide 134 text

Stage class Stage(object): def __init__(self, next_stage=None): def add_actor(self, actor): def on_blit(self): def on_event(self, event): def on_level_exit(self): def on_setup(self, game): def on_start(self): def on_update(self):

Slide 135

Slide 135 text

Stage StageScripted StagePlayable StagePlayableBoss Stage

Slide 136

Slide 136 text

Stage class Stage(object): def __init__(self, next_stage=None): def add_actor(self, actor): def on_blit(self): def on_event(self, event): def on_level_exit(self): def on_setup(self, game): def on_start(self): def on_update(self):

Slide 137

Slide 137 text

class Stage(object): def add_actor(self, actor): if actor not in self.actors: actor.on_setup(stage=self) self.actors.append(actor) def on_blit(self): self.on_draw_background() for actor in self.actors: actor.on_blit() def on_update(self): if not self.game.paused: for actor in self.actors: actor.on_update(time_passed=self.game.time_passed) for time, entry in self._runtime_script.items(): if pygame.time.get_ticks() > self._start_time + time: entry = self._runtime_script.pop(time) entry.on_execute() Stage

Slide 138

Slide 138 text

Stage StageScripted StagePlayable StagePlayableBoss Stage

Slide 139

Slide 139 text

Stage StageScripted StagePlayable StagePlayableBoss Stage

Slide 140

Slide 140 text

class StageScripted(Stage): def on_setup(self, game): ... self.script = {} def on_start(self): self._start_time = pygame.time.get_ticks() self._runtime_script = self.script.copy() def on_update(self): if not self.game.paused: for actor in self.actors: actor.on_update(time_passed=self.game.time_passed) for time, entry in self._runtime_script.items(): if pygame.time.get_ticks() > self._start_time + time: entry = self._runtime_script.pop(time) entry.on_execute() Stage

Slide 141

Slide 141 text

class StageTitle(StageScripted): def on_setup(self, *args, **kwargs): super(...) text_press_enter = DisplayText(string=, position=, font_file=, size=, effect=, horizontal_align=, vertical_align=) text_credit = text_version = self.game.can_be_pause = False self.game.ask_exit_confirm = False self.script = { 0000: Background(image_file=, fit=), 0001: PlayMusic(filename=, loop=True), 0002: ActorCommand(text_press_enter, lambda x: x.show()), 0003: ActorCommand(text_credit, lambda x: x.show()), 0004: ActorCommand(text_version, lambda x: x.show()), } Stage

Slide 142

Slide 142 text

def on_event(self, event): if event.type == pygame.KEYDOWN: post_event(event=EVENT_CHANGE_STAGE, stage=self.next_stage) Stage

Slide 143

Slide 143 text

class StoryStage(StageScripted): def on_setup(self, *args, **kwargs): ... evil_spaceship = ActorSpaceship(game=self.game) tractor_beam = ActorTracktorBeam(game=self.game) book_01 = ActorBook01(game=self.game) book_02 = ActorBook02(game=self.game) book_03 = ActorBook03(game=self.game) book_04 = ActorBook04(game=self.game) book_05 = ActorBook05(game=self.game) human_ship = ActorHumanShip(game=self.game) text_time = DisplayText(string=TEXT_YEAR, position=(0, 400), effect=TypeWriter (150, 'assets/sounds/08.ogg'), horizontal_align=CenterAlign) text_book_1 = DisplayText(string=TEXT_STEAL_BOOKS_1, position=(0, 350), effect=TypeWriter(50), horizontal_align=CenterAlign) text_book_2 = DisplayText(string=TEXT_STEAL_BOOKS_2, position=(0, 380), effect=TypeWriter(50), horizontal_align=CenterAlign) text_hero = DisplayText(string=TEXT_HERO, position=(0, 350), effect=TypeWriter (50), horizontal_align=CenterAlign) Stage

Slide 144

Slide 144 text

class StoryStage(StageScripted): def on_setup(self, *args, **kwargs): ... self.script = { 0000: Background(image_file='assets/backgrounds/earth.png'), 0001: PlayMusic('assets/music/LongDarkLoop.ogg'), 1000: ActorCommand(text_time, lambda x: x.show()), 6000: ActorCommand(text_time, lambda x: x.hide()), 6500: ActorCommand(text_book_1, lambda x: x.show()), 9000: ActorCommand(text_book_2, lambda x: x.show()), 15000: ActorCommand(text_book_1, lambda x: x.hide()), 15001: ActorCommand(text_book_2, lambda x: x.hide()), 16500: ActorCommand(evil_spaceship, lambda x: x.set_position(220, -65)), 16501: ActorCommand(evil_spaceship, lambda x: x.show()), 16502: ActorCommand(evil_spaceship, lambda x: x.set_destination(235, 30, 0.04)), 17500: ActorCommand(evil_spaceship, lambda x: x.set_destination(235, 30, 0.03)), 19000: ActorCommand(evil_spaceship, lambda x: x.set_destination(235, 30, 0.01)),

Slide 145

Slide 145 text

class StoryStage(StageScripted): def on_setup(self, *args, **kwargs): ... self.script = … 21000: ActorCommand(tractor_beam, lambda x: x.set_position(253, 70)), 21001: ActorCommand(tractor_beam, lambda x: x.show()), 21200: ActorCommand(tractor_beam, lambda x: x.strobe_start()), 22100: ActorCommand(book_01, lambda x: x.set_destination(240, 40, 0.02)), 22101: ActorCommand(book_01, lambda x: x.set_position(253, 170)), 22102: ActorCommand(book_01, lambda x: x.show()),

Slide 146

Slide 146 text

Actors

Slide 147

Slide 147 text

Actors class Actor(object): animation_death_file = None animation_death_fps = 8 animation_loop = True animation_normal_files = [] attack_points = 1 total_hit_points = 100 invincible = False fps = 1 sound_hit_file = None sound_die_file = None speed = 0.05 team = None scale = 1 rotation = 0 flip_x = False flip_y = False can_overflow = True

Slide 148

Slide 148 text

Actors def __init__(self, game): def change_animation(self, images): def destroy(self): def hide(self): def is_alive(self): def set_animation_fps(self, fps): def set_destination(self, x_position, y_position, speed): def set_direction(self, vector): def set_flip_x(self, flip): def set_flip_y(self, flip): def set_invincible(self, offset): def set_position(self, x_position, y_position): def set_rotation(self, degrees): def set_scale(self, scale): def show(self): def strobe_start(self): def strobe_stop(self):

Slide 149

Slide 149 text

Actors @staticmethod def load_sliced_sprites(width, height, filename, origin_y=0): File Memory self.images = []

Slide 150

Slide 150 text

Actors

Slide 151

Slide 151 text

Actors def set_animation_fps(self, fps): self._animation_delay = 1000 / fps def on_animate(self): if self.animation_active or force: t = pygame.time.get_ticks() if t - self._last_update > self._animation_delay: self._frame += 1 if self._frame >= len(self.images): if self.animation_loop: self._frame = 0 else: self._frame -= 1 self.on_animation_finished() self.image = self.images[self._frame] self._last_update = t

Slide 152

Slide 152 text

Actors def is_alive(self): return self.hit_points > 0 def change_animation(self, images): self.images = images self._frame = -1 self._last_update = 0 self.rect = self.images[0].get_rect() self.size = self.images[0].get_size() self.image = self.images[0]

Slide 153

Slide 153 text

Actors def on_hit(self, attack_points): self.hit_points -= attack_points if self.sound_hit: self.sound_hit.play() if self.hit_points <= 0: self.hit_points = 0 self.on_death() def on_death(self): self.hit_points = 0 self.direction = vec2d(0, 0) if self.sound_die: self.sound_die.play() self.animation_loop = False if self.animation_death_fps: self.set_animation_fps(self.animation_death_fps) if self.animation_death: self.change_animation(self.animation_death)

Slide 154

Slide 154 text

Actors def on_animate(self, time_passed, force=False): def on_animation_finished(self): def on_blit(self): def on_calculate_movement(self): def on_collision(self, foreign_actor): def on_death(self): def on_execute(self): def on_event(self, event): def on_hit(self, attack_points): def on_setup(self, stage): def on_update(self, time_passed, force=False):

Slide 155

Slide 155 text

Actors def on_animate(self, time_passed, force=False): def on_animation_finished(self): def on_blit(self): def on_calculate_movement(self): def on_collision(self, foreign_actor): def on_death(self): def on_execute(self): def on_event(self, event): def on_hit(self, attack_points): def on_setup(self, stage): def on_update(self, time_passed, force=False):

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

No content

Slide 158

Slide 158 text

Displacement (how much movement) Position (where)

Slide 159

Slide 159 text

Position (where) Direction vector * time Displacement (how much movement)

Slide 160

Slide 160 text

Position (where)

Slide 161

Slide 161 text

Direction vector

Slide 162

Slide 162 text

Direction vector

Slide 163

Slide 163 text

Vec2D

Slide 164

Slide 164 text

new_position = old_position + vector * speed * time_delta time_delta = current_time - last_calculation_time

Slide 165

Slide 165 text

new_position = old_position + vector * speed * time_delta time_delta = [given by pygame]

Slide 166

Slide 166 text

new_position = old_position + vector * speed * time_delta

Slide 167

Slide 167 text

new_position = old_position + vector * speed

Slide 168

Slide 168 text

vector * speed

Slide 169

Slide 169 text

vector

Slide 170

Slide 170 text

Player = Moves by keyboard Enemy = Follows player Boss = Bounces

Slide 171

Slide 171 text

class Actor(object): def set_direction(self, vector): self.direction = vector def on_update(self, time_passed, force=False): if self.is_alive(): self.on_calculate_movement() displacement = vec2d( self.direction.x * self.speed * time_passed, self.direction.y * self.speed * time_passed ) self.pos += displacement

Slide 172

Slide 172 text

class ActorPlayer(Actor): def on_calculate_movement(self): super(ActorPlayer, self).on_calculate_movement() if self.game.running and self.is_alive(): keys_pressed = pygame.key.get_pressed() direction_y = 0 direction_x = 0 if keys_pressed[pygame.K_LEFT]: direction_x = -1 if keys_pressed[pygame.K_RIGHT]: direction_x = 1 if keys_pressed[pygame.K_UP]: direction_y = -1 if keys_pressed[pygame.K_DOWN]: direction_y = 1 self.set_direction(vec2d(direction_x, direction_y).normalized())

Slide 173

Slide 173 text

class ActorEnemy(Actor): def on_calculate_movement(self): # Follow player self.set_direction((self.game.player.pos - self.pos).normalized())

Slide 174

Slide 174 text

5 * -1 = -5 -5 * -1 = 5 -1 ~ NOT

Slide 175

Slide 175 text

from libraries.vec2d import vec2d >>> vec2d(1, 0) * vec2d(-1, 1) # Moves right, invert (x) vec2d(-1, 0) # Now moves left >>> vec2d(-1, 0) * vec2d(-1, 1) # Moves left, invert (x) vec2d(1, 0) # Now moves right >>> vec2d(0, -1) * vec2d(-1, 1) # Moves up, invert (x) vec2d(0, -1) # Keeps moving up

Slide 176

Slide 176 text

class ActorBoss(Actor): def on_calculate_movement(self): bounds_rect = self.game.surface.get_rect() if self.rect.left <= bounds_rect.left: self.direction.x *= -1 elif self.rect.right >= bounds_rect.right: self.direction.x *= -1 elif self.rect.top <= bounds_rect.top: self.direction.y *= -1 elif self.rect.bottom >= bounds_rect.bottom: self.direction.y *= -1

Slide 177

Slide 177 text

Demo

Slide 178

Slide 178 text

No content

Slide 179

Slide 179 text

Questions? @siloraptor [email protected] https://github.com/rosarior http://robertorosario.com

Slide 180

Slide 180 text

Thank you! @siloraptor [email protected] https://github.com/rosarior http://robertorosario.com