Finally added a simple player movement system (no animated sprites yet tho)

This commit is contained in:
Martin Dvořák 2026-10-22 01:49:30 +02:00
commit e15e1fc5a8
6 changed files with 91 additions and 8 deletions

4
.gitignore vendored
View file

@ -18,6 +18,10 @@ out/
!**/src/main/**/out/ !**/src/main/**/out/
!**/src/test/**/out/ !**/src/test/**/out/
# Java
build/
staging/
### Eclipse ### ### Eclipse ###
.apt_generated .apt_generated
.classpath .classpath

View file

@ -1,11 +1,11 @@
# My own SpaceSim written in Java # My own SpaceSim written in Java
pretty self-explanatory idk what else to write... pretty self-explanatory idk what else to write...
## docs i'd like to keep close ## docs I'd like to keep close
Swing tutorial (Oracle): https://docs.oracle.com/javase/tutorial/uiswing/ JFrame, JPanel, key listeners, timers, etc. Swing tutorial (Oracle): https://docs.oracle.com/javase/tutorial/uiswing/ JFrame, JPanel, key listeners, timers, etc.
Java 2D graphics tutorial: https://docs.oracle.com/javase/tutorial/2d/ drawing shapes, images, and Graphics2D. Java 2D graphics tutorial: https://docs.oracle.com/javase/tutorial/2d/ drawing shapes, images, and Graphics2D.
## credits for assets ## credits for assets
Ive used font from [OpenFont Library](https://openfont.org/) (the one called Caveat Brush) Ive used font from [OpenFont Library](https://openfont.org/) (the one called Caveat Brush)
all other assets are made by me, images are made in Krita. while not a direct copy I took heavy inspiration for the plyer sprites from this guy: https://tnypixels.itch.io/top-down-rpg-character-sprite-walk-idle-animation
while not a direct copy I took heavy inspiration for the plyer sprites from this guy: https://tnypixels.itch.io/top-down-rpg-character-sprite-walk-idle-animation all other assets are made by me, images/sprites are made in Krita.

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;
@ -107,9 +107,17 @@ public class Game extends JPanel implements ActionListener {
public int actionTimer = 0; public int actionTimer = 0;
// defines the initial player variables // defines the initial player variables
public int playerX = 525; public int playerX = 423;
public int playerY = 375; public int playerY = 310;
public int playerOldY = playerY;
public int playerSizeX = 14*4;
public int playerSizeY = 18*4;
public String playerDirection = "forward";
public BufferedImage playerCurrentSprite;
public int playerWalkingModifier = 5;
public boolean playerOnFeet = true;
public boolean playerJumped = false;
public boolean playerActive = true;
// 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

@ -9,8 +9,30 @@ public class input implements KeyListener {
} }
@Override @Override
public void keyPressed(KeyEvent e) { // input public void keyPressed(KeyEvent e) { // input
if (m.playerActive) {
switch (e.getKeyCode()) {
case KeyEvent.VK_A:
m.playerDirection = "left";
break;
case KeyEvent.VK_D:
m.playerDirection = "right";
break;
case KeyEvent.VK_SPACE:
if (!m.playerJumped && m.playerOnFeet) {
m.playerJumped = true;
m.playerOldY = m.playerY;
}
break;
/* ONLY FOR DEBUGGING BELOW
case KeyEvent.VK_A -> m.playerX -= m.playerWalkingModifier;
case KeyEvent.VK_D -> m.playerX += m.playerWalkingModifier;
case KeyEvent.VK_S -> m.playerY += 1;
case KeyEvent.VK_W -> m.playerY -= 1; */
}
}
switch (e.getKeyCode()) { switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE -> System.exit(0); case KeyEvent.VK_ESCAPE -> System.exit(0);
case KeyEvent.VK_CONTROL -> System.out.println("X+Y="+m.playerX+"+"+m.playerY);
} }
} }
@ -19,6 +41,17 @@ public class input implements KeyListener {
@Override @Override
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) {
switch (e.getKeyCode()) {
case KeyEvent.VK_A -> m.playerDirection = "forward";
case KeyEvent.VK_D -> m.playerDirection = "forward";
/* ONLY FOR DEBUGGING BELOW
case KeyEvent.VK_A -> m.playerX -= m.playerWalkingModifier;
case KeyEvent.VK_D -> m.playerX += m.playerWalkingModifier;
case KeyEvent.VK_S -> m.playerY += 1;
case KeyEvent.VK_W -> m.playerY -= 1; */
}
}
} }

View file

@ -44,6 +44,8 @@ public class loadAssets {
m.playerRight0 = ImageIO.read(Game.class.getResource("/assets/player/playerSpriteRight.png")); m.playerRight0 = ImageIO.read(Game.class.getResource("/assets/player/playerSpriteRight.png"));
m.playerRight1 = ImageIO.read(Game.class.getResource("/assets/player/playerSpriteRightWalk0.png")); m.playerRight1 = ImageIO.read(Game.class.getResource("/assets/player/playerSpriteRightWalk0.png"));
m.playerRight2 = ImageIO.read(Game.class.getResource("/assets/player/playerSpriteRightWalk1.png")); m.playerRight2 = ImageIO.read(Game.class.getResource("/assets/player/playerSpriteRightWalk1.png"));
m.playerCurrentSprite = m.playerForward0;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -29,7 +29,7 @@ public class update {
} }
if (m.windowWidth - m.fadeAnimation <= 0) { m.level = 2; } if (m.windowWidth - m.fadeAnimation <= 0) { m.level = 2; }
} }
if (m.level == 2) { else if (m.level == 2) {
if (m.actionTimer < 1000 && m.actionTimer > 950) { if (m.actionTimer < 1000 && m.actionTimer > 950) {
m.opacityAnimation = 0.01f; m.opacityAnimation = 0.01f;
} }
@ -38,9 +38,42 @@ public class update {
m.actionTimer++; m.actionTimer++;
m.fadeAnimation += 20; m.fadeAnimation += 20;
if (m.actionTimer > 1760) { if (m.actionTimer > 1760) {
m.playerActive = true;
m.level = 3; m.level = 3;
} }
} }
if (m.playerActive) {
if (m.playerDirection == "forward"){
m.playerCurrentSprite = m.playerForward0;
} else if (m.playerDirection == "backward") {
m.playerCurrentSprite = m.playerBackward0;
} else if (m.playerDirection == "right") {
m.playerCurrentSprite = m.playerRight0;
m.playerX += m.playerWalkingModifier;
} else if (m.playerDirection == "left") {
m.playerCurrentSprite = m.playerLeft0;
m.playerX -= m.playerWalkingModifier;
}
if (m.playerJumped) {
m.playerOnFeet = false;
m.playerY -= 10;
if (m.playerOldY-100 >= m.playerY) { m.playerJumped = false; }
}
// collisions
if (m.level == 3 && (m.playerY < 338 || (m.playerX >= 583 || m.playerX <= 274) && m.playerY < 366)) {
m.playerY += 5;
m.playerOnFeet = false;
} else if (m.playerX < 583 && m.playerX > 274) {
m.playerY = 338;
m.playerOnFeet = true;
} else {
m.playerOnFeet = true;
}
// first ship
if (m.playerX < -m.playerSizeX || m.playerX > m.windowWidth) { m.level = 4; }
}
// triggers render // triggers render
m.repaint(); m.repaint();
} }
@ -188,6 +221,9 @@ public class update {
g.drawString("that I could ship it for now so I", 30, m.windowHeight/2-160+280); g.drawString("that I could ship it for now so I", 30, m.windowHeight/2-160+280);
g.drawString("could be safe from ratticus (I'm scared)", 30, m.windowHeight/2-160+350); g.drawString("could be safe from ratticus (I'm scared)", 30, m.windowHeight/2-160+350);
} }
if (m.playerActive) {
g.drawImage(m.playerCurrentSprite, m.playerX, m.playerY, m.playerSizeX, m.playerSizeY, m);
}
Toolkit.getDefaultToolkit().sync(); Toolkit.getDefaultToolkit().sync();
} }
} }