我正在为我的最终项目制作Java游戏。我有我的基本类结构。我想要它当我按下逃生它会弹出一个暂停菜单,如果我再次按它然后它将退出该程序。 这是我到目前为止的代码。
package Pong;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
public class Pong implements ActionListener, KeyListener
{
public static Pong pong;
public int width = 1300, height = 750;
public RenderingClass renderer;
int balls;
int gameStart = 0;
public Paddles paddle1, paddle2;
public Ball ball1;
public boolean w, s, up, down;
public int time = 0;
public Pong()
{
JFrame frame = new JFrame("Extreme Pong: Final Game");
Timer timer = new Timer(time, this);
renderer = new RenderingClass();
frame.setSize(width + 15, height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener(this);
frame.add(renderer);
start();
timer.start();
}
public void start()
{
paddle1 = new Paddles(this, 1);
paddle2 = new Paddles(this, 2);
ball1 = new Ball(this, 1);
}
public void update()
{
if(w)
{
paddle1.move(true);
}
if(s)
{
paddle1.move(false);
}
if(up)
{
paddle2.move(true);
}
if(down)
{
paddle2.move(false);
}
}
public void render(Graphics2D page)
{
page.setColor(Color.BLACK);
page.fillRect(0, 0, width, height);
page.setColor(Color.WHITE);
if(gameStart == 0)
{
page.setFont(new Font("3 by 5 Regular", 1, 50));
page.drawString("PONG 2.0", width / 2 - 50, 50);
}
else if(gameStart == 1)
{
page.setFont(new Font("3 by 5 Regular", 1, 50));
page.drawString("GAME PAUSED", width / 2 - 120, height / 2);
}
else if(gameStart == 2)
{
page.setColor(Color.WHITE);
page.setStroke(new BasicStroke(5));
page.drawLine(width / 2, 5, width / 2, height);
page.setFont(new Font("3 by 5 Regular", 1, 100));
page.drawString(Integer.toString(0), width / 3, 75);
page.drawString(Integer.toString(0), width / 3 * 2 - 60, 75);
paddle1.render(page);
paddle2.render(page);
ball1.render(page);
}
}
public void actionPerformed(ActionEvent event)
{
if(gameStart == 2)
{
update();
}
renderer.repaint();
update();
}
public static void main(String[] args)
{
pong = new Pong();
}
public void keyPressed(KeyEvent event)
{
int source = event.getKeyCode();
if(source == KeyEvent.VK_W)
{
w = true;
}
else if(source == KeyEvent.VK_S)
{
s = true;
}
else if(source == KeyEvent.VK_UP)
{
up = true;
}
else if(source == KeyEvent.VK_DOWN)
{
down = true;
}
else if(source == KeyEvent.VK_SPACE)
{
gameStart = 2;
}
else if(source == KeyEvent.VK_ESCAPE) !!!!!!!!!!
{
gameStart = 1;
if(source == KeyEvent.VK_ESCAPE)
{
System.exit(1);
}
}
}
public void keyReleased(KeyEvent event)
{
int source = event.getKeyCode();
if(source == KeyEvent.VK_W)
{
w = false;
}
else if(source == KeyEvent.VK_S)
{
s = false;
}
else if(source == KeyEvent.VK_UP)
{
up = false;
}
else if(source == KeyEvent.VK_DOWN)
{
down = false;
}
}
public void keyTyped(KeyEvent event)
{
}
}
使用此代码,它会在第一次按下时退出,但我希望它在第二次按下时退出。谢谢。
答案 0 :(得分:0)
您可以简单地使用变量来了解它是第一次还是第二次使用 逃生,说
boolean escapeHit = false;
然后使用它
else if(source == KeyEvent.VK_ESCAPE)
{
gameStart = 1;
// second escape key
if(escapeHit){
System.exit(1);
}
else
{
// first escape key
// display pause menu or whatever..
escapeHit = true;
}
}
答案 1 :(得分:0)
您将变量设置为1(我假设它会调出暂停菜单吗?)但是您的第二个if
仍然通过,因此程序会退出。
尝试这样的事情:
public void keyPressed(KeyEvent event)
{
...
else if(source == KeyEvent.VK_ESCAPE)
{
if(gameStart!=1)
{
gameStart = 1;
}
else
{
System.exit(1);
}
}
}