1 Star 0 Fork 0

乔之恒 / 贪吃蛇

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Main.java 3.98 KB
一键复制 编辑 原始数据 按行查看 历史
乔之恒 提交于 2020-10-04 10:25 . update Main.java.
import javax.swing.*;
import java.awt.*;
import java.util.Queue;
import java.util.LinkedList;
import java.awt.event.*;
import java.util.Random;
public class Main extends JPanel implements KeyListener{
/*以下包括本注释请在编译前删除,否则可能在一些计算机上编译错误*/
public static int shu = 12,heng = 15; //行数,列数
public static int block_size = 80; //每格边长(>8)
private mpair dir,dir_real;
private Queue<mpair> snake_que = new LinkedList<mpair>();
private mpair front;
private mpair food;
private int size_init = 3; //初始长度
private int size_change = 0;
private int zengjia = 3; //每次长度增加的长度
private int sleep_time = 100; //每次刷新后睡眠时间(最好不要太短)
public static void main(String args[]) {
JFrame mf = new JFrame();
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.setBounds(20,20,heng*block_size,shu*block_size+20);
Main mp = new Main();
mf.add(mp);
mf.setVisible(true);
mp.requestFocus();
mf.setResizable(false);
while(true) {
try {
Thread.sleep(mp.sleep_time);
} catch (InterruptedException e) {}
mp.change();
if(mp.test_dead()==true) {
System.exit(0);
}
mp.test_food();
mp.repaint();
}
}
private void add_food() {
Random rand_x = new Random();
Random rand_y = new Random();
while(true) {
this.food = new mpair(rand_x.nextInt(shu)+1,rand_y.nextInt(heng)+1);
boolean boo = true;
for(mpair i: this.snake_que) {
if(food.a == i.a && food.b == i.b) {
boo = false;
break;
}
}
if(boo) return;
}
}
public void test_food() {
if(this.front.a == this.food.a && this.front.b == this.food.b) {
this.add_food();
this.size_change = this.zengjia;
}
}
public boolean test_dead() {
int u=1;
for(mpair i : this.snake_que) {
if((i.a == front.a && i.b == front.b) && u!=this.snake_que.size()) {
return true;
}
u++;
}
return false;
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(new Color(0,0,0));
g.fillRect(0,0,this.block_size*heng,this.block_size*shu);
g.setColor(new Color(255,255,255));
for(mpair i : this.snake_que) {
g.fillRect((i.b-1)*block_size+1,(i.a-1)*block_size+1,block_size-2,block_size-2);
}
g.setColor(new Color(0,0,170));
g.fillRect((food.b-1)*block_size+4,
(food.a-1)*block_size+4,block_size-8,block_size-8);
}
public void change() {
this.dir_real = this.dir;
mpair nefront = new mpair(
front.a+dir_real.a,
front.b+dir_real.b
);
if(nefront.a > this.shu) nefront.a = 1;
else if(nefront.a < 1) nefront.a = this.shu;
if(nefront.b > this.heng) nefront.b = 1;
else if(nefront.b < 1) nefront.b = this.heng;
this.snake_que.offer(nefront);
this.front = nefront;
if(size_change == 0) this.snake_que.poll();
else size_change--;
}
public Main() {
for(int i=1;i<=this.size_init;i++) {
this.snake_que.offer(new mpair(1,i));
}
this.dir = new mpair(0,1);
this.front = new mpair(1,this.size_init);
this.add_food();
this.addKeyListener(this);
}
@Override
public void keyPressed(KeyEvent e) {
mpair lin = new mpair(0,0);
if(e.getKeyCode()==27) System.exit(0);
if(e.getKeyCode()==38) lin = new mpair(-1,0);
else if(e.getKeyCode()==37) lin = new mpair(0,-1);
else if(e.getKeyCode()==40) lin = new mpair(1,0);
else if(e.getKeyCode()==39) lin = new mpair(0,1);
else {
return;
}
if(this.dir_real.a == -lin.a && this.dir_real.b == -lin.b) return;
this.dir = lin;
}
@Override
public void keyReleased(KeyEvent keyEvent) { }
@Override
public void keyTyped(KeyEvent keyEvent) { }
}
class mpair {
int a,b;
public mpair(int x,int y) {
a = x;
b = y;
}
}
Java
1
https://gitee.com/qiao_zhi_heng/greedy-snake.git
git@gitee.com:qiao_zhi_heng/greedy-snake.git
qiao_zhi_heng
greedy-snake
贪吃蛇
master

搜索帮助