我想做一个八字和十字架游戏,所以你用鼠标选择一个框,然后在键盘上用 X 或 O 输入答案。我已经让框选择工作但似乎无法让 x 和 o 工作。我的代码可能不是最好的,因为这是我制作的第一款游戏。
这是我的主要课程:
package com;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Board extends JPanel implements ActionListener, MouseListener,
KeyListener {
public int box = 1;
public int x, y;
public int cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4, cx5, cy5;
public boolean player1 = true;
public boolean player2 = false;
public boolean box1 = false;
public boolean box2 = false;
public boolean box3 = false;
public boolean box4 = false;
public boolean box5 = false;
public boolean box6 = false;
public boolean box7 = false;
public boolean box8 = false;
public boolean box9 = false;
public boolean xpress = false;
public boolean cross1 = false;
public boolean cross2 = false;
public boolean cross3 = false;
public boolean cross4 = false;
public boolean cross5 = false;
public Thread t;
Timer time;
JButton cross, naught;
Image Board, Select, Cross, Cross2, Cross3, Cross4, Cross5, Naught;
Board() {
super();
x = 0;
y = 60;
t = new Thread();
time = new Timer(5, this);
time.start();
t.start();
setFocusable(true);
addMouseListener(this);
addKeyListener(this);
ImageIcon i = new ImageIcon(getClass().getResource(
"/images/Board_N+C.png"));
Board = i.getImage();
ImageIcon i1 = new ImageIcon(getClass().getResource(
"/images/Select_N+C.png"));
Select = i1.getImage();
ImageIcon i2 = new ImageIcon(getClass().getResource(
"/images/Cross_N+C.png"));
Cross = i2.getImage();
Cross2 = i2.getImage();
Cross3 = i2.getImage();
Cross4 = i2.getImage();
Cross5 = i2.getImage();
ImageIcon i3 = new ImageIcon(getClass().getResource(
"/images/Naught_N+C.png"));
Naught = i3.getImage();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(Board, 0, 60, null);
if (cross1) {
}
if (x < 170 && y < 230 && !xpress) {
g2d.drawImage(Select, 0, 60, null);
repaint();
} else if (x < 340 && x > 170 && y < 230 && !xpress) {
g2d.drawImage(Select, 170, 60, null);
repaint();
} else if (x < 510 && x > 340 && y < 230 && !xpress) {
g2d.drawImage(Select, 340, 60, null);
repaint();
} else if (x < 170 && y < 400 && y > 230 && !xpress) {
g2d.drawImage(Select, 0, 230, null);
repaint();
} else if (x < 340 && x > 170 && y < 400 && y > 230 && !xpress) {
g2d.drawImage(Select, 170, 230, null);
repaint();
} else if (x < 510 && x > 340 && y < 400 && y > 230 && !xpress) {
g2d.drawImage(Select, 340, 230, null);
repaint();
} else if (x < 170 && y < 570 && y > 400 && !xpress) {
g2d.drawImage(Select, 0, 400, null);
repaint();
} else if (x < 340 && x > 170 && y < 570 && y > 400 && !xpress) {
g2d.drawImage(Select, 170, 400, null);
repaint();
} else if (x < 510 && x > 340 && y < 570 && y > 400 && !xpress) {
g2d.drawImage(Select, 340, 400, null);
repaint();
}
if (xpress && !player2) {
if (x < 170 && y < 230) {
cross1 = true;
while (cross1) {
g2d.drawImage(Cross, 0, 60, null);
System.out.println("Cross");
}repaint();
xpress = false;
} else if (x < 340 && x > 170 && y < 230) {
repaint();
} else if (x < 510 && x > 340 && y < 230) {
repaint();
} else if (x < 170 && y < 400 && y > 230) {
repaint();
} else if (x < 340 && x > 170 && y < 400 && y > 230) {
repaint();
} else if (x < 510 && x > 340 && y < 400 && y > 230) {
repaint();
} else if (x < 170 && y < 570 && y > 400) {
repaint();
} else if (x < 340 && x > 170 && y < 570 && y > 400) {
repaint();
} else if (x < 510 && x > 340 && y < 570 && y > 400) {
repaint();
}
}
}
public void mouseClicked(MouseEvent me) {
x = me.getX();
y = me.getY();
}
@Override
public void actionPerformed(ActionEvent e) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == e.VK_X) {
if (player1) {
xpress = true;
player1 = false;
player2 = true;
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
如何在按键时显示图像并将其保留在屏幕上直到线程停止?
希望有人可以提供帮助。如果您需要更多信息,请询问。