本文目录一览:
- 1、js中当贪吃蛇达到一定长度加速的代码怎么写
- 2、各位大侠,本人新手跪拜写贪吃蛇的代码,只需提供具体步骤 编译环境 涉及的函数 无需具体代码!不胜感激呀
- 3、高手帮忙解释一下JAVA 贪食蛇游戏中蛇的代码
js中当贪吃蛇达到一定长度加速的代码怎么写
获取长度变量的值,判断长度,大于一定长度,改变速度就好了
各位大侠,本人新手跪拜写贪吃蛇的代码,只需提供具体步骤 编译环境 涉及的函数 无需具体代码!不胜感激呀
我这里有一个之前用js写的贪吃蛇,只要用浏览器就可以看到效果。
你新建一个snake.html,然后右击-打开方式-记事本,把下面的代码复制到里面,然后保存好。再用浏览器打开这个页面就能看到html。
-------------------------------------------------以下是代码-------------------------------------
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312" /
title贪吃蛇/title
script type="text/javascript"
/**************js代码************************/
var width = 10 ; //单元格的宽高,食物的宽高
var num = 60 ; //单元格列数
var row = 30; //单元格行数
var map_width = 0; //地图的宽度
var map_height = 0; //地图的宽度
var map_arr = new Array() ;//地图数组
var border = 5;//地图边框厚度
var borderColor = "#ff0"; //地图边框颜色
var footColor = "#f00"; //食物颜色
var snakeColor = "#faa"; //蛇的颜色
var Sx,Sy; //蛇的初始化位置
var Gx = 0,Gy = 0;//蛇移动方向
var oTime = 0,ot = 200; //蛇移动速度
var dd = 10; //每升一级提升的速度
var fen = 0 ; //得分
var den = 1 ; //等级
var lock = 1; //操作锁
var sfColor = "#f55"; //食物吞进后的颜色
var times = 0;//计时器
var timer;
var easyColor = "blue";
var easyNum = 3; //阻碍个数
//创建地图
function create_map(){
var map = document.createElement("div");
map_width = width*num;
map_height = width*row;
//获得屏幕宽高
var sw = window.screen.width;
var sh = window.screen.height;
//设置map样式
map.setAttribute('id','map');
map.style.position = 'absolute';
map.style.width = map_width+2*border+'px';
map.style.height = map_height+2*border+'px';
map.style.border = border+"px solid "+borderColor;
map.style.left = (sw/2) - (map_width/2) + 'px';
map.style.top = (sh/2) - (map_height/2) - 120 + 'px';
document.body.appendChild(map);
//设置地图数组初始值
for(var i = 0;i=num;i++ ){
map_arr[i] = new Array();
for(var j = 0;j=row;j++){
map_arr[i][j] = 1;
}
}
}
//创建食物
function create_foot(){
var fw = parseInt(Math.random()*num);
var fh = parseInt(Math.random()*row);
if(map_arr[fw][fh] != 1){
create_foot();
return ;
}else{
var span = document.createElement("span");
span.style.width = width+'px';
span.style.height = width + 'px';
span.style.position = "absolute";
span.style.marginLeft = width*fw+'px';
span.style.marginTop = width*fh+'px';
span.style.backgroundColor = footColor;
span.style.overflow = 'hidden';
span.setAttribute('id','foot');
document.getElementById('map').appendChild(span);
map_arr[fw][fh] = 2;
}
}
//创建蛇
function create_snake(){
var snake = document.createElement('div');
snake.style.position = 'absolute';
snake.style.left = Sx*width+'px';
snake.style.top = Sy*width+'px';
snake.style.width = width+'px';
snake.style.height = width+'px';
snake.style.overflow = 'hidden';
snake.style.backgroundColor = snakeColor;
snake.style.color = snakeColor;
//存储当前蛇体的位置
snake.innerHTML = Sx+'#'+Sy;
//吞进的食物另做处理
if(map_arr[Sx][Sy] == 2){
snake.style.backgroundColor = sfColor;
snake.style.color = sfColor;
}
document.getElementById('map').appendChild(snake);
map_arr[Sx][Sy] = 3;
}
//蛇移动的前方碰到食物
function move1(){
//吃掉食物
//map_arr[Sx][Sy] = 1;
obj = document.getElementById('foot');
document.getElementById('map').removeChild(obj);
//创建蛇头
create_snake();
//创建食物
create_foot();
//获得分数
resetFen();
}
//重置分数和加快游戏速度
function resetFen(){
fen += 10;
document.getElementById('shu').innerHTML = fen;
den = parseInt(fen/100)+1;
document.getElementById('den').innerHTML = den;
oTime = ot - (den-1)*dd;
}
//蛇移动前方为空地
function move2(){
//删除蛇尾
pos = document.getElementById('map').getElementsByTagName('div')[0].innerHTML;
arr = pos.split('#');
x = parseInt(arr[0]);
y = parseInt(arr[1]);
map_arr[x][y] = 1;
obj = document.getElementById('map').getElementsByTagName('div')[0];
document.getElementById('map').removeChild(obj);
//创建蛇头
create_snake();
}
//重新开始
function move3(){
window.clearInterval(timer);
if(confirm("Game Over,重新开始?"))
window.location.reload()
}
//蛇移动
function move(){
Sx += Gx;
Sy += Gy;
if(Sx0 || Sy0 || Sxnum || Syrow){
move3();
}else if(map_arr[Sx][Sy] == 1){
move2();
window.setTimeout("move()",oTime);
}else if(map_arr[Sx][Sy] == 2){
move1();
window.setTimeout("move()",oTime);
}else{
move3();
}
lock = 1;
}
window.onload = function(){
getSoure();
oTime = ot - (den-1)*dd;
//创建地图
create_map();
//设置蛇的初始位置
Sx = parseInt(Math.random()*num);
Sy = parseInt(Math.random()*row);
create_snake();
//创建食物
create_foot();
//设置阻碍
for(var i=0;ieasyNum;i++){
len = parseInt(Math.random()*row/2)+5;
rows = parseInt(Math.random()*row);
cols = parseInt(Math.random()*num);
bool = parseInt(Math.random()*100)%2;
setEasy(len,cols,rows,bool);
}
window.document.onkeydown = function(e){
e = window.event || e;
if(e.keyCode == 37 Gx != 1 lock == 1 ){
lock = 0;
goes(-1,0);
}else if(e.keyCode == 39 Gx != -1 lock == 1){
lock = 0;
goes(1,0)
}else if(e.keyCode == 38 Gy != 1 lock == 1){
lock = 0;
goes(0,-1)
}else if(e.keyCode == 40 Gy != -1 lock == 1){
lock = 0;
goes(0,1)
}else if(e.keyCode == 27){
alert("游戏暂停");
}
}
}
var start = 1;
function goes(x,y){
Gx = x;
Gy = y;
if(start == 1){
move();
start = 0;
setTime();
}
}
//计时
function setTime(){
window.clearInterval(timer);
timer = window.setInterval(function(){
times += 1;
var min = parseInt(times/6000);
var s = parseInt(times/100);
s = s%60;
var ms = parseInt(times%100);
(min 10) (min = '0'+min);
(s 10) (s = '0'+s);
(ms 10) (ms = '0'+ms);
document.getElementById('time').innerHTML = min+":"+s+":"+ms
},10)
}
//创建得分及等级窗口
function getSoure(){
var source = document.createElement('div');
source.style.position = "absolute";
source.style.width = "300px";
source.style.height = "200px";
source.style.border = "2px solid #ff0";
var cheng = document.createElement('div');
cheng.style.width = "200px";
cheng.style.height = "30px";
cheng.style.paddingTop = "10px";
var title = document.createElement('span');
title.style.width = "90px";
title.style.paddingTop = "15px";
title.style.paddingLeft = "30px";
title.style.cssFloat = "left";
title.style.color = "red";
title.style.fontSize = "14px";
title.style.fontWeight = "900";
title.innerHTML = "分 数:";
var shu = document.createElement('span');
shu.style.width = "50px";
shu.style.paddingLeft = "30px";
shu.style.paddingTop = "10px";
shu.style.cssFloat = "left";
shu.style.color = "red";
shu.style.fontSize = "24px";
shu.style.fontWeight = "900";
shu.setAttribute('id','shu');
shu.innerHTML = "0";
cheng.appendChild(title);
cheng.appendChild(shu);
source.appendChild(cheng);
cheng = document.createElement('div');
cheng.style.width = "200px";
cheng.style.height = "30px";
cheng.style.paddingTop = "20px";
title = document.createElement('span');
title.style.width = "90px";
title.style.paddingTop = "15px";
title.style.paddingLeft = "30px";
title.style.cssFloat = "left";
title.style.color = "red";
title.style.fontSize = "14px";
title.style.fontWeight = "900";
title.innerHTML = "等 级:";
shu = document.createElement('span');
shu.style.width = "50px";
shu.style.paddingLeft = "30px";
shu.style.paddingTop = "10px";
shu.style.cssFloat = "left";
shu.style.color = "red";
shu.style.fontSize = "24px";
shu.style.fontWeight = "900";
shu.setAttribute('id','den');
shu.innerHTML = "1";
cheng.appendChild(title);
cheng.appendChild(shu);
source.appendChild(cheng);
cheng = document.createElement('div');
cheng.style.width = "200px";
cheng.style.height = "auto";
cheng.style.paddingTop = "20px";
title = document.createElement('span');
title.style.width = "90px";
title.style.paddingTop = "15px";
title.style.paddingLeft = "30px";
title.style.cssFloat = "left";
title.style.color = "red";
title.style.fontSize = "14px";
title.style.fontWeight = "900";
title.innerHTML = "计 时:";
shu = document.createElement('span');
shu.style.width = "50px";
shu.style.paddingLeft = "30px";
shu.style.paddingTop = "10px";
shu.style.cssFloat = "left";
shu.style.color = "red";
shu.style.fontSize = "24px";
shu.style.fontWeight = "900";
shu.setAttribute('id','time');
shu.innerHTML = "00:00:00";
cheng.appendChild(title);
cheng.appendChild(shu);
source.appendChild(cheng);
document.body.appendChild(source);
}
//阻栏
function setEasy(len,nums,rows,bool){
for(var i=0;ilen;i++){
var fw = bool == 0 ? nums : nums+i;
var fh = bool == 0 ? rows+i : rows;
if(fw = num fh = row map_arr[fw][fh] == 1){
var span = document.createElement("span");
span.style.width = width+'px';
span.style.height = width + 'px';
span.style.position = "absolute";
span.style.marginLeft = width*fw+'px';
span.style.marginTop = width*fh+'px';
span.style.backgroundColor = easyColor;
span.style.overflow = 'hidden';
document.getElementById('map').appendChild(span);
map_arr[fw][fh] = 4;
}
}
}
/script
/head
body
/body
/html
高手帮忙解释一下JAVA 贪食蛇游戏中蛇的代码
public
class
Snake
implements
Data//蛇类实现了data接口
{
public
Snake()
{
array.add(new
Point(10,
10));//
array.add(new
Point(10,
11));//
array.add(new
Point(10,
12));//
array.add(new
Point(10,
13));//
array.add(new
Point(10,
14));//这五句话是构造一个蛇,这条蛇由五个坐标点连成
currentFlag
=UPFLAG
;//当前的运动方向设为向上
lifeFlag
=
true;//当前蛇的生命设为活着
}
public
void
move()//蛇的运动函数
{
tair
=
(Point)array.get(array.size()
-
1);//得到蛇的尾巴
int
len
=
array.size()
-
2;
for(int
i
=
len;
i
=
0;
i--)//这个for循环把蛇的每一个点都坐标偏移即运动了
{
Point
leftPoint
=
(Point)array.get(i);
Point
rightPoint
=
(Point)array.get(i
+
1);
rightPoint.x
=
leftPoint.x;
rightPoint.y
=
leftPoint.y;//每一个右边的点等于其左边的点,像蛇那样的一缩一张的运动
}
}
public
void
moveHeadLeft()//把蛇的头部转向左边
{
if(currentFlag
==
RIGHTFLAG
||
currentFlag
==
LEFTFLAG)//如果运动方向设为向左或向右则不执行
{
return;
}
move();
Point
point
=
(Point)(array.get(0));
//得到蛇头部
point.x
=
point.x
-
1;//蛇头部横坐标减一,纵坐标不变
point.y
=
point.y;
if(point.x
LEFT)//如果蛇头部不能再左,就不执行
{
lifeFlag
=
false;
}
currentFlag
=
LEFTFLAG;//将蛇运动方向改为左
}