Ruby

Attention Rubyists: you can write video games

Attention Rubyists: you can write video games

by Cory Chamblin

In this talk from RubyConf 2016, speaker Cory Chamblin discusses the potential for Ruby programmers to create video games using the Gosu library. Though Ruby is often associated with web development, Chamblin argues that it is also a powerful language for game development, particularly for those interested in pursuing it as a hobby. He encourages programmers to explore game creation for its creative and educational benefits.

Key points covered in the talk include:

- Motivation for Game Development: Chamblin shares his personal experiences with game programming, highlighting the nostalgic enjoyment of creating games and the creative expression it allows. He emphasizes the importance of having a creative outlet for personal and professional growth as a programmer.

- Why Choose Ruby?: While many dismiss Ruby for game development due to performance concerns, Chamblin asserts that it is suitable for creating delightful 2D games. He introduces Gosu, a user-friendly library that empowers developers to build games without the complexity associated with more traditional game development languages like C++.

- Structure of Games: Chamblin outlines the fundamental structure common in game development using Gosu, detailing three core methods:

1. Initialization: Sets up the game window, assets, and initial game state.

2. Update Method: Processes user input and updates the game state accordingly.

3. Draw Method: Renders the current game state to the screen for display.

- Game Examples: Two simple games are demonstrated:
- Winner Winner Chicken Spinner: A game where a chicken spins, and the outcome is determined by user interaction.

- Running Hero: A side-scrolling platformer where the player controls a character’s movement and jumping mechanics, including collision detection and scoring mechanics.

- Resources for Game Developers: Chamblin concludes with resources for independent game developers, including where to find open-source artwork and music, encouraging attendees to leverage these assets in their projects.

In summary, the presentation highlights the scope of game development in Ruby through Gosu, emphasizing that it provides a viable way for developers to harness their creativity while enhancing their programming skills. Chamblin invites everyone to engage with game development, noting that regardless of prior experience, anyone with Ruby knowledge can begin creating games immediately.

00:00:15.070 Hello everyone, my name is Cory Chamblin. I am a programmer and write back-end software at a company called PagerDuty. But today, I'm not going to talk about that. Instead, I'm going to discuss something much sweeter: how to build video games in Ruby.
00:00:20.720 I don't do this for my job, but I enjoy writing games, and I believe Ruby is a good language for it. You can find the GitHub for the talk at github.com/Chamblin, where you can download the slides and all the code associated with this presentation.
00:00:32.330 First, we'll cover the reasons why you might want to make games, or why you might want to pursue this as a hobby to pass the time on weekends. We will talk about why Ruby is a pretty good choice for getting started with game development. In the main portion of this talk, we will discuss how games are structured in various languages and frameworks. We will also take a look at Gosu, an amazing little library in Ruby for building your own games.
00:01:00.140 Finally, if we have some time at the end, we'll explore a few resources for aspiring independent game developers. If you're not an artist, you can find open-source artwork, music, sound effects, and some tools to help you as you build your own games.
00:01:51.619 Why do I make games, and why should you? I enjoy making games because it reminds me of when I first started programming. Back then, we would have these giant books of BASIC programs that were mostly games. You’d spend all afternoon typing one of these programs into your computer, and if you were very lucky and didn't make mistakes, you’d run it and see something exciting happen on the screen. That visceral feeling of creating something, of seeing your code come to life, is something all professional programmers should experience.
00:02:36.680 I encourage you to try writing games if you don’t already have a creative outlet. If video games resonate with you, fantastic! If not, I highly encourage you to find some way to express your creativity within your craft because it’s crucial for your growth as an engineer or programmer.
00:03:13.970 Now, what about Ruby? Many people don’t consider Ruby a high-performance programming language. So, is Ruby the right tool for building games? When I first became a professional programmer and was eager to dabble in game development, I thought I had to learn C++ and several graphics libraries.
00:03:25.100 However, most languages have libraries that allow you to write delightful 2D independent games without the need for all the heavy lifting often associated with game development. Ruby is no exception; with Gosu, you have plenty of power to write fun and satisfying games. It’s similar to how photographers often say the best camera is the one you have with you. If you want to make games, don’t let the breadth of knowledge required deter you. If you know how to write Ruby programs, you can create games today.
00:04:06.290 If at some point you start to feel limited by Ruby and seek more performance, don’t worry! The lessons learned while developing in Ruby and Gosu translate well to other, more commercial tools. I have dabbled in Unity and I’ve found that the vocabulary, semantics, and structure of writing games are quite similar across numerous platforms. You can gain foundational experience writing games in Ruby, and if you decide to pursue it professionally, you have plenty of room to grow into higher-end tools.
00:05:01.369 For me, I love Ruby, and since I am not making a ton of money writing games, I choose to have fun with it by using Ruby. Let's dive into the core of the talk now. In Gosu, games can essentially be structured around one class that inherits from the window class.
00:05:56.119 Generally, you will implement three key methods that form the basic structure of game development across virtually all frameworks. The first is the constructor for your game, responsible for setting up the window with details like size and title, handling fullscreen mode, and loading necessary assets from disk, such as image files, sounds, and music. It's critical to have all assets loaded in memory since you want quick access to them during gameplay.
00:06:59.700 The second method is the update method, which handles user input and updates the state of the game. For instance, if the user presses a button, you might shoot a fireball or move the character. The final method is the draw method, which renders the game state to the screen based on its current state without modifying anything. Each time we pass through this loop—known as the game loop—counts as a frame. The frame rate commonly targets 60 frames per second.
00:08:21.960 To demonstrate these concepts, I wrote a simple game called 'Winner Winner Chicken Spinner.' In this game, a chicken spins until you press the spacebar, after which the game ends. If the chicken stops in the highlighted area, you win; if not, you lose. With this in mind, the implementation starts with inheriting from Gosu's window and calling the show method.
00:09:35.589 In the initialize method, we set up the window dimensions, title, and load all the assets into memory. We also define the initial state of the game, such as the current rotation of the chicken and the game-over flag. The update function checks for user input, allowing players to stop the chicken's spin or continue spinning, while the draw method renders the current state of the game.
00:10:53.250 In the draw function, I always draw certain elements, like the arrow for where to press the spacebar. I make sure to consider the coordinate system; in Gosu, (0,0) starts at the top-left corner of the window. The draw function will render various images based on the game state. After covering these basics, I would like to introduce another simple game called 'Running Hero,' where you'll be able to move a character around and jump just like in a traditional platform game.
00:13:34.620 In 'Running Hero,' the aim is to control the character's movements as he walks or jumps around within the game. The character's position is stored in an array, while the update function checks user input to adjust the character's x-coordinate accordingly. Jumping requires calculating vertical velocity, where the character can only jump if they’re on the ground. Each frame, the game checks and updates the character's position based on their vertical velocity and gravity until they land back on the ground.
00:21:11.710 Next, we will enhance our character by animating his movements and allowing him to face left or right. By adding a new state to determine which direction the character is facing, we can use a simple scale transformation to flip the character image as they move. We will also implement sprite sheets to organize various character poses and implement animation logic to switch between different frames based on the character's current action.
00:34:15.220 Once the character has movement and animation capabilities, we will add obstacles and enemies to our game, requiring collision detection. We place rectangles around the character and obstacles to check if they intersect, which indicates a collision. When a collision occurs, we will handle it appropriately by determining which side the character was hit from and applying the corresponding effects, like getting hurt or bouncing off an enemy.
00:41:15.850 Finally, we will introduce scoring mechanics to track the player's success with point incrementing whenever they successfully bounce on an enemy's head. In summary, developing a game creates a fantastic opportunity to use creativity while learning programming concepts. I encourage you to explore game development using Ruby and Gosu. Now, if anyone has any questions, feel free to ask.
00:42:10.019 Questions about testing and unit tests within Gosu can often lead to insights about the iterative and experimental nature of game development. While traditional testing methodologies might be cumbersome, regular sanity checks are appropriate. Ultimately, whether you're creating simple or complex games, there is plenty of room to innovate and experiment in this domain.
00:43:55.740 Regarding VR games and Ruby, the performance constraints might make it difficult, although libraries may exist that I am unaware of. Over time, game development has become more intricate, leading to innovations such as sprite management and collision detection. If you're curious to learn more, head to my GitHub where I will share everything mentioned in this talk. Thank you all for your time!