用Java做的贪吃蛇,简单版......
效果图片:
:
话不多说,上代码:
一共三个类:
①
public class Body {
int x;
int y;
public Body(int x, int y) {
this.x = x;
this.y = y;
}
}
②
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.JFrame;
public class BallThread extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static int RedX = 120; // 小球初始位置
private static int RedY = 120;
private Color color = Color.RED; // 小球初始颜色
private Thread run;
private String direction;
private Body[] body = new Body[100];
int body_length = 1;
int randomx = 40, randomy = 40;
private boolean pause = false;
// 有参构造方法
public BallThread() {
// 实例化数组
for (int i = 0; i < 100; i++) {
body[i] = new Body(0, 0);
}
body[0].x = RedX;
body[0].y = RedY;
setSize(400, 400); // 设置大小
setLocationRelativeTo(null);
setTitle("贪吃球");
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("到这来.........");
super.keyPressed(e);
// System.out.println(e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
direction = "L";
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
direction = "R";
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
direction = "U";
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
direction = "D";
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (pause == false) {
pause = true;
} else {
pause = false;
}
}
}
});
}
public void paint(Graphics g) {
// 必须传递给父类
super.paint(g);
// System.out.println("运行到这来");
// 画网格
// g.drawLine(500, 500, 600, 400);
g.clipRect(1, 1, 400, 400);
// 1.画方格
// 竖线
for (int i = 1; i < 500; i = i + 20) {
g.setColor(Color.BLACK);
g.drawLine(i, 1, i, i + 400);
// 填充
g.setColor(Color.GREEN);
// 画横墙
g.fill3DRect(i + 1, 20 + 1, 20, 20, true);
g.fill3DRect(i + 1, 380 + 1, 20, 20, true);
}
// 横线
for (int i = 1; i < 500; i = i + 20) {
g.setColor(Color.BLACK);
g.drawLine(1, i, i + 400, i);
// 填充
g.setColor(Color.green);
g.fill3DRect(1, i + 1, 20, 20, true);
g.fill3DRect(382, i + 1, 20, 20, true);
}
// 画颜色圈
g.setColor(Color.blue);
g.drawRoundRect(20, 40, 20, 20, 20, 20);
g.setColor(Color.RED);
g.drawRoundRect(80, 120, 20, 20, 20, 20);
g.setColor(Color.PINK);
g.drawRoundRect(220, 220, 20, 20, 20, 20);
if (isEated()) {
produceFood();
g.setColor(Color.yellow);
g.fillRect(randomx, randomy, 20, 20);
} else {
g.setColor(Color.yellow);
g.fillRect(randomx, randomy, 20, 20);
}
// 小球
g.setColor(color); // 颜色
g.drawRoundRect(RedX, RedY, 20, 20, 20, 20);
g.fillRoundRect(RedX, RedY, 20, 20, 20, 20);
body[0].x = RedX;
body[0].y = RedY;
for (int i = 1; i < body_length; i++) {
System.out.println("身体显示了");
System.out.println("X==" + body[i].x + " Y======" + body[i].y);
g.setColor(Color.BLACK);
g.drawRect(body[i].x, body[i].y, 20, 20);
g.fillRect(body[i].x, body[i].y, 20, 20);
}
g.setColor(color); // 颜色
g.drawRoundRect(RedX, RedY, 20, 20, 20, 20);
g.fillRoundRect(RedX, RedY, 20, 20, 20, 20);
}
// 消除闪烁(下面这两行代码可写可不写,因为还没实现解决闪烁这个问题,有兴趣的可以自己解决闪烁问题)
public void update(Graphics g) {
paint(g);
}
private boolean isEated() {
// TODO Auto-generated method stub
if (RedX == randomx && RedY == randomy) {
return true;
} else {
return false;
}
}
// 小球移动
public void move() {
System.out.println(body.length);
long millis = 600; // 每隔300毫秒刷新一次
run = new Thread() {
public void run() {
while (true) {
try {
System.out.println("public void Thread:" + Thread.currentThread().getName());
Thread.sleep(millis);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!pause) {
// 移动
if (direction == "L") {
if (RedX >= 40)
RedX -= 20;
}
if (direction == "R") {
if (RedX <= 340)
RedX += 20;
}
if (direction == "U") {
if (RedY >= 60)
RedY -= 20;
}
if (direction == "D") {
if (RedY <= 340)
RedY += 20;
}
System.out.println("body_length====" + body_length);
if (RedX == randomx && RedY == randomy) {
body_length++;
body[body_length - 1].x = randomx;
body[body_length - 1].y = randomy;
System.out.println("body[body_length-1].x" + body[body_length - 1].x);
System.out.println(body[body_length - 1].y);
}
// 身体刷新
for (int i = body_length - 1; i > 0; i--) {
body[i].x = body[i - 1].x;
body[i].y = body[i - 1].y;
}
// 给球头换颜色
if (RedY == 40 && RedX == 20) {
color = Color.blue;
}
if (RedX == 80 && RedY == 120) {
color = Color.RED;
}
if (RedY == 220 && RedX == 220) {
color = Color.PINK;
}
repaint();
}
}
}
};
run.start();
}
// 生产食物
public void produceFood() {
boolean flag = true;
Random r = new Random();
randomx = (r.nextInt(18) + 1) * 20;
randomy = (r.nextInt(17) + 2) * 20;
while (flag) {
for (int i = 0; i < body_length; i++) {
if (body[i].x == randomx && body[i].y == randomy) {
// 如果生产的食物在球身上,那么就重新生产
randomx = (r.nextInt(18) + 1) * 20;
randomy = (r.nextInt(17) + 2) * 20;
flag = true;
break;
} else {
if (i == body_length - 1) {
flag = false;
}
}
}
}
}
}
③
public class BallMove {
public static void main(String[] args) {
BallThread Ball = new BallThread();
Ball.move();
}
}
如果你有其他想法或建议,写下来:我们一块讨论啊!!!!!!!!!!!!!!!!