Game Development with Rust and WebAssembly

Book description

Write an endless runner game for the web in Rust and test, deploy, and debug your 2D game using the WebAssembly toolchain

Key Features

  • Build and deploy an endless runner game for the web from scratch through this helpful guide with key images printed in color
  • Learn how to use Rust for web development with WebAssembly
  • Explore modern game development and programming techniques to build 2D games using Rust

Book Description

The Rust programming language has held the most-loved technology ranking on Stack Overflow for 6 years running, while JavaScript has been the most-used programming language for 9 years straight as it runs on every web browser. Now, thanks to WebAssembly (or Wasm), you can use the language you love on the platform that's everywhere.

This book is an easy-to-follow reference to help you develop your own games, teaching you all about game development and how to create an endless runner from scratch. You'll begin by drawing simple graphics in the browser window, and then learn how to move the main character across the screen. You'll also create a game loop, a renderer, and more, all written entirely in Rust. After getting simple shapes onto the screen, you'll scale the challenge by adding sprites, sounds, and user input. As you advance, you'll discover how to implement a procedurally generated world. Finally, you'll learn how to keep your Rust code clean and organized so you can continue to implement new features and deploy your app on the web.

By the end of this Rust programming book, you'll build a 2D game in Rust, deploy it to the web, and be confident enough to start building your own games.

What you will learn

  • Build and deploy a Rust application to the web using WebAssembly
  • Use wasm-bindgen and the Canvas API to draw real-time graphics
  • Write a game loop and take keyboard input for dynamic action
  • Explore collision detection and create a dynamic character that can jump on and off platforms and fall down holes
  • Manage animations using state machines
  • Generate levels procedurally for an endless runner
  • Load and display sprites and sprite sheets for animations
  • Test, refactor, and keep your code clean and maintainable

Who this book is for

This game development book is for developers interested in Rust who want to create and deploy 2D games to the web. Game developers looking to build a game on the web platform using WebAssembly without C++ programming or web developers who want to explore WebAssembly along with JavaScript web will also find this book useful. The book will also help Rust developers who want to move from the server side to the client side by familiarizing them with the WebAssembly toolchain. Basic knowledge of Rust programming is assumed.

Table of contents

  1. Game Development with Rust and WebAssembly
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Code in Action
    6. Download the color images
    7. Conventions used
    8. Get in touch
    9. Share Your Thoughts
  6. Part 1: Getting Started with Rust, WebAssembly, and Game Development
  7. Chapter 1: Hello WebAssembly
    1. Technical requirements
    2. What is WebAssembly?
    3. A Rust project skeleton
    4. Drawing to the canvas
      1. The current code
      2. Drawing a triangle
      3. A Sierpiński triangle
    5. Summary
  8. Chapter 2: Drawing Sprites
    1. Technical requirements
    2. A quick game design session
    3. Rendering a sprite
      1. Loading images
      2. Canvas coordinates
      3. Drawing images
      4. JavaScript callbacks
      5. Async Rust
    4. Sprite sheets
      1. Loading JSON
      2. Parsing JSON
      3. Drawing with our "cookie cutter"
    5. Adding animation
    6. Summary
  9. Part 2: Writing Your Endless Runner
  10. Chapter 3: Creating a Game Loop
    1. Technical requirements
    2. Minimal architecture
      1. Good? Bad? I'm the guy with code
      2. Layered architecture
      3. Creating the browser module
      4. Loading an image
    3. Creating a game loop
      1. RequestAnimationFrame
      2. A game trait
      3. Fixing our time step
      4. Loading assets
      5. Cleaner drawing
      6. Integrating the game loop
    4. Adding keyboard input
    5. Moving Red Hat Boy
    6. Summary
  11. Chapter 4: Managing Animations with State Machines
    1. Technical requirements
    2. Introducing state machines
      1. Defining a state machine
      2. Implementing with types
    3. Managing animation
      1. Transitioning between states
      2. Managing the state machine
      3. Using Into for clean code
      4. Integrating the state machine
      5. Drawing RedHatBoy
      6. Updating RHB
      7. Adding the Running state
      8. Transitioning to sliding
      9. Transitioning to sliding and back again
      10. Every little thing I think I see
      11. Transitioning to jumping
    4. Summary
  12. Chapter 5: Collision Detection
    1. Technical requirements
    2. Creating a real scene
      1. Adding the background
      2. Adding an obstacle
    3. Axis-aligned bounding boxes
      1. Collision
    4. Trimming the sprite sheet
      1. Adding the trimmed sheet
    5. Colliding with an obstacle
      1. A bounding box for a stone
      2. A bounding box for RedHatBoy
      3. Crashing on the collision
    6. Jumping onto a platform
      1. Adding a platform
      2. Collision from below
      3. Transparency in bounding boxes
    7. Summary
  13. Chapter 6: Creating an Endless Runner
    1. Technical requirements
    2. Scrolling the background
      1. Fixing RHB in x
      2. An infinite background
    3. Refactoring for endless running
      1. f32 versus i16
      2. A more useful Rect
      3. Obstacle traits
      4. Removing obstacles as they go off screen
      5. Sharing a sprite sheet
      6. Many different platforms
    4. Creating a dynamic level
      1. Creating one segment
      2. Adding a timeline
      3. Creating segments
    5. Summary
  14. Chapter 7: Sound Effects and Music
    1. Technical requirements
    2. Adding the Web Audio API to the engine
      1. Playing a sound in Rust
      2. Loading the sound
      3. Adding audio to the engine
    3. Playing sound effects
      1. Refactoring RedHatBoyContext and RedHatBoy
      2. Adding a sound effect
    4. Playing long music
    5. Summary
  15. Chapter 8: Adding a UI
    1. Technical requirements
    2. Design a new game button
      1. Preparing a UI
      2. Showing the button with Rust
    3. Show the button on game over
      1. A state machine review
      2. Redesigning to a state machine
    4. Start a new Game
      1. Pre-loading
    5. Summary
  16. Part 3: Testing and Advanced Tricks
  17. Chapter 9: Testing, Debugging, and Performance
    1. Technical requirements
    2. Creating automated tests
      1. Test-driven development
      2. Getting started
      3. Pure functions
      4. Hiding the Browser module
      5. Browser tests
      6. Async tests
    3. Debugging the game
      1. Log versus error versus panic
      2. Linting and Clippy
    4. Measuring performance with a browser
      1. Frame rate counter
      2. Browser debugger
      3. Checking memory
    5. Summary
  18. Chapter 10: Continuous Deployment
    1. Technical requirements
    2. Creating a CI/CD pipeline
      1. GitHub Actions
      2. Node.js and webpack
    3. Deploying test and production builds
    4. Summary
  19. Chapter 11: Further Resources and What's Next?
    1. Technical requirements
    2. A challenging review
      1. What happened to the dog?
    3. Further resources
      1. Using JavaScript libraries
    4. Summary
    5. Why subscribe?
  20. Other Books You May Enjoy
    1. Packt is searching for authors like you

Product information

  • Title: Game Development with Rust and WebAssembly
  • Author(s): Eric Smith
  • Release date: April 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781801070973