html贪吃蛇代码最近在搞⾃⼰的⽹站,维护的时候准备放个贪吃蛇上去,顶⼀下原有的页⾯。
这个贪吃蛇有⼀点毒。原来设定了100级;100级刚开局就挂了。后来改掉了选项菜单,修复了。
还有什么bug,欢迎点击侧边的QQ按钮联系我。
代码部分:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>⽹站维护中</title>
<script language="javascript">
function snake(money,card){
this.x = money;
this.y = card;
}
function n97(){
var initX = 10;
var initY = 10;
var SIZE = 20;
this.k209 = 0;
this.targetX = 0;
this.targetY = 0;
this.manager = null;
this.setObserver = function(obs){
this.manager = obs;
}
this.init =  function(){
for(i = 0;i <= SIZE + 1; i++ ){
}
for (i = 0; i <= SIZE + 1; i++) {
}
for (i = 5; i <= initX; i++) {
var point = new snake(i, initY);
this.addsnake(point);
}
this.k209 = 3;
this.productFood();
}
var head = Head();
var point = new snake(head.x,head.y);
switch (this.k209) {
case 1:
point.x-- ;
break;
case 2:
point.y--;
break;
case 3:
point.x++;
break;
case 4:
point.y++;
break;
}
this.process(point);
}
this.turn = function(code){
var head = Head();
var point = new snake(head.x,head.y);
switch(code - 36){
case 1:
if(this.k209 == 1 || this.k209 == 3)
return;
point.x--;
break;
case 2:
if(this.k209 == 2 || this.k209 == 4)
return;
point.y--;
break;
case 3:
if(this.k209 == 1 || this.k209 == 3)
return;
point.x++;
break;
case 4:
if(this.k209 == 2 || this.k209 == 4)
return;
point.y++;
break;
}
this.k209 = code - 36;
this.process(point);
}
this.process = function(point){
if (this.ifDie(point)) {
alert("你挂了!");
this.manager.stopGame();
return;
}
if (this.eatable(point)) {
vesnake(point);
this.addsnake(point);
this.manager.addScore();
this.productFood();
}
else {
this.addsnake(point);
this.delTailsnake();
}
}
this.ifDie = function(point){
kia5230[point.x][point.y] == 1;
}
kia6700[0];
}
kia6700.length - 1];
}
this.eatable = function(head){
return (head.x == this.targetX && head.y == this.targetY);
}
贪吃蛇的编程代码this.addsnake = function(point){
this.manager.drawsnake(point);
}
this.delTailsnake = function(){
var point = kia6700.pop();
vesnake(point);
}
this.productFood = function(){
do {
var x = und(Math.random() * 100 % SIZE);
var y = und(Math.random() * 100 % SIZE);
}
while (kia5230[x][y] == 1)
this.targetX = x;
this.targetY = y;
this.manager.drawFood(x,y);
}
}
function Ga1900(canvasId){
var WIDTH = 20;
var canvas = ElementById(canvasId);
var RED = "#FF0000"
var WHITE = "#FFFFFF";
var BLACK = "#000000";
< = Context("2d");
var e398 = new n97();
this.gamePanel = ElementById("gamePanel"); this.scoreLabel =  ElementById("score");
this.maxScoreLabel =  ElementById("highestScore"); this.step = 0;
this.score = 0;
this.maxScore = 0;
if(localStorage.maxScore)
this.maxScore = localStorage.maxScore;
this.maxScoreLabel.innerHTML = this.maxScore;
e398.setObserver(this);
this.startGame = function(step){
this.clear();
e398.init();
this.score = 0;
this.scoreLabel.innerHTML = this.score;
keydown = onKeyDown;
keydown = onKeyDown;
this.step = parseInt(step);
}
var move = function(){
}
this.stopGame = function(){
this.pause();
localStorage.maxScore = this.maxScore;
}
this.pause = function(){
veHandle);
keydown = null;
}
< = function(){
keydown = onKeyDown;
}
this.addScore = function(){
this.score += this.step;
this.scoreLabel.innerHTML = this.score;
if(this.score > this.maxScore){
this.maxScore = this.score;
this.maxScoreLabel.innerHTML = this.score;
}
}
var onKeyDown = function(e){
if (e.which < 37 || e.which > 40)
return;
e398.turn(e.which);
}
}
this.drawFood = function(x,y){
}
this.drawsnake = function(point){
}
}
this.clear = function(){
}
}
</script>
</head>
<body>
<div align="center">
<h1>⽹站正在维护,先玩会贪吃蛇</h1>
<div >
<div id="gamePanel" tabindex="0"  >
<canvas id="myCanvas" width="400" height="400" >
您的浏览器不⽀持canvas,请尝试更换浏览器,建议使⽤opera浏览本站,你会有更好的体验。
</canvas>
</div>
<div id="scoreboard" >
最⾼分
<div id="highestScore" >
</div>
<p></p>
得分
<div id="score" >
</div>
</div>
</div>
<p>
等级: <select id="speed">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>9</option>
</select>
<button id="start" onclick="startGame(this)">开始</button>
<button id="control" onclick="control(this)" disabled="true">暂停</button> </p>
</div>
<script language="JavaScript">
var gameMrg = new Ga1900("myCanvas");
function startGame(startBtn){
var step = ElementById("speed").value;
gameMrg.startGame(step);
pause = true;
var btn = ElementById("control");
btn.innerHTML = "暂停";
btn.disabled = false;
}
var pause = true;
function control(btn){
if (pause) {
gameMrg.pause();
btn.innerHTML = "继续";
}
else{
<();
btn.innerHTML = "暂停";
}
pause = !pause;
}
</script>
</body>
</html>
贪吃蛇
注意:浏览器必须⽀持canvas才⾏。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。