From b4b03de400652d03080d0d272ce6534b1716d6f8 Mon Sep 17 00:00:00 2001 From: dvorakmortonxd Date: Mon, 19 Oct 2026 14:48:20 +0200 Subject: [PATCH] made the stars sparkle and added some neat clicking animations --- src/main/java/dev/dvorakmortonxd/Game.java | 141 +++++++++++++-------- 1 file changed, 89 insertions(+), 52 deletions(-) diff --git a/src/main/java/dev/dvorakmortonxd/Game.java b/src/main/java/dev/dvorakmortonxd/Game.java index 9f3a65b..c79f3c3 100644 --- a/src/main/java/dev/dvorakmortonxd/Game.java +++ b/src/main/java/dev/dvorakmortonxd/Game.java @@ -16,26 +16,29 @@ import java.util.Random; public class Game extends JPanel implements ActionListener, KeyListener { private final int windowWidth = 1000; private final int windowHeight = 600; - private final int MENU_STAR_COUNT = 256; + private final int MENU_STAR_COUNT = 64; private final int[] starX = new int[MENU_STAR_COUNT]; private final int[] starY = new int[MENU_STAR_COUNT]; private final Timer timer = new Timer(16, this); // 60 FPS private Font caveat; + private int level = 1; + private int mouseX, mouseY; private BufferedImage planetZX311; - private BufferedImage planetZX311h; + private BufferedImage planetZX311H; private int planetYOffset = 0; private String planetYOffsetDirection = "up"; // this makes the cool floating animation on the main menu private int planetX = 580; - private int planetY = 140 + planetYOffset /6; + private int planetYPre = 140; + private int planetY = planetYPre + planetYOffset /6; private int planetSize = 128*3; private Rectangle planetCollision = new Rectangle(planetX, planetY, planetSize, planetSize); private boolean hoveringOverPlanet = false; private BufferedImage button; - private BufferedImage buttonh; + private BufferedImage buttonH; private int buttonX = 130; private int playButtonY = 210; private int quitButtonY = 350; @@ -65,20 +68,14 @@ public class Game extends JPanel implements ActionListener, KeyListener { // loads up the image assets try { planetZX311 = ImageIO.read(new File("assets/planet-ZX311.png")); - planetZX311h = ImageIO.read(new File("assets/planet-ZX311_highlighted.png")); + 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")); + buttonH = ImageIO.read(new File("assets/button_highlighted.png")); } catch (IOException e) { e.printStackTrace(); } - addMouseMotionListener(new MouseAdapter() { - @Override - public void mouseMoved(MouseEvent e) { - mouseX = e.getX(); - mouseY = e.getY(); - } - }); + initMouse(); setBackground(Color.BLACK); setFocusable(true); @@ -104,53 +101,55 @@ public class Game extends JPanel implements ActionListener, KeyListener { planetYOffsetDirection = "up"; } } - planetY = 140 + planetYOffset /6; + planetY = planetYPre + planetYOffset /6; repaint(); // triggers render } @Override protected void paintComponent(Graphics g) { // render step super.paintComponent(g); // this clears the previous frame - - g.setColor(Color.WHITE); - for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars - g.fillOval(starX[i], starY[i], 2, 2); - } - - g.setColor(Color.GRAY); - g.setFont(caveat.deriveFont(Font.BOLD, 100f)); - g.drawString("Very Cool Space Sim", 40, 100); - - if (planetZX311 != null) { - if (!hoveringOverPlanet) { - g.drawImage(planetZX311, planetX, planetY, planetSize, planetSize, this); - } else { - g.drawImage(planetZX311h, planetX, planetY, planetSize, planetSize, this); + if (level == 1) { + g.setColor(Color.WHITE); + Random rand = new Random(); + for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars + g.fillOval(starX[i], starY[i], rand.nextInt(1,3), rand.nextInt(1,2)); } - } - if (button != null) { - if (!hoveringOverPlayButton) { - g.drawImage(button, buttonX, playButtonY, buttonWidth, buttonHeight, this); - g.setColor(Color.BLACK); - g.setFont(caveat.deriveFont(Font.BOLD, 48f)); - g.drawString("Start the game", buttonX+25, playButtonY+67); - } else { - g.drawImage(buttonh, buttonX, playButtonY, buttonWidth, buttonHeight, this); - g.setColor(Color.WHITE); - g.setFont(caveat.deriveFont(Font.BOLD, 48f)); - g.drawString("Start the game", buttonX+25, playButtonY+67); + g.setColor(Color.GRAY); + g.setFont(caveat.deriveFont(Font.BOLD, 100f)); + g.drawString("Very Cool Space Sim", 40, 100); + + if (planetZX311 != null) { + if (!hoveringOverPlanet) { + g.drawImage(planetZX311, planetX, planetY, planetSize, planetSize, this); + } else { + g.drawImage(planetZX311H, planetX, planetY, planetSize, planetSize, this); + } } - if (!hoveringOverQuitButton) { - g.drawImage(button, buttonX, quitButtonY, buttonWidth, buttonHeight, this); - g.setColor(Color.BLACK); - g.setFont(caveat.deriveFont(Font.BOLD, 40f)); - g.drawString("Quit (pretty lame)", buttonX+24, quitButtonY+67); - } else { - g.drawImage(buttonh, buttonX, quitButtonY, buttonWidth, buttonHeight, this); - g.setColor(Color.WHITE); - g.setFont(caveat.deriveFont(Font.BOLD, 40f)); - g.drawString("Quit (pretty lame)", buttonX+24, quitButtonY+67); + + if (button != null) { + if (!hoveringOverPlayButton) { + g.drawImage(button, buttonX, playButtonY, buttonWidth, buttonHeight, this); + g.setColor(Color.BLACK); + g.setFont(caveat.deriveFont(Font.BOLD, 48f)); + g.drawString("Start the game", buttonX+25, playButtonY+67); + } else { + g.drawImage(buttonH, buttonX, playButtonY, buttonWidth, buttonHeight, this); + g.setColor(Color.WHITE); + g.setFont(caveat.deriveFont(Font.BOLD, 48f)); + g.drawString("Start the game", buttonX+25, playButtonY+67); + } + if (!hoveringOverQuitButton) { + g.drawImage(button, buttonX, quitButtonY, buttonWidth, buttonHeight, this); + g.setColor(Color.BLACK); + g.setFont(caveat.deriveFont(Font.BOLD, 40f)); + g.drawString("Quit (pretty lame)", buttonX+24, quitButtonY+67); + } else { + g.drawImage(buttonH, buttonX, quitButtonY, buttonWidth, buttonHeight, this); + g.setColor(Color.WHITE); + g.setFont(caveat.deriveFont(Font.BOLD, 40f)); + g.drawString("Quit (pretty lame)", buttonX+24, quitButtonY+67); + } } } } @@ -162,6 +161,44 @@ public class Game extends JPanel implements ActionListener, KeyListener { } } + private void initMouse() { + addMouseMotionListener(new MouseAdapter() { + @Override + public void mouseMoved(MouseEvent e) { + mouseX = e.getX(); + mouseY = e.getY(); + } + }); + + addMouseListener(new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverQuitButton) { + System.exit(0); + } + if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlayButton) { + level = 2; + } + if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlanet) { + planetX = planetX-5; + planetYPre = planetYPre+5; + } + + } + }); + + addMouseListener(new MouseAdapter() { + @Override + public void mouseReleased(MouseEvent e) { + if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlanet) { + planetX = planetX+5; + planetYPre = planetYPre-5; + } + + } + }); + } + @Override public void keyReleased(KeyEvent e) { // what happens when you let go of a key } @@ -172,7 +209,7 @@ public class Game extends JPanel implements ActionListener, KeyListener { frame.setResizable(false); // makes it so gamers cant just resize the window and leave black parts of the window frame.add(new Game()); frame.pack(); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // makes the X button on the titlebar functional + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // makes the X button on the title bar functional frame.setLocationRelativeTo(null); // centers the window on the screen frame.setVisible(true); // makes the window visible }