There are good arguments to use threading, however for the purposes of this tutorial we're not going to consider it. Generally this results in more stable and maintainable code. This helps prevent a whole bunch of complexities synchronizing game updates and game drawing. Client side games tend to be single threaded. The game loop is a remarkably important part of any game. create the buffering strategy which will allow AWT The only thing that needs to be specified is how many buffers to use to manage the screen, in this case we're going to use just 2. We simply ask the Canvas to do it for us. This supports us using page flipping and accelerated graphics.Ĭreating a BufferStrategy couldn't be simpler. The BufferStrategy is named that because its a strategy for managing buffers, or rather the swapping of buffers. To manage our accelerated graphics canvas we're going to rely on a class provided from the JDK. going to do that our self in accelerated mode Tell AWT not to bother repainting our canvas since we're Finally, we get the window to resolve its size, prevent the accelerated graphics) we need to prevent Java AWT attempting to redraw our Since the canvas we're working with is going to be actively redrawn (i.e. setup our canvas size and put it into the content of the frame JPanel panel = (JPanel) container.getContentPane() get hold the content of the frame and set up the JFrame container = new JFrame("Space Invaders 101") However, since the window may have decoration the content must be set to 800圆00 and we must rely on pack() (shown a little later) to actually size the window appropriately. We're going to fix our resolution to 800圆00. Note, that it needs to be a subclass of Canvas since its one of the only components that supports using accelerated graphics.įirst we need to create our window and configure its contents.
Game will be a subclass of Canvas, since it will be the main element displaying the graphics. From here we're going to create an instance of our main class which will start everything else running. This is where the application starts when its run. In java our entry point is "public static void main(String arg)".
ECLIPSE JAVA SHOOTING GAME CODE CODE
The following sections cover the initial sections of code in the main class. Our basic window is going to be created and maintained by a central class, Game.
So we add a pair of classes to our design, Sprite and SpriteStore. To manage this it'd be nice to add a class to manage loading of the Sprites and storing them for future use. In addition we don't want to waste graphics memory so we'd like to only load each sprite once. It seems logically therefore to keep the sprite as a separate object from the entity. However, we might use the same Sprite for multiple entities, for instance the aliens. Entity with 3 subclasses, ShipEntity, AlienEntity and ShotEntityįinally for each Entity we have we'd like to have an image displayed, using an old term, a Sprite. Since "thing" is such a terrible name for a class, for our design I'm going to call them Entities. they display a graphic and move around the screen) we can infer a common class to represent each one with potentially subclasses to define the specific behaviors of these different types. Since all of these things have common properties (i.e. The player's ship, the aliens and the shots that the players fire. In that window we want to see some things moving around. For now we can call this class Game since it represents our main game. It also needs to respond to the player's key presses to move our player's ship around. The window needs to use accelerated graphics. You don't need to read this section unless you're interested in why the source is written the way it is.įor our space invaders game we're going to have a main window. I don't guarantee that the provided source is perfect or that that it provides best practices.īefore starting any game its always a good idea to work out what's going to be in the game and how you're going to build your classes around that. The tutorial isn't going to cover every line of code but should give you enough to fully understand how it works.Ĭontext highlighted source is also available here:ĭisclaimer: This tutorial is provided as is. Its intended that you read through this tutorial with the source code at your side. The complete source for the tutorial can be found here. We're going to cover the following areas at a fairly simplistic level: This tutorial hopes to give the reader a simple introduction to the world of 2D games using Java. Space Invaders 101 An Accelerated Java 2D Tutorial
Space Invaders 101 - An Accelerated Java 2D Tutorial Space Invaders