made the stars sparkle and added some neat clicking animations

This commit is contained in:
dvorakmortonxd 2026-10-19 14:48:20 +02:00
commit b4b03de400

View file

@ -16,26 +16,29 @@ import java.util.Random;
public class Game extends JPanel implements ActionListener, KeyListener { public class Game extends JPanel implements ActionListener, KeyListener {
private final int windowWidth = 1000; private final int windowWidth = 1000;
private final int windowHeight = 600; 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[] starX = new int[MENU_STAR_COUNT];
private final int[] starY = 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 final Timer timer = new Timer(16, this); // 60 FPS
private Font caveat; private Font caveat;
private int level = 1;
private int mouseX, mouseY; private int mouseX, mouseY;
private BufferedImage planetZX311; private BufferedImage planetZX311;
private BufferedImage planetZX311h; private BufferedImage planetZX311H;
private int planetYOffset = 0; private int planetYOffset = 0;
private String planetYOffsetDirection = "up"; // this makes the cool floating animation on the main menu private String planetYOffsetDirection = "up"; // this makes the cool floating animation on the main menu
private int planetX = 580; 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 int planetSize = 128*3;
private Rectangle planetCollision = new Rectangle(planetX, planetY, planetSize, planetSize); private Rectangle planetCollision = new Rectangle(planetX, planetY, planetSize, planetSize);
private boolean hoveringOverPlanet = false; private boolean hoveringOverPlanet = false;
private BufferedImage button; private BufferedImage button;
private BufferedImage buttonh; private BufferedImage buttonH;
private int buttonX = 130; private int buttonX = 130;
private int playButtonY = 210; private int playButtonY = 210;
private int quitButtonY = 350; private int quitButtonY = 350;
@ -65,20 +68,14 @@ public class Game extends JPanel implements ActionListener, KeyListener {
// loads up the image assets // loads up the image assets
try { try {
planetZX311 = ImageIO.read(new File("assets/planet-ZX311.png")); 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")); 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
addMouseMotionListener(new MouseAdapter() { initMouse();
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
});
setBackground(Color.BLACK); setBackground(Color.BLACK);
setFocusable(true); setFocusable(true);
@ -104,53 +101,55 @@ public class Game extends JPanel implements ActionListener, KeyListener {
planetYOffsetDirection = "up"; planetYOffsetDirection = "up";
} }
} }
planetY = 140 + planetYOffset /6; planetY = planetYPre + planetYOffset /6;
repaint(); // triggers render repaint(); // triggers render
} }
@Override @Override
protected void paintComponent(Graphics g) { // render step protected void paintComponent(Graphics g) { // render step
super.paintComponent(g); // this clears the previous frame super.paintComponent(g); // this clears the previous frame
if (level == 1) {
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars Random rand = new Random();
g.fillOval(starX[i], starY[i], 2, 2); 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));
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 (button != null) { g.setColor(Color.GRAY);
if (!hoveringOverPlayButton) { g.setFont(caveat.deriveFont(Font.BOLD, 100f));
g.drawImage(button, buttonX, playButtonY, buttonWidth, buttonHeight, this); g.drawString("Very Cool Space Sim", 40, 100);
g.setColor(Color.BLACK);
g.setFont(caveat.deriveFont(Font.BOLD, 48f)); if (planetZX311 != null) {
g.drawString("Start the game", buttonX+25, playButtonY+67); if (!hoveringOverPlanet) {
} else { g.drawImage(planetZX311, planetX, planetY, planetSize, planetSize, this);
g.drawImage(buttonh, buttonX, playButtonY, buttonWidth, buttonHeight, this); } else {
g.setColor(Color.WHITE); g.drawImage(planetZX311H, planetX, planetY, planetSize, planetSize, this);
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); if (button != null) {
g.setColor(Color.BLACK); if (!hoveringOverPlayButton) {
g.setFont(caveat.deriveFont(Font.BOLD, 40f)); g.drawImage(button, buttonX, playButtonY, buttonWidth, buttonHeight, this);
g.drawString("Quit (pretty lame)", buttonX+24, quitButtonY+67); g.setColor(Color.BLACK);
} else { g.setFont(caveat.deriveFont(Font.BOLD, 48f));
g.drawImage(buttonh, buttonX, quitButtonY, buttonWidth, buttonHeight, this); g.drawString("Start the game", buttonX+25, playButtonY+67);
g.setColor(Color.WHITE); } else {
g.setFont(caveat.deriveFont(Font.BOLD, 40f)); g.drawImage(buttonH, buttonX, playButtonY, buttonWidth, buttonHeight, this);
g.drawString("Quit (pretty lame)", buttonX+24, quitButtonY+67); 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 @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
} }
@ -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.setResizable(false); // makes it so gamers cant just resize the window and leave black parts of the window
frame.add(new Game()); frame.add(new Game());
frame.pack(); 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.setLocationRelativeTo(null); // centers the window on the screen
frame.setVisible(true); // makes the window visible frame.setVisible(true); // makes the window visible
} }