在我的游戏中,我有不同的状态,第一个状态是主菜单。但是,它不会因为一个未知的原因而向我绘制主菜单。
主代码:
package Game.Window;
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Window {
public static void main(String[] args){
JFrame frame = new JFrame("TITLE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
frame.add(new Game(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
游戏代码:
package Game.Window;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import Game.Framework.GameStateManager;
public class Game extends JPanel implements Runnable, KeyListener{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = WIDTH/12*9;
private Thread thread;
private boolean isRunning = true;
private int FPS = 60;
private long targetTime = 1000/FPS;
private GameStateManager gsm;
public Game(){
setPreferredSize(new Dimension(WIDTH, HEIGHT));
start();
}
private void start(){
isRunning = true;
thread = new Thread(this);
thread.start();
}
public void run() {
long start, elapsed, wait;
gsm = new GameStateManager();
while(isRunning){
start = System.nanoTime();
tick();
repaint();
elapsed = System.nanoTime();
wait = targetTime - elapsed/1000000;
if(wait <= 0){
wait = 5;
}
try{
Thread.sleep(wait);
}catch(Exception e){
e.printStackTrace();
}
}
}
public void tick(){
gsm.tick();
// Shows Current FPS: System.out.println(FPS);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.clearRect(0, 0, WIDTH, WIDTH);
gsm.draw(g);
}
public void keyPressed(KeyEvent e) {
gsm.keyPressed(e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
gsm.keyReleased(e.getKeyCode());
}
public void keyTyped(KeyEvent e) {
}
}
GameStateManager代码:
package Game.Framework;
import java.awt.Graphics;
import java.util.Stack;
import Game.States.MainMenu;
public class GameStateManager {
private Stack<GameState> states;
public GameStateManager(){
states = new Stack<GameState>();
states.push(new MainMenu(this));
}
public void tick(){
states.peek().tick();
}
public void draw(Graphics g){
states.peek().draw(g);
}
public void keyPressed(int k){
states.peek().keyPressed(k);
}
public void keyReleased(int k){
states.peek().keyReleased(k);
}
}
游戏状态代码:
package Game.Framework;
import java.awt.Graphics;
public abstract class GameState {
protected GameStateManager gsm;
public GameState(GameStateManager gsm){
this.gsm = gsm;
init();
}
public abstract void init();
public abstract void tick();
public abstract void draw(Graphics g);
public abstract void keyPressed(int k);
public abstract void keyReleased(int k);
}
主菜单代码:
package Game.States;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import Game.Framework.GameState;
import Game.Framework.GameStateManager;
import Game.Window.Game;
public class MainMenu extends GameState{
private String[] options = {"New Game", "Load Game", "Options", "Exit"};
private int currentSelection = 0;
public MainMenu(GameStateManager gsm) {
super(gsm);
}
public void init() {}
public void tick() {
}
public void draw(Graphics g) {
for(int i = 0; i < options.length; i++){
if(i == currentSelection){
g.setColor(Color.GREEN);
}else{
g.setColor(Color.BLACK);
}
g.setFont(new Font("Arial", Font.PLAIN, 24));
g.drawString(options[i], Game.WIDTH/2-50, 50*i*30);
}
}
public void keyPressed(int k) {
}
public void keyReleased(int k) {
}
}
然而,我可以从第65行的游戏代码中绘制矩形。
答案 0 :(得分:1)
你的数学看起来有点不对劲。
g.drawString(options[i], Game.WIDTH/2-50, 50 * i + 30);
//add, not multiply^
其他一些事情
class MainMenu extends GameState{
//declare font once
private static final Font ARIAL_24 = new Font("Arial", Font.PLAIN, 24);
private String[] options = {"New Game", "Load Game", "Options", "Exit"};
private int currentSelection = 0;
public MainMenu(GameStateManager gsm) {
super(gsm);
}
public void init() {}
public void tick() {
}
public void draw(Graphics g) {
for(int i = 0; i < options.length; i++){
//simple ternary operator can replace what was here before.
g.setColor(i == currentSelection ? Color.GREEN : Color.BLACK);
//since this never changes make it static and final
//avoid recreating objects in a paint method, or when not neccesary
g.setFont(ARIAL_24);
g.drawString(options[i], Game.WIDTH/2-50, 50 * i + 30);
}
}
public void keyPressed(int k) {
}
public void keyReleased(int k) {
}
}
答案 1 :(得分:0)
我认为你想从上到下绘制这些字符串,所以在MainMenu-code中,第32行,&#34; y&#34;应该是&#34; 50 * i + 30&#34;。