Add bomb sprites, text box, main menu
After Width: | Height: | Size: 3.8 KiB |
After Width: | Height: | Size: 2.5 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 4.6 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 2.1 KiB |
After Width: | Height: | Size: 2.2 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 1.6 KiB |
|
@ -11,6 +11,7 @@ import java.awt.*;
|
|||
import java.awt.event.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
import javax.swing.*;
|
||||
|
@ -27,8 +28,10 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
|
|||
public Thread gameThread;
|
||||
public Image image;
|
||||
public Graphics graphics;
|
||||
public static Player player;
|
||||
public BackgroundImage background;
|
||||
public TextBox title, enter, settings;
|
||||
public ArrayList<TextBox> textBoxArray = new ArrayList<TextBox>();
|
||||
public Font standardFont = new Font(Font.MONOSPACED, Font.BOLD, 60);
|
||||
public int playerFrame, enemyFrame;
|
||||
// keeps track of how many ticks has elapsed since last frame change
|
||||
public int currentBox = 0;
|
||||
|
@ -39,6 +42,12 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
|
|||
public MenuPanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
|
||||
this.gameFrame = gameFrame;
|
||||
|
||||
title = new TextBox(100, 400, 100, GAME_WIDTH, standardFont, "Detroit", null);
|
||||
enter = new TextBox(300, 600, 100, GAME_WIDTH, standardFont, "Start Game", "game");
|
||||
settings = new TextBox(400, 600, 100, GAME_WIDTH, standardFont, "Settings", "menu");
|
||||
textBoxArray.add(enter);
|
||||
textBoxArray.add(settings);
|
||||
|
||||
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10);
|
||||
// the height of 35 is set because it is half of the original tile height (i.e., 70px)
|
||||
this.setFocusable(true); //make everything in this class appear on the screen
|
||||
|
@ -79,6 +88,11 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
|
|||
//call the draw methods in each class to update positions as things move
|
||||
public void draw(Graphics g, int playerFrame, int enemyFrame){
|
||||
background.draw(g);
|
||||
title.draw(g,null, Color.black);
|
||||
for (TextBox t: textBoxArray) {
|
||||
t.draw(g, null, Color.cyan);
|
||||
}
|
||||
textBoxArray.get(currentBox).draw(g, Color.gray, Color.blue);
|
||||
}
|
||||
|
||||
//call the move methods in other classes to update positions
|
||||
|
@ -112,27 +126,36 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
|
|||
//if a key is pressed, we'll send it over to the Player class for processing
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||
// logic for different screens starts here
|
||||
CardLayout cardLayout = (CardLayout) gameFrame.getLayout();
|
||||
cardLayout.next(gameFrame);
|
||||
cardLayout.show(gameFrame, textBoxArray.get(currentBox).id);
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
|
||||
currentBox = (currentBox + 1) % TOTAL_BOXES;
|
||||
// sleep to prevent excessively fast scrolling
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
currentBox = (currentBox + 1) % textBoxArray.size();
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
|
||||
// if currentBox > 0, subtract one
|
||||
// else, set to TOTAL_BOXES-1
|
||||
currentBox = currentBox > 0 ? currentBox - 1:TOTAL_BOXES - 1;
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
// if currentBox > 0, subtract one
|
||||
// else, set to TOTAL_BOXES-1
|
||||
currentBox = currentBox > 0 ? currentBox - 1:textBoxArray.size() - 1;
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
// if currentBox > total box amount - 1, set to 0
|
||||
// else, set to TOTAL_BOXES-1
|
||||
currentBox = currentBox < textBoxArray.size() - 1 ? currentBox + 1:0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//if a key is released, we'll send it over to the Player class for processing
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
import java.awt.*;
|
||||
|
||||
public class TextBox extends Rectangle {
|
||||
int y;
|
||||
int xWidth, yHeight;
|
||||
int newX, newY;
|
||||
Font font;
|
||||
String text;
|
||||
public final String id;
|
||||
|
||||
public TextBox(int y, int xWidth, int yHeight, int totalWidth, Font font, String text, String id) {
|
||||
newX = (totalWidth - xWidth)/2;
|
||||
newY = y - yHeight/2;
|
||||
this.yHeight = yHeight;
|
||||
this.xWidth = xWidth;
|
||||
this.y = y;
|
||||
this.font = font;
|
||||
this.text = text;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
// isHover is not needed for now and is also probably implemented badly
|
||||
public boolean isHover(int x, int y) {
|
||||
if (x >= newX && x <= newX + xWidth) {
|
||||
return (y >= newY && y <= newY + yHeight);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void drawCenteredTextBox(Graphics g, String text, Color backgroundColor, Color textColor) {
|
||||
if (backgroundColor != null) {
|
||||
g.setColor(backgroundColor);
|
||||
g.fillRect(newX, newY, xWidth, yHeight);
|
||||
}
|
||||
g.setColor(textColor);
|
||||
drawCenteredString(g, y, newX, xWidth, text);
|
||||
}
|
||||
|
||||
public static void drawCenteredString(Graphics g, int y, int x, int xWidth, String text) {
|
||||
int newX, newY;
|
||||
// get font size
|
||||
FontMetrics metrics = g.getFontMetrics();
|
||||
// determine x for the text
|
||||
newX = x + (xWidth - metrics.stringWidth(text)) / 2;
|
||||
// center y (half is above y value, half is below y value)
|
||||
newY = y + (metrics.getAscent() - metrics.getDescent())/2;
|
||||
// draw centered string
|
||||
g.drawString(text, newX, newY);
|
||||
System.out.println(newX + " " + y + " " + newY);
|
||||
}
|
||||
|
||||
// TODO: make this good
|
||||
public void draw(Graphics g, Color backgroundColor, Color textColor) {
|
||||
g.setFont(font);
|
||||
drawCenteredTextBox(g, text, backgroundColor, textColor);
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
import java.awt.*;
|
||||
|
||||
public final class TextManager {
|
||||
// utility class, constructor is private to prevent initialization
|
||||
private TextManager() {}
|
||||
|
||||
public static void drawCenteredTextBox(Graphics g, int y, int xWidth, int yHeight, int totalWidth,
|
||||
String text, Color backgroundColor, Color textColor) {
|
||||
int newX = (totalWidth - xWidth)/2;
|
||||
int newY = y - yHeight/2;
|
||||
g.setColor(backgroundColor);
|
||||
g.drawRect(newX, newY, xWidth, yHeight);
|
||||
g.setColor(textColor);
|
||||
drawCenteredString(g, y, newX, xWidth, text);
|
||||
}
|
||||
|
||||
public static void drawCenteredString(Graphics g, int y, int x, int xWidth, String text) {
|
||||
int newX, newY;
|
||||
// get font size
|
||||
FontMetrics metrics = g.getFontMetrics();
|
||||
// determine x for the text
|
||||
newX = x + (xWidth - metrics.stringWidth(text)) / 2;
|
||||
// center y (half is above y value, half is below y value)
|
||||
newY = y - (metrics.getAscent() - metrics.getDescent())/2;
|
||||
// draw centered string
|
||||
g.drawString(text, newX, newY);
|
||||
}
|
||||
}
|