Tecnologias
* Beginning Java Game Programming, 3rd Edition
* by Jonathan S. Harbour
* GALACTIC WAR, Chapter 14
*****************************************************/
import java.awt.*;
import java.util.*;
import java.lang.System;
import java.awt.event.*;
public class GalacticWar extends Game {
//these must be static because they are passedto a constructor
static int FRAMERATE = 60;
static int SCREENWIDTH = 800;
static int SCREENHEIGHT = 600;
//misc global constants
final int ASTEROIDS = 10;
final int BULLET_SPEED = 4;
final double ACCELERATION = 0.05;
final double SHIPROTATION = 5.0;
//sprite state values
final int STATE_NORMAL = 0;
final int STATE_COLLIDED = 1;
final int STATE_EXPLODING = 2;
//spritetypes
final int SPRITE_SHIP = 1;
final int SPRITE_ASTEROID_BIG = 10;
final int SPRITE_ASTEROID_MEDIUM = 11;
final int SPRITE_ASTEROID_SMALL = 12;
final int SPRITE_ASTEROID_TINY = 13;
final int SPRITE_BULLET = 100;
final int SPRITE_EXPLOSION = 200;
//various toggles
boolean showBounds = false;
boolean collisionTesting = true;
//define the images used in the gameImageEntity background;
ImageEntity bulletImage;
ImageEntity[] bigAsteroids = new ImageEntity[5];
ImageEntity[] medAsteroids = new ImageEntity[2];
ImageEntity[] smlAsteroids = new ImageEntity[3];
ImageEntity[] tnyAsteroids = new ImageEntity[4];
ImageEntity[] explosions = new ImageEntity[2];
ImageEntity[] shipImage = new ImageEntity[2];
//create a random number generator
Random rand =new Random();
//used to make ship temporarily invulnerable
long collisionTimer = 0;
//some key input tracking variables
boolean keyLeft, keyRight, keyUp, keyFire, keyB, keyC;
/*****************************************************
* constructor
*****************************************************/
public GalacticWar() {
//call base Game class' constructor
super(FRAMERATE,SCREENWIDTH, SCREENHEIGHT);
}
/*****************************************************
* gameStartup event passed by game engine
*****************************************************/
void gameStartup() {
//load the background image
background = new ImageEntity(this);
background.load("bluespace.png");
//create the ship sprite--first in the sprite list
shipImage[0] = newImageEntity(this);
shipImage[0].load("spaceship.png");
shipImage[1] = new ImageEntity(this);
shipImage[1].load("ship_thrust.png");
AnimatedSprite ship = new AnimatedSprite(this, graphics());
ship.setSpriteType(SPRITE_SHIP);
ship.setImage(shipImage[0].getImage());
ship.setFrameWidth(ship.imageWidth());
ship.setFrameHeight(ship.imageHeight());
ship.setPosition(newPoint2D(SCREENWIDTH/2, SCREENHEIGHT/2));
ship.setAlive(true);
ship.setState(STATE_NORMAL);
sprites().add(ship);
//load the bullet sprite image
bulletImage = new ImageEntity(this);
bulletImage.load("plasmashot.png");
//load the explosion sprite image
explosions[0] = new ImageEntity(this);
explosions[0].load("explosion.png");
explosions[1] = new ImageEntity(this);explosions[1].load("explosion2.png");
//load the big asteroid images (5 total)
for (int n = 0; n<5; n++) {
bigAsteroids[n] = new ImageEntity(this);
String fn = "asteroid" + (n+1) + ".png";
bigAsteroids[n].load(fn);
}
//load the medium asteroid images (2 total)
for (int n = 0; n<2; n++) {
medAsteroids[n] = new ImageEntity(this);
String fn = "medium" + (n+1) + ".png";
medAsteroids[n].load(fn);
}//load the small asteroid images (3 total)
for (int n = 0; n<3; n++) {
smlAsteroids[n] = new ImageEntity(this);
String fn = "small" + (n+1) + ".png";
smlAsteroids[n].load(fn);
}
//load the tiny asteroid images (4 total)
for (int n = 0; n<4; n++) {
tnyAsteroids[n] = new ImageEntity(this);
String fn = "tiny" + (n+1) + ".png";
tnyAsteroids[n].load(fn);
}
//create...
Regístrate para leer el documento completo.