创建额外的菜单选项以转到其他选项

时间:2017-03-07 14:56:42

标签: java

所以,我有一个带有一个命令的菜单:“按Enter键”将播放游戏。我希望有一些额外的选项来增加我的菜单屏幕。换句话说,我想要一个按键去指令“页面”标准,这将指示如何玩游戏。 我目前的菜单代码是:

package menupackagev1;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import displaypackagev1.Sea_InvadersDisplay;
import statepackagev1.Sea_InvadersStateMachine;
import statepackagev1.StateMachineInterface;

public class MenuScreen extends StateMachineInterface implements KeyListener {

private Font titleFont = new Font("Arial", Font.PLAIN, 64);
private Font startFont = new Font("Arial", Font.PLAIN, 32);
private String title = "Space Invaders";
private String start = "Press Enter";
private String instruct = " Press H for Instructions";

public MenuScreen(Sea_InvadersStateMachine stateMachine) {
    super(stateMachine);
}

@Override
public void update(double delta) {

}

@Override
public void draw(Graphics2D g) {
    g.setFont(titleFont);
    int tittleWidth = g.getFontMetrics().stringWidth(title);
    g.setColor(Color.yellow);
    g.drawString(title, ((Sea_InvadersDisplay.WIDTH/2)-(tittleWidth/2))-2, (Sea_InvadersDisplay.HEIGHT/2)-123);
    g.setColor(Color.green);
    g.drawString(title, (Sea_InvadersDisplay.WIDTH/2)-(tittleWidth/2), (Sea_InvadersDisplay.HEIGHT/2)-125);

    g.setFont(startFont);
    g.setColor(Color.white);
    int startWidth = g.getFontMetrics().stringWidth(start);
    g.drawString(start, (Sea_InvadersDisplay.WIDTH/2)-(startWidth/2), (Sea_InvadersDisplay.HEIGHT/2)+75);
}

@Override
public void init(Canvas canvas) {
    canvas.addKeyListener(this);
}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        getStateMachine().setState((byte) 1);
    }
}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

    }

}

显示:

package displaypackagev1;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

import statepackagev1.Sea_InvadersStateMachine;

public class Sea_InvadersDisplay extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;

public static void main(String[] args) {
    Sea_InvadersDisplay display = new Sea_InvadersDisplay();
    JFrame frame = new JFrame();
    frame.add(display);
    frame.pack();
    frame.setTitle("Space Invaders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setVisible(true);
    display.start();
}

private boolean running = false;
private Thread thread;

public synchronized void start() {
    if (running)
        return;

    running = true;

    thread = new Thread(this);
    thread.start();
}

public synchronized void stop() {
    if (!running)
        return;

    running = false;

    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public static int WIDTH = 800, HEIGHT = 600;
public int FPS;

public static Sea_InvadersStateMachine state;

public Sea_InvadersDisplay() {
    this.setSize(WIDTH, HEIGHT);
    this.setFocusable(true);

    state = new Sea_InvadersStateMachine(this);
    state.setState((byte) 0); 
}

@Override
public void run() {
    long timer = System.currentTimeMillis();
    long lastLoopTime = System.nanoTime();
    final int TARGET_FPS = 60;
    final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
    int frames = 0;

    this.createBufferStrategy(3);
    BufferStrategy bs = this.getBufferStrategy();
    while (running) {
        long now = System.nanoTime();
        long updateLength = now - lastLoopTime;
        lastLoopTime = now;
        double delta = updateLength / ((double) OPTIMAL_TIME);

        frames++;

        if (System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            FPS = frames;
            frames = 0;
            System.out.println(FPS);
        }

        draw(bs);
        update(delta);

        try {
            Thread.sleep(((lastLoopTime - System.nanoTime()) + OPTIMAL_TIME) / 1000000);
        } catch (Exception e) {
        }
        ;
    }
}

public void draw(BufferStrategy bs) {
    do {
        do {
            Graphics2D g = (Graphics2D) bs.getDrawGraphics();
            g.setColor(Color.BLUE);
            g.fillRect(0, 0, WIDTH + 50, HEIGHT + 50);

            state.draw(g);

            g.dispose();
        } while (bs.contentsRestored());
        bs.show();
    } while (bs.contentsLost());
}

public void update(double delta) {
    state.update(delta);
    }

}

状态机:

package statepackagev1;

import java.awt.Canvas;
import java.awt.Graphics2D;
import java.util.ArrayList;

import menupackagev1.MenuScreen;
import gamescreenpackagev1.GameScreen;

public class Sea_InvadersStateMachine {
private ArrayList<StateMachineInterface> states = new ArrayList<StateMachineInterface>();
private Canvas canvas;
private byte selectState = 0;

public Sea_InvadersStateMachine(Canvas canvas){
    StateMachineInterface game = new GameScreen(this);
    StateMachineInterface menu = new MenuScreen(this);
    states.add(menu);
    states.add(game);

    this.canvas = canvas;
}

public void draw(Graphics2D g){
    states.get(selectState).draw(g); //Depending on which State is selected, that state that has been selected will be drawn.
}

public void update(double delta){
    states.get(selectState).update(delta);
}

public void setState(byte i){
    for(int r =0; r < canvas.getKeyListeners().length; r++)
        canvas.removeKeyListener(canvas.getKeyListeners()[r]);
    selectState = i;
    states.get(selectState).init(canvas);
}

public byte getStates() {
    return selectState;
    }
}

SM界面:

package statepackagev1;

import java.awt.Canvas;
import java.awt.Graphics2D;

public abstract class StateMachineInterface {

private Sea_InvadersStateMachine stateMachine;

public StateMachineInterface(Sea_InvadersStateMachine stateMachine) {
    this.stateMachine = stateMachine;
}

public abstract void update(double delta);
public abstract void draw(Graphics2D g);
public abstract void init(Canvas canvas);

public Sea_InvadersStateMachine getStateMachine() {
    return stateMachine;
    }
}

1 个答案:

答案 0 :(得分:1)

在您的MenuScreen类中:

@Override
public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        getStateMachine().setState((byte) 1);
    } else if (e.getKeyCode() == KeyEvent.SOME_OTHER_KEY) {
        // Do some other stuff here...
    }
}