Got animated walking player sprites working

This commit is contained in:
dvorakmortonxd 2026-10-22 12:00:52 +02:00
commit bbf6b24fa0
3 changed files with 52 additions and 17 deletions

View file

@ -60,7 +60,7 @@ public class Game extends JPanel implements ActionListener {
/* defines the level which is used to determine what exactly shows (for example main menu is level 1), /* defines the level which is used to determine what exactly shows (for example main menu is level 1),
levelProgress which is only used for the animation from level 1 to level 2 levelProgress which is only used for the animation from level 1 to level 2
and fadeAnimation which is also used for the animation from level 1 to level 2 */ and fadeAnimation which is also used for the animation from level 1 to level 2 */
public int level = 1; public int level = 3;
public boolean levelProgress = false; // this is used to make the transition from the Game menu to the game level look cool public boolean levelProgress = false; // this is used to make the transition from the Game menu to the game level look cool
public int fadeAnimation = 0; public int fadeAnimation = 0;
@ -112,12 +112,16 @@ public class Game extends JPanel implements ActionListener {
public int playerOldY = playerY; public int playerOldY = playerY;
public int playerSizeX = 14*4; public int playerSizeX = 14*4;
public int playerSizeY = 18*4; public int playerSizeY = 18*4;
public String playerDirection = "forward"; public String playerDirection = "none";
public BufferedImage playerCurrentSprite; public BufferedImage playerCurrentSprite;
public int playerWalkingModifier = 5; public int playerWalkingModifier = 5;
public boolean playerOnFeet = true; public boolean playerOnFeet = true;
public boolean playerJumped = false; public boolean playerJumped = false;
public boolean playerActive = false; public boolean playerActive = true;
public int walkTick;
public static int ticksPerFrame = 8;
public static int[] walkCycle = {1, 0, 2, 0};
// Starts update.java which is basically a class that contains all the stuff that happens every tick // Starts update.java which is basically a class that contains all the stuff that happens every tick
@Override @Override
public void actionPerformed(ActionEvent e) { // update step public void actionPerformed(ActionEvent e) { // update step

View file

@ -11,9 +11,15 @@ public class input implements KeyListener {
public void keyPressed(KeyEvent e) { // input public void keyPressed(KeyEvent e) { // input
if (m.playerActive) { if (m.playerActive) {
switch (e.getKeyCode()) { switch (e.getKeyCode()) {
case KeyEvent.VK_W:
m.playerDirection = "forward";
break;
case KeyEvent.VK_A: case KeyEvent.VK_A:
m.playerDirection = "left"; m.playerDirection = "left";
break; break;
case KeyEvent.VK_S:
m.playerDirection = "backward";
break;
case KeyEvent.VK_D: case KeyEvent.VK_D:
m.playerDirection = "right"; m.playerDirection = "right";
break; break;
@ -43,8 +49,10 @@ public class input implements KeyListener {
public void keyReleased(KeyEvent e) { // what happens when you let go of a key public void keyReleased(KeyEvent e) { // what happens when you let go of a key
if (m.playerActive) { if (m.playerActive) {
switch (e.getKeyCode()) { switch (e.getKeyCode()) {
case KeyEvent.VK_A -> m.playerDirection = "forward"; case KeyEvent.VK_W -> m.playerDirection = "none";
case KeyEvent.VK_D -> m.playerDirection = "forward"; case KeyEvent.VK_A -> m.playerDirection = "none";
case KeyEvent.VK_S -> m.playerDirection = "none";
case KeyEvent.VK_D -> m.playerDirection = "none";
/* ONLY FOR DEBUGGING BELOW /* ONLY FOR DEBUGGING BELOW
case KeyEvent.VK_A -> m.playerX -= m.playerWalkingModifier; case KeyEvent.VK_A -> m.playerX -= m.playerWalkingModifier;
case KeyEvent.VK_D -> m.playerX += m.playerWalkingModifier; case KeyEvent.VK_D -> m.playerX += m.playerWalkingModifier;

View file

@ -1,9 +1,19 @@
package dev.dvorakmortonxd; package dev.dvorakmortonxd;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random; import java.util.Random;
public class update { public class update {
public static BufferedImage spriteFor(Game m, String direction, int frame) {
return switch (direction) {
case "forward" -> new BufferedImage[]{m.playerForward0, m.playerForward1, m.playerForward2}[frame];
case "backward" -> new BufferedImage[]{m.playerBackward0, m.playerBackward1, m.playerBackward2}[frame];
case "left" -> new BufferedImage[]{m.playerLeft0, m.playerLeft1, m.playerLeft2}[frame];
case "right" -> new BufferedImage[]{m.playerRight0, m.playerRight1, m.playerRight2}[frame];
default -> m.playerForward0;
};
}
static void performPerTick(Game m) { static void performPerTick(Game m) {
Random rand = new Random(); Random rand = new Random();
m.starSize = rand.nextInt(2,4); m.starSize = rand.nextInt(2,4);
@ -43,34 +53,47 @@ public class update {
} }
} }
if (m.playerActive) { if (m.playerActive) {
if (m.playerDirection == "forward"){ boolean moving = false;
m.playerCurrentSprite = m.playerForward0; if (m.playerDirection.equals("right")) {
} else if (m.playerDirection == "backward") {
m.playerCurrentSprite = m.playerBackward0;
} else if (m.playerDirection == "right") {
m.playerCurrentSprite = m.playerRight0;
m.playerX += m.playerWalkingModifier; m.playerX += m.playerWalkingModifier;
} else if (m.playerDirection == "left") { moving = true;
m.playerCurrentSprite = m.playerLeft0; } else if (m.playerDirection.equals("left")) {
m.playerX -= m.playerWalkingModifier; m.playerX -= m.playerWalkingModifier;
moving = true;
} else if (m.playerDirection.equals("forward")) {
moving = true;
} else if (m.playerDirection.equals("backward")) {
moving = true;
} else {
moving = false;
}
int frame = 0;
if (moving) {
frame = m.walkCycle[(m.walkTick / m.ticksPerFrame) % m.walkCycle.length];
m.walkTick++;
} else {
m.walkTick = 0;
} }
if (m.playerJumped) { if (m.playerJumped) {
m.playerOnFeet = false; m.playerOnFeet = false;
m.playerY -= 10; m.playerY -= 20;
if (m.playerOldY-100 >= m.playerY) { m.playerJumped = false; } if (m.playerOldY-80 >= m.playerY) { m.playerJumped = false; }
} }
// collisions // collisions
if (m.level == 3 && (m.playerY < 338 || (m.playerX >= 583 || m.playerX <= 274) && m.playerY < 366)) { if (m.level == 3 && (m.playerY < 338 || (m.playerX >= 583 || m.playerX <= 274) && m.playerY < 366)) {
m.playerY += 5; m.playerY += 10;
m.playerOnFeet = false; m.playerOnFeet = false;
} else if (m.playerX < 583 && m.playerX > 274) { } else if (m.level == 3 && m.playerX < 583 && m.playerX > 274) {
m.playerY = 338; m.playerY = 338;
m.playerOnFeet = true; m.playerOnFeet = true;
} else { } else {
m.playerOnFeet = true; m.playerOnFeet = true;
} }
// update the player sprite based on direction and if hes walking and frame
m.playerCurrentSprite = spriteFor(m, m.playerDirection, frame);
// first ship // first ship
if (m.playerX < -m.playerSizeX || m.playerX > m.windowWidth) { m.level = 4; } if (m.playerX < -m.playerSizeX || m.playerX > m.windowWidth) { m.level = 4; }
} }