Java 2012

Páginas: 22 (5398 palabras) Publicado: 6 de marzo de 2012
Introduction

This tutorial hopes to give the reader a simple introduction to the world of 2D games using Java. We're going to cover the following areas at a fairly simplistic level:

* Accelerated Mode Graphics
* Simple game loops
* Keyboard Input
* Brute force collision detection

The final game can be see here. The complete source for the tutorial can be found here. Itsintended that you read through this tutorial with the source code at your side. The tutorial isn't going to cover every line of code but should give you enough to fully understand how it works.
Context highlighted source is also available here:
Game.java
SpriteStore.java
Sprite.java
Entity.java
ShipEntity.java
ShotEntity.java
AlienEntity.java
Disclaimer: This tutorial is provided as is. Idon't guarantee that the provided source is perfect or that that it provides best practices.
Design

Before 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. You don't need to read this section unless you're interested in why the source is written the way it is.
For our space invaders game we're goingto have a main window. The window needs to use acceleated graphics. It also needs to respond to the player's key presses to move our player's ship around. For now we can call this class Game since it represents our main game.
In that window we want to see some things moving around. The player's ship, the aliens and the shots that the players fire. Since all of these things have common properites(i.e. 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 behaviours of these different types. Since "thing" is such a terrible name for a class, for our design I'm going to call them Entities. From this we get 4 classes. Entity with 3 subclasses, ShipEntity, AlienEntity and ShotEntity
Finally foreach Entity we have we'd like to have an image displayed, using an old term, a Sprite. However, we might use the same Sprite for multiple entities, for instance the aliens. It seems logically therefore to keep the sprite as a seperate object from the entity. In addition we don't want to waste graphics memory so we'd like to only load each sprite once. To manage this it'd be nice to add a class tomanage loading of the Sprites and storing them for future use. So we add a pair of classes to our design, Sprite and SpriteStore.
The Basic Window
Our basic window is going to be created and maintained by a central class, Game. The following sections cover the initial sections of code in the main class.
Game Entry Point
In java our entry point is "public static void main(String arg[])". This iswhere the application starts when its run. From here we're going to create an instance of our main class which will start everything else running. Game will be a subclass of Canvas, since it will be the main element displaying the graphics. Note, that it needs to be a subclass of Canvas since its one of the only components that supports using accelerated graphics.public static void main(String argv[]) {
Game g = new Game();
g.gameLoop();
}
Creating the window
First we need to create our window and configure its contents. We're going to fix our resolution to 800x600. However, since the window may have decoration the content must be set to 800x600 and we must rely on pack() (shown a littlelater) to actually size the window appropriately.
// create a frame to contain our game

JFrame container = new JFrame("Space Invaders 101");

// get hold the content of the frame and set up the
// resolution of the game
JPanel panel = (JPanel) container.getContentPane();...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • guis en java 1pp 2012
  • Java
  • Java
  • Java
  • java
  • JAVA
  • java
  • java

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS