CCB Premier Import
This commit is contained in:
37
01-SOURCES/Asteroid.java
Normal file
37
01-SOURCES/Asteroid.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*****************************************************
|
||||
* Beginning Java Game Programming, 2nd Edition
|
||||
* by Jonathan S. Harbour
|
||||
* Asteroid class - For polygonal asteroid shapes
|
||||
*****************************************************/
|
||||
|
||||
import java.awt.Polygon;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
/*********************************************************
|
||||
* Asteroid class derives from BaseVectorShape
|
||||
**********************************************************/
|
||||
public class Asteroid extends BaseVectorShape {
|
||||
//define the asteroid polygon shape
|
||||
private int[] astx = {-20,-13, 0,20,22, 20, 12, 2,-10,-22,-16};
|
||||
private int[] asty = { 20, 23,17,20,16,-20,-22,-14,-17,-20, -5};
|
||||
|
||||
//rotation speed
|
||||
protected double rotVel;
|
||||
public double getRotationVelocity() { return rotVel; }
|
||||
public void setRotationVelocity(double v) { rotVel = v; }
|
||||
|
||||
//bounding rectangle
|
||||
public Rectangle getBounds() {
|
||||
Rectangle r;
|
||||
r = new Rectangle((int)getX() - 20, (int) getY() - 20, 40, 40);
|
||||
return r;
|
||||
}
|
||||
|
||||
//default constructor
|
||||
Asteroid() {
|
||||
setShape(new Polygon(astx, asty, astx.length));
|
||||
setAlive(true);
|
||||
setRotationVelocity(0.0);
|
||||
}
|
||||
|
||||
}
|
||||
440
01-SOURCES/Asteroids.java
Normal file
440
01-SOURCES/Asteroids.java
Normal file
@@ -0,0 +1,440 @@
|
||||
/*****************************************************
|
||||
* Beginning Java Game Programming, 2nd Edition
|
||||
* by Jonathan S. Harbour
|
||||
* Chapter 3 - ASTEROIDS GAME
|
||||
*****************************************************/
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.geom.*;
|
||||
import java.awt.image.*;
|
||||
import java.util.*;
|
||||
|
||||
/*****************************************************
|
||||
* Primary class for the game inherits from Applet
|
||||
*****************************************************/
|
||||
public class Asteroids extends Applet implements Runnable, KeyListener {
|
||||
|
||||
//the main thread becomes the game loop
|
||||
Thread gameloop;
|
||||
|
||||
//use this as a double buffer
|
||||
BufferedImage backbuffer;
|
||||
|
||||
//the main drawing object for the back buffer
|
||||
Graphics2D g2d;
|
||||
|
||||
//toggle for drawing bounding boxes
|
||||
boolean showBounds = false;
|
||||
|
||||
//create the asteroid array
|
||||
int ASTEROIDS = 20;
|
||||
Asteroid[] ast = new Asteroid[ASTEROIDS];
|
||||
|
||||
//create the bullet array
|
||||
int BULLETS = 10;
|
||||
Bullet[] bullet = new Bullet[BULLETS];
|
||||
int currentBullet = 0;
|
||||
|
||||
//the player's ship
|
||||
Ship ship = new Ship();
|
||||
|
||||
//create the identity transform (0,0)
|
||||
AffineTransform identity = new AffineTransform();
|
||||
|
||||
//create a random number generator
|
||||
Random rand = new Random();
|
||||
|
||||
/*****************************************************
|
||||
* applet init event
|
||||
*****************************************************/
|
||||
public void init() {
|
||||
//create the back buffer for smooth graphics
|
||||
backbuffer = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
|
||||
g2d = backbuffer.createGraphics();
|
||||
|
||||
//set up the ship
|
||||
ship.setX(320);
|
||||
ship.setY(240);
|
||||
|
||||
//set up the bullets
|
||||
for (int n = 0; n<BULLETS; n++) {
|
||||
bullet[n] = new Bullet();
|
||||
}
|
||||
|
||||
//create the asteroids
|
||||
for (int n = 0; n<ASTEROIDS; n++) {
|
||||
ast[n] = new Asteroid();
|
||||
ast[n].setRotationVelocity(rand.nextInt(3)+1);
|
||||
ast[n].setX((double)rand.nextInt(600)+20);
|
||||
ast[n].setY((double)rand.nextInt(440)+20);
|
||||
ast[n].setMoveAngle(rand.nextInt(360));
|
||||
double ang = ast[n].getMoveAngle() - 90;
|
||||
ast[n].setVelX(calcAngleMoveX(ang));
|
||||
ast[n].setVelY(calcAngleMoveY(ang));
|
||||
}
|
||||
|
||||
//start the user input listener
|
||||
addKeyListener(this);
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* applet update event to redraw the screen
|
||||
*****************************************************/
|
||||
public void update(Graphics g) {
|
||||
//start off transforms at identity
|
||||
g2d.setTransform(identity);
|
||||
|
||||
//erase the background
|
||||
g2d.setPaint(Color.BLACK);
|
||||
g2d.fillRect(0, 0, getSize().width, getSize().height);
|
||||
|
||||
//print some status information
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawString("Ship: " + Math.round(ship.getX()) + "," +
|
||||
Math.round(ship.getY()) , 5, 10);
|
||||
g2d.drawString("Move angle: " + Math.round(
|
||||
ship.getMoveAngle())+90, 5, 25);
|
||||
g2d.drawString("Face angle: " + Math.round(
|
||||
ship.getFaceAngle()), 5, 40);
|
||||
|
||||
//draw the game graphics
|
||||
drawShip();
|
||||
drawBullets();
|
||||
drawAsteroids();
|
||||
|
||||
//repaint the applet window
|
||||
paint(g);
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* drawShip called by applet update event
|
||||
*****************************************************/
|
||||
public void drawShip() {
|
||||
g2d.setTransform(identity);
|
||||
g2d.translate(ship.getX(), ship.getY());
|
||||
g2d.rotate(Math.toRadians(ship.getFaceAngle()));
|
||||
g2d.setColor(Color.ORANGE);
|
||||
g2d.fill(ship.getShape());
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* drawBullets called by applet update event
|
||||
*****************************************************/
|
||||
public void drawBullets() {
|
||||
|
||||
//iterate through the array of bullets
|
||||
for (int n = 0; n < BULLETS; n++) {
|
||||
|
||||
//is this bullet currently in use?
|
||||
if (bullet[n].isAlive()) {
|
||||
|
||||
//draw the bullet
|
||||
g2d.setTransform(identity);
|
||||
g2d.translate(bullet[n].getX(), bullet[n].getY());
|
||||
g2d.setColor(Color.MAGENTA);
|
||||
g2d.draw(bullet[n].getShape());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* drawAsteroids called by applet update event
|
||||
*****************************************************/
|
||||
public void drawAsteroids() {
|
||||
|
||||
//iterate through the asteroids array
|
||||
for (int n = 0; n < ASTEROIDS; n++) {
|
||||
|
||||
//is this asteroid being used?
|
||||
if (ast[n].isAlive()) {
|
||||
|
||||
//draw the asteroid
|
||||
g2d.setTransform(identity);
|
||||
g2d.translate(ast[n].getX(), ast[n].getY());
|
||||
g2d.rotate(Math.toRadians(ast[n].getMoveAngle()));
|
||||
g2d.setColor(Color.DARK_GRAY);
|
||||
g2d.fill(ast[n].getShape());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* applet window repaint event--draw the back buffer
|
||||
*****************************************************/
|
||||
public void paint(Graphics g) {
|
||||
|
||||
//draw the back buffer onto the applet window
|
||||
g.drawImage(backbuffer, 0, 0, this);
|
||||
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* thread start event - start the game loop running
|
||||
*****************************************************/
|
||||
public void start() {
|
||||
|
||||
//create the gameloop thread for real-time updates
|
||||
gameloop = new Thread(this);
|
||||
gameloop.start();
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* thread run event (game loop)
|
||||
*****************************************************/
|
||||
public void run() {
|
||||
|
||||
//acquire the current thread
|
||||
Thread t = Thread.currentThread();
|
||||
|
||||
//keep going as long as the thread is alive
|
||||
while (t == gameloop) {
|
||||
|
||||
try {
|
||||
//update the game loop
|
||||
gameUpdate();
|
||||
|
||||
//target framerate is 50 fps
|
||||
Thread.sleep(20);
|
||||
}
|
||||
catch(InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
/*****************************************************
|
||||
* thread stop event
|
||||
*****************************************************/
|
||||
public void stop() {
|
||||
|
||||
//kill the gameloop thread
|
||||
gameloop = null;
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* move and animate the objects in the game
|
||||
*****************************************************/
|
||||
private void gameUpdate() {
|
||||
updateShip();
|
||||
updateBullets();
|
||||
updateAsteroids();
|
||||
checkCollisions();
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* Update the ship position based on velocity
|
||||
*****************************************************/
|
||||
public void updateShip() {
|
||||
|
||||
//update ship's X position
|
||||
ship.incX(ship.getVelX());
|
||||
|
||||
//wrap around left/right
|
||||
if (ship.getX() < -10)
|
||||
ship.setX(getSize().width + 10);
|
||||
else if (ship.getX() > getSize().width + 10)
|
||||
ship.setX(-10);
|
||||
|
||||
//update ship's Y position
|
||||
ship.incY(ship.getVelY());
|
||||
|
||||
//wrap around top/bottom
|
||||
if (ship.getY() < -10)
|
||||
ship.setY(getSize().height + 10);
|
||||
else if (ship.getY() > getSize().height + 10)
|
||||
ship.setY(-10);
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* Update the bullets based on velocity
|
||||
*****************************************************/
|
||||
public void updateBullets() {
|
||||
|
||||
//move each of the bullets
|
||||
for (int n = 0; n < BULLETS; n++) {
|
||||
|
||||
//is this bullet being used?
|
||||
if (bullet[n].isAlive()) {
|
||||
|
||||
//update bullet's x position
|
||||
bullet[n].incX(bullet[n].getVelX());
|
||||
|
||||
//bullet disappears at left/right edge
|
||||
if (bullet[n].getX() < 0 ||
|
||||
bullet[n].getX() > getSize().width)
|
||||
{
|
||||
bullet[n].setAlive(false);
|
||||
}
|
||||
|
||||
//update bullet's y position
|
||||
bullet[n].incY(bullet[n].getVelY());
|
||||
|
||||
//bullet disappears at top/bottom edge
|
||||
if (bullet[n].getY() < 0 ||
|
||||
bullet[n].getY() > getSize().height)
|
||||
{
|
||||
bullet[n].setAlive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* Update the asteroids based on velocity
|
||||
*****************************************************/
|
||||
public void updateAsteroids() {
|
||||
|
||||
//move and rotate the asteroids
|
||||
for (int n = 0; n < ASTEROIDS; n++) {
|
||||
|
||||
//is this asteroid being used?
|
||||
if (ast[n].isAlive()) {
|
||||
|
||||
//update the asteroid's X value
|
||||
ast[n].incX(ast[n].getVelX());
|
||||
|
||||
//warp the asteroid at screen edges
|
||||
if (ast[n].getX() < -20)
|
||||
ast[n].setX(getSize().width + 20);
|
||||
else if (ast[n].getX() > getSize().width + 20)
|
||||
ast[n].setX(-20);
|
||||
|
||||
//update the asteroid's Y value
|
||||
ast[n].incY(ast[n].getVelY());
|
||||
|
||||
//warp the asteroid at screen edges
|
||||
if (ast[n].getY() < -20)
|
||||
ast[n].setY(getSize().height + 20);
|
||||
else if (ast[n].getY() > getSize().height + 20)
|
||||
ast[n].setY(-20);
|
||||
|
||||
//update the asteroid's rotation
|
||||
ast[n].incMoveAngle(ast[n].getRotationVelocity());
|
||||
|
||||
//keep the angle within 0-359 degrees
|
||||
if (ast[n].getMoveAngle() < 0)
|
||||
ast[n].setMoveAngle(360 - ast[n].getRotationVelocity());
|
||||
else if (ast[n].getMoveAngle() > 359)
|
||||
ast[n].setMoveAngle(ast[n].getRotationVelocity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* Test asteroids for collisions with ship or bullets
|
||||
*****************************************************/
|
||||
public void checkCollisions() {
|
||||
|
||||
//iterate through the asteroids array
|
||||
for (int m = 0; m<ASTEROIDS; m++) {
|
||||
|
||||
//is this asteroid being used?
|
||||
if (ast[m].isAlive()) {
|
||||
|
||||
/*
|
||||
* check for collision with bullet
|
||||
*/
|
||||
for (int n = 0; n < BULLETS; n++) {
|
||||
|
||||
//is this bullet being used?
|
||||
if (bullet[n].isAlive()) {
|
||||
|
||||
//perform the collision test
|
||||
if (ast[m].getBounds().contains(
|
||||
bullet[n].getX(), bullet[n].getY()))
|
||||
{
|
||||
bullet[n].setAlive(false);
|
||||
ast[m].setAlive(false);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check for collision with ship
|
||||
*/
|
||||
if (ast[m].getBounds().intersects(ship.getBounds())) {
|
||||
ast[m].setAlive(false);
|
||||
ship.setX(320);
|
||||
ship.setY(240);
|
||||
ship.setFaceAngle(0);
|
||||
ship.setVelX(0);
|
||||
ship.setVelY(0);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* key listener events
|
||||
*****************************************************/
|
||||
public void keyReleased(KeyEvent k) { }
|
||||
public void keyTyped(KeyEvent k) { }
|
||||
public void keyPressed(KeyEvent k) {
|
||||
int keyCode = k.getKeyCode();
|
||||
|
||||
switch (keyCode) {
|
||||
|
||||
case KeyEvent.VK_LEFT:
|
||||
//left arrow rotates ship left 5 degrees
|
||||
ship.incFaceAngle(-5);
|
||||
if (ship.getFaceAngle() < 0) ship.setFaceAngle(360-5);
|
||||
break;
|
||||
|
||||
case KeyEvent.VK_RIGHT:
|
||||
//right arrow rotates ship right 5 degrees
|
||||
ship.incFaceAngle(5);
|
||||
if (ship.getFaceAngle() > 360) ship.setFaceAngle(5);
|
||||
break;
|
||||
|
||||
case KeyEvent.VK_UP:
|
||||
//up arrow adds thrust to ship (1/10 normal speed)
|
||||
ship.setMoveAngle(ship.getFaceAngle() - 90);
|
||||
ship.incVelX(calcAngleMoveX(ship.getMoveAngle()) * 0.1);
|
||||
ship.incVelY(calcAngleMoveY(ship.getMoveAngle()) * 0.1);
|
||||
break;
|
||||
|
||||
//Ctrl, Enter, or Space can be used to fire weapon
|
||||
case KeyEvent.VK_CONTROL:
|
||||
case KeyEvent.VK_ENTER:
|
||||
case KeyEvent.VK_SPACE:
|
||||
//fire a bullet
|
||||
currentBullet++;
|
||||
if (currentBullet > BULLETS - 1) currentBullet = 0;
|
||||
bullet[currentBullet].setAlive(true);
|
||||
|
||||
//point bullet in same direction ship is facing
|
||||
bullet[currentBullet].setX(ship.getX());
|
||||
bullet[currentBullet].setY(ship.getY());
|
||||
bullet[currentBullet].setMoveAngle(ship.getFaceAngle() - 90);
|
||||
|
||||
//fire bullet at angle of the ship
|
||||
double angle = bullet[currentBullet].getMoveAngle();
|
||||
double svx = ship.getVelX();
|
||||
double svy = ship.getVelY();
|
||||
bullet[currentBullet].setVelX(svx + calcAngleMoveX(angle) * 2);
|
||||
bullet[currentBullet].setVelY(svy + calcAngleMoveY(angle) * 2);
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* calculate X movement value based on direction angle
|
||||
*****************************************************/
|
||||
public double calcAngleMoveX(double angle) {
|
||||
return (double) (Math.cos(angle * Math.PI / 180));
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* calculate Y movement value based on direction angle
|
||||
*****************************************************/
|
||||
public double calcAngleMoveY(double angle) {
|
||||
return (double) (Math.sin(angle * Math.PI / 180));
|
||||
}
|
||||
|
||||
}
|
||||
422
01-SOURCES/AsteroidsACompleter.java
Normal file
422
01-SOURCES/AsteroidsACompleter.java
Normal file
@@ -0,0 +1,422 @@
|
||||
/*****************************************************
|
||||
* Beginning Java Game Programming, 2nd Edition
|
||||
* by Jonathan S. Harbour
|
||||
* Chapter 3 - ASTEROIDS GAME
|
||||
*****************************************************/
|
||||
|
||||
import java.applet.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.awt.geom.*;
|
||||
import java.awt.image.*;
|
||||
import java.util.*;
|
||||
|
||||
/*****************************************************
|
||||
* Primary class for the game inherits from Applet
|
||||
*****************************************************/
|
||||
public class AsteroidsACompleter extends Applet implements Runnable, KeyListener {
|
||||
|
||||
//the main thread becomes the game loop
|
||||
Thread gameloop;
|
||||
|
||||
//use this as a double buffer
|
||||
BufferedImage backbuffer;
|
||||
|
||||
//the main drawing object for the back buffer
|
||||
Graphics2D g2d;
|
||||
|
||||
//toggle for drawing bounding boxes
|
||||
boolean showBounds = false;
|
||||
|
||||
//create the asteroid array
|
||||
int ASTEROIDS = 20;
|
||||
Asteroid[] ast = new Asteroid[ASTEROIDS];
|
||||
|
||||
//create the bullet array
|
||||
int BULLETS = 10;
|
||||
Bullet[] bullet = new Bullet[BULLETS];
|
||||
int currentBullet = 0;
|
||||
|
||||
//the player's ship
|
||||
Ship ship = new Ship();
|
||||
|
||||
//create the identity transform (0,0)
|
||||
AffineTransform identity = new AffineTransform();
|
||||
|
||||
//create a random number generator
|
||||
Random rand = new Random();
|
||||
|
||||
/*****************************************************
|
||||
* applet init event
|
||||
*****************************************************/
|
||||
public void init() {
|
||||
//create the back buffer for smooth graphics
|
||||
backbuffer = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
|
||||
g2d = backbuffer.createGraphics();
|
||||
|
||||
//set up the ship
|
||||
ship.setX(320);
|
||||
ship.setY(240);
|
||||
|
||||
//set up the bullets
|
||||
for (int n = 0; n<BULLETS; n++) {
|
||||
bullet[n] = new Bullet();
|
||||
}
|
||||
|
||||
//create the asteroids
|
||||
for (int n = 0; n<ASTEROIDS; n++) {
|
||||
ast[n] = new Asteroid();
|
||||
ast[n].setRotationVelocity(rand.nextInt(3)+1);
|
||||
ast[n].setX((double)rand.nextInt(600)+20);
|
||||
ast[n].setY((double)rand.nextInt(440)+20);
|
||||
ast[n].setMoveAngle(rand.nextInt(360));
|
||||
double ang = ast[n].getMoveAngle() - 90;
|
||||
ast[n].setVelX(calcAngleMoveX(ang));
|
||||
ast[n].setVelY(calcAngleMoveY(ang));
|
||||
}
|
||||
|
||||
//start the user input listener
|
||||
addKeyListener(this);
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* applet update event to redraw the screen
|
||||
*****************************************************/
|
||||
public void update(Graphics g) {
|
||||
//start off transforms at identity
|
||||
g2d.setTransform(identity);
|
||||
|
||||
//erase the background
|
||||
g2d.setPaint(Color.BLACK);
|
||||
g2d.fillRect(0, 0, getSize().width, getSize().height);
|
||||
|
||||
//print some status information
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.drawString("Ship: " + Math.round(ship.getX()) + "," +
|
||||
Math.round(ship.getY()) , 5, 10);
|
||||
g2d.drawString("Move angle: " + Math.round(
|
||||
ship.getMoveAngle())+90, 5, 25);
|
||||
g2d.drawString("Face angle: " + Math.round(
|
||||
ship.getFaceAngle()), 5, 40);
|
||||
|
||||
//draw the game graphics
|
||||
drawShip();
|
||||
drawBullets();
|
||||
drawAsteroids();
|
||||
|
||||
//repaint the applet window
|
||||
paint(g);
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* drawShip called by applet update event
|
||||
*****************************************************/
|
||||
public void drawShip() {
|
||||
g2d.setTransform(identity);
|
||||
g2d.translate(ship.getX(), ship.getY());
|
||||
g2d.rotate(Math.toRadians(ship.getFaceAngle()));
|
||||
g2d.setColor(Color.ORANGE);
|
||||
g2d.fill(ship.getShape());
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* drawBullets called by applet update event
|
||||
*****************************************************/
|
||||
public void drawBullets() {
|
||||
|
||||
//iterate through the array of bullets
|
||||
for (int n = 0; n < BULLETS; n++) {
|
||||
|
||||
//is this bullet currently in use?
|
||||
if (bullet[n].isAlive()) {
|
||||
|
||||
//draw the bullet
|
||||
g2d.setTransform(identity);
|
||||
g2d.translate(bullet[n].getX(), bullet[n].getY());
|
||||
g2d.setColor(Color.MAGENTA);
|
||||
g2d.draw(bullet[n].getShape());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* drawAsteroids called by applet update event
|
||||
*****************************************************/
|
||||
public void drawAsteroids() {
|
||||
|
||||
//iterate through the asteroids array
|
||||
for (int n = 0; n < ASTEROIDS; n++) {
|
||||
|
||||
//is this asteroid being used?
|
||||
if (ast[n].isAlive()) {
|
||||
|
||||
//draw the asteroid
|
||||
g2d.setTransform(identity);
|
||||
g2d.translate(ast[n].getX(), ast[n].getY());
|
||||
g2d.rotate(Math.toRadians(ast[n].getMoveAngle()));
|
||||
g2d.setColor(Color.DARK_GRAY);
|
||||
g2d.fill(ast[n].getShape());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* applet window repaint event--draw the back buffer
|
||||
*****************************************************/
|
||||
public void paint(Graphics g) {
|
||||
|
||||
//draw the back buffer onto the applet window
|
||||
g.drawImage(backbuffer, 0, 0, this);
|
||||
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* thread start event - start the game loop running
|
||||
*****************************************************/
|
||||
public void start() {
|
||||
|
||||
//create the gameloop thread for real-time updates
|
||||
gameloop = new Thread(this);
|
||||
gameloop.start();
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* thread run event (game loop)
|
||||
*****************************************************/
|
||||
public void run() {
|
||||
|
||||
//acquire the current thread
|
||||
Thread t = Thread.currentThread();
|
||||
|
||||
//keep going as long as the thread is alive
|
||||
while (t == gameloop) {
|
||||
|
||||
try {
|
||||
//update the game loop
|
||||
gameUpdate();
|
||||
|
||||
//target framerate is 50 fps
|
||||
Thread.sleep(20);
|
||||
}
|
||||
catch(InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
/*****************************************************
|
||||
* thread stop event
|
||||
*****************************************************/
|
||||
public void stop() {
|
||||
|
||||
//kill the gameloop thread
|
||||
gameloop = null;
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* move and animate the objects in the game
|
||||
*****************************************************/
|
||||
private void gameUpdate() {
|
||||
updateShip();
|
||||
updateBullets();
|
||||
updateAsteroids();
|
||||
checkCollisions();
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* Update the ship position based on velocity
|
||||
*****************************************************/
|
||||
public void updateShip() {
|
||||
|
||||
//update ship's X position
|
||||
ship.incX(ship.getVelX());
|
||||
|
||||
//wrap around left/right
|
||||
if (ship.getX() < -10)
|
||||
ship.setX(getSize().width + 10);
|
||||
else if (ship.getX() > getSize().width + 10)
|
||||
ship.setX(-10);
|
||||
|
||||
//update ship's Y position
|
||||
ship.incY(ship.getVelY());
|
||||
|
||||
//wrap around top/bottom
|
||||
if (ship.getY() < -10)
|
||||
ship.setY(getSize().height + 10);
|
||||
else if (ship.getY() > getSize().height + 10)
|
||||
ship.setY(-10);
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* Update the bullets based on velocity
|
||||
*****************************************************/
|
||||
public void updateBullets() {
|
||||
|
||||
//move each of the bullets
|
||||
for (int n = 0; n < BULLETS; n++) {
|
||||
|
||||
//is this bullet being used?
|
||||
if (bullet[n].isAlive()) {
|
||||
|
||||
//update bullet's x position
|
||||
bullet[n].incX(bullet[n].getVelX());
|
||||
|
||||
//bullet disappears at left/right edge
|
||||
if (bullet[n].getX() < 0 ||
|
||||
bullet[n].getX() > getSize().width)
|
||||
{
|
||||
bullet[n].setAlive(false);
|
||||
}
|
||||
|
||||
//update bullet's y position
|
||||
bullet[n].incY(bullet[n].getVelY());
|
||||
|
||||
//bullet disappears at top/bottom edge
|
||||
if (bullet[n].getY() < 0 ||
|
||||
bullet[n].getY() > getSize().height)
|
||||
{
|
||||
bullet[n].setAlive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* Update the asteroids based on velocity
|
||||
*****************************************************/
|
||||
public void updateAsteroids() {
|
||||
|
||||
//move and rotate the asteroids
|
||||
for (int n = 0; n < ASTEROIDS; n++) {
|
||||
|
||||
//is this asteroid being used?
|
||||
if (ast[n].isAlive()) {
|
||||
|
||||
//update the asteroid's X value
|
||||
ast[n].incX(ast[n].getVelX());
|
||||
|
||||
//warp the asteroid at screen edges
|
||||
if (ast[n].getX() < -20)
|
||||
ast[n].setX(getSize().width + 20);
|
||||
else if (ast[n].getX() > getSize().width + 20)
|
||||
ast[n].setX(-20);
|
||||
|
||||
//update the asteroid's Y value
|
||||
ast[n].incY(ast[n].getVelY());
|
||||
|
||||
//warp the asteroid at screen edges
|
||||
if (ast[n].getY() < -20)
|
||||
ast[n].setY(getSize().height + 20);
|
||||
else if (ast[n].getY() > getSize().height + 20)
|
||||
ast[n].setY(-20);
|
||||
|
||||
//update the asteroid's rotation
|
||||
ast[n].incMoveAngle(ast[n].getRotationVelocity());
|
||||
|
||||
//keep the angle within 0-359 degrees
|
||||
if (ast[n].getMoveAngle() < 0)
|
||||
ast[n].setMoveAngle(360 - ast[n].getRotationVelocity());
|
||||
else if (ast[n].getMoveAngle() > 359)
|
||||
ast[n].setMoveAngle(ast[n].getRotationVelocity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* Test asteroids for collisions with ship or bullets
|
||||
*****************************************************/
|
||||
public void checkCollisions() {
|
||||
|
||||
//iterate through the asteroids array
|
||||
for (int m = 0; m<ASTEROIDS; m++) {
|
||||
|
||||
//is this asteroid being used?
|
||||
if (ast[m].isAlive()) {
|
||||
|
||||
/*
|
||||
* check for collision with bullet
|
||||
*/
|
||||
for (int n = 0; n < BULLETS; n++) {
|
||||
|
||||
//is this bullet being used?
|
||||
if (bullet[n].isAlive()) {
|
||||
|
||||
//perform the collision test
|
||||
if (ast[m].getBounds().contains(
|
||||
bullet[n].getX(), bullet[n].getY()))
|
||||
{
|
||||
bullet[n].setAlive(false);
|
||||
ast[m].setAlive(false);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* check for collision with ship
|
||||
*/
|
||||
if (ast[m].getBounds().intersects(ship.getBounds())) {
|
||||
ast[m].setAlive(false);
|
||||
ship.setX(320);
|
||||
ship.setY(240);
|
||||
ship.setFaceAngle(0);
|
||||
ship.setVelX(0);
|
||||
ship.setVelY(0);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* key listener events
|
||||
*****************************************************/
|
||||
public void keyReleased(KeyEvent k) { }
|
||||
public void keyTyped(KeyEvent k) { }
|
||||
public void keyPressed(KeyEvent k) {
|
||||
int keyCode = k.getKeyCode();
|
||||
|
||||
switch (keyCode) {
|
||||
|
||||
case KeyEvent.VK_LEFT:
|
||||
//left arrow rotates ship left 5 degrees
|
||||
break;
|
||||
|
||||
case KeyEvent.VK_RIGHT:
|
||||
//right arrow rotates ship right 5 degrees
|
||||
break;
|
||||
|
||||
case KeyEvent.VK_UP:
|
||||
//up arrow adds thrust to ship (1/10 normal speed)
|
||||
break;
|
||||
|
||||
//Ctrl, Enter, or Space can be used to fire weapon
|
||||
case KeyEvent.VK_CONTROL:
|
||||
case KeyEvent.VK_ENTER:
|
||||
case KeyEvent.VK_SPACE:
|
||||
//fire a bullet
|
||||
|
||||
//point bullet in same direction ship is facing
|
||||
|
||||
//fire bullet at angle of the ship
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* calculate X movement value based on direction angle
|
||||
*****************************************************/
|
||||
public double calcAngleMoveX(double angle) {
|
||||
return (double) (Math.cos(angle * Math.PI / 180));
|
||||
}
|
||||
|
||||
/*****************************************************
|
||||
* calculate Y movement value based on direction angle
|
||||
*****************************************************/
|
||||
public double calcAngleMoveY(double angle) {
|
||||
return (double) (Math.sin(angle * Math.PI / 180));
|
||||
}
|
||||
|
||||
}
|
||||
54
01-SOURCES/BaseVectorShape.java
Normal file
54
01-SOURCES/BaseVectorShape.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/*****************************************************
|
||||
* Beginning Java Game Programming, 2nd Edition
|
||||
* by Jonathan S. Harbour
|
||||
* Base vector shape class for polygonal shapes
|
||||
*****************************************************/
|
||||
|
||||
import java.awt.Shape;
|
||||
|
||||
public class BaseVectorShape {
|
||||
//variables
|
||||
private Shape shape;
|
||||
private boolean alive;
|
||||
private double x,y;
|
||||
private double velX, velY;
|
||||
private double moveAngle, faceAngle;
|
||||
|
||||
//accessor methods
|
||||
public Shape getShape() { return shape; }
|
||||
public boolean isAlive() { return alive; }
|
||||
public double getX() { return x; }
|
||||
public double getY() { return y; }
|
||||
public double getVelX() { return velX; }
|
||||
public double getVelY() { return velY; }
|
||||
public double getMoveAngle() { return moveAngle; }
|
||||
public double getFaceAngle() { return faceAngle; }
|
||||
|
||||
//mutator methods
|
||||
public void setShape(Shape shape) { this.shape = shape; }
|
||||
public void setAlive(boolean alive) { this.alive = alive; }
|
||||
public void setX(double x) { this.x = x; }
|
||||
public void incX(double i) { this.x += i; }
|
||||
public void setY(double y) { this.y = y; }
|
||||
public void incY(double i) { this.y += i; }
|
||||
public void setVelX(double velX) { this.velX = velX; }
|
||||
public void incVelX(double i) { this.velX += i; }
|
||||
public void setVelY(double velY) { this.velY = velY; }
|
||||
public void incVelY(double i) { this.velY += i; }
|
||||
public void setFaceAngle(double angle) { this.faceAngle = angle; }
|
||||
public void incFaceAngle(double i) { this.faceAngle += i; }
|
||||
public void setMoveAngle(double angle) { this.moveAngle = angle; }
|
||||
public void incMoveAngle(double i) { this.moveAngle += i; }
|
||||
|
||||
//default constructor
|
||||
BaseVectorShape() {
|
||||
setShape(null);
|
||||
setAlive(false);
|
||||
setX(0.0);
|
||||
setY(0.0);
|
||||
setVelX(0.0);
|
||||
setVelY(0.0);
|
||||
setMoveAngle(0.0);
|
||||
setFaceAngle(0.0);
|
||||
}
|
||||
}
|
||||
27
01-SOURCES/Bullet.java
Normal file
27
01-SOURCES/Bullet.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*****************************************************
|
||||
* Beginning Java Game Programming, 2nd Edition
|
||||
* by Jonathan S. Harbour
|
||||
* Bullet class - polygonal shape of a bullet
|
||||
*****************************************************/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
/*********************************************************
|
||||
* Bullet class derives from BaseVectorShape
|
||||
**********************************************************/
|
||||
public class Bullet extends BaseVectorShape {
|
||||
|
||||
//bounding rectangle
|
||||
public Rectangle getBounds() {
|
||||
Rectangle r;
|
||||
r = new Rectangle((int)getX(), (int) getY(), 1, 1);
|
||||
return r;
|
||||
}
|
||||
|
||||
Bullet() {
|
||||
//create the bullet shape
|
||||
setShape(new Rectangle(0, 0, 1, 1));
|
||||
setAlive(false);
|
||||
}
|
||||
}
|
||||
9
01-SOURCES/Hello.java
Normal file
9
01-SOURCES/Hello.java
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
public class Hello {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
System.out.println ("Enchant<EFBFBD> de faire votre connaissance");
|
||||
}
|
||||
|
||||
}
|
||||
8
01-SOURCES/HelloWorld.java
Normal file
8
01-SOURCES/HelloWorld.java
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
public class HelloWorld {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Bonjour les DUT 1 AN !");
|
||||
}
|
||||
|
||||
}
|
||||
86
01-SOURCES/Methodes.java
Normal file
86
01-SOURCES/Methodes.java
Normal file
@@ -0,0 +1,86 @@
|
||||
|
||||
public class Methodes {
|
||||
|
||||
// L'ordre de declaration des methodes n'a pas d'importance
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Declarer un nombre variable d'arguments
|
||||
//------------------------------------------------------------------
|
||||
static int somme (int ... valeurs) {
|
||||
int s = 0 ;
|
||||
for (int i=0; i < valeurs.length ; i++)
|
||||
s += valeurs [i];
|
||||
return s ;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Procedure tentant d'echanger 2 parametres entiers
|
||||
// Resultat :
|
||||
//------------------------------------------------------------------
|
||||
static void echange (int a, int b) {
|
||||
int aux = b ;
|
||||
b = a ;
|
||||
a = aux ;
|
||||
System.out.println ("durant l'appel apres echange : a = " + a + ", b = " + b);
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Procedure tentant de modifier la case d'un tableau passe en parametre
|
||||
// Resultat :
|
||||
//------------------------------------------------------------------
|
||||
static void modifierTab (int t[]) {
|
||||
t[0] = 9 ;
|
||||
}
|
||||
|
||||
static void affiche (int a) {
|
||||
System.out.println ("Entree dans affiche : a = " + a);
|
||||
a = 20 ;
|
||||
System.out.println ("Avant de sortir de affiche : a = " + a);
|
||||
}
|
||||
//------------------------------------------------------------------
|
||||
// Programme principal
|
||||
//------------------------------------------------------------------
|
||||
public static void main(String[] args) {
|
||||
|
||||
int a ;
|
||||
a = 10 ;
|
||||
System.out.println ("Avant appel : a = " + a);
|
||||
affiche (a);
|
||||
System.out.println ("Apres appel : a = " + a);
|
||||
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Nombre variable de parametres
|
||||
//------------------------------------------------------------------
|
||||
System.out.println (somme (1, 2, 3));
|
||||
System.out.println (somme (2, 8));
|
||||
System.out.println (somme ());
|
||||
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Appel de la procedure tentant d'echanger 2 parametres entiers
|
||||
//------------------------------------------------------------------
|
||||
int i = 1 ;
|
||||
int j = 2 ;
|
||||
System.out.println ("avant appel echange : i = " + i + ", j = " + j);
|
||||
echange (i, j);
|
||||
System.out.println ("apres appel echange : i = " + i + ", j = " + j);
|
||||
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Appel de la procedure tentant de modifier la case d'un tableau passe en parametre
|
||||
//------------------------------------------------------------------
|
||||
int t[] = {1, 2, 3};
|
||||
|
||||
System.out.println ("\n Affichage avant appel : ");
|
||||
for (int valeur : t)
|
||||
System.out.println (valeur);
|
||||
|
||||
modifierTab (t);
|
||||
|
||||
System.out.println ("\n Affichage apres appel : ");
|
||||
for (int valeur : t)
|
||||
System.out.println (valeur);
|
||||
}
|
||||
}
|
||||
25
01-SOURCES/Ship.java
Normal file
25
01-SOURCES/Ship.java
Normal file
@@ -0,0 +1,25 @@
|
||||
/*****************************************************
|
||||
* Beginning Java Game Programming, 2nd Edition
|
||||
* by Jonathan S. Harbour
|
||||
* Ship class - polygonal shape of the player's ship
|
||||
*****************************************************/
|
||||
import java.awt.Polygon;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
public class Ship extends BaseVectorShape {
|
||||
//define the ship polygon
|
||||
private int[] shipx = { -6, -3, 0, 3, 6, 0 };
|
||||
private int[] shipy = { 6, 7, 7, 7, 6, -7 };
|
||||
|
||||
//bounding rectangle
|
||||
public Rectangle getBounds() {
|
||||
Rectangle r;
|
||||
r = new Rectangle((int)getX() - 6, (int) getY() - 6, 12,12);
|
||||
return r;
|
||||
}
|
||||
|
||||
Ship() {
|
||||
setShape(new Polygon(shipx, shipy, shipx.length));
|
||||
setAlive(true);
|
||||
}
|
||||
}
|
||||
79
01-SOURCES/TableauxEtBoucles.java
Normal file
79
01-SOURCES/TableauxEtBoucles.java
Normal file
@@ -0,0 +1,79 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public class TableauxEtBoucles {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//-----------------------------------------------------
|
||||
// Tableau de type de base
|
||||
//-----------------------------------------------------
|
||||
int t [] ; // DECLARATION d'une REFERENCE de type tableau
|
||||
t = new int [3]; // ALLOCATION d'une zone mŽmoire pour n entiers dans le TAS
|
||||
t[2] = 7; // AFFECTATION d'une valeur ˆ une case du tableau
|
||||
System.out.println(t[2]); // ACCES / LECTURE de la valeur d'une case du tableau et affichage
|
||||
|
||||
//-----------------------------------------------------
|
||||
// Parcours du tableau
|
||||
//-----------------------------------------------------
|
||||
|
||||
// Avec une boucle et un boolŽen
|
||||
boolean fini = false ;
|
||||
int i = 0 ;
|
||||
System.out.println ("\n Parcours du tableau avec un boolŽen et une boucle while : ");
|
||||
while (! fini) {
|
||||
System.out.println (t[i]);
|
||||
i++ ;
|
||||
fini = i == t.length ;
|
||||
}
|
||||
|
||||
// Avec une boucle foreach
|
||||
System.out.println ("\n Parcours du tableau avec une boucle foreach : ");
|
||||
for (int valeur : t)
|
||||
System.out.println (valeur);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Copie d'un tableau
|
||||
int [] t2 = t ; // RECOPIE JUSTE L'ADRESSE DU TABLEAU
|
||||
t2 [0] = 33 ;
|
||||
|
||||
|
||||
|
||||
// Affichage apr<70>s copie : ........................
|
||||
System.out.println ("\n Parcours du tableau t apr<70>s modification t2 = t ; t2[0] = 33 : ");
|
||||
for (int valeur : t)
|
||||
System.out.println (valeur);
|
||||
|
||||
// Initialiser un tableau lors de la dŽclaration
|
||||
double notes [] = {12, 8, 17.5, 2, 19};
|
||||
|
||||
// Affichage avant tri
|
||||
System.out.println ("\n Affichage avant tri : ");
|
||||
for (double valeur : notes)
|
||||
System.out.println (valeur);
|
||||
|
||||
// Trier un tableau
|
||||
Arrays.sort(notes);
|
||||
|
||||
// Affichage apr<70>s tri
|
||||
System.out.println ("\n Affichage apr<70>s tri : ");
|
||||
for (double valeur : notes)
|
||||
System.out.println (valeur);
|
||||
|
||||
//-----------------------------------------------------
|
||||
// Exceptions
|
||||
//-----------------------------------------------------
|
||||
|
||||
// DŽbordement du tableau
|
||||
System.out.println (t[2]);
|
||||
|
||||
// Oubli d'allouer le tableau
|
||||
int tab [];
|
||||
tab = null ;
|
||||
// ...
|
||||
tab[0] = 10 ;
|
||||
}
|
||||
}
|
||||
86
01-SOURCES/TypesDeBase.java
Normal file
86
01-SOURCES/TypesDeBase.java
Normal file
@@ -0,0 +1,86 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
public class TypesDeBase {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
//-----------------------------------------------------
|
||||
// D<>clarations de variables locales <20> la m<>thode main () : types de base qui existent en C
|
||||
//-----------------------------------------------------
|
||||
short s ; // taille : 2 octets
|
||||
int i ; // taille : 4 octets
|
||||
long l ; // taille : 8 octets
|
||||
|
||||
float f ; // taille : 4 octets
|
||||
double d ; // taille : 8 octets
|
||||
char c ; // format Unicode sur 2 octets
|
||||
|
||||
//-----------------------------------------------------
|
||||
// D<>clarations de types de base qui n<>existent pas en C
|
||||
//-----------------------------------------------------
|
||||
byte by ; // taille : 1 octet
|
||||
boolean b ;
|
||||
|
||||
//-----------------------------------------------------
|
||||
// Affectation de quelques valeurs
|
||||
//-----------------------------------------------------
|
||||
i = 0 ;
|
||||
b = true ;
|
||||
c = 'a' ;
|
||||
|
||||
//-----------------------------------------------------
|
||||
// Affichages
|
||||
//-----------------------------------------------------
|
||||
System.out.print (i); // AFFICHE 0.
|
||||
System.out.println (i); // AFFICHE 0 ET VA A LA LIGNE.....
|
||||
System.out.println ("i vaut " + i); // AFFICHE i vaut 0
|
||||
System.out.printf ("affichage format<61> : %3.2f", 100.678); // 100,68.
|
||||
System.out.println (); // ALLER A LA LIGNE
|
||||
|
||||
//-----------------------------------------------------
|
||||
// Lecture au clavier
|
||||
//-----------------------------------------------------
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Veuillez saisir un mot : ");
|
||||
String str = sc.nextLine();
|
||||
System.out.println("Vous avez saisi : " + str); // AFFICHE Vous avez saisi : au revoir
|
||||
|
||||
System.out.println("Veuillez saisir un entier : ");
|
||||
int j = sc.nextInt();
|
||||
System.out.println("Vous avez saisi le nombre : " + j); // Vous avez saisi le nombre : 20
|
||||
|
||||
//-----------------------------------------------------
|
||||
// D<>claration de constantes et initialisations
|
||||
//-----------------------------------------------------
|
||||
final int N = 20 ; //.....................
|
||||
|
||||
final int M ;
|
||||
System.out.println("Veuillez saisir un entier : ");
|
||||
M = sc.nextInt();
|
||||
// N = 10 ; => erreur de compilation
|
||||
|
||||
//-----------------------------------------------------
|
||||
// Conversions implicites
|
||||
//-----------------------------------------------------
|
||||
int k = 1000 ;
|
||||
long lo ;
|
||||
lo = k ;
|
||||
|
||||
//-----------------------------------------------------
|
||||
// Conversions explicites (cast)
|
||||
//-----------------------------------------------------
|
||||
i = (int) 2.5 ;
|
||||
System.out.println ("valeur de (int) 2.5 : " + i);
|
||||
|
||||
//-----------------------------------------------------
|
||||
// Gestion des erreurs en Java
|
||||
//-----------------------------------------------------
|
||||
|
||||
// Diviser par z<>ro
|
||||
i = i / 0 ;
|
||||
|
||||
// Saisir un caract<63>re alors qu'on attend un nombre
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user