Alexanders Final Project

<< Shooter Game by Josh and Chris | OldProjectsTrailIndex | Alexi's Final Project >>

Click here for a working demonstration

SOLDIER_WAR GAME:

Project Zip file

GRAPHICS

Graphics Zip file

SoldierWar.java

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.Timer;

public class SoldierWar extends Applet implements KeyListener
{
	public static final int amount = 20;
	private static final Graphics g = null;
	private Soldier soldier;
	private ArrayList<Projectile> bullets = new ArrayList<Projectile>();
	private String BULLET_LEFT_FILENAME = "Bad_Ass_Bullet_Left.gif";
	private String BULLET_RIGHT_FILENAME = "Bad_Ass_Bullet_Right.gif";
	private String STANDING_SOLDIER_FILENAME = "Soldier_Standing_Default.gif"; // right
	private String CROUCHING_SOLDIER_FILENAME = "Soldier_Crouching_Default.gif"; // right
	private String STANDING_LEFT= "Soldier_Standing_Left.gif";
	private String CROUCHING_LEFT= "Soldier_Crouching_Left.gif";
	private final int SCREEN_WIDTH = 1200;
	private final int SCREEN_HEIGHT = 800;
	private final int VERT_PADDING = 100;
	private final int HORIZ_PADDING = 100;
	private Soldier evilSoldier;

	public void init()
	{
		resize(SCREEN_WIDTH, SCREEN_HEIGHT);

		this.addKeyListener(this);
		soldier = new Soldier(STANDING_SOLDIER_FILENAME, CROUCHING_SOLDIER_FILENAME, HORIZ_PADDING + 30, 585);
		evilSoldier = new Soldier(STANDING_LEFT, CROUCHING_LEFT, SCREEN_WIDTH - HORIZ_PADDING - 130, 585);

		final int DELAY = 5; // 10 ms between movements of bullet

		ActionListener fireTask = new ActionListener() 
		{		
			private final int DELTA_X = 20; // number pixels it moves in 10 ms

			public void actionPerformed(ActionEvent evt)
			{
				for(Projectile p : bullets) 
				{
					if (p.isGoingRight()) {
						p.setX(p.getX() + DELTA_X);
					} else {
						p.setX(p.getX() - DELTA_X);
					}
				}

				repaint();

			}
		};

		Timer fireTimer = new Timer(DELAY, fireTask);
		fireTimer.setRepeats(true);
		fireTimer.start();
	}

	public void paint(Graphics g)
	{

		Background sky = new Background("Sky.gif",100, 75);
		Background grass = new Background("Grass.gif", 100, 675);

		Background HorizontalBoundaryUP = new Background("Horizontal_Boundary.gif", 100, 50);
		Background HorizontalBoundaryDOWN = new Background("Horizontal_Boundary.gif", 100, 725);
		Background VerticalBoundaryLEFT = new Background("Vertical_Boundary.gif", 75, 75);
		Background VerticalBoundaryRIGHT = new Background("Vertical_Boundary.gif", 1100, 75);

		Background CornerUpLeft = new Background("Corner_Boundary.gif", 75, 50);
		Background CornerDownLeft = new Background("Corner_Boundary.gif", 75, 725);
		Background CornerUpRight = new Background("Corner_Boundary.gif", 1100, 50);
		Background CornerDownRight = new Background("Corner_Boundary.gif", 1100, 725);

		// Background soldierStanding = new Background("Soldier_Standing.gif", 100, 585);
		// Background soldierCrouching = new Background("Soldier_Crouching.gif", 100, 585);

		// Soldier SoldierStanding = new Soldier("Soldier_Standing.gif", 0, 575);
		// Soldier SoldierCrouching = new Soldier("Soldier_Crouching.gif", 0, 575);

		sky.draw(g);
		grass.draw(g);

		HorizontalBoundaryUP.draw(g);
		HorizontalBoundaryDOWN.draw(g);
		VerticalBoundaryLEFT.draw(g);
		VerticalBoundaryRIGHT.draw(g);

		CornerUpLeft.draw(g);
		CornerDownLeft.draw(g);
		CornerUpRight.draw(g);
		CornerDownRight.draw(g);

		// soldierStanding.draw(g);
		// soldierCrouching.draw(g);

		// SoldierStanding.draw(g);
		// SoldierCrouching.draw(g);

		soldier.draw(g);
		evilSoldier.draw(g);

		Iterator<Projectile> itr = bullets.iterator();
		while (itr.hasNext())
		{
			Projectile p = itr.next();
			if(!isInScreen(p)) {
				itr.remove(); // get rid of the bullet if it exits the screen
				System.out.println("BULLET DISAPPEARED");
			} 
			else 
			{
				p.draw(g);
				System.out.println("FIRE");
			}
		}
	}

	public void keyPressed(KeyEvent e)
	{
		char keyCode = e.getKeyChar();


		if(keyCode == 'w') // JUMP
		{
			jump(soldier);
		}

		else if (keyCode == 's') // CROUCH
		{
			crouch(soldier);
		}

		else if (keyCode == 'a') // LEFT
		{
			moveLeft(soldier);
		}

		else if (keyCode == 'd') // RIGHT
		{
			moveRight(soldier);
		}

		else if (keyCode == 'f') // FIRE PROJECTILE [IE - BULLET] 
		{
			int FACTOR = 13; // drop bullet this many pixels from top of soldier image

			Projectile bullet = new Projectile(BULLET_RIGHT_FILENAME, soldier.getX() + soldier.getGraphicWidth(), soldier.getY() + FACTOR, true);
			bullets.add(bullet);

			// set up jump using a timer

		}

		if(keyCode == 'i') // JUMP
		{
			jump(evilSoldier);
		}

		else if (keyCode == 'k') // CROUCH
		{
			crouch(evilSoldier);
		}

		else if (keyCode == 'j') // LEFT
		{
			moveLeft(evilSoldier);
		}

		else if (keyCode == 'l') // RIGHT
		{
			moveRight(evilSoldier);
		}

		else if (keyCode == 'h') // FIRE PROJECTILE [IE - BULLET] 
		{
			int FACTOR = 13; // drop bullet this many pixels from top of soldier image

			Projectile bullet = new Projectile(BULLET_LEFT_FILENAME, evilSoldier.getX(), evilSoldier.getY() + FACTOR, false);
			bullets.add(bullet);

			// set up jump using a timer

		}

		repaint();
	}

	public void moveRight(final Soldier p) {
		if (isOnFarLeft(p) || !isOnFarRight(p)) {
			p.setX(p.getX() + amount);
		}
	}

	public void moveLeft(final Soldier p) {
		if (isOnFarRight(p) || !isOnFarLeft(p)) {
			p.setX(p.getX() - amount);
		}
	}

	public void crouch (final Soldier p) {
		// set up jump using a timer
		int delay = 100; //milliseconds



		ActionListener task = new ActionListener() 
		{ 

			boolean done = false;

			public void actionPerformed(ActionEvent evt)
			{
				if (!p.isCrouched() && !done)
				{
					System.out.println("crouching");
					p.crouch();
					repaint();
				}

				else if (!done) 
				{
					System.out.println("standing");
					p.stand();
					repaint();
					done = true;
				} 

				else 
				{	
					// do nothing
				}
			}
		};

		p.setCrouchTimer(new Timer(delay, task));
		p.getCrouchTimer().setRepeats(true);
		p.getCrouchTimer().start();
	}

	public void jump (final Soldier p) {
		// set up jump using a timer
		int delay = 100; //milliseconds

		ActionListener task = new ActionListener() 
		{ 
			private boolean jumped = false; 
			boolean jumpActionDone = false;

			public void actionPerformed(ActionEvent evt)
			{
				if (!jumped && !jumpActionDone)
				{
					p.setY(p.getY() + Soldier.JUMP_UP);
					jumped = true;
					repaint();

					System.out.println("JUMP UP");
				}

				else if (jumped && !jumpActionDone)
				{
					p.setY(p.getY() + Soldier.FALL_DOWN);
					repaint();
					jumpActionDone = true;

					System.out.println("FALL DOWN");
				}

				else
				{
					// do nothing
				}
			}
		};

		p.setJumpTimer(new Timer(delay, task));
		p.getJumpTimer().setRepeats(true);
		p.getJumpTimer().start();
	}

	// returns true if the object is in the screen, false otherwise
	public boolean isInScreen(GraphicCartesian p) {
		if(p.getX() > (SCREEN_WIDTH - HORIZ_PADDING - p.getGraphicWidth()) || p.getX() < (HORIZ_PADDING)) {
			return false;
		}
		return true;
	}

	public boolean isOnFarLeft(GraphicCartesian p) {
		if(p.getX() < (HORIZ_PADDING)) {
			return true;
		}
		return false;
	}

	public boolean isOnFarRight(GraphicCartesian p) {
		if(p.getX() > (SCREEN_WIDTH - HORIZ_PADDING - p.getGraphicWidth())) {
			return true;
		}
		return false;
	}

	public void keyReleased(KeyEvent e)
	{

	}

	public void keyTyped(KeyEvent e)
	{

	}	
}

Background.java:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Background 
{

	private int x;
	private int y;
	private BufferedImage img;

	public Background(String graphicFileName, int x, int y)
	{
		this.x=x;
		this.y=y;
		this.img = null;

		try 
		{
		    img = ImageIO.read(new File(graphicFileName));
		} 
		catch (IOException e) 
		{
		}
	}
	public void setX(int x)
	{
		this.x = x;
	}
	public void setY(int y)
	{
		this.y = y;
	}
	public void draw(Graphics g)
	{
		g.drawImage(img, x, y, null);
	}
}

Soldier.java:

import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Timer;

public class Soldier implements GraphicCartesian
{
	private int x;
	private int y;
	private BufferedImage img;
	private BufferedImage crouched_img;
	public static final int JUMP_UP = -200;
	public static final int FALL_DOWN = 200;
	private boolean isCrouched = false;
	private Timer jumpTimer;
	private Timer crouchTimer;

	public Soldier(String graphicFileName, String crouchedFilename, int x, int y)
	{
		this.x = x;
		this.y = y;
		this.img = null;

		try 
		{
		    img = ImageIO.read(new File(graphicFileName));
		    crouched_img = ImageIO.read(new File(crouchedFilename));
		} 
		catch (IOException e) 
		{
			System.out.println("Whoops, file IO error.");
		}
	}

	public int getGraphicWidth() 
	{
		return img.getWidth();
	}

	public int getGraphicHeight() 
	{
		return img.getHeight();
	}

	public boolean isCrouched() 
	{
		return isCrouched;
	}

	public void crouch() 
	{
		isCrouched = true;
	}

	public void stand() 
	{
		isCrouched = false;
	}

	public void draw(Graphics g)
	{	
		if (!isCrouched()) 
		{
			g.drawImage(img, x, y, null);
		} 

		else 
		{	
			g.drawImage(crouched_img, x, y, null);
		}
	}

	public int getX()
	{
		return x;
	}

	public int getY()
	{
		return y;
	}

	public void setX(int x)
	{
		this.x = x;
	}

	public void setY(int y)
	{
		this.y = y;
	}

	public void setJumpTimer(Timer jumpTimer) {
		this.jumpTimer = jumpTimer;
	}

	public Timer getJumpTimer() {
		return jumpTimer;
	}

	public void setCrouchTimer(Timer crouchTimer) {
		this.crouchTimer = crouchTimer;
	}

	public Timer getCrouchTimer() {
		return crouchTimer;
	}
}

Projectile.java:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Projectile implements GraphicCartesian
{
	private int x;
	private int y;
	private BufferedImage img;
	private boolean goingRight;

	public Projectile(String graphicFileName, int x, int y, boolean right)
	{
		goingRight = right;
		this.x = x;
		this.y = y;
		this.img = null;

		try 
		{
		    img = ImageIO.read(new File(graphicFileName));
		} 
		catch (IOException e) 
		{
			System.out.println("Whoops, file IO error.");
		}
	}

	public boolean isGoingRight() {
		return goingRight;
	}

	public int getGraphicWidth() 
	{
		return img.getWidth();
	}

	public int getGraphicHeight() 
	{
		return img.getHeight();
	}

	public void draw(Graphics g)
	{	
			g.drawImage(img, x, y, null);
	}

	public int getX()
	{
		return x;
	}

	public int getY()
	{
		return y;
	}

	public void setX(int x)
	{
		this.x = x;
	}

	public void setY(int y)
	{
		this.y = y;
	}
}

GraphicCartesian.java

public interface GraphicCartesian 
{
	public int getX();
	public int getY();
	public int getGraphicWidth();
	public int getGraphicHeight();
}