I really needed to perfect the main menu bro..
This commit is contained in:
parent
b4b03de400
commit
d49011edec
1 changed files with 39 additions and 19 deletions
|
|
@ -14,12 +14,18 @@ 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;
|
// defines the window dimensions
|
||||||
private final int windowHeight = 600;
|
private final int windowWidth = 900;
|
||||||
private final int MENU_STAR_COUNT = 64;
|
private final int windowHeight = 550;
|
||||||
|
// defines the star count for the main menu and also creates the arrays for the star positions
|
||||||
|
private final int MENU_STAR_COUNT = 96;
|
||||||
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 int starSize = 1;
|
||||||
|
|
||||||
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 level = 1;
|
||||||
|
|
@ -30,7 +36,7 @@ public class Game extends JPanel implements ActionListener, KeyListener {
|
||||||
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 = 450;
|
||||||
private int planetYPre = 140;
|
private int planetYPre = 140;
|
||||||
private int planetY = planetYPre + planetYOffset /6;
|
private int planetY = planetYPre + planetYOffset /6;
|
||||||
private int planetSize = 128*3;
|
private int planetSize = 128*3;
|
||||||
|
|
@ -39,13 +45,14 @@ public class Game extends JPanel implements ActionListener, KeyListener {
|
||||||
|
|
||||||
private BufferedImage button;
|
private BufferedImage button;
|
||||||
private BufferedImage buttonH;
|
private BufferedImage buttonH;
|
||||||
private int buttonX = 130;
|
private int playButtonX = 70;
|
||||||
|
private int quitButtonX = 70;
|
||||||
private int playButtonY = 210;
|
private int playButtonY = 210;
|
||||||
private int quitButtonY = 350;
|
private int quitButtonY = 350;
|
||||||
private int buttonWidth = 108*3;
|
private int buttonWidth = 108*3;
|
||||||
private int buttonHeight = 108;
|
private int buttonHeight = 108;
|
||||||
private Rectangle playButtonCollision = new Rectangle(buttonX, playButtonY, buttonWidth, buttonHeight);
|
private Rectangle playButtonCollision = new Rectangle(playButtonX, playButtonY, buttonWidth, buttonHeight);
|
||||||
private Rectangle quitButtonCollision = new Rectangle(buttonX, quitButtonY, buttonWidth, buttonHeight);
|
private Rectangle quitButtonCollision = new Rectangle(quitButtonX, quitButtonY, buttonWidth, buttonHeight);
|
||||||
private boolean hoveringOverPlayButton = false;
|
private boolean hoveringOverPlayButton = false;
|
||||||
private boolean hoveringOverQuitButton = false;
|
private boolean hoveringOverQuitButton = false;
|
||||||
|
|
||||||
|
|
@ -112,12 +119,13 @@ public class Game extends JPanel implements ActionListener, KeyListener {
|
||||||
g.setColor(Color.WHITE);
|
g.setColor(Color.WHITE);
|
||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars
|
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));
|
starSize = rand.nextInt(1,3);
|
||||||
|
g.fillOval(starX[i]-starSize, starY[i]-starSize, starSize, starSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
g.setColor(Color.GRAY);
|
g.setColor(Color.GRAY);
|
||||||
g.setFont(caveat.deriveFont(Font.BOLD, 100f));
|
g.setFont(caveat.deriveFont(Font.BOLD, 100f));
|
||||||
g.drawString("Very Cool Space Sim", 40, 100);
|
g.drawString("Very Cool Space Sim", 72, 100);
|
||||||
|
|
||||||
if (planetZX311 != null) {
|
if (planetZX311 != null) {
|
||||||
if (!hoveringOverPlanet) {
|
if (!hoveringOverPlanet) {
|
||||||
|
|
@ -129,26 +137,26 @@ public class Game extends JPanel implements ActionListener, KeyListener {
|
||||||
|
|
||||||
if (button != null) {
|
if (button != null) {
|
||||||
if (!hoveringOverPlayButton) {
|
if (!hoveringOverPlayButton) {
|
||||||
g.drawImage(button, buttonX, playButtonY, buttonWidth, buttonHeight, this);
|
g.drawImage(button, playButtonX, playButtonY, buttonWidth, buttonHeight, this);
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
g.setFont(caveat.deriveFont(Font.BOLD, 48f));
|
g.setFont(caveat.deriveFont(Font.BOLD, 48f));
|
||||||
g.drawString("Start the game", buttonX+25, playButtonY+67);
|
g.drawString("Start the game", playButtonX +25, playButtonY+67);
|
||||||
} else {
|
} else {
|
||||||
g.drawImage(buttonH, buttonX, playButtonY, buttonWidth, buttonHeight, this);
|
g.drawImage(buttonH, playButtonX, playButtonY, buttonWidth, buttonHeight, this);
|
||||||
g.setColor(Color.WHITE);
|
g.setColor(Color.WHITE);
|
||||||
g.setFont(caveat.deriveFont(Font.BOLD, 48f));
|
g.setFont(caveat.deriveFont(Font.BOLD, 48f));
|
||||||
g.drawString("Start the game", buttonX+25, playButtonY+67);
|
g.drawString("Start the game", playButtonX +25, playButtonY+67);
|
||||||
}
|
}
|
||||||
if (!hoveringOverQuitButton) {
|
if (!hoveringOverQuitButton) {
|
||||||
g.drawImage(button, buttonX, quitButtonY, buttonWidth, buttonHeight, this);
|
g.drawImage(button, quitButtonX, quitButtonY, buttonWidth, buttonHeight, this);
|
||||||
g.setColor(Color.BLACK);
|
g.setColor(Color.BLACK);
|
||||||
g.setFont(caveat.deriveFont(Font.BOLD, 40f));
|
g.setFont(caveat.deriveFont(Font.BOLD, 40f));
|
||||||
g.drawString("Quit (pretty lame)", buttonX+24, quitButtonY+67);
|
g.drawString("Quit (pretty lame)", quitButtonX +24, quitButtonY+67);
|
||||||
} else {
|
} else {
|
||||||
g.drawImage(buttonH, buttonX, quitButtonY, buttonWidth, buttonHeight, this);
|
g.drawImage(buttonH, quitButtonX, quitButtonY, buttonWidth, buttonHeight, this);
|
||||||
g.setColor(Color.WHITE);
|
g.setColor(Color.WHITE);
|
||||||
g.setFont(caveat.deriveFont(Font.BOLD, 40f));
|
g.setFont(caveat.deriveFont(Font.BOLD, 40f));
|
||||||
g.drawString("Quit (pretty lame)", buttonX+24, quitButtonY+67);
|
g.drawString("Quit (pretty lame)", quitButtonX +24, quitButtonY+67);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -174,10 +182,12 @@ public class Game extends JPanel implements ActionListener, KeyListener {
|
||||||
@Override
|
@Override
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverQuitButton) {
|
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverQuitButton) {
|
||||||
System.exit(0);
|
quitButtonX = quitButtonX - 5;
|
||||||
|
quitButtonY = quitButtonY + 5;
|
||||||
}
|
}
|
||||||
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlayButton) {
|
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlayButton) {
|
||||||
level = 2;
|
playButtonX = playButtonX - 5;
|
||||||
|
playButtonY = playButtonY + 5;
|
||||||
}
|
}
|
||||||
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlanet) {
|
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlanet) {
|
||||||
planetX = planetX-5;
|
planetX = planetX-5;
|
||||||
|
|
@ -190,6 +200,16 @@ public class Game extends JPanel implements ActionListener, KeyListener {
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseReleased(MouseEvent e) {
|
public void mouseReleased(MouseEvent e) {
|
||||||
|
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverQuitButton) {
|
||||||
|
quitButtonX = quitButtonX + 5;
|
||||||
|
quitButtonY = quitButtonY - 5;
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlayButton) {
|
||||||
|
playButtonX = playButtonX + 5;
|
||||||
|
playButtonY = playButtonY - 5;
|
||||||
|
level = 2;
|
||||||
|
}
|
||||||
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlanet) {
|
if (e.getButton() == MouseEvent.BUTTON1 && hoveringOverPlanet) {
|
||||||
planetX = planetX+5;
|
planetX = planetX+5;
|
||||||
planetYPre = planetYPre-5;
|
planetYPre = planetYPre-5;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue