html5小游戏贪吃蛇手机,html5贪吃蛇小游戏

这是一个使用HTML5 Canvas实现的贪吃蛇游戏代码示例。游戏包含了初始化、移动、转弯、碰撞检测和食物生成等功能。代码中定义了两个主要的类`n97`和`Ga1900`,分别代表游戏逻辑和画布渲染。游戏逻辑类处理蛇的移动、转弯、生死判断和分数更新,而画布渲染类负责绘制游戏元素并更新画布。此外,代码还提供了开始、暂停和继续游戏的控制功能。
摘要由CSDN通过智能技术生成

HTML

导入代码模板:

贪吃蛇

function snake(money, card) {

this.x = money;

this.y = card;

}

function n97() {

var initX = 10;

var initY = 10;

var SIZE = 20;

this.nokia6700 = new Array();

this.nokia5230 = new Array();

this.k209 = 0;

this.targetX = 0;

this.targetY = 0;

this.manager = null;

this.setObserver = function(obs) {

this.manager = obs;

}

this.init = function() {

this.nokia5230.length = 0;

this.nokia6700.length = 0;

for (i = 0; i <= SIZE + 1; i++) {

this.nokia5230[i] = new Array();

}

for (i = 0; i <= SIZE + 1; i++) {

this.nokia5230[i][0] = 1;

this.nokia5230[SIZE + 1][i] = 1;

this.nokia5230[0][i] = 1;

this.nokia5230[i][SIZE + 1] = 1;

}

for (i = 5; i <= initX; i++) {

var point = new snake(i, initY);

this.addsnake(point);

}

this.k209 = 3;

this.productFood();

}

this.move = function() {

var head = this.getHead();

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 = this.getHead();

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)) {

this.manager.removesnake(point);

this.addsnake(point);

this.manager.addScore();

this.productFood();

} else {

this.addsnake(point);

this.delTailsnake();

}

}

this.ifDie = function(point) {

return this.nokia5230[point.x][point.y] == 1;

}

this.getHead = function() {

return this.nokia6700[0];

}

this.getTail = function() {

return this.nokia6700[this.nokia6700.length - 1];

}

this.eatable = function(head) {

return (head.x == this.targetX && head.y == this.targetY);

}

this.addsnake = function(point) {

this.nokia5230[point.x][point.y] = 1;

this.nokia6700.unshift(point);

this.manager.drawsnake(point);

}

this.delTailsnake = function() {

var point = this.nokia6700.pop();

this.nokia5230[point.x][point.y] = 0;

this.manager.removesnake(point);

}

this.productFood = function() {

do {

var x = Math.round(Math.random() * 100 % SIZE);

var y = Math.round(Math.random() * 100 % SIZE);

}

while (this.nokia5230[x][y] == 1)

this.targetX = x;

this.targetY = y;

this.manager.drawFood(x, y);

}

}

function Ga1900(canvasId) {

var WIDTH = 20;

var canvas = document.getElementById(canvasId);

var RED = "#FF0000"

var WHITE = "#FFFFFF";

var BLACK = "#000000";

this.cxt = canvas.getContext("2d");

var e398 = new n97();

this.moveHandle = null;

this.gamePanel = document.getElementById("gamePanel");

this.scoreLabel = document.getElementById("score");

this.maxScoreLabel = document.getElementById("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;

this.gamePanel.onkeydown = onKeyDown;

this.gamePanel.onkeydown = onKeyDown;

this.step = parseInt(step);

this.moveHandle = setInterval(move, 500 - 50 * this.step);

}

var move = function() {

e398.move();

}

this.stopGame = function() {

this.pause();

document.getElementById("control").disabled = true;

localStorage.maxScore = this.maxScore;

}

this.pause = function() {

clearInterval(this.moveHandle);

this.gamePanel.onkeydown = null;

}

this.goon = function() {

this.gamePanel.onkeydown = onKeyDown;

this.moveHandle = setInterval(move, 500 - 50 * this.step);

}

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.removeFood = function(x, y) {

this.cxt.fillStyle = WHITE;

this.cxt.fillRect((x - 1) * WIDTH, (y - 1) * WIDTH, WIDTH, WIDTH);

}

this.drawFood = function(x, y) {

this.cxt.fillStyle = RED;

this.cxt.fillRect((x - 1) * WIDTH, (y - 1) * WIDTH, WIDTH, WIDTH);

}

this.drawsnake = function(point) {

this.cxt.fillStyle = BLACK;

this.cxt.fillRect((point.x - 1) * WIDTH, (point.y - 1) * WIDTH, WIDTH, WIDTH);

}

this.removesnake = function(point) {

this.cxt.fillStyle = WHITE;

this.cxt.fillRect((point.x - 1) * WIDTH, (point.y - 1) * WIDTH, WIDTH, WIDTH);

}

this.clear = function() {

this.cxt.fillStyle = WHITE;

this.cxt.fillRect(0, 0, 20 * WIDTH, 20 * WIDTH);

}

}

贪吃蛇

您的浏览器不支持canvas,请尝试更换浏览器,建议使用opera浏览本站,你会有更好的体验。

最高分

0

得分

0

等级:

1

2

3

4

5

6

7

8

9

9

10

11

12

13

100

开始

暂停

var gameMrg = new Ga1900("myCanvas");

function startGame(startBtn) {

var step = document.getElementById("speed").value;

gameMrg.startGame(step);

document.getElementById("gamePanel").focus();

pause = true;

var btn = document.getElementById("control");

btn.innerHTML = "暂停";

btn.disabled = false;

}

var pause = true;

function control(btn) {

if (pause) {

gameMrg.pause();

btn.innerHTML = "继续";

} else {

gameMrg.goon();

btn.innerHTML = "暂停";

document.getElementById("gamePanel").focus();

}

pause = !pause;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值