用Java做的贪吃蛇,简单版......
2021-02-12 17:18
标签:png lazy 必须 ++ public cli interrupt dap gre 效果图片: : 代码(里面有调试留下的代码,例如注释,System.out.println())的代码可省略,这样可以减少代码量(这里我就不去除了,I am lazy.)hahahaha: 一共三个类: ① public class Body { public Body(int x, int y) { } ② import java.awt.Color; import javax.swing.JFrame; public class BallThread extends JFrame { /** // 有参构造方法 addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { } }); public void paint(Graphics g) { // System.out.println("运行到这来"); // 画网格 g.clipRect(1, 1, 400, 400); // 1.画方格 if (isEated()) { // 消除闪烁 private boolean isEated() { } // 小球移动 if (RedX == randomx && RedY == randomy) { // 身体刷新 } } run.start(); // 生产食物 while (flag) { } ③ public class BallMove { BallThread Ball = new BallThread(); } 如果你有其他想法或建议,写下来:我们一块讨论啊!!!!!!!!!!!!!!!! 用Java做的贪吃蛇,简单版...... 标签:png lazy 必须 ++ public cli interrupt dap gre 原文地址:https://www.cnblogs.com/12zan/p/12730759.html
int x;
int y;
this.x = x;
this.y = y;
}
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
*
*/
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 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);
System.out.println("到这来.........");
super.keyPressed(e);
// System.out.println(e.getKeyCode());
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;
}
}
}
// 必须传递给父类
super.paint(g);
// g.drawLine(500, 500, 600, 400);
// 竖线
for (int i = 1; i 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 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);
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 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);
}
// 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 RedX += 20;
}
if (direction == "U") {
if (RedY >= 60)
RedY -= 20;
}
if (direction == "D") {
if (RedY RedY += 20;
}
System.out.println("body_length====" + body_length);
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();
}
}
};
}
public void produceFood() {
boolean flag = true;
Random r = new Random();
randomx = (r.nextInt(18) + 1) * 20;
randomy = (r.nextInt(17) + 2) * 20;
for (int i = 0; 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 static void main(String[] args) {
Ball.move();
}
文章标题:用Java做的贪吃蛇,简单版......
文章链接:http://soscw.com/index.php/essay/54537.html