[java]代码库
public class Snake{
//食物成员变量
Food food = new Food();
//蛇的成员变量
int[] snakex = new int[150];
int[] snakey = new int[150];
int len = 3 , score = 0 , speed = 1;
int i = 20 , j = 0;
int tempx = 0 , tempy = 0;
int tempx1 = 0 , tempy1 = 0;
boolean start = false;
boolean failed = true;
boolean isMove = false;
boolean pause = true;
//小蛇移动的方式
public boolean move(){
tempx = snakex[0];
tempy = snakey[0];
snakex[0] += i;
snakey[0] += j;
for (int m = len ; m > 0 ; m--){
if(m != 1){
tempx1 = snakex[m-1];
snakex[m] = tempx1;
tempy1 = snakey[m-1];
snakey[m] = tempy1;
}
else{
snakex[m] = tempx;
snakey[m] = tempy;
}
}
//遇到墙体
if( snakex[0] < 210)
snakex[0] = 770;
if(snakex[0] >= 790)
snakex[0] = 210;
if(snakey[0] < 80)
snakey[0] = 540;
if(snakey[0] >= 560)
snakey[0] = 80;
// 吃食物
if(snakex[0] == food.eatx && snakey[0] == food.eaty){
len ++;
score += 50*speed;
food.eatx = food.r.nextInt(29)*20+210;
food.eaty = food.r.nextInt(24)*20+80;
for(int k = 0 ; k < len ; k++){
if(food.eatx == snakex[k] && food.eaty == snakey[k]){
food.eatx = food.r.nextInt(29)*20+210;
food.eaty = food.r.nextInt(24)*20+80;
}
}
}
// 如果蛇头碰到自己的身体游戏结束
for (int i = 1 ; i < len; i ++){
if (snakex[0] == snakex[i] && snakey[0] == snakey[i]){
failed = false;
start = false;
}
}
//分数越高速度越快
if(score == 150)
speed = 2;
if(score == 450)
speed = 3;
if(score == 900)
speed = 4;
if(score == 1500)
speed = 5;
if(score == 2250)
speed = 6;
if(score == 3150)
speed = 7;
return isMove = false;
}
//蛇的初始化
public void setup(){
i = 20;
j = 0;
len = 3;
score = 0;
speed = 1;
snakex[0] = 310; snakex[1] = 290; snakex[2] = 270;
snakey[0] = 300; snakey[1] = 300; snakey[2] = 300;
}
}
[代码运行效果截图]
[源代码打包下载]
初级程序员
by: 做个梦给你 发表于:2019-06-07 18:20:31 顶(0) | 踩(0) 回复
6666
回复评论