Space Game

Description

This example takes you through the process of creating a complete game within AGK. The game is set in space, in a randomly generated side scrolling level that is populated with dangerous aliens. The objective is to pilot your ship through the level, destroying as many aliens along the way, avoiding any obstacles and ensure you reach safety. Here's a screen shot of the final result:

Before going any further we would recommend playing the game and getting an understanding of its contents and some kind of general overview of what is happening.

Overview

The game doesn't require a massive amount of source code, but in order to keep things easier to work with it has been split into multiple source files:

Splitting source code in this manner can make navigating the project a lot easier and it's well worth doing, especially for large scale projects.

Main.agc

The code starts by making a call to a function named Begin, which can be found within main.agc. This function kicks things of by taking the following steps:

After Begin has been called the main loop is set up. The main loop calls:

Aliens.agc

This file contains all the source code relating to the aliens within the game.

When the game starts CreateAliens is called. It is responsible for loading images for the aliens and creating the aliens. A pool of 10 aliens are made and reused throughout the level.

The function CreateAlienExplosions creates 5 explosions for use in the game. The reason behind using 5 explosions instead of 10 (to match the number of aliens) is simple - it reduces overheads and memory usage. The end result is that when an alien explodes a check is made to find an unused explosion from the pool of 5, if one is available it is selected and displayed on screen and then resets once it has finished. This leaves us with a maximum of 5 explosions at once on screen.

UpdateAliens runs through the 10 aliens and positions them. It moves the alien sprites and their engines from right to left. Once an alien is out of the game view it gets repositioned off screen to the right.

The next function ResetAliens will ensure that all the aliens are in position and have their initial properties set.

The ExplodeAlien function gets called when a collision has taken place between a bullet from the player and an alien. At this point an available explosion is checked for from the pool of 5, and if one is available a large explosion is created at the position of the alien.

CheckBulletsWithAliens runs through the 10 aliens and 10 bullets and performs a simple collision check to determine whether collisions have occurred.

Level.agc

This file contains code to create the background, star field and the level. The level is randomly generated by placing blocks at the top and bottom of the screen, there's also a few positioned at locations near to the centre of the screen.

Misc.agc

Functions in this file are used to create text entities for the score display, updating these displays and loading music and sound effects.

Player.agc

Code relating to the player exists in this file.

The function CreatePlayer loads an image for the player's ship and applies this to a sprite. This is followed with a call to ResetPlayer and ResetBullets.

The ResetPlayer function is called by CreatePlayer and when the game restarts. It's a simple function that returns the player to its default state, by setting its position and creating a particle emitter to be used as an engine trail.

Conclusion

Making this game turned out to be a fairly simple process in AGK and the major benefit is that it runs on multiple platforms without any alterations.