Commit e3a3ed38 authored by Melissa Tjong's avatar Melissa Tjong
Browse files

Merge origin/master into Melissa2

Conflicts:
	src/project/GameOver.java
	src/project/Level.java
	src/project/Level1.java
	src/project/Level2.java
	src/project/Level3.java
	src/project/Level4.java
	src/project/Level5.java
	src/project/Level6.java
parents 70c89b2e 9fbcf3c8
......@@ -24,6 +24,7 @@ import org.newdawn.slick.SpriteSheet;
import project.Box;
/**
* Class for the different entities in the game
*
* @author njb013
*/
......@@ -44,8 +45,6 @@ public abstract class Entity extends Box {
public abstract void init();
public void render(GameContainer gc, Graphics g) {
}
public abstract void render(GameContainer gc, Graphics g);
}
......@@ -24,12 +24,13 @@ import org.newdawn.slick.SlickException;
import project.Level1;
/**
* Class for the obstacles in the game
*
* @author njb013
*/
public class Obstacle extends Entity {
private float speed;
private float speed; //speed attribute
public Obstacle(float s) {
super();
......@@ -42,8 +43,11 @@ public class Obstacle extends Entity {
height = 50;
x = Level1.WIDTH;
y = (float) (Math.random() * (Level1.HEIGHT - height));
//y coordinate is randomly generated
width = (int) (4.5 * height);
//width is 4.5 times the height
try {
//set the image and scale it
image = new Image("Images/BlueLaser.png");
image = image.getScaledCopy((int) width, (int) height);
} catch (SlickException ex) {
......@@ -55,14 +59,14 @@ public class Obstacle extends Entity {
public void update(GameContainer gc, int delta) {
if (this.getEndX() > 0) {
//moves the obstacle left
x -= speed;
}
}
@Override
public void render(GameContainer gc, Graphics g) {
image.draw(x, y);
image.draw(x, y); //draws obstacle
}
......
......@@ -34,12 +34,13 @@ import project.Level1;
public class Player extends Entity {
private boolean sprite = false;
public Image risingImage = null;
public Image fallingImage = null;
private boolean isRising = false;
private boolean isFalling = false;
private Image risingImage = null; //Image of the character when they are rising
private Image fallingImage = null;//Image of the character when they are falling
private int character;
private boolean isRising = false;//true if the character is rising
private boolean isFalling = false;//true if the character is falling
public Player() {
super();
......@@ -47,19 +48,34 @@ public class Player extends Entity {
public Player(int character) {
this.character = character;
init();
}
// /**
// * Initializes a Player with a spritesheet, rising image, and falling image
// *
// * @param s, spritesheet
// * @param rise, rising image
// * @param fall , falling image
// */
// public Player(SpriteSheet s, Image rise, Image fall) {
// sprite = true;
// this.spriteSheet = s;
// risingImage = rise;
// fallingImage = fall;
//
// }
@Override
public void render(GameContainer gc, Graphics g) {
if (!isRising && !isFalling) {
//draws player if they are running on the ground
this.getAnimation().draw(x, y);
}
else if (isRising) {
//draws player when they are rising
this.getRisingImage().draw(x, y);
}
else if (isFalling) {
//draws player when they are falling
this.getFallingImage().draw(x, y);
}
......@@ -68,19 +84,23 @@ public class Player extends Entity {
public void update(GameContainer gc, int delta) {
Input input = gc.getInput();
if (input.isKeyDown(Input.KEY_SPACE)) {
y -= 0.75;
//when space bar is pressed, player will move up
y -= 1;
isRising = true;
}
else {
//player is falling
y += 0.75;
isRising = false;
isFalling = true;
}
if (y > HEIGHT - height) {
//if player hits the ceiling, they will stay there
y = HEIGHT - (height);
isFalling = false;
}
if (y < 0) {
//if the player hits the floor
y = 0;
}
......@@ -99,31 +119,30 @@ public class Player extends Entity {
64);
this.risingImage = new Image("Images/xeonRising.png");
this.fallingImage = new Image("Images/xeonFalling.png");
playerAnimation = new Animation(spriteSheet, 75);
width = image.getWidth();
height = image.getHeight();
} catch (SlickException ex) {
Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null,
ex);
}
}
if (this.character == 1) {
try {
this.image = new Image("Images/megaManIcon.png");
this.spriteSheet = new SpriteSheet("Images/megaManSprite.png",
114,
100);
this.spriteSheet = new SpriteSheet(
"Images/megaManSprite.png",
114,
100);
this.risingImage = new Image("Images/megaManRising.png");
this.fallingImage = new Image("Images/megaManFalling.png");
this.fallingImage = new Image(
"Images/megaManFalling.png");
playerAnimation = new Animation(spriteSheet, 75);
width = image.getWidth();
height = image.getHeight();
} catch (SlickException ex) {
Logger.getLogger(Player.class.getName()).log(Level.SEVERE, null,
ex);
Logger.getLogger(Player.class.getName()).log(
Level.SEVERE, null,
ex);
}
}
......
......@@ -96,6 +96,12 @@ public class Box {
return y + getHalfHeight();
}
/**
* tests if two box objects are intersecting
*
* @param b, box to test collision
* @return true if boxes are colliding, false otherwise
*/
public boolean hitTest(Box b) {
boolean rightB = b.x <= this.getEndX();
boolean bottomB = b.y <= this.getEndY();
......
......@@ -25,6 +25,7 @@ import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
/**
* Class for Game Over state
*
* @author njb013
*/
......@@ -53,7 +54,7 @@ public class GameOver extends BasicGameState {
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
g.drawRect(0, 0, Level1.WIDTH, Level1.HEIGHT);
//Draw game over image
gameOver.draw(0, 0, Level1.WIDTH, Level1.HEIGHT);
}
......@@ -62,11 +63,13 @@ public class GameOver extends BasicGameState {
input = container.getInput();
if (input.isMousePressed(0)) {
//enters main menu state when mouse clicked
loseMusic.stop();
game.getState(Main.MAINMENU).init(container, game);
game.enterState(Main.MAINMENU);
}
if (!loseMusic.playing()) {
//plays music
loseMusic.loop();
}
......
......@@ -27,6 +27,7 @@ import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
/**
* Game win state
*
* @author njb013
*/
......@@ -58,6 +59,7 @@ public class GameWin extends BasicGameState {
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
g.setColor(Color.white);
g.fillRect(0, 0, Level1.WIDTH, Level1.HEIGHT);
//Writes 'You Win' on the screen
float width = ((Level1.WIDTH) / 2) - tff.getWidth("YOU WIN");
tff.drawString(width, (Level1.HEIGHT) / 2, "YOU WIN!",
Color.black);
......@@ -69,6 +71,7 @@ public class GameWin extends BasicGameState {
if (input.isMousePressed(0)) {
winMusic.stop();
game.getState(Main.MAINMENU).init(container, game);
game.enterState(Main.MAINMENU);
}
......
......@@ -15,12 +15,8 @@
*/
package project;
import entities.Laser;
import entities.Player;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
......@@ -31,36 +27,15 @@ import org.newdawn.slick.state.StateBasedGame;
*/
public abstract class Level extends BasicGameState {
private Player character = null;
private Image background = null;
private Image fireImage = null;
Music mainMusic = null;
private float startY = (float) (HEIGHT - char_Height);
private float startX = WIDTH / 6;
//Width and height of the window
public static final int HEIGHT = 700;
public static final int WIDTH = 1500;
private static int char_Width = 100;
private static int char_Height = 100;
private static float backgroundX2 = (float) WIDTH;
private static float backgroundX = (float) 0.0;
private static float dX = (float) 1;
public static int pointCounter;
private String meters = "M";
public int topScore;
private boolean isRising = false;
private boolean isFalling = false;
public static int pointCounter; //Counts the players points
Laser laser;
public int topScore; //Players top score
public static final int levelLength = 10000;
public static final int levelLength = 15000; //level length in terms of points
public int getTopScore() {
return topScore;
......
......@@ -23,7 +23,6 @@ import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
......@@ -39,27 +38,18 @@ public class Level1 extends Level {
private Image background = null;
Music mainMusic = null;
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
private float startY = (float) (HEIGHT - char_Height);
private float startX = WIDTH / 6;
private static int char_Width = 100;
private static int char_Height = 100;
ArrayList<Obstacle> obstacles;
private static float backgroundX2;
private static float backgroundX;
private static float dX = (float) 1;
private String meters = "M";
private static float dX;
private int topScore;
private final String meters = "M";
Font font;
TrueTypeFont ttf;
private static final int spawnInterval = levelLength / 5;
private static final int SPAWN_INTERVAL = levelLength / 25;
public int getTopScore() {
return topScore;
......@@ -80,11 +70,14 @@ public class Level1 extends Level {
public void init(GameContainer container, StateBasedGame sbg) throws SlickException {
container.setSmoothDeltas(true);
pointCounter = 0;
pointCounter = 0; //initialize point counter
//Values used for scrolling background
backgroundX2 = (float) WIDTH;
backgroundX = (float) 0.0;
dX = (float) 1;
//sets the background image
Image city = new Image("Images/city.png");
city = city.getScaledCopy(WIDTH + 55, HEIGHT);
background = city;
......@@ -92,7 +85,6 @@ public class Level1 extends Level {
switch (MainMenu.chosenPlayer) {
case 0:
character = new Player(0);
break;
case 1:
character = new Player(1);
......@@ -102,33 +94,39 @@ public class Level1 extends Level {
if (character.hasSprite()) {
character.getAnimation().setPingPong(false);
}
//sets the music for this level
Music sonic = new Music("Music/SonicTheme.ogg");
mainMusic = sonic;
font = new Font("Courier", Font.PLAIN, 12);
ttf = new TrueTypeFont(font, true);
obstacles = new ArrayList<>();
}
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
Input input = container.getInput();
backgroundX2 += dX;
backgroundX += dX;
if (character.hasSprite()) {
//updates character animation
character.getAnimation().update(delta);
}
character.update(container, delta);
character.update(container, delta); //updates character position
if (!((int) ((pointCounter / levelLength) + 0.5) == 1)) {
if (pointCounter % spawnInterval == 0) {
obstacles.add(new Obstacle(1));
}
if (pointCounter % SPAWN_INTERVAL == 0) {
//adds a new obstacle based on the spawn interval
obstacles.add(new Obstacle(1));
}
for (Obstacle o : obstacles) {
//updates each obstacle
o.update(container, delta);
if (character.hitTest(o)) {
topScore = pointCounter / 20;
//deals with when the player is hit by an obstacle
if (topScore < pointCounter / 20) {
topScore = pointCounter / 20;
}
sbg.getState(Main.LOSE).init(container, sbg);
sbg.enterState(Main.LOSE);
}
......@@ -136,6 +134,7 @@ public class Level1 extends Level {
pointCounter += 1;
if ((pointCounter % levelLength == 0)) {
//goes to the next level
mainMusic.stop();
sbg.getState(Main.LVL2).init(container, sbg);
sbg.enterState(Main.LVL2);
......@@ -145,6 +144,7 @@ public class Level1 extends Level {
public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException {
/* Renders the scrolling background*/
if (backgroundX2 == WIDTH * 2) {
backgroundX2 = WIDTH;
}
......@@ -155,30 +155,25 @@ public class Level1 extends Level {
if (backgroundX2 > WIDTH) {
g.drawImage(background, (float) (WIDTH - backgroundX), 0, null);
}
//Plays music in a loop
if (!mainMusic.playing()) {
mainMusic.loop();
}
//Draws the points and top score at the top of the screen
g.drawString(Integer.toString(pointCounter / 20) + meters, 0, 0);
g.drawString(Integer.toString(getTopScore()) + meters, 0, 20);
//Writes the level number on the screen
ttf.drawString(Level.WIDTH - 70, 0, "Level 1", Color.black);
Input input = container.getInput();
character.render(container, g);
character.render(container, g); //renders the character
for (Obstacle o : obstacles) {
//renders each obstacle
o.render(container, g);
}
}
public float getStartY() {
return startY;
}
public float getStartX() {
return startX;
}
}
......@@ -41,7 +41,7 @@ public class Level2 extends Level {
private Image background = null;
Music mainMusic = null;
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
ArrayList<Obstacle> obstacles;
private float startY = (float) (HEIGHT - char_Height);
......@@ -99,6 +99,7 @@ public class Level2 extends Level {
font = new Font("Courier", Font.PLAIN, 12);
ttf = new TrueTypeFont(font, true);
obstacles = new ArrayList<Obstacle>();
}
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
......@@ -119,7 +120,9 @@ public class Level2 extends Level {
for (Obstacle o : obstacles) {
o.update(container, delta);
if (character.hitTest(o)) {
topScore = pointCounter / 20;
if (pointCounter / 20 > topScore) {
topScore = pointCounter / 20;
}
sbg.getState(Main.LOSE).init(container, sbg);
sbg.enterState(Main.LOSE);
}
......
......@@ -41,7 +41,7 @@ public class Level3 extends Level {
private Image background = null;
Music mainMusic = null;
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
ArrayList<Obstacle> obstacles;
private float startY = (float) (HEIGHT - char_Height);
......@@ -56,7 +56,7 @@ public class Level3 extends Level {
private String meters = "M";
private static final int spawnInterval = levelLength / 5;
private static final int spawnInterval = levelLength / 25;
Font font;
TrueTypeFont ttf;
......@@ -100,6 +100,7 @@ public class Level3 extends Level {
font = new Font("Courier", Font.PLAIN, 12);
ttf = new TrueTypeFont(font, true);
obstacles = new ArrayList<Obstacle>();
}
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
......@@ -120,7 +121,9 @@ public class Level3 extends Level {
for (Obstacle o : obstacles) {
o.update(container, delta);
if (character.hitTest(o)) {
topScore = pointCounter / 20;
if (topScore < pointCounter / 20) {
topScore = pointCounter / 20;
}
sbg.getState(Main.LOSE).init(container, sbg);
sbg.enterState(Main.LOSE);
}
......
......@@ -40,7 +40,7 @@ public class Level4 extends Level {
private Image background = null;
Music mainMusic = null;
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
ArrayList<Obstacle> obstacles;
private float startY = (float) (HEIGHT - char_Height);
......@@ -55,7 +55,7 @@ public class Level4 extends Level {
private String meters = "M";
private static final int spawnInterval = levelLength / 10;
private static final int spawnInterval = levelLength / 50;
Font font;
TrueTypeFont ttf;
......@@ -98,6 +98,7 @@ public class Level4 extends Level {
font = new Font("Courier", Font.PLAIN, 12);
ttf = new TrueTypeFont(font, true);
obstacles = new ArrayList<Obstacle>();
}
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
......@@ -118,7 +119,9 @@ public class Level4 extends Level {
for (Obstacle o : obstacles) {
o.update(container, delta);
if (character.hitTest(o)) {
topScore = pointCounter / 20;
if (topScore < pointCounter / 20) {
topScore = pointCounter / 20;
}
sbg.getState(Main.LOSE).init(container, sbg);
sbg.enterState(Main.LOSE);
}
......
......@@ -40,7 +40,7 @@ public class Level5 extends Level {
private Image background = null;
Music mainMusic = null;
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
ArrayList<Obstacle> obstacles;
private float startY = (float) (HEIGHT - char_Height);
......@@ -55,7 +55,7 @@ public class Level5 extends Level {
private String meters = "M";
private static final int spawnInterval = levelLength / 15;
private static final int spawnInterval = levelLength / 75;
Font font;
TrueTypeFont ttf;
......@@ -97,6 +97,7 @@ public class Level5 extends Level {
font = new Font("Courier", Font.PLAIN, 12);
ttf = new TrueTypeFont(font, true);
obstacles = new ArrayList<Obstacle>();
}
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
......@@ -117,7 +118,9 @@ public class Level5 extends Level {
for (Obstacle o : obstacles) {
o.update(container, delta);
if (character.hitTest(o)) {
topScore = pointCounter / 20;
if (topScore < pointCounter / 20) {
topScore = pointCounter / 20;
}
sbg.getState(Main.LOSE).init(container, sbg);
sbg.enterState(Main.LOSE);
}
......
......@@ -40,7 +40,7 @@ public class Level6 extends Level {
private Image background = null;
Music mainMusic = null;
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
ArrayList<Obstacle> obstacles;
private float startY = (float) (HEIGHT - char_Height);
......@@ -98,6 +98,7 @@ public class Level6 extends Level {
font = new Font("Courier", Font.PLAIN, 12);
ttf = new TrueTypeFont(font, true);
obstacles = new ArrayList<Obstacle>();
}