Andrew Hannon - Software Development Portfolio

Java Game

Links:

Github Repositiory
itch.io Page

Languages/Tools Used:

Java
IntelliJ

Quick Summary

Developed a custom 2D game from scratch using Java, where I implemented the entire game loop, rendering system, animations, and input handling. The core game loop was built using continuous updates, the sprite drawing and animation system was built from the ground up using Java's BufferedImage subclass and user input was managed using Java's KeyListener interface.

The Goal

With this project I wanted to build a video game from scratch using a object-oriented programming language with no additional libraries. I chose to use Java because in my quick research it appeared to have enough built-in functions to create a game loop and 2D animation system using PNG files. The challenges were to build the game loop, draw sprites and animations in the application window, and enable user controls.

What I learned

Learning the basics of a game loop and animation system were my biggest take aways from this. They proved to be simpler tasks than expected and surprisingly it was easier setting everything up and running in Java than I expected, proving you don't need to utilize heavy game engines such as Unreal and Unity to build a game as long as you judge your scope accordingly.

It also taught me some valuable lessons about making a Java game based around mathematical operations as there are some limitations due to the variable type sizes that causes mathematical inaccuracies if I don't limit the decimal places and number size.

How it was Built

The game concept is quite simple. You are given a grid of numbers/letters and operators (add, subtract, multiply, etc.) and you have to move the player character to apply mathematical operations in an attempt to get your solution (at the bottom of the screen) to match the goal displayed at the top of the screen to unlock the exit and leave the level. It has basic WASD controls for movement as well as R to reset the level.

I started with creating the Main class that would set up the game window and initialize a GamePanel class that extended JPanel to set the game screen’s size, background colour etc. This class also utilizes the Runnable interface so that it can run as a thread, since this class will be responsible for the game loop. The game loop is accomplished by the run method in the GamePanel class. I set a draw interval which will tell the game to draw the next frame after this interval has elapsed and in a continuous while loop I call a paint (draw) and update method at a rate of this draw interval that will draw all the visual components of the game on the screen and update the values of animations, character movement, puzzle progress etc.

With the core game loop finished I started to get my level, player, and tile classes set up to implement the mechanics of the game. The game's mechanics were a basic math and word puzzle game, but there were scenarios I would run into when testing that required numbers with large decimal places that would begin to create inaccuracies the more you divide and multiply them. I had to design a character limit for the player solution and use Java's BigDecimal class to handle this properly

Now that I had everything in place, all I needed was to implement an animation system. This proved to be surprisingly easier than I expected. I first created a Sprite class that would load in the spritesheet of the animation as a BufferedImage and use a method called getSpriteSheetFrames to separate the spritesheet into each frame. To do this I separated the spritesheet using the height of each frame, width of each frame, and number of frames in the spritesheet and then calling getSubImage from the BufferedImage class to get each frame and then store them in a BufferedImage array. Then I made an Animation class that when instantiated would accept a BufferedImage array (the frames of the spritesheet) and the frame delay to indicate how many game updates would have to elapse before the next frame in the animation would be drawn. Then in the update method of the animation class, all that would have to happen is increment the index of the frame the animation is on every time the frame delay has elapsed. With this I now had the game running start to end in Java! Woo!