added a nice rocket animation + optimizations

This commit is contained in:
Martin Dvořák 2026-10-19 22:48:28 +02:00
commit 7fe2bd30b1

View file

@ -27,7 +27,7 @@ public class Game extends JPanel implements ActionListener, KeyListener {
private final Timer timer = new Timer(16, this); // 60 FPS
private Font caveat;
private Font titleCaveat;
private int level = 1;
private boolean levelProgress = false; // this is used to make the transition from the main menu to the game level look cool
private int fadeAnimation = 0;
@ -36,11 +36,11 @@ public class Game extends JPanel implements ActionListener, KeyListener {
private BufferedImage planetZX311;
private BufferedImage planetZX311H;
private int planetYOffset = 0;
private String planetYOffsetDirection = "up"; // this makes the cool floating animation on the main menu
private int hoverYOffset = 0;
private String hoverDirection = "up"; // this makes the cool floating animation on the main menu
private int planetX = 450;
private int planetYPre = 140;
private int planetY = planetYPre + planetYOffset /6;
private int planetY = planetYPre + hoverYOffset /6;
private int planetSize = 128*3;
private Rectangle planetCollision = new Rectangle(planetX, planetY, planetSize, planetSize);
private boolean hoveringOverPlanet = false;
@ -58,6 +58,9 @@ public class Game extends JPanel implements ActionListener, KeyListener {
private boolean hoveringOverPlayButton = false;
private boolean hoveringOverQuitButton = false;
private BufferedImage rocket;
private int rocketYPre = windowHeight/2-125;
private int rocketY = windowHeight/2-125;
public Game() {
// sets the window size
setPreferredSize(new Dimension(windowWidth,windowHeight));
@ -70,6 +73,7 @@ public class Game extends JPanel implements ActionListener, KeyListener {
// loads up the fonts
try {
caveat = Font.createFont(Font.TRUETYPE_FONT, new File("assets/CaveatBrush-Regular.ttf"));
titleCaveat = caveat.deriveFont(Font.BOLD, 100f);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
caveat = new Font("Arial", Font.PLAIN, 12);
@ -80,6 +84,7 @@ public class Game extends JPanel implements ActionListener, KeyListener {
planetZX311H = ImageIO.read(new File("assets/planet-ZX311_highlighted.png"));
button = ImageIO.read(new File("assets/button.png"));
buttonH = ImageIO.read(new File("assets/button_highlighted.png"));
rocket = ImageIO.read(new File("assets/rocket.png"));
} catch (IOException e) {
e.printStackTrace();
}
@ -96,21 +101,25 @@ public class Game extends JPanel implements ActionListener, KeyListener {
@Override
public void actionPerformed(ActionEvent e) { // update step
Random rand = new Random();
starSize = rand.nextInt(2,4);
hoveringOverPlanet = planetCollision.contains(mouseX, mouseY);
hoveringOverPlayButton = playButtonCollision.contains(mouseX, mouseY);
hoveringOverQuitButton = quitButtonCollision.contains(mouseX, mouseY);
if (planetYOffsetDirection == "up") {
planetYOffset++;
if (planetYOffset > 90) {
planetYOffsetDirection = "down";
if (hoverDirection == "up") {
hoverYOffset++;
if (hoverYOffset > 90) {
hoverDirection = "down";
}
} else {
planetYOffset--;
if (planetYOffset <= 0) {
planetYOffsetDirection = "up";
hoverYOffset--;
if (hoverYOffset <= 0) {
hoverDirection = "up";
}
}
planetY = planetYPre + planetYOffset /6;
if (level == 1) { planetY = planetYPre + hoverYOffset /6; }
if (level == 2) {rocketY = rocketYPre + hoverYOffset /6; }
repaint(); // triggers render
}
@ -119,14 +128,12 @@ public class Game extends JPanel implements ActionListener, KeyListener {
super.paintComponent(g); // this clears the previous frame
if (level == 1) {
g.setColor(Color.WHITE);
Random rand = new Random();
for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars
starSize = rand.nextInt(1,3);
g.fillOval(starX[i]-starSize, starY[i]-starSize, starSize, starSize);
}
g.setColor(Color.GRAY);
g.setFont(caveat.deriveFont(Font.BOLD, 100f));
g.setFont(titleCaveat);
g.drawString("Very Cool Space Sim", 72, 100);
if (planetZX311 != null) {
@ -171,19 +178,19 @@ public class Game extends JPanel implements ActionListener, KeyListener {
}
if (level == 2) {
g.setColor(Color.WHITE);
Random rand = new Random();
for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars
starSize = rand.nextInt(1,8);
g.fillOval(starX[i]-starSize, starY[i]-starSize, starSize, starSize);
}
g.drawImage(rocket, windowWidth/2-73, rocketY, 147, 234, this);
if (levelProgress) {
g.setColor(Color.BLACK);
g.fillRect(windowWidth-fadeAnimation, 0, windowWidth, windowHeight);
fadeAnimation += 20;
}
}
Toolkit.getDefaultToolkit().sync();
}
@Override