8

Basic Game Loop

Commodore 64 • Phase 1 • Tier 1

Complete the Foundation phase with statistics tracking and game state management

⚪ easy
⏱️ 30-45 minutes
đź’» Code Examples
🛠️ Exercise
🎯

Learning Objectives

  • Implement move counting and statistics
  • Add time tracking and display
  • Create game state management
  • Build information display systems
đź§ 

Key Concepts

Game state management - tracking statistics Information display - player feedback Statistics tracking - move counting Game loop architecture - complete systems

Basic Game Loop

“My game tracks everything and gives me feedback!”

Welcome to the final Foundation lesson! You’ll complete your journey by adding statistics tracking and game state management. This transforms your movement system into a complete, information-rich game experience.

The Magic You’ll Create

“Amazing! I can see all my statistics in real-time!”

By the end of this lesson, your sprite will display comprehensive statistics: move count, time elapsed, current position, and game state. You’ll have created a complete game loop that tracks everything and provides rich feedback to the player!

Screenshot of completed lesson

Why Game Statistics Matter

“This is how games provide feedback and engagement!”

Game statistics are essential for:

  • Player feedback - Showing progress and performance
  • Game progression - Tracking achievements and milestones
  • Replay value - Encouraging players to improve their scores
  • Game balance - Understanding how players interact with your game

Every game from Pac-Man to modern titles uses statistics to create engaging feedback loops!

Understanding Game State Management

Before we build it, let’s understand what makes a complete game loop:

Core Statistics to Track

  1. Move counter - How many moves the player has made
  2. Time elapsed - How long the game has been running
  3. Current position - Where the sprite is located
  4. Game state - Current mode (playing, paused, etc.)

Display Organization

PIXEL PATROL - LESSON 8
POS: 3,2    MOVES: 127
TIME: 02:34  STATE: PLAY

Building the Complete Game Loop

Let’s add comprehensive statistics and state management!

Step 1: Statistics Variables

“This is how we track everything that happens!”

; Game statistics variables
move_count:
        !word 0                ; 16-bit move counter
        
time_seconds:
        !byte 0                ; Seconds elapsed
        
time_minutes:
        !byte 0                ; Minutes elapsed
        
frame_counter:
        !byte 0                ; Frame counter for timing
        
game_state:
        !byte 0                ; 0 = playing, 1 = paused, 2 = game over

Magic Moment: Your game now remembers everything that happens!

Step 2: Move Counter System

“This is how we track player actions!”

increment_move_count:
        ; Increment the 16-bit move counter
        inc move_count         ; Increment low byte
        bne move_count_done    ; If not zero, we're done
        
        ; Low byte wrapped to zero, increment high byte
        inc move_count + 1
        
move_count_done:
        rts

display_move_count:
        ; Display "MOVES: " label
        ldx #0
display_moves_label:
        lda moves_label,x
        beq moves_label_done
        sta $0400 + 40*2 + 20,x ; Row 2, column 20
        inx
        jmp display_moves_label
moves_label_done:

        ; Convert and display move count
        lda move_count + 1     ; High byte
        jsr convert_to_decimal
        sta $0400 + 40*2 + 27  ; Display hundreds
        
        lda move_count         ; Low byte
        jsr convert_to_decimal
        sta $0400 + 40*2 + 28  ; Display tens
        sta $0400 + 40*2 + 29  ; Display ones
        
        rts

moves_label:
        !text "MOVES: "
        !byte 0

Magic Moment: Every move you make is counted and displayed!

Step 3: Time Tracking System

“This is how we track elapsed time!”

update_time:
        ; Increment frame counter
        inc frame_counter
        
        ; Check if one second has passed (50 frames for PAL)
        lda frame_counter
        cmp #50                ; 50 frames = 1 second
        bne time_done
        
        ; Reset frame counter
        lda #0
        sta frame_counter
        
        ; Increment seconds
        inc time_seconds
        
        ; Check if minute has passed
        lda time_seconds
        cmp #60
        bne time_done
        
        ; Reset seconds and increment minutes
        lda #0
        sta time_seconds
        inc time_minutes
        
time_done:
        rts

display_time:
        ; Display "TIME: " label
        ldx #0
display_time_label:
        lda time_label,x
        beq time_label_done
        sta $0400 + 40*3 + 2,x ; Row 3, column 2
        inx
        jmp display_time_label
time_label_done:

        ; Display minutes
        lda time_minutes
        jsr convert_to_decimal
        sta $0400 + 40*3 + 8   ; Minutes tens
        sta $0400 + 40*3 + 9   ; Minutes ones
        
        ; Display colon
        lda #58                ; ASCII colon
        sta $0400 + 40*3 + 10
        
        ; Display seconds
        lda time_seconds
        jsr convert_to_decimal
        sta $0400 + 40*3 + 11  ; Seconds tens
        sta $0400 + 40*3 + 12  ; Seconds ones
        
        rts

time_label:
        !text "TIME: "
        !byte 0

Magic Moment: Your game tracks time like a professional stopwatch!

Step 4: Game State Management

“This is how we track what mode the game is in!”

display_game_state:
        ; Display "STATE: " label
        ldx #0
display_state_label:
        lda state_label,x
        beq state_label_done
        sta $0400 + 40*3 + 15,x ; Row 3, column 15
        inx
        jmp display_state_label
state_label_done:

        ; Display current state
        lda game_state
        cmp #0
        beq display_playing
        cmp #1
        beq display_paused
        
        ; Game over state
        ldx #0
display_over:
        lda over_text,x
        beq state_display_done
        sta $0400 + 40*3 + 22,x
        inx
        jmp display_over
        
display_playing:
        ldx #0
display_play:
        lda play_text,x
        beq state_display_done
        sta $0400 + 40*3 + 22,x
        inx
        jmp display_play
        
display_paused:
        ldx #0
display_pause:
        lda pause_text,x
        beq state_display_done
        sta $0400 + 40*3 + 22,x
        inx
        jmp display_pause
        
state_display_done:
        rts

state_label:
        !text "STATE: "
        !byte 0
        
play_text:
        !text "PLAY"
        !byte 0
        
pause_text:
        !text "PAUSE"
        !byte 0
        
over_text:
        !text "OVER"
        !byte 0

Magic Moment: Your game displays its current state clearly!

Step 5: Complete Game Loop with Statistics

“This is how we create a complete, information-rich game!”

game_loop:
        ; Handle input and movement
        jsr read_joystick
        
        ; Only process movement if playing
        lda game_state
        bne skip_movement      ; Don't move if paused/game over
        
        ; Store old position to detect actual movement
        lda grid_x
        sta old_grid_x
        lda grid_y
        sta old_grid_y
        
        ; Process movement
        jsr validate_movement
        
        ; Check if we actually moved
        lda grid_x
        cmp old_grid_x
        bne movement_occurred
        lda grid_y
        cmp old_grid_y
        bne movement_occurred
        jmp no_movement_occurred
        
movement_occurred:
        ; Increment move counter
        jsr increment_move_count
        
no_movement_occurred:
skip_movement:
        ; Update time (always runs)
        jsr update_time
        
        ; Update display
        jsr convert_grid_to_pixels
        jsr update_sprite_position
        jsr display_current_position
        jsr display_move_count
        jsr display_time
        jsr display_game_state
        
        ; Frame synchronization
        jsr wait_frame
        jmp game_loop

Magic Moment: Your game now tracks and displays everything in real-time!

Create Your Complete Game

Now it’s your turn to build a fully-featured game with comprehensive statistics!

Build Your Statistics-Tracking Game

  1. Create the program: All code is in pixel-patrol-08.asm
  2. Build it: Run make clean && make all
  3. Experience the completeness: Execute make run

Expected Wonder

When you run this program, you’ll experience:

  • Move counting: Every move is tracked and displayed
  • Time tracking: Real-time elapsed time display
  • Position display: Current grid coordinates
  • Game state: Clear indication of current mode
  • Complete feedback: Rich information about your gameplay
  • Professional feel: “This feels like a real game!”

Understanding Game Loop Magic

The Power of Feedback

“Information creates engagement!”

Comprehensive statistics provide:

  • Progress tracking: Players can see their improvement
  • Goal setting: Encouraging players to beat their best
  • Immediate feedback: Instant response to actions
  • Game awareness: Understanding the current state

Efficient Statistics Management

Your statistics system is optimized:

  • Minimal overhead: Statistics update only when needed
  • Real-time display: Information updates every frame
  • Memory efficient: Uses minimal RAM for tracking
  • Modular design: Each statistic is independent

Professional Game Architecture

Your complete game loop includes:

  • Input handling: Responsive player controls
  • Game logic: Movement, constraints, and rules
  • State management: Tracking game mode and statistics
  • Display updates: Real-time information presentation
  • Timing synchronization: Smooth, consistent frame rate

Make It Even More Amazing

Ready to enhance your game statistics?

Challenge 1: High Score System

Wonder Goal: “I can track my best performance!”

Add high score tracking:

; Track best move count and time
; Save to memory and display
; "BEST: 89 MOVES, 01:23"

Challenge 2: Performance Metrics

Wonder Goal: “I can see detailed performance data!”

Add more statistics:

; Track moves per minute
; Calculate efficiency ratings
; Show movement patterns

Challenge 3: Achievement System

Wonder Goal: “I can unlock achievements!”

Add milestone tracking:

; "FIRST 100 MOVES!"
; "UNDER 2 MINUTES!"
; "PERFECT NAVIGATION!"

Challenge 4: Save System

Wonder Goal: “My statistics persist between sessions!”

Add data persistence:

; Save statistics to disk
; Load previous best scores
; Track cumulative statistics

What You’ve Achieved

🎉 Congratulations! You’ve completed the Foundation phase!

The Complete System You Built

  • ✨ Move counting - Action tracking and feedback
  • ✨ Time tracking - Real-time elapsed time display
  • ✨ Game state management - Mode tracking and display
  • ✨ Statistics display - Rich information presentation
  • ✨ Complete game loop - Professional game architecture

Your Foundation Journey

You’ve just completed the most important phase of your C64 programming journey! You’ve built a complete, statistics-tracking game system that forms the foundation for every game you’ll ever create.

What You’ve Mastered

Through these 8 Foundation lessons, you’ve learned:

  1. Hardware control - Direct VIC-II and CIA programming
  2. Sprite management - Creation, movement, and positioning
  3. Input handling - Joystick reading and response
  4. Coordinate systems - Pixel and grid position management
  5. Movement mechanics - Smooth and discrete movement
  6. Constraint systems - Boundaries and collision detection
  7. Game state - Statistics and information management
  8. Complete architecture - Professional game loop design

Historical Achievement

You’ve just implemented the same systems used in legendary C64 games:

  • Pac-Man - Grid movement and statistics
  • Rogue - Position tracking and game state
  • Jumpman - Movement constraints and feedback
  • Impossible Mission - Complete game loop architecture

Modern Relevance

These Foundation concepts power:

  • Game engines - Unity, Unreal Engine core systems
  • Mobile games - Touch input and state management
  • Web games - JavaScript game loops and statistics
  • VR/AR applications - Position tracking and feedback systems

Foundation Phase Complete!

“I’ve built a complete game system from scratch!”

You’ve successfully completed the Foundation phase of your C64 programming journey. You now have all the fundamental skills needed to create any type of game:

Your Foundation Toolkit

  • Hardware mastery - Direct chip programming
  • Movement systems - Pixel and grid-based movement
  • Input handling - Responsive player controls
  • Coordinate mathematics - Position calculations
  • Constraint systems - Collision and boundary detection
  • Game architecture - Complete game loop design
  • Statistics management - Player feedback systems

What’s Next?

You’re now ready to move beyond the Foundation phase and start building real games! In the next phases, you’ll learn:

  • Game mechanics - Scoring, power-ups, and objectives
  • Enemy AI - Computer-controlled opponents
  • Sound effects - Audio feedback and music
  • Visual effects - Particles, animations, and polish
  • Level design - Creating challenging and engaging levels

Congratulations on completing the Foundation phase! You’re now a C64 programmer with the skills to create amazing games!


Technical Reference

Statistics Variables

  • move_count: 16-bit move counter (0-65535)
  • time_seconds: Seconds elapsed (0-59)
  • time_minutes: Minutes elapsed (0-255)
  • frame_counter: Frame timing (0-49)
  • game_state: Current mode (0=play, 1=pause, 2=over)

Time Tracking

  • Frame rate: 50 FPS (PAL) / 60 FPS (NTSC)
  • Timing precision: 1/50th second accuracy
  • Display format: MM:SS (minutes:seconds)
  • Maximum time: 255:59 (about 4 hours)

Statistics Display Layout

  • Row 1: Game title
  • Row 2: Position and move count
  • Row 3: Time and game state
  • Real-time updates: Every frame

Game Loop Architecture

  1. Input processing: Read joystick state
  2. Game logic: Movement and constraints
  3. Statistics update: Track moves and time
  4. Display refresh: Update all information
  5. Frame synchronization: Maintain consistent timing

This complete Foundation system is the basis for every game you’ll ever create on the C64!