root/Project Folder/bump_n_jump/Game.java

User picture

Author: ahaurw01

Revision: 85 («Previous)


File Size: 14.1 KB

(April 07, 2009 21:34 UTC) About 3 years ago


  

 
Show/hide line numbers
package bump_n_jump;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;

/*
 * Game - the class that contains the main game loop (is a panel).
 */
public class Game extends JPanel implements Runnable, KeyListener  {

	private static final long serialVersionUID = 1L;
	private Thread mainLoop;
	
	private static final int DEFAULT_FPS = 50;
	private int fps, threadDelay;
	private Image offImage;
	private Graphics offGraphics;
	private Dimension offDimension;
	private Image backgroundImage;
	
	private Level level;
	private LevelCreator levelCreator;
	private int levelNum, livesLeft, starsLeft;
	
	private boolean gameStarted;
	private boolean beatTheGame, gameOver;
	private int menuScreen;
	private static final int MENU = 0, INSTRUCTIONS = 1, CREDITS = 2;
	private Image menuImage, instructionsImage, menuImageBeat, menuImageGameOver, credits;
	private Image arrow;
	
	private static final int ARROWY1 = 230;
	private static final int ARROWY2 = 360;
	private static final int ARROWY3 = 505;
	private int arrowX = 115, arrowY = ARROWY1;
	
	//Sounds
	//private SoundPlayback	bounceSnd;
	private SoundPlayback	dogSnd;
	private	SoundPlayback	trampSnd;
	private SoundPlayback	deadSnd;
	private	SoundPlayback	starSnd;
	private SoundPlayback	newLevelSnd;
	private SoundPlayback	levelMusicSnd;
	private SoundPlayback	gameplaySnd;
	private SoundPlayback	openingSnd;

	
	
	
	public Game() {
		//initialize vars
		fps = DEFAULT_FPS;
		threadDelay = 1000 / fps;
		
		//key press info.
		addKeyListener(this);
		requestFocus();
		setFocusable(true);

		
		
		//Initialize Sounds
		//bounceSnd		=	new SoundPlayback("music/Bounce.aif");
		dogSnd			=	new SoundPlayback("music/Dog.aif");
		trampSnd		=	new SoundPlayback("music/Tramp.aif");
		deadSnd			=	new SoundPlayback("music/Dead.aif");
		starSnd			=	new SoundPlayback("music/Star.aif");
		newLevelSnd		=	new SoundPlayback("music/NewLevel.aif");
		levelMusicSnd	=	new SoundPlayback("music/Level_music.aif", true);
		gameplaySnd		=	new SoundPlayback("music/gameplaymusic.aif");
		openingSnd		=	new SoundPlayback("music/openingmusic.aif");
		

		livesLeft = 6;
		levelNum = 0;
		levelCreator = new LevelCreator();
		nextLevel();
		
		//create new thread
		mainLoop = new Thread(this);
		gameStarted = beatTheGame = gameOver = false;
		menuScreen = MENU;
		try {
			menuImage = ImageIO.read(getClass().getResource("../images/menu.jpg"));
			instructionsImage = ImageIO.read(getClass().getResource("../images/instructions.jpg"));
			menuImageBeat = ImageIO.read(getClass().getResource("../images/winScreen.jpg"));
			menuImageGameOver = ImageIO.read(getClass().getResource("../images/gameOver.jpg"));
			credits = ImageIO.read(getClass().getResource("../images/credits.jpg"));
			arrow = ImageIO.read(getClass().getResource("../images/arrow.png"));
		}
		catch (IOException e) {
			System.err.println(e.toString());
		}
		openingSnd.start();
	}
	
	private void startEverythingOver() {
		livesLeft = 6;
		levelNum = 0;
		levelCreator = new LevelCreator();
		nextLevel();
		
		//create new thread
		mainLoop = new Thread(this);
		gameStarted = beatTheGame = gameOver = false;
		menuScreen = MENU;
		gameplaySnd.stop();
		openingSnd.start();
		menuScreen();
	}
	
	/**
	 * shows the menu screen with options to show instructions, play the game, and quit.
	 */
	public void menuScreen() {
		repaint();
	}
	
	
	public void start() {
		mainLoop.start();
	}
	
	public void run() {
		
		openingSnd.stop();
		gameplaySnd.start();

		while (Thread.currentThread() == mainLoop) {
			// Remember the starting time
			long tm = System.currentTimeMillis();
			BallSprite bs = level.getBallSprite();
			
			repaint();
			
			
			//check collisions between objects:
			checkCollisions();
			
			//update everybody's positions
			if (bs.isDead()) {
				deadSnd.stop();
				deadSnd.start();

				if (livesLeft == 0) {
					// Stop level music at end of game
					gameplaySnd.stop();
					gameStarted = false;
					gameOver = true;
					bs.getCircle().setFrame(-10, -10, 0,0);
					bs.setIsDead(false);
					try {
						Thread.sleep(1000L);
					} catch (InterruptedException e) {}
					repaint();
					//mainLoop.stop();
				}
				else {
					livesLeft--;
					Level doOver = levelCreator.create(level.filename, level.getBackImageName());
					level = doOver;
					starsLeft = level.getNumStars();
				}
			}
			level.getBallSprite().updateCoordinates();
			level.getBallSprite().updateFrame();

			for (Iterator <Dog> iter = level.getDogs().iterator(); iter.hasNext();) {
				Dog d = iter.next();
				d.updateCoordinates();
				d.updateFrame();
			}
			
			
			
			//if there are no stars left, open the door.
			if (!level.isDoorOpen() && starsLeft == 0 && level.getDoor().open()) {
				//we get here if we are supposed to open the door and the door is done opening.
				level.setDoorOpen(true);
			}
			
			// Delay depending on how far we are behind
			try {
				tm += threadDelay;
				Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
			}
			catch (InterruptedException e) {
				System.err.println("An error occurred: " + e.toString());
				break;
			}
		}
	}
	
	/**
	 * steps through all the lists of elements and checks for relevant collisions.
	 */
	public void checkCollisions() {
		BallSprite b = level.getBallSprite();

		//check ball collisions with dogs:
		for (Iterator <Dog> iter = level.getDogs().iterator(); iter.hasNext();) {
			Dog d = iter.next();
			if(b.collisionWith(d))
			{
				//play dog sound when dog collides with wall
				dogSnd.stop();
				dogSnd.start();
				//dogSnd.stop();
			}

		}

		//check ball collisions with ground tiles
		for (Iterator <Tile> iter = level.getTiles().iterator(); iter.hasNext();) {
			Tile t = iter.next();
			b.collisionWithBottom(t);
			b.collisionWithSide(t);
			
			if (b.collisionWithTop(t)) {
				//plays bounce sound
				//bounceSnd.stop();
				//bounceSnd.start();
			}

		}

		//check dog collisions with sides of tiles (should reverse them)
		for (Iterator <Dog> iter = level.getDogs().iterator(); iter.hasNext();) {
			Dog d = iter.next();
			for (Iterator <Tile> i = level.getTiles().iterator(); i.hasNext();) {
				Tile t = i.next();
				d.collisionWithWall(t);
			}
			
		}
		
		//check ball collisions with spikes
		for (Iterator <Spike> iter = level.getSpikes().iterator(); iter.hasNext();) {
			Spike s = iter.next();
			b.collisionWith(s);
		}
		
		//check collisions with the door
		if (level.isDoorOpen() && b.collidesWith(level.getDoor())) {
			try {
				Thread.sleep(1000L);
			} catch (InterruptedException e) {}
			nextLevel();
			repaint();
			
		}

		for (Iterator <Trampoline> iter = level.getTramps().iterator(); iter.hasNext();) {
			Trampoline t = iter.next();
			
			if(b.collisionWith(t))
			{
				//plays trampoline sound
				trampSnd.stop();
				trampSnd.start();
				//trampSnd.stop();
			}

		}
		//collisions with stars
		for (Iterator <Star> iter = level.getStars().iterator(); iter.hasNext();) {
			Star s = iter.next();
			if (b.collidesWith(s)) {
				starSnd.stop();
				starSnd.start();
				//starSnd.stop();

				s.setIsExploding(true);
				starsLeft--;
			}
		}
		
		for (Iterator <Warp> iter = level.getWarps().iterator(); iter.hasNext();) {
			Warp w = iter.next();
			if (b.collidesWith(w)) {
				b.setX(w.getExitX());
				b.setY(w.getExitY());
				
				//play a woosh sound?
			}
		}
	}
	
	/**
	 * called every time we do a repaint(); starts the repainting process for all of the things in the level.
	 */
	public void paintComponent(Graphics g) {
		
		Dimension d = getSize();
		
		//The following was stolen from AnimatedSpriteDemoPanel.java
		// Create the offscreen graphics context
		if ((offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height)) {
			offDimension = d;
			offImage = createImage(d.width, d.height);
			offGraphics = offImage.getGraphics();
		}

		// Erase the previous image
		offGraphics.setColor(getBackground());
		offGraphics.fillRect(0, 0, d.width, d.height);
		offGraphics.setColor(Color.black);
		
		if (gameStarted) {
			// Paint the frame into the image
			paintFrame(offGraphics);
		}
		else {
			paintMenu(offGraphics);
		}

		// Paint the image onto the screen
		g.drawImage(offImage, 0, 0, null);
	}
	
	/**
	 * paints the menu screen.
	 * @param g
	 */
	private void paintMenu(Graphics g) {
		if (menuScreen == MENU) {
			if (!beatTheGame && !gameOver) {
				g.drawImage(menuImage, 0, 0, null);
				//draw the arrow.
				g.drawImage(arrow, arrowX, arrowY, null);
			}
			else if (beatTheGame) {
				mainLoop = null;
				g.drawImage(menuImageBeat, 0, 0, null);
			}
			else if (gameOver) {
				mainLoop = null;
				g.drawImage(menuImageGameOver, 0, 0, null);
				deadSnd.stop();
			}
			
		}
		else if (menuScreen == INSTRUCTIONS) {
			g.drawImage(instructionsImage, 0, 0, null);
		}
		else if (menuScreen == CREDITS) {
			g.drawImage(credits, 0, 0, null);
		}
	}
	
	/** 
	 * steps through all level elements and repaints them.
	 * @param g
	 */
	private void paintFrame(Graphics g) {
		//paint the level #, lives, and stars left at the top of the window.
		Graphics2D g2D = (Graphics2D)g;
		g2D.drawImage(backgroundImage, 0, 0, null);
		
		g2D.setFont(new Font("Helvetica", Font.BOLD,  20));
		g2D.drawString("Lives: " + livesLeft, 40, 20);
		g2D.drawString("Level " + Integer.toString(levelNum), 300, 20);
		g2D.drawString("Stars left: " + Integer.toString(starsLeft), 580, 20);
		
		//draw the door
		level.getDoor().drawSprite(g);
		
		
		
		for (Iterator <Tile> it = level.getTiles().iterator(); it.hasNext();) {
			Tile t = it.next();
			
			//Draw the tile
			t.drawSprite(g);
		}
		for (Iterator <Star> it = level.getStars().iterator(); it.hasNext();) {
			Star s = it.next();
			
			//Draw the star
			if (s.isShown()) {
				if (s.isExploding()) {
					s.explode();
				}
				s.drawSprite(g);
			}
		}
		for (Iterator <Trampoline> it = level.getTramps().iterator(); it.hasNext();) {
			Trampoline t = it.next();
			
			//Draw the trampoline
			t.drawSprite(g);
		}
		for (Iterator <Spike> it = level.getSpikes().iterator(); it.hasNext();) {
			Spike s = it.next();
			
			//Draw the Spikes
			s.drawSprite(g);
		}
		for (Iterator <Warp> it = level.getWarps().iterator(); it.hasNext();) {
			Warp w = it.next();
			
			//Draw the Spikes
			w.drawSprite(g);
		}
		
		if (level.getDogs() != null) {
			for (Iterator <Dog> it = level.getDogs().iterator(); it.hasNext();) {
				Dog d = it.next();
				
				//Draw the dog
				d.drawSprite(g);
			}
		}
		//draw the ball		
		level.getBallSprite().drawSprite(g);
	}
	
	public void nextLevel() {
		//play sound for new level
		newLevelSnd.stop();
		newLevelSnd.start();
		//newLevelSnd.stop();

		try {
			Thread.sleep(1000L); //sleep one second for emphasis.
		}
		catch (InterruptedException e) {
			System.err.println("An error occurred: " + e.toString());
		}
		
		
		levelNum++;
		switch (levelNum) {
		case 1:
			level = levelCreator.create("levels/level1.txt", "../images/Warp_background.jpg");
			starsLeft = level.getNumStars();
			backgroundImage = level.getBackImage();
			break;
		case 2: 
			level = levelCreator.create("levels/level2.txt", "../images/Warp_background.jpg");
			starsLeft = level.getNumStars();
			break;
		case 3:
			level = levelCreator.create("levels/level3.txt", "../images/Warp_background.jpg");
			starsLeft = level.getNumStars();
			break;
		case 4:
			level = levelCreator.create("levels/level4.txt", "../images/Spike_background.jpg");
			starsLeft = level.getNumStars();
			break;
		case 5:
			level = levelCreator.create("levels/level5.txt", "../images/Spike_background.jpg");
			starsLeft = level.getNumStars();
			break;
		case 6:
			level = levelCreator.create("levels/level6.txt", "../images/Spike_background.jpg");
			starsLeft = level.getNumStars();
			break;
		case 7: //beat the game
			gameStarted = false;
			beatTheGame = true;
			repaint();
			break;
			
		}
		
	}
	
	public void keyTyped(KeyEvent e) {	
	}
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			level.getBallSprite().setLeftDown(true);
		}
		else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			level.getBallSprite().setRightDown(true);
		}
		else if (e.getKeyCode() == KeyEvent.VK_DOWN && !gameStarted && menuScreen == MENU) {
			//move the arrow down
			if (arrowY == ARROWY1)
				arrowY = ARROWY2;
			else if (arrowY == ARROWY2) 
				arrowY = ARROWY3;
			
			repaint();
		}
		else if (e.getKeyCode() == KeyEvent.VK_UP && !gameStarted && menuScreen == MENU) {
			//move arrow up.
			if (arrowY == ARROWY3)
				arrowY = ARROWY2;
			else if (arrowY == ARROWY2) 
				arrowY = ARROWY1;
			
			repaint();
		}
		else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			//do action that arrow is on.
			if (arrowY == ARROWY1) {
				//start the game.
				gameStarted = true;
				start();
				repaint();
			}
			else if (arrowY == ARROWY2) {
				//show instructions
				menuScreen = INSTRUCTIONS;
				repaint();
			}
			else if (arrowY == ARROWY3){
				menuScreen = CREDITS;
				repaint();
			}
		}
		else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			if (menuScreen == INSTRUCTIONS || menuScreen == CREDITS) {
				menuScreen = MENU;
				arrowY = ARROWY1;
				repaint();
			}
			else if ((gameOver && !gameStarted) || beatTheGame){
				gameOver = false;
				gameStarted = false;
				startEverythingOver();
			}
		}
		else if (e.getKeyCode() == KeyEvent.VK_F12) {
			nextLevel();
		}
	}
	public void keyReleased(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			level.getBallSprite().setLeftDown(false);
		}
		else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			level.getBallSprite().setRightDown(false);
		}
	}

}