Sunday, December 3, 2017

Upcoming Book: Java Game Development with LibGDX (second edition)

I recently finished writing and editing the manuscript for Java Game Development for LibGDX! Hopefully it will be in print by January 2018; the book website is up at Apress.com. This is a second edition of my earlier book on Java game development, but so much has changed, it feels like an entirely new book. About 200 pages longer, this edition also covers new topics such as visual novel games, rhythm tapper games, and advanced topics such as random maze generation algorithms and shader (GPU) programming with OpenGL.


Here is a brief overview of what you will learn in this book.

Part 1: Fundamental Concepts

In the first part, you will be introduced to the LibGDX library, and build a custom framework on top of this library to simplify creating a variety of games.

Chapter 1: Getting Started with Java and LibGDX

This chapter explains how to set up BlueJ, a Java development environment, which is chosen for simplicity and user-friendliness. Instructions for setting up the LibGDX software library are given, and a visual “Hello, World!” program is presented (which displays an image of the world in a window).

Chapter 2: The LibGDX Framework

This chapter begins by discussing the overall structure of a video game program, including the stages that a game program progresses through, and the tasks that must be accomplished at each stage. Many of the major features and classes of the LibGDX library are introduced in the process of creating a basic game called Starfish Collector. This game is a recurring example throughout the book: features will be added to this project when introducing new topics.

Chapter 3: Extending the Framework

In this chapter, you’ll start with one of the core LibGDX classes that represents game entities, and create an extension of this class to support animation, physics-based movement, improved collision detection, and the ability to manage multiple instances of an entity using lists.


Chapter 4: Shoot-em-up Games

This chapter demonstrates the power of the framework you have created by using that framework to make an entirely new game: Space Rocks, a space themed shoot-em-up game inspired by the classic arcade game Asteroids.


Chapter 5: Text and User Interfaces

In this chapter, you will learn how to display text, create buttons that display an image or text, and design a user interface using tables. First, you will be introduced to these skills by adding these features to the Starfish Collector game from Chapter 3. Then you will build on and strengthen these skills while learning how to create cutscenes (sometimes called in-game cinematics) that provide a narrative element to your games. In the final section, you will create a visual novel style game called The Missing Homework, which focuses on a story and allows the player to make decisions about how the story proceeds.


Chapter 6: Audio

In this chapter, you will learn how to add audio elements - sound effects and background music - to your game. First, you will be introduced to these topics by adding these features to the Starfish Collector. Then you will build on these skills by a musical rhythm-based game called Rhythm Tapper, in which the player presses a sequence of keys indicated visually and synchronized with music playing in the background.


Part 2: Intermediate Examples

With the solid foundation in the fundamental concepts and classes in LibGDX and the custom framework you developed and refined in Part 1, you are now prepared to create a variety of video games from different genres, each featuring different mechanics. 

Chapter 7: Side-Scrolling Games

In this chapter, you will create a side-scrolling action game called Plane Dodger, inspired by modern smartphone games such as Flappy Bird and Jetpack Joyride. Along the way, you will learn how to create an endless scrolling background effect, simulate gravity using acceleration settings, and implement a difficulty ramp that increases the challenge to the player as time passes.


Chapter 8: Bouncing and Collision Games

In this chapter, you will create a ball-bouncing, brick-breaking game called Rectangle Destroyer, inspired by arcade and early console games such as Breakout and Arkanoid. New features that will be implemented in this game include moving an object using the mouse, simulating objects bouncing off of other objects, and creating power-up items that the player can collect.


Chapter 9: Drag and Drop Games

In this chapter, you will learn how to add drag and drop functionality to your games, and create a new class containing the related code. To demonstrate the flexibility of this new class, you will create two new games that make use of this class. The first will be a jigsaw puzzle game, which consists of an image that has been broken into pieces and must be rearranged correctly on a grid. The second will be a solitaire card game called 52 Card Pickup, where a standard deck of playing cards must be organized into piles.



Chapter 10: Tilemaps

This chapter will explain how to use Tiled, a general-purpose map editing software program that can be used for multiple aspects of the level design process. Then you will create a class that allows you to import the data from tilemap files into the custrom framework you have developed. This knowledge will be used to improve two previous game projects:  for the Starfish Collector game, you will design a maze-like level (using rocks for walls) and add some scenery, while for the Rectangle Destroyer game, you will design a colorful layout of bricks. 



Chapter 11: Platform Games

In this chapter, you will learn how to create the platform game Jumping Jack, inspired by arcade and console games such as Donkey Kong and Super Mario Bros. New concepts introduced in this chapter include game entities with multiple animations, platform physics, using extra actors as "sensors" to monitor the area around an object for overlap and collision, jump-through platforms, and key-and-lock mechanics.


Chapter 12: Adventure Games

This chapter features the most ambitious game project in the entire book: a combat-based adventure game named Treasure Quest, inspired by classic console games such as The Legend of Zelda. This game uses new features such as enemy combat with two different types of weapons (a sword and an arrow), non-player characters (NPCs) with messages that depend on the state of the game (such as the number of enemies remaining), and an item shop mechanic.


Part 3: Advanced Topics

This final part of the book contains some additional optional features that can be added to many of the previous projects, and some game projects and involve advanced algorithms and graphics.

Chapter 13: Alternative Sources of User Input

This chapter will explore two alternative sources of user input: gamepad controllers and touch-screen controls. In particular, you will add these alternative sources of user input to the Starfish Collector game that has been featured in previous chapters.


Chapter 14: Maze Games

In this chapter, you will learn how to create the maze-based game Maze Runman, inspired by arcade games such as Pac-Man and the early console game Maze Craze. The main new concepts in this chapter are algorithms for generating and solving mazes.


Chapter 15: Advanced 2D Graphics

In this chapter, you will learn two techniques for incorporating sophisticated graphics into your projects. The first topic is particle systems, which can create special effects such as explosions, which will be incorporated into the Space Rocks game in place of spritesheet-based animations. The second topic is shader programming, which manipulate the pixels of a rendered image to create effects such as blurring or glowing, which will be incorporated into the Starfish Collector game.


Chapter 16: Introduction to 3D Graphics and Games

This chapter introduces some of the 3D graphics capabilities of LibGDX and the concepts and classes necessary to describe and render a three-dimensional scene. You’ll create the game Starfish Collector 3D, a three-dimensional version of the Starfish Collector game introduced at the beginning of the book.


Chapter 17: The Journey Continues

This final chapter presents a variety of steps to consider as you continue on in game development. Among these, you’ll explore working on additional projects, learning skills in related areas, and bringing your games to a wider audience. Along the way, the chapter presents lists of resources of all types, and general advice for many situations.



Thursday, June 1, 2017

Book published! Game Development with Construct 2

I've published another book: Game Development with Construct 2.


The book is currently available at:
https://www.amazon.com/Game-Development-Construct-Design-Realization/dp/1484227832/
and
http://www.apress.com/us/book/9781484227831
(The Apress site contains downloadable graphics used to create the games.)

What is this book about? From the introduction:

In this book, you’ll learn how to create video games using the Construct 2 game engine, an ideal program for aspiring game developers who have no prior experience, as well as experienced game developers looking for a tool to rapidly create prototypes of games. The games you will create in this book are inspired by classic arcade games such as Asteroids, Frogger, Breakout, and PacMan, general genres such as car racing or tower defense games, and console games such as Super Mario Bros. and The Legend of Zelda.

Construct 2 is both user-friendly and powerful. The software has been around for more than five years, has been downloaded more than 3.5 million times, and has an active user community and responsive development team. Games created with Construct 2 can be exported to run on a variety of platforms and operating systems, such as web browsers (HTML5), Windows, MacOS, Linux, Android, and iOS. A free version of Construct 2 is available for download, and is sufficient for all the game projects contained in this book.

Much like the software itself, this book does not assume that you have any prior programming or game development experience. Over the course of the book, you will be guided in creating a series of 12 different video games of increasing complexity that will teach you both the features of the Construct 2 game engine, as well as game development topics and logical programming concepts that can serve you well for software development in general.


Finally, here are some screenshots from the games you can learn to create in this book:


Starfish Collector
(an item-collecting game)
Space Rocks
(inspired by Asteroids)
Cleanup Challenge
(inspired by Frogger)

Plane Dodger
(inspired by Flappy Bird)

Racecar 500
(a top-down racing game)

Rectangle Destroyer
(inspired by Breakout and Arkanoid)

Spell Shooter
(a top-down targeting game)

Airplane Assault
(inspired by the arcade game 1942)

Tower Defenders
(a tower defense style game)

Maze Runman
(inspired by PacMan)

Jumping Jack
(inspired by Super Mario Bros.)

Treasure Quest
(inspired by the Legend of Zelda)




Wednesday, January 6, 2016

LibGDX game jam - dev log #2

The next big addition to my LibGDX game jam project has been the addition of character dialog menus, inspired by those from the early Final Fantasy games.



To support this feature, the main additions have been:
  • a DialogSequence object, which contains an ArrayList of Strings to store the lines of text that appear, and an Iterator and some basic access methods for convenience (add, next, hasNext)
  • a DialogTrigger object, which extends the Box2DActor class and contains:
    • a DialogSequence object
    • a boolean variable called displayOnce that, as the name suggests, determines if the dialog displays only once, or will appear repeatedly
  • a set of corresponding user interface elements: the text is displayed in a Label (with a NinePatch background) on the Stage containing the user interface elements. The alignment is managed with a Table. To continue, the user needs to press the C key. Thus, in addition, there is an Image of the keyboard C key overlayed on the lower right corner of the Label, with is accomplished using a Stack object.

When the player comes into contact with a DialogTrigger object (as determined by a Box2D ContactListener object), the text is displayed on the dialog Label and the Stack containing the objects discussed above is set to visible. While the dialog text is visible, the program automatically returns from the update method, which means that the physics simulation does not run and continuous user inputs are not polled (thus none of the game world entities move). However, Actions are still processed, and discrete user input is still processed - this enables a subtle animated "pulsing" effect on the keyboard key image to take place, and also allows the user to press the key to cycle through the text contained in the DialogSequence object.

Some of the DialogTrigger data can be set up in Tiled; this is particularly useful for situations where dialog should be triggered when the player walks over a certain area that doesn't correspond to a pre-existing game world object. This is accomplished by creating a Rectangle on the Object layer, named "DialogTrigger", and then by adding a set of properties whose names contain the letters "Text" (Text0, Text1, Text2, etc.) and whose data are the strings to be displayed. By including a property called "Once", the DialogTrigger object will be configured (a boolean value will be set) so that the dialog sequence only displays once. Other dialog sequences can be configured in the code itself to be displayed when a pre-existing object should trigger a dialog sequence -- for example, when obtaining a key, you may want to display the message "You obtained a key!"

Finally, if the DialogTrigger field displayOnce is set to true, then when the message is complete, it gets added to removeList, which removes the corresponding body (that triggered the interaction) from the physics world, and also removes the object from its previously set "parentList", the ArrayList of DialogTrigger objects contained in the room, so even if the room is reloaded again later, the dialog will not display again.

The code that implements these features is currently posted on GitHub.


Tuesday, December 29, 2015

LibGDX game jam - dev log #1


The LibGDX Game Jam is on!

In addition to using LibGDX for the coding, I'll be using Tiled (http://www.mapeditor.org/) for level design and Box2D (http://box2d.org/) for physics. For prototyping I'll be using some spritesheets from (http://kenney.nl/). If time permits, I'll work on some custom art assets later.

All the code will be posted on GitHub at https://github.com/stemkoski/LibGDX-Jam

The timing is great, as I've just finished writing Beginning Java Game Development with LibGDX, and this will be an excellent opportunity to build on the foundations presented by that book (source code from the book can be downloaded at http://www.apress.com/9781484215012). To get things up and running quickly, I'll be using (and improving) the following Java classes developed in the book:

  • BaseGame and BaseScreen. These are abstract classes that extend the LibGDX Game and Screen classes, respectively.
  • BaseActor. Extends the LibGDX Actor class, contains Texture data and collision Shape data (which we won't use, instead delegating collision handling to Box2D).
  • AnimatedActor. Extends the BaseActor class, contains Animation data. 
  • Box2DActor. Extends the AnimatedActor class, contains physics-related (Body) data for Box2D.
  • GameUtils. A utility class containing methods to simplify (1) creating animations from files, and (2) processing collisions from Box2D.
  • ParticleActor. Extends the Actor class, is useful for particle effects created with the LibGDX particle editor.


The theme for the LibGDX game jam is "Life in Space". My first thought was outer space -- planets, stars, rocketships, asteroids, etc. From a game design perspective, I've always had a great deal of respect for the classic arcade game Asteroids, for two reasons:

  • its use of immersion: the controls move the spaceship relative to its orientation, rather than relative to the screen directions
  • the shape of its universe: this game features wraparound (the left side of the screen is connected to the right; the top side of the screen is connected to the bottom), and so the Asteroids universe actually exists on the surface of a torus (the shape of a doughnut)

And this train of thought led me to my interpretation of the theme: a key gameplay mechanic is going to be the player figuring out the shape of the space they are in. There will be a series of rooms, but in this universe, they will not be connected as a flat grid. This game will be more puzzle/maze based rather than combat based.

The first step is going to be creating a data structure (called a Room) to hold tilemap data, physics objects, stage objects, etc. The current plan is for each map created with Tiled to have three layers: a tile image layer, a layer for interactive game objects (keys, doors, etc.), and a layer to store non-interactive physics objects (the walls). Each room will have an ID, a number of SpawnPoint objects (each with its own ID), and a number of Portal objects (which contain a destination room ID and spawn point ID). The main game will read in the Tiled TMX files, store each in a custom Room object, and store the set of Rooms in a HashMap (indexed by ID).

The images below show a room in Tiled, and as rendered in LibGDX.

a room in Tiled
room rendered in LibGDX

Working code that implements these steps is currently available on GitHub.


Monday, December 28, 2015

Book Published! Beginning Java Game Development with LibGDX


I've written a book called Beginning Java Game Development with LibGDX, published by Apress, and available here: http://www.apress.com/9781484215012



Here's what the book is about (quoted from the introduction):
In this book, you’ll learn how to program games in Java using the LibGDX game development framework. The LibGDX libraries are both powerful and easy to use, and they will enable you to create a great variety of games quickly and efficiently. LibGDX is free and open-source, can be used to make 2D and 3D games, and integrates easily with third-party libraries to support additional features. Applications created in LibGDX are truly cross-platform; supported systems include Windows, Mac OS X, Linux, Android, iOS, and HTML5/WebGL. 
I have taught courses in Java programming and video game development for many years, and I’ve often struggled to find game programming books that I can recommend to my students without reservation, which led me to write this book. In particular, you will find that this book contains the following unique combination of features, chosen with the aspiring game developer in mind:
  • This book recommends and explains how to use a simple Java development environment (BlueJ) so that you can move on to programming games more quickly.
  • By using the LibGDX framework, you won’t have to “reinvent the wheel” for common programming tasks such as rendering graphics and playing audio. (An explanation of how to write such code from scratch could easily require fifty or more additional pages of reading.) LibGDX streamlines the development process and allows you to focus on game mechanics and design.
  • This book contains many examples of video games that can be developed with LibGDX. The first few example projects will introduce you to the basic features provided by the framework; these starter projects will be extended in the chapters that follow to illustrate how to add visual polish and advanced functionality. Later projects will focus on implementing game mechanics from a variety of genres: shoot-’em-ups, infinite side scrollers, drag-and-drop games, platform games, adventure games with a top-down perspective, and 2.5D games. I believe that working through many examples is fundamental in the learning process; you will observe programming patterns common to many games, you will see the benefits of writing reusable code in practice, you will have the opportunity to compare and contrast code from different projects, and you will gain experience by implementing additional features on your own.
  • At the beginning of this book, I am only assuming that you have a basic familiarity with Java programming. (Details about what background knowledge you need are discussed in the appendix.) Throughout the first few chapters of this book, advanced programming concepts will be introduced and explained as they arise naturally and are needed in the context of game programming. By the time you reach the end of this book, you will have learned about many advanced Java programming topics that are also useful for software development in general.
And here are some screenshots of some of the many games and demos you'll create in the book:

Cheese, Please!
Balloon Buster
Starfish Collector
Space Rocks - inspired by Asteroids
Plane Dodger - inspired by Flappy Bird
Rectangle Destroyer - inspired by Breakout
52 Card Pickup
Starscape - a particle effects demo
Jumping Jack - a sandbox style physics demo
Jumping Jack 2 - a platformer game
Treasure Quest - a top-down adventure/rpg style game
Pirate's Cove - an interactive 3D (2.5D) demo

Interested? Check it out at: http://www.apress.com/9781484215012!


Thursday, December 5, 2013

Interactive Earth with Three.js

I have always been amazed by the Small Arms Import/Export Chrome Experiment at http://www.chromeexperiments.com/detail/arms-globe/ -- it combines data visualization (geography and timeline) with an interactive 3D model of the Earth, using the amazing Javascript library Three.js. The code is open source, available on GitHub, and I wanted to adapt part of it to a project I've been working on.

As a first step, I wanted to implement the country selection and highlighting. I tried reading through the source code, but eventually got lost in the morass of details.  Fortunately, I stumbled upon a blog post written by Michael Chang about creating this visualization; read it at http://mflux.tumblr.com/post/28367579774/armstradeviz. There are many great ideas discussed here, in particular about encoding information in images.  Summarizing briefly: to determine the country that is clicked, there is a grayscale image in the background, with each country colored a unique shade of gray. Determine the gray value (0-255) of the pixel that was clicked and the corresponding country can be identified by a table lookup. (This technique is also mentioned at well-formed-data.net.) The highlighting feature is similarly cleverly implemented: a second image, stored as a canvas element with dimensions 256 pixels by 1 pixel, contains the colors that will correspond to each country. The country corresponding to the value X in the previously mentioned table lookup will be rendered with the color of the pixel on the canvas at position (X,0). A shader is used to make this substitution and color each country appropriately. When a mouse click is detected, the canvas image is recolored as necessary; if the country corresponding to value X is clicked, the canvas pixel at (X,0) is changed to the highlight color, all other pixels are changed to the default color (black).

For aesthetic reasons, I also decided to blend in a satellite image of Earth. Doing this with shaders is straightforward: one includes an additional texture and then mixes the color (vec4) values in the desired ratio in the fragment shader. However, the grayscale image that Chang uses is slightly offset and somehow differently projected from standard Earth images, which typically place the longitude of 180 degrees aligned with the left border of the image.  So I did some image editing, using the satellite image as the base layer and Chang's grayscale image as a secondary translucent layer, then I attempted to cut and paste and move and redraw parts of the grayscale image so that they lined up. The results are far from pixel perfect, but adequate for my purposes. (If anyone reading this knows of a source for a more exact/professional such image, could you drop me a line? Either stemkoski@gmail or @ProfStemkoski on Twitter.) Then I used an edge detection filter to produce an image consisting of outlined countries, and also blended that into the final result in the fragment shader code. The result:


The code discussed above is available online at http://stemkoski.github.io/Three.js/Country-Select-Highlight.html.

More posts to follow on this project...

Happy coding!

Friday, July 5, 2013

Shaders in Three.js: glow and halo effects revisited

While updating my collection of Three.js examples to v58 of Three.js, I decided to clean up some old code.  In particular, I hoped that the selective glow and atmosphere examples could be simplified - removing the need for blending the results of a secondary scene - by using shaders more cleverly.  I posted a question at StackOverflow, and as it often happens, the process of writing the question as clearly as possible for an audience other than myself, helped to clarify the issue.  The biggest hint came from a post at John Chapman's Graphics blog:
The edges ... need to be softened somehow, and that's where the vertex normals come in. We can use the dot product of the view space normal ... with the view vector ... as a metric describing how how near to the edge ... the current fragment is.
Linear algebra is amazingly useful.  Typically, for those faces that appear (with respect to the viewer) to be near the center of the mesh, the normal of the face will point in nearly the same direction as the "view vector" -- the vector from the camera to the face -- and then the dot product of the normalized vectors will be approximately equal to 1.  For those faces that appear to be on the edge as viewed from the camera, their normal vectors will be perpendicular to the view vector, resulting in a dot product approximately equal to 0.  Then we just need to adjust the intensity/color/opacity of the material based on the value of this dot product, and voila! we have a material whose appearance looks the same (from the point of view of the camera) no matter where the object or camera is, which is the basis of the glow effect. (The glow shouldn't look different as we rotate or pan around the object.)  By adjusting some parameters, we can produce an "inner glow" effect, a "halo" or "atmospheric" effect, or a "shell" or "surrounding force field" effect.  To see the demo in action, check out http://stemkoski.github.io/Three.js/Shader-Glow.html.



In particular, click on the information button in the top left and adjust the values as recommended (or however you like) to see some of the possible effects.

Happy coding!