master
Chara1236 2022-06-05 12:05:49 -04:00
parent 4d58cece36
commit 76d914955f
1 changed files with 7 additions and 14 deletions

View File

@ -79,7 +79,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
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(1200, 100, slimeSpriteArray, 50, 28));
enemy.add(new NonPlayer(0, 100, slimeSpriteArray, 50, 28)); enemy.add(new NonPlayer(0, 100, slimeSpriteArray, 50, 28));
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', spriteArray); //create a player controlled player, set start location to middle of screenk
// 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
@ -133,7 +132,7 @@ 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) { for (NonPlayer n: enemy) {
n.isGrounded = false; n.isGrounded = false;
} }
for (Tile i : map) { for (Tile i : map) {
@ -144,9 +143,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
player.y = 0; player.y = 0;
} }
if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) { if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT; player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT;
player.yVelocity = 0; player.yVelocity = 0;
player.isGrounded = true; player.isGrounded = true;
} }
if (player.x <= 0) { if (player.x <= 0) {
player.x = 0; player.x = 0;
@ -155,15 +154,14 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
player.x = GAME_WIDTH - Player.PLAYER_WIDTH; player.x = GAME_WIDTH - Player.PLAYER_WIDTH;
} }
for (NonPlayer n : enemy) { for (NonPlayer n: enemy) {
if (n.y >= GAME_HEIGHT - n.npcHeight) { if (n.y >= GAME_HEIGHT - n.npcHeight) {
n.y = GAME_HEIGHT - n.npcHeight; n.y = GAME_HEIGHT - n.npcHeight;
n.yVelocity = 0; n.yVelocity = 0;
n.isGrounded = true; n.isGrounded = true;
} }
} }
} }
//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(){
@ -191,14 +189,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
repaint(); repaint();
if (playerFrameCounter > 5) { if (playerFrameCounter > 5) {
// increment sprite image to be used and keeps it below 12 // increment sprite image to be used and keeps it below 12
playerFrame += 1; playerFrame = (playerFrame + 1) % 11;
playerFrameCounter -= 5; playerFrameCounter -= 5;
} }
if (enemyFrameCounter > 5) {
// increment sprite image to be used and keeps it below 12
enemyFrame += 1;
enemyFrameCounter -= 5;
}
delta--; delta--;
} }
} }