Enemy Causes Death

master
Chara1236 2022-06-05 22:18:10 -04:00
parent 5fec81cf0e
commit 2f5ded58ab
6 changed files with 76 additions and 25 deletions

View File

@ -14,5 +14,5 @@ sssssssssssssssssd 1 zxc z
sssssssssssssssssd sssssssssssssssssd
sssssssssssssssssd qwwwwwwwwe sssssssssssssssssd qwwwwwwwwe
sssssssssssssssssd qrsssssssstwe sssssssssssssssssd qrsssssssstwe
sssssssssssssssssd qwwrssssssssssstwe qwe sssssssssssssssssd qwwrssssssssssstwe !!! qwe
ssssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrsssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrstwwwwww3 ssssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrsssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrstwwwwww3

View File

@ -27,15 +27,15 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public Thread gameThread; public Thread gameThread;
public Image image; public Image image;
public Graphics graphics; public Graphics graphics;
public Player player; public static Player player;
public BackgroundImage background; public BackgroundImage background;
public int playerFrame, enemyFrame; public int playerFrame, enemyFrame;
// keeps track of how many ticks has elapsed since last frame change // keeps track of how many ticks has elapsed since last frame change
public int playerFrameCounter = 0; public int playerFrameCounter = 0;
public int enemyFrameCounter = 0; public int enemyFrameCounter = 0;
public BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11]; public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11];
public BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3]; public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3];
public static ArrayList<Tile>map = new ArrayList<Tile>(); public static ArrayList<Tile>map = new ArrayList<Tile>();
public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>(); public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>();
@ -77,8 +77,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
e.printStackTrace(); e.printStackTrace();
} }
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', playerSpriteArray); //create a player controlled player, set start location to middle of screenk player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', playerSpriteArray); //create a player controlled player, set start location to middle of screenk
enemy.add(new NonPlayer(1200, 100, slimeSpriteArray, 50, 28));
enemy.add(new NonPlayer(0, 100, slimeSpriteArray, 50, 28));
// the height of 35 is set because it is half of the original tile height (i.e., 70px) // the height of 35 is set because it is half of the original tile height (i.e., 70px)
this.setFocusable(true); //make everything in this class appear on the screen this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input this.addKeyListener(this); //start listening for keyboard input
@ -116,7 +114,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
enemy.get(i).draw(g, enemyFrame); enemy.get(i).draw(g, enemyFrame);
} }
playerFrameCounter += player.draw(g, playerFrame); playerFrameCounter += player.draw(g, playerFrame);
g.drawString(camera.x+" "+map.get(0).x,100,100); g.drawString(camera.x+" "+player.y,100,100);
} }
//call the move methods in other classes to update positions //call the move methods in other classes to update positions
@ -131,12 +129,17 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//handles all collision detection and responds accordingly //handles all collision detection and responds accordingly
public void checkCollision() { public void checkCollision() {
player.isGrounded = false; player.isGrounded = false;
for (NonPlayer n: enemy) {
n.isGrounded = false;
}
for (Tile i : map) { for (Tile i : map) {
i.update(); i.update();
} }
for (NonPlayer n: enemy) {
n.update();
n.isGrounded = false;
if(n.collidePlayer(player)){
player.alive = false;
}
}
//force player to remain on screen (For the most part) //force player to remain on screen (For the most part)
if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) { if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
@ -163,11 +166,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen //run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){ public void run(){
LevelManager.setLevel(1); LevelManager.setLevel(1);
try {
MapReader.inputMap(map, "saves/Level1.txt");
} catch (IOException | SpriteException e) {
throw new RuntimeException(e);
}
//the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen. //the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
long lastTime = System.nanoTime(); long lastTime = System.nanoTime();
double amountOfTicks = 60; double amountOfTicks = 60;
@ -198,11 +196,16 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//if a key is pressed, we'll send it over to the Player class for processing //if a key is pressed, we'll send it over to the Player class for processing
public void keyPressed(KeyEvent e){ public void keyPressed(KeyEvent e){
player.keyPressed(e); player.keyPressed(e);
} }
//if a key is released, we'll send it over to the Player class for processing //if a key is released, we'll send it over to the Player class for processing
public void keyReleased(KeyEvent e){ public void keyReleased(KeyEvent e){
player.keyReleased(e); player.keyReleased(e);
if(e.getKeyChar() == 'p'){
LevelManager.nextLevel();
}
} }
//left empty because we don't need it; must be here because it is required to be overridded by the KeyListener interface //left empty because we don't need it; must be here because it is required to be overridded by the KeyListener interface

View File

@ -1,3 +1,7 @@
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;
public class LevelManager { public class LevelManager {
public static int level = 1; public static int level = 1;
public static int xSpawn = 0; public static int xSpawn = 0;
@ -8,12 +12,25 @@ public class LevelManager {
LevelManager.level = level; LevelManager.level = level;
if(level == 1){ if(level == 1){
xSpawn = 0; xSpawn = 0;
ySpawn = 600; ySpawn = 300;
filePath = "saves/Level1.txt"; filePath = "saves/Level1.txt";
} else if(level == 2){ } else if(level == 2){
xSpawn = 0; xSpawn = 200;
ySpawn = 600; ySpawn = 100;
filePath = "saves/Level2.txt"; filePath = "saves/Level2.txt";
} }
try {
MapReader.inputMap(GamePanel.map, GamePanel.enemy,filePath);
} catch (IOException | SpriteException e) {
throw new RuntimeException(e);
} catch (UnsupportedAudioFileException e) {
throw new RuntimeException(e);
} catch (LineUnavailableException e) {
throw new RuntimeException(e);
}
GamePanel.player.reset();
}
public static void nextLevel(){
setLevel(level+1);
} }
} }

View File

@ -1,3 +1,5 @@
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Scanner; import java.util.Scanner;
@ -15,7 +17,9 @@ public class MapReader {
!: Slime !: Slime
Grass: Grass:
*/ */
public static void inputMap(ArrayList<Tile> map, String filePath) throws IOException, SpriteException { public static void inputMap(ArrayList<Tile> map, ArrayList<NonPlayer>enemy , String filePath) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
enemy.clear();
map.clear();
String file = FileManager.readFile(LevelManager.filePath); String file = FileManager.readFile(LevelManager.filePath);
int x = -GamePanel.GAME_WIDTH/2 + Tile.length; int x = -GamePanel.GAME_WIDTH/2 + Tile.length;
int y = 0; int y = 0;
@ -59,6 +63,9 @@ public class MapReader {
} else if(file.charAt(i)=='b'){ } else if(file.charAt(i)=='b'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/box.png"))); map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/box.png")));
map.get(map.size()-1).collision = false; map.get(map.size()-1).collision = false;
} else if(file.charAt(i)=='!'){
enemy.add(new NonPlayer(x, y, GamePanel.slimeSpriteArray, 50, 28));
} }
x+=Tile.length; x+=Tile.length;
} }

View File

@ -16,6 +16,9 @@ public class NonPlayer extends GenericSprite {
public int currentXDirection, currentYDirection; public int currentXDirection, currentYDirection;
public boolean isDead; public boolean isDead;
public int realX;
// private final Sound bump; // private final Sound bump;
public BufferedImage[][][] spriteArray; public BufferedImage[][][] spriteArray;
@ -40,6 +43,13 @@ public class NonPlayer extends GenericSprite {
} }
return false; return false;
} }
public boolean collidePlayer(Player p){
if(realX+npcWidth>p.x&&realX<p.x+Player.PLAYER_WIDTH&&y-p.y<Player.PLAYER_HEIGHT&&p.y-y<npcHeight){
return true;
}
return false;
}
public boolean canUpdate(double x, double y){ public boolean canUpdate(double x, double y){
boolean canUpdate = true; boolean canUpdate = true;
for(Tile i: GamePanel.map){ for(Tile i: GamePanel.map){
@ -51,7 +61,9 @@ public class NonPlayer extends GenericSprite {
return canUpdate; return canUpdate;
} }
public void update(){
realX = x-GamePanel.camera.x;
}
public void move(){ public void move(){
if(!canUpdate(xVelocity, 0)){ if(!canUpdate(xVelocity, 0)){
xVelocity*=-1; xVelocity*=-1;

View File

@ -12,11 +12,12 @@ import java.io.IOException;
public class Player extends GenericSprite { public class Player extends GenericSprite {
public final int SPEED = 5; public final int SPEED = 5;
public static final int PLAYER_WIDTH = 64; public static final int PLAYER_WIDTH = 52;
public static final int PLAYER_HEIGHT = 94; public static final int PLAYER_HEIGHT = 94;
public int lastXDirection, lastYDirection, lastFrame; public int lastXDirection, lastYDirection, lastFrame;
public int upKey, downKey, rightKey, leftKey; public int upKey, downKey, rightKey, leftKey;
public boolean alive;
private final Sound jump; private final Sound jump;
// sA[0] is -x, -y // sA[0] is -x, -y
// sA[1] is x, -y // sA[1] is x, -y
@ -31,6 +32,7 @@ public class Player extends GenericSprite {
this.leftKey = leftKey; this.leftKey = leftKey;
this.rightKey = rightKey; this.rightKey = rightKey;
spriteArray = sprites; spriteArray = sprites;
alive = true;
} }
@ -53,6 +55,7 @@ public class Player extends GenericSprite {
downPressed = true; downPressed = true;
} }
} }
// stops moving paddle when key is released // stops moving paddle when key is released
@ -84,8 +87,8 @@ public class Player extends GenericSprite {
public boolean canUpdate(double x, double y){ public boolean canUpdate(double x, double y){
boolean canUpdate = true; boolean canUpdate = true;
for(Tile i: GamePanel.map){ for(int i=0; i<GamePanel.map.size(); i++) {
if(collide(i,this.x+x,this.y+y)){ if(collide(GamePanel.map.get(i),this.x+x,this.y+y)){
canUpdate = false; canUpdate = false;
break; break;
} }
@ -151,19 +154,28 @@ public class Player extends GenericSprite {
yVelocity+=1; yVelocity+=1;
} }
} }
if(!alive){
alive = true;
reset();
}
capSpeed(); capSpeed();
} }
public void reset(){
GamePanel.camera.x = LevelManager.xSpawn;
y = LevelManager.ySpawn;
}
public int draw(Graphics g, int frame) { public int draw(Graphics g, int frame) {
frame %= spriteArray[0][0].length; frame %= spriteArray[0][0].length;
if (!upPressed && !downPressed && !leftPressed && !rightPressed) { if (!upPressed && !downPressed && !leftPressed && !rightPressed) {
g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x-6, y, null); g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x-10, y, null);
return 0; return 0;
} else { } else {
lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2; lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2;
lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2; lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2;
lastFrame = frame; lastFrame = frame;
g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x-6, y, null); g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x-10, y, null);
return 1; return 1;
} }
} }