为简单GUI游戏创建登录系统扩展了Canvas

时间:2017-05-12 01:26:42

标签: java swing user-interface canvas awt

我创建了一个类似于pacman的游戏,它扩展了Canvas并实现了Runnable。在编译和运行游戏时,游戏会显示“请按Enter键开始”#39;屏幕然后游戏立即开始。

我想创建一个简单的登录系统,允许用户创建用户名和密码。然后能够使用该用户名登录并开始游戏。

我有一个使用JDialog工作的示例登录系统,但登录只能作为一个单独的项目。如果包含在游戏中,则不会显示。

这是因为Canvas是一个awt对象,但JDialog,JPanels等正在摆动?

什么是使登录系统正常工作的最简单方法?

(我是编程的新手,所以寻找一个简单的解决方案) 以下是主类的代码:

public class Game extends Canvas implements Runnable, KeyListener {
private boolean isRunning = false;
private static boolean gameStart = false;
public static final int WIDTH = 640, HEIGHT = 480;
public static final int boxWidth = 400, boxHeight = 250;
public static final int xx = Game.WIDTH / 2 - boxWidth / 2;
public static final int yy = Game.HEIGHT / 2 - boxHeight / 2;
public static final String TITLE = "Simple Strategic Game";
private Thread thread;
public static Player player;
public static Level level;
public static final int START_SCREEN = 0, GAME = 1;
public static int STATE = -1;
public boolean isEnter = false;
private int time = 0;
private int targetFrames = 30;
private boolean showText = true;

public Game(){
    Dimension dimension = new Dimension(Game.WIDTH, Game.HEIGHT);
    setPreferredSize(dimension);
    setMinimumSize(dimension);
    setMaximumSize(dimension);
    addKeyListener(this);
    STATE = START_SCREEN;   
}

private synchronized void start(){
        if(isRunning) return;
        isRunning = true;
        thread = new Thread(this);
        thread.start();
}

public synchronized void stop(){
    if(isRunning) return;
    isRunning = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

private void tick(){

    if(STATE == GAME){
    player.tick();
    level.tick();
    } else if(STATE == START_SCREEN){
        time++;
        if(time == targetFrames){
            time = 0;
            if(showText){
                showText = false;   
            }else{
                showText = true;
            }
        }
        if(isEnter){
            isEnter = false;
            player = new Player(Game.WIDTH/2, Game.HEIGHT/2);                   //Starts player in middle of screen
            level = new Level("/map/map.png");
            STATE = GAME;
        }
    }
}

private void render(){
    BufferStrategy bs = getBufferStrategy();
    if(bs == null)
    {
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    g.setColor(Color.black);
    g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);
    if(STATE == GAME){
    player.render(g);
    level.render(g);
    }else if(STATE == START_SCREEN){
        g.setColor(new Color(120,157,213));
        g.fillRect(xx, yy, boxWidth, boxHeight);

        g.setColor(Color.white);
        g.setFont(new Font(Font.DIALOG, Font.BOLD, 19));
        if(showText) g.drawString("Press Enter to start the Game", xx+60, yy+100);

    }
    g.dispose();
    bs.show();  
}

@Override
public void run() {
    requestFocus();
    int fps = 0;
    double timer = System.currentTimeMillis();
    long lastTime = System.nanoTime();
    double targetTick = 60.0;
    double delta = 0;
    double ns = 1000000000 / targetTick;

    while(isRunning){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;

        while (delta >= 1){
            tick();
            render();
            fps++;
            delta--;
        }
        if(System.currentTimeMillis() - timer >= 1000){
            System.out.println(fps);
            fps = 0;
            timer += 1000;
        }
    }
    stop();
}

public static void main(String[] args){
    Game game = new Game();
    JFrame frame = new JFrame(Game.TITLE);
    frame.add(game);
    frame.setResizable(false);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    game.start();   
}

@Override
public void keyPressed(KeyEvent e) {
    if(STATE == GAME){
        if(e.getKeyCode() == KeyEvent.VK_RIGHT) player.right = true;
        if(e.getKeyCode() == KeyEvent.VK_LEFT) player.left = true;
        if(e.getKeyCode() == KeyEvent.VK_UP) player.up = true;
        if(e.getKeyCode() == KeyEvent.VK_DOWN) player.down = true;
    } else if(STATE == Game.START_SCREEN){
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            isEnter = true;
        }
    }
}

@Override
public void keyReleased(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_RIGHT) player.right = false;
    if(e.getKeyCode() == KeyEvent.VK_LEFT) player.left = false;
    if(e.getKeyCode() == KeyEvent.VK_UP) player.up = false;
    if(e.getKeyCode() == KeyEvent.VK_DOWN) player.down = false;
}
@Override
public void keyTyped(KeyEvent e) {}
}

0 个答案:

没有答案