Java 贪吃蛇游戏:添加不可回头和撞身体结束功能
import java.awt.; import javax.swing.; import java.awt.event.; import java.util.;
public class test extends JFrame implements ActionListener { String name; JButton jbn = new JButton('开始'); D jpn = new D();
test() {
Container cn = getContentPane();
cn.setLayout(new BorderLayout());
cn.add(jbn, BorderLayout.NORTH);
cn.add(jpn, BorderLayout.CENTER);
jbn.addActionListener(this);
setSize(600, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Thread t1 = new Thread(jpn);
t1.start();
jpn.requestFocus(true);
}
public static void main(String args[]) {
new test();
}
}
class D extends JPanel implements Runnable, KeyListener {
int x = 10;
int direction = 0;
Random r = new Random();
Vector
D() {
v.add(new Point(20, 10));
v.add(new Point(20, 10));
v.add(new Point(20, 10));
v.add(new Point(20, 10));
int t = r.nextInt(20);
food.x = t * 10;
food.y = t * 10;
addKeyListener(this);
}
public void run() {
try {
while (true) {
for (int i = 0; i < v.size() - 1; i++) {
Point p = v.get(i);
Point p2 = v.get(i + 1);
p.x = p2.x;
p.y = p2.y;
}
if (direction == 0) {
Point p = v.get(v.size() - 1);
p.x = p.x + 10;
// 撞墙检测以及得分系统
if (p.x > 580) {
JOptionPane.showMessageDialog(null, '你GG了! 最终得分为 ' + (v.size() - 4));
System.exit(0);
}
}
if (direction == 1) {
Point p = v.get(v.size() - 1);
p.x = p.x - 10;
// 撞墙检测以及得分系统
if (p.x < 10) {
JOptionPane.showMessageDialog(null, '你GG了! 最终得分为 ' + (v.size() - 4));
System.exit(0);
}
}
if (direction == 2) {
Point p = v.get(v.size() - 1);
p.y = p.y - 10;
// 撞墙检测以及得分系统
if (p.y < 10) {
JOptionPane.showMessageDialog(null, '你GG了! 最终得分为 ' + (v.size() - 4));
System.exit(0);
}
}
if (direction == 3) {
Point p = v.get(v.size() - 1);
p.y = p.y + 10;
// 撞墙检测以及得分系统
if (p.y > 370) {
JOptionPane.showMessageDialog(null, '你GG了! 最终得分为 ' + (v.size() - 4));
System.exit(0);
}
}
// 撞到身体检测以及得分系统
for (int i = 0; i < v.size() - 1; i++) {
Point p = v.get(i);
Point p2 = v.get(v.size() - 1);
if (p.x == p2.x && p.y == p2.y) {
JOptionPane.showMessageDialog(null, '你GG了! 最终得分为 ' + (v.size() - 4));
System.exit(0);
}
}
for (int i = 0; i < v.size(); i++) {
Point p = v.get(i);
if (p.x == food.x && p.y == food.y) {
int t = r.nextInt(20);
food.x = t * 10;
food.y = t * 10;
v.add(new Point(p.x, p.y));
break;
}
}
repaint();
Thread.sleep(100);
}
} catch (Exception e) {
}
}
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < v.size(); i++) {
Point p = v.get(i);
int x = p.x;
int y = p.y;
if (i == v.size() - 1) {
g.setColor(Color.blue);
g.fillOval(x, y, 9, 9);
} else {
g.setColor(Color.black);
g.fillOval(x, y, 9, 9);
}
}
int x = food.x;
int y = food.y;
g.setColor(Color.red);
g.fillRect(x, y, 9, 9);
}
public void keyPressed(KeyEvent e) {
// 不可回头功能
if (e.getKeyCode() == KeyEvent.VK_UP && direction != 3) {
direction = 2;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN && direction != 2) {
direction = 3;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT && direction != 0) {
direction = 1;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT && direction != 1) {
direction = 0;
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
原文地址: https://www.cveoy.top/t/topic/oECg 著作权归作者所有。请勿转载和采集!