我遵循了在 java 中创建迷宫游戏的教程。完成后,我决定添加一些东西(主菜单、更多关卡等)。我目前卡在主菜单上,因为我无法让鼠标输入工作。当我在开始游戏框中左键单击时,我试图切换到 screen = 1。这是我当前的课程代码。
package Maze;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener{
private Timer timer;
private Map m;
private Player p;
private int screen = 0;
private String Message = "";
private Font font = new Font("Serif", Font.BOLD, 48);
private Font start = new Font("Serif", Font.BOLD, 36);
private Rectangle start2 = new Rectangle(135,350,180,55);
public Board(){
m = new Map();
p = new Player();
addKeyListener(new Al());
addMouseListener(new A2());
setFocusable(true);
timer = new Timer(25,this);
timer.start();
}
public void actionPerformed(ActionEvent e){
if(m.getMap(p.getTileX(),p.getTileY()).equals("f")){
Message = "Winner";
screen = 10;
}
repaint();
}
public void paint(Graphics g){
super.paint(g);
if(screen==0){
g.setColor(Color.BLACK);
g.fillRect(0, 0, 464, 485);
g.setColor(Color.RED);
g.setFont(font);
g.drawString("Maze Game", 100,50);
g.setFont(start);
g.setColor(Color.WHITE);
g.drawString("Created by: ", 135,90);
g.drawString("Chad Jordan", 125,130);
g.setColor(Color.PINK);
g.fillRect(start2.x, start2.y, start2.width, start2.height);
g.setColor(Color.WHITE);
g.drawString("Start Game", start2.x, start2.y+40);
}
if((screen==1)){
for(int y = 0; y<14;y++){
for(int x=0; x<14; x++){
if(m.getMap(x, y).equals("g")){
g.drawImage(m.getGrass(), x*32, y*32, null);
}
if(m.getMap(x, y).equals("w")){
g.drawImage(m.getWall(), x*32, y*32, null);
}
if(m.getMap(x,y).equals("f")){
g.drawImage(m.getFinish(), x*32, y*32, null);
}
}
}
g.drawImage(p.getPlayer(), p.getTileX()*32, p.getTileY()*32, null);
}
if(screen==10){
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(Message, 150,200);
}
}
public class A2 extends MouseAdapter{
public void MousePressed(MouseEvent e){
int mx = e.getX();
int my = e.getY();
System.out.println("2");
if((screen ==0) && start2.contains(mx,my)){
screen=1;
System.out.println("1");
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
public class Al extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keycode = e.getKeyCode();
if(( keycode == KeyEvent.VK_W) || (keycode == KeyEvent.VK_UP)){
if(!m.getMap(p.getTileX(),p.getTileY() -1).equals("w")){
p.move(0, -1);
}
}
if(( keycode == KeyEvent.VK_A) || (keycode == KeyEvent.VK_LEFT)){
if(!m.getMap(p.getTileX()-1,p.getTileY()).equals("w")){
p.move(-1, 0);
}
}
if(( keycode == KeyEvent.VK_D) || (keycode == KeyEvent.VK_RIGHT)){
if(!m.getMap(p.getTileX()+1,p.getTileY()).equals("w")){
p.move(1, 0);
}
}
if(( keycode == KeyEvent.VK_S) || (keycode == KeyEvent.VK_DOWN)){
if(!m.getMap(p.getTileX(),p.getTileY() +1).equals("w")){
p.move(0, 1);
}
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
}
}
我使用“System.out.println("1");” 查看它是否注册了鼠标按下但不打印。