首页 > 分享 > JavaScript写的贪吃蛇小游戏

JavaScript写的贪吃蛇小游戏

最新推荐文章于 2023-06-09 20:35:51 发布

码仙♥ 于 2018-10-25 09:44:49 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

目录

0.码仙励志

1.完整版代码加注释

2.游戏说明

3.游戏截图

0.码仙励志

做不了决定的时候,让时间帮你决定。如果还是无法决定,做了再说。宁愿犯错,不留遗憾

1.完整版代码加注释

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>title</title>

<style>

.map {

width: 800px;

height: 600px;

background-color: #CCC;

position: relative;

}

</style>

</head>

<body>

<div class="map"></div>

<script>

(function () {

var elements = [];

function Food(x, y, width, height, color) {

this.x = x || 0;

this.y = y || 0;

this.width = width || 20;

this.height = height || 20;

this.color = color || "green";

}

Food.prototype.init = function (map) {

remove();

var div = document.createElement("div");

map.appendChild(div);

div.style.width = this.width + "px";

div.style.height = this.height + "px";

div.style.backgroundColor = this.color;

div.style.position = "absolute";

this.x = parseInt(Math.random() * (map.offsetWidth / this.width)) * this.width;

this.y = parseInt(Math.random() * (map.offsetHeight / this.height)) * this.height;

div.style.left = this.x + "px";

div.style.top = this.y + "px";

elements.push(div);

};

function remove() {

for (var i = 0; i < elements.length; i++) {

var ele = elements[i];

ele.parentNode.removeChild(ele);

elements.splice(i, 1);

}

}

window.Food = Food;

}());

(function () {

var elements = [];

function Snake(width, height, direction) {

this.width = width || 20;

this.height = height || 20;

this.body = [

{x: 3, y: 2, color: "red"},

{x: 2, y: 2, color: "orange"},

{x: 1, y: 2, color: "orange"}

];

this.direction = direction || "right";

}

Snake.prototype.init = function (map) {

remove();

for (var i = 0; i < this.body.length; i++) {

var obj = this.body[i];

var div = document.createElement("div");

map.appendChild(div);

div.style.position = "absolute";

div.style.width = this.width + "px";

div.style.height = this.height + "px";

div.style.left = obj.x * this.width + "px";

div.style.top = obj.y * this.height + "px";

div.style.backgroundColor = obj.color;

elements.push(div);

}

};

Snake.prototype.move = function (food, map) {

var i = this.body.length - 1;

for (; i > 0; i--) {

this.body[i].x = this.body[i - 1].x;

this.body[i].y = this.body[i - 1].y;

}

switch (this.direction) {

case "right":

this.body[0].x += 1;

break;

case "left":

this.body[0].x -= 1;

break;

case "top":

this.body[0].y -= 1;

break;

case "bottom":

this.body[0].y += 1;

break;

}

var headX = this.body[0].x * this.width;

var headY = this.body[0].y * this.height;

if (headX == food.x && headY == food.y) {

var last = this.body[this.body.length - 1];

this.body.push({

x: last.x,

y: last.y,

color: last.color

});

food.init(map);

}

}

;

function remove() {

var i = elements.length - 1;

for (; i >= 0; i--) {

var ele = elements[i];

ele.parentNode.removeChild(ele);

elements.splice(i, 1);

}

}

window.Snake = Snake;

}());

(function () {

var that = null;

function Game(map) {

this.food = new Food();

this.snake = new Snake();

this.map = map;

that = this;

}

Game.prototype.init = function () {

this.food.init(this.map);

this.snake.init(this.map);

this.runSnake(this.food, this.map);

this.bindKey();

};

Game.prototype.runSnake = function (food, map) {

var timeId = setInterval(function () {

this.snake.move(food, map);

this.snake.init(map);

var maxX = map.offsetWidth / this.snake.width;

var maxY = map.offsetHeight / this.snake.height;

var headX = this.snake.body[0].x;

var headY = this.snake.body[0].y;

if (headX < 0 || headX >= maxX) {

clearInterval(timeId);

alert("游戏结束");

}

if (headY < 0 || headY >= maxY) {

clearInterval(timeId);

alert("游戏结束");

}

}.bind(that), 150);

};

Game.prototype.bindKey = function () {

document.addEventListener("keydown", function (e) {

switch (e.keyCode) {

case 37:

this.snake.direction = "left";

break;

case 38:

this.snake.direction = "top";

break;

case 39:

this.snake.direction = "right";

break;

case 40:

this.snake.direction = "bottom";

break;

}

}.bind(that), false);

};

window.Game = Game;

}());

var gm = new Game(document.querySelector(".map"));

gm.init();

</script>

</body>

</html>

2.游戏说明

此游戏用键盘上的上下左右按键来指挥小蛇的方向,当蛇头碰到自己的身体时游戏不会结束,唯一的结束方式是超出游戏的边界

3.游戏截图

本篇博客来自于传智播客视频教程的总结以及笔记的整理,仅供学习交流,切勿用于商业用途,如有侵权,请联系博主删除,博主QQ:194760901 

相关知识

C#贪吃蛇小游戏
使用MaxKB 增加互动小游戏——贪吃蛇
贪吃蛇案例详解
贪吃蛇游戏程序设计实验报告
基于单片机的贪吃蛇游戏设计
用JavaScript写一个可以聊天的桌面宠物
python+pygame 贪吃蛇游戏
用TypeScript写贪吃蛇(1):开发环境搭建
可爱宠物竞技网游 贪吃蛇
C的课程规范设计贪吃蛇小游戏内附完整源码及附件.doc

网址: JavaScript写的贪吃蛇小游戏 https://m.mcbbbk.com/newsview961314.html

所属分类:萌宠日常
上一篇: python简单游戏——贪吃蛇
下一篇: 2024OGAV参观攻略(时间+