Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Alice

TargetGame


Description

Run it
Download jar file (Right click)

ABullet.java
AClip.java
AClipGun.java
ATarget.java
AppletStarter.java
BulletTimer.java
GameTimer.java
LSBullet.java
LSClip.java
LSGun.java
ResetButton.java
TargetGame.java
TargetTimer.java

AClip.java

//-----------------------------------------------------------------------------
// Class: 	AClip
// Author:	Written by Chuck Cusack
// Date: 	September 2001
// Description: A class to represent a clip. No graphics are implemented.
//	A class needs to extend this class to implement the graphics.
//	The clip can accept ABullets, and be placed in an AClipGun.
//-----------------------------------------------------------------------------
import java.awt.*;
import aLibrary.*;
import java.awt.event.*;
//-----------------------------------------------------------------------------

public abstract class AClip extends AView {

//-----------------------------------------------------------------------------
// instance variables
//
    protected int numberBullets;  // The number of bullets currently in the clip
    protected int capacity;       // The capacity of the clip
    private ABullet[] theBullet;// The bullets
    private AClipGun myGun;     // The gun the clip is in
//-----------------------------------------------------------------------------
// The constructor needs to know how many bullets the clip can hold.
// Postcondition:
//    numberBullets==0,
//    capacity==theCapacity,
//    theBullet==an array of size capacity,
//    theBullet[i]==null for i=0,...,capacity
//    myGun==null;
//    the gun is drawn as specified in drawClip.
//
  public AClip(int x,int y,int width,int height,int cap) {
      super(x,y,width,height);
      capacity=cap;
      numberBullets=0;
      myGun=null;
      theBullet=new ABullet[capacity];
      int i;
      for(i=0;i<capacity;i++) {
          theBullet[i]=null;
      }
      drawClip();
  }

//-----------------------------------------------------------------------------
// These methods should be defined in a subclass that wishes to draw AClips
//
  protected void drawClip() {}
  protected void drawBulletOnClip(ABullet A) {}
  protected void undrawBulletOnClip(ABullet A) {}

//-----------------------------------------------------------------------------
// if the clip is not full, inserts Bullet B into the clip and return true;
// otherwise, do not modify the class and return false.
//
  public boolean insertBullet(ABullet B) {
        if(!isFull() && !isInGun()) {
            theBullet[numberBullets]=B;
            numberBullets++;
            drawBulletOnClip(B);
            return true;
        }
        else {
            return false;
        }
  }

//-----------------------------------------------------------------------------
// if the clip is not empty, removes the most recently inserted Bullet B from
// the clip and return B;
// otherwise, do not modify the class and return null.
// Bullets can be removed in or out of the gun, since they must be removed
// when the gun is fired.
//
  public ABullet removeBullet() {
        if(!isEmpty()) {
            numberBullets--;
            ABullet aBullet=theBullet[numberBullets];
            theBullet[numberBullets]=null;
            undrawBulletOnClip(aBullet);
            return aBullet;
        }
        else {
            return null;
        }
  }

//-----------------------------------------------------------------------------
// Returns true   if the clip is full (numberBullets==capacity) and
//         false  otherwise
//
// does not modify the class
//
  public boolean isFull() {
      return (numberBullets==capacity);
  }

//-----------------------------------------------------------------------------
// Returns true   if the clip is empty (numberBullets==0) and
//         false  otherwise
//
// does not modify the class
//
  public boolean isEmpty() {
      return (numberBullets==0);
  }

//-----------------------------------------------------------------------------
// Returns: numberBullets
//
  public int numberBullets() {
      return numberBullets;
  }

//-----------------------------------------------------------------------------
// postcondition: myGun==G
//
  public void setMyGun(AClipGun G) {
      myGun=G;
  }

//-----------------------------------------------------------------------------
// returns true if myGun!=null and false otherwise
//
  public boolean isInGun() {
      return (myGun!=null);
  }

//-----------------------------------------------------------------------------
}