|
TargetGame
TargetGame.java
//-----------------------------------------------------------------------------
// Class: TargetGame
// Author: Written by Chuck Cusack
// Date: September 2001
// Description: A class to display LSGun, LSClip, LSBullets, ATargets, and
// play a simple target shooting game.
//-----------------------------------------------------------------------------
import aLibrary.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent.*;
import javax.swing.*;
//-----------------------------------------------------------------------------
public class TargetGame extends AWindow {
//-----------------------------------------------------------------------------
// instance variables
//
//private AWindow this; // The window upon which everything is placed
private AClip theClip; // The clip
private LSGun theGun; // The gun
private ATarget target[]; // the targets
private int level; // the number of targets
private int score; // score
private int numberHits; // The number of targets hit
private GameTimer myTimer; // Timer for each level
private ALabel scoreDisplay; // To display the score
private ALabel timeDisplay; // To display time left
private ALabel levelDisplay; // To display the current level
private ALabel lose; // The You Lose message
private boolean playing; // Is the game being played?
private ResetButton RButton; // The reset button
//-----------------------------------------------------------------------------
// The contructor
// Creates the gun, clip, and targets.
//
public TargetGame() {
super(20,20,600,500);
init();
}
//-----------------------------------------------------------------------------
public void init() {
// Draw the gun
theGun=new LSGun(40,20,this);
theGun.place(this);
// Draw the clip
theClip=new LSClip(5,200);
theClip.place(this);
// Set the score, level, and number of hits so far.
level=1;
numberHits=0;
score=0;
playing=true;
//Set up labels
scoreDisplay=new ALabel(50,450,200,20);
scoreDisplay.setFontSize(18);
updateScore();
scoreDisplay.place(this);
levelDisplay=new ALabel(200,450,200,20);
levelDisplay.setFontSize(18);
levelDisplay.setText("Level= "+level);
levelDisplay.place(this);
timeDisplay=new ALabel(350,450,200,20);
timeDisplay.setFontSize(18);
timeDisplay.setText("Time Left= "+100);
timeDisplay.place(this);
lose=new ALabel(50,100,500,600);
lose.setFontSize(48);
lose.setText("");
lose.place(this);
// Start the first level
newLevel(1);
// Place the reset button
RButton=new ResetButton(10,450,this);
RButton.place(this);
// Make sure everything is drawn
this.repaint();
System.out.println("Done");
}
//-----------------------------------------------------------------------------
// key Event
// --- ---------------------
// a move the gun up
// z move the gun down
// f fire the gun
// b insert bullet into clip
// r remove bullet from clip
// l load clip into gun
// u unload clip from gun
//
public void keyTyped(KeyEvent k) {
char K=k.getKeyChar();
if(playing) {
if (K=='a' && theGun.getY()>20)
{ theGun.translate(0,-20); }
else if(K=='z' && theGun.getY()+120<this.getHeight())
{ theGun.translate(0,20); }
else if(K=='f')
{ theGun.fire(); }
else if(K=='b') {
ABullet b=new LSBullet(0,0);
theClip.insertBullet(b);
}
else if(K=='r' && !theClip.isInGun()) {
theClip.removeBullet();
}
else if(K=='l' && !theGun.hasClip()) {
theGun.insertClip(theClip);
this.repaint();
}
else if(K=='u' && theGun.hasClip()) {
theClip=theGun.removeClip();
theClip.setLocation(5,200);
theClip.place(this);
this.repaint();
}
else if(K=='p') {
myTimer.stop();
playing=false;
for(int i=0;i<level;i++) {
target[i].stop();
}
}
}
else if(K=='p') {
playing=true;
myTimer.start();
for(int i=0;i<level;i++) {
target[i].start();
}
}
}
//-----------------------------------------------------------------------------
public void reinit() {
// Remove the targets
for(int i=0;i<level;i++) {
target[i].stop();
target[i].remove();
}
// Place the gun where it started
theGun.setLocation(40,20);
theGun.place(this);
// Remove clip from gum, empty all bullets
if(theGun.hasClip()) {
theClip=theGun.removeClip();
theClip.setLocation(5,200);
theClip.place(this);
}
while(!theClip.isEmpty()) {
theClip.removeBullet();
}
theClip.place(this);
// Reset the level, score, etc.
level=1;
numberHits=0;
score=0;
playing=true;
updateScore();
levelDisplay.setText("Level= "+level);
timeDisplay.setText("Time Left= "+100);
lose.setText("");
RButton.place(this);
// Start the first level again
newLevel(1);
// Make sure everything is drawn
this.repaint();
}
//-----------------------------------------------------------------------------
// If you lose, get rid of the targets, stop the timers, etc,and
// draw a big message
//
public void youLose() {
playing=false;
myTimer.stop();
numberHits=-1; // So it won't go to next level
for(int i=0;i<level;i++) {
target[i].stop();
target[i].remove();
}
lose.setText("You Lose");
theGun.remove();
if(theGun.hasClip()) {
theGun.removeClip();
theClip.setLocation(5,200);
}
theClip.remove();
this.repaint();
}
//-----------------------------------------------------------------------------
// If you win (as if), get rid of the targets, stop the timers, etc,and
// draw a big message
//
public void youWin() {
ALabel win=new ALabel(50,100,500,600);
myTimer.stop();
numberHits=-1; // So it won't go to next level
for(int i=0;i<level;i++) {
target[i].stop();
target[i].remove();
}
win.setFontSize(48);
win.setText("You Win");
win.place(this);
//theGun.remove();
//theClip.remove();
this.repaint();
}
//-----------------------------------------------------------------------------
// Create a new level
//
public void newLevel(int num) {
if(num==21) {
youWin();
}
else {
Color choices[]={Color.red,Color.green,Color.blue,Color.yellow,
Color.orange,Color.cyan};
level=num;
numberHits=0;
target=new ATarget[level];
for(int i=0;i<level;i++) {
int height=(int)(Math.random()*(210-level*10))+2*(21-level);
int ylocation=(int)(Math.random()*(this.getHeight()-height));
target[i]=new ATarget(580-i*20,ylocation,10,height,level,this);
target[i].place(this);
}
if(myTimer!=null) {
myTimer.stop();
}
myTimer=new GameTimer(105-5*level,this);
myTimer.scheduleEvents(.5);
updateTime(105-5*level);
levelDisplay.setText("Level= "+level);
}
}
//-----------------------------------------------------------------------------
// return true if a bullet hits one of the targets.
//
public boolean checkForHit(ABullet B) {
boolean hit=false;
for(int i=0;i<level;i++) {
boolean didHit=target[i].isHit(B);
if(didHit) {
targetHit(target[i]);
numberHits++;
}
hit=hit || didHit;
}
if(numberHits==level) {
newLevel(level+1);
}
return hit;
}
//-----------------------------------------------------------------------------
public void updateTime(int t) {
timeDisplay.setText("TimeLeft= "+t);
if(t==0) {
youLose();
}
this.repaint();
}
//-----------------------------------------------------------------------------
// Redisplay the updated score
//
public void updateScore() {
scoreDisplay.setText("Score= "+score);
this.repaint();
}
//-----------------------------------------------------------------------------
// Change score if bullet is fired
//
public void bulletFired() {
score=score-level*10;
updateScore();
}
//-----------------------------------------------------------------------------
// Update score if target is hit
//
public void targetHit(ATarget t) {
score=score+t.getValue();
updateScore();
}
//-----------------------------------------------------------------------------
}
|