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<Point> v = new Vector<Point>();
Point food = new Point();
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) {
}
}
解释一下这个程序的围墙以及得分系统内容:这个程序中的围墙是指游戏界面的边界,当蛇的头部碰到边界时,游戏结束,弹出得分窗口。得分系统是指在游戏中,每吃到一个食物,蛇的长度增加一节,得分也会相应增加一分。最终得分为蛇的长度减去初始长度(4)。如果蛇撞到了自己的身体,游戏也结束。
原文地址: https://www.cveoy.top/t/topic/oFMW 著作权归作者所有。请勿转载和采集!