Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
65 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 The Game
 Threads
 Keyboard and Mouse
 Double Buffering
 Coding the Game

 Printable version
 Discuss this article
 in the forums


The Series
 The Basics
 Making a
 Simple Game

 The Power of Arrays

In the first part of this series we looked at how to create a basic applet, display text and shapes, and get random numbers. In this much anticipated second part (sorry for the delay and thanks for all the emails) I will cover how to use threads and double buffering as well as how to get input from the mouse and keyboard to create a simple air hockey game.

The Game

Most of you are probably familiar with the popular game of Pong. For those of you who aren’t, the game consists of two paddles and a puck which is hit back and forth until someone misses it. A miss by a player equals a point for their opponent and after a predetermined number of points one player will win. The one change that we will make is that we will make our game into an air hockey game which I’m sure many of you are familiar with. The only real difference between our game and a game of Pong will be that our air hockey game could have smaller goal areas, but we will discuss this in more detail later on. Now, this may seem rather simple, but it can be an extremely fun little game to play and is an excellent place for us to start our journey into the world of java games.

Before we get into the topics of this article we should first examine what we will need for the game so we know what we will need to focus on. There are four important phases that we will need to look for in most games: input, processing, output, and loop.

The input phase consists of getting the intended actions from the players by looking at the states of the keyboard and mouse. We can then interpret the states of these input devices and have the game respond appropriately to them. For our game of air hockey the main action that the user is going to want to perform is moving their paddle up and down. While it is not found in standard pong like games we may want to also allow for the paddle to moved left and right. It is not immediately apparent that we may want to use the mouse, but we will find a possible use for it later on.

The processing phase consists of moving the paddles based on the input, moving the puck, and checking for collisions. The types of collisions that we are looking at are puck to paddle, puck to goal area, and paddle to edge of board. If we allow for the user to move the paddles left and right we also have to check for paddle to paddle collisions. Another item that we should keep track of in the processing phase is the current state of the game. There are three main states that we will probably want to have for our game: puck moving, puck set to go after goal or at beginning of game, and game over.

The third phase is to display the appropriate output on the screen. The means that we are going to have to display the two paddles, the puck, and the scores for each player. We might also want to display some additional information, but for now we will stick with the basics.

The fourth and final phase is what I loop. The purpose of loop is to perform the previous three phases over and over. How much fun would our air hockey game be if we only accepted input once, only calculated puck and paddle movement once, and only drew the paddles and puck once? The answer of course is not very much fun at all. In order to ensure that the input, processing, and output occurs again and again until the game is over we will end up using threads which I will talk about in a moment.

Before we get our hands dirty with some coding I want to say that it is important to roughly sketch out what you want to do in your game before you start to code. It is natural for programmers to want to jump right in and code , but it is important to at least spend a little time going over what you want to do and how you are going to go about doing it. This time spent planning will ensure that you have a clear and focused goal, that your idea is feasible, and that you go about your programming in an organized manner.





Next : Threads