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 {
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,17 +101,18 @@ 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
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], 2, 2);
g.fillOval(starX[i], starY[i], rand.nextInt(1,3), rand.nextInt(1,2));
}
g.setColor(Color.GRAY);
@ -125,7 +123,7 @@ public class Game extends JPanel implements ActionListener, KeyListener {
if (!hoveringOverPlanet) {
g.drawImage(planetZX311, planetX, planetY, planetSize, planetSize, this);
} else {
g.drawImage(planetZX311h, planetX, planetY, planetSize, planetSize, this);
g.drawImage(planetZX311H, planetX, planetY, planetSize, planetSize, this);
}
}
@ -136,7 +134,7 @@ public class Game extends JPanel implements ActionListener, KeyListener {
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.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);
@ -147,13 +145,14 @@ public class Game extends JPanel implements ActionListener, KeyListener {
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.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);
}
}
}
}
@Override
public void keyPressed(KeyEvent e) { // input
@ -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
}