javascript实现贪吃蛇游戏(娱乐版)

时间:2021-12-10 03:16:23

本文实例为大家分享了javascript实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

一共三个对象map,snake,food,代表的含义如名字。snake和food其实就是数组,表示位置,map来画图、判断得分、失败等等,直接上代码,可直接运行。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!doctype html>
<html>
 
<body>
 <canvas id="map" width="400" height="400" style="background:Black"></canvas>
 <h1>Score:</h1>
 <h2 id="score">0</h2>
 
 <script>
 //地图
 function Map() {
 this.field = document.getElementById("map").getContext("2d"); //画布
 this.draw = function (something) { //画蛇或者食物
 this.field.fillStyle = something.color;
 var position;
 for (position in something.positions) {
 this.field.fillRect(something.positions[position][0], something.positions[position][1], 20, 20);
 }
 }
 this.clear = function () { //清除画布
 this.field.clearRect(0, 0, 400, 400);
 }
 this.judge = function (snake, food) { //判断状态(得分、失败、普通)
 var snakeHeadX = snake.positions[0][0];
 var snakeHeadY = snake.positions[0][1];
 var foodX = food.positions[0][0];
 var foodY = food.positions[0][1];
 if ((snakeHeadX == foodX) && (snakeHeadY == foodY)) { //吃食物
 snake.positions.unshift([foodX, foodY]);
 food.positions[0] = [Math.floor(Math.random() * 20) * 20, Math.floor(Math.random() * 20) * 20];
 this.clear();
 this.draw(food);
 this.draw(snake);
 var score = document.getElementById('score');
 score.innerHTML = (Number(score.innerHTML)+1).toString();
 }
 else if ((snakeHeadX+20 > 400) || (snakeHeadX < 0) || (snakeHeadY+20 > 400) || (snakeHeadY < 0)) {
 alert('GIME OVER!'); //撞墙
 }
 else {
 this.clear();
 this.draw(food);
 this.draw(snake);
 }
 }
 }
 
 //蛇
 function Snake() {
 this.positions = [[40 + 20, 40], [40, 40], [40 - 20, 40]]; //蛇的躯干
 this.color = "Yellow";
 this.direction = [1,0]; //蛇头方向
 this.move = function () { //移动
 this.positions.unshift([this.positions[0][0] + this.direction[0] * 20, this.positions[0][1] + this.direction[1] * 20]);
 this.positions.pop();
 }
 this.obeyOrders = function (snake = this) { //等待键盘上下左右
 document.onkeydown = function (event) {
 var e = event || window.event || arguments.callee.caller.arguments[0];
 var order = e.keyCode;
 console.log(snake.direction);
 switch (order) {
 case 37:
 snake.direction[0] = -1;
 snake.direction[1] = 0;
 break;
 case 38:
 snake.direction[1] = -1;
 snake.direction[0] = 0;
 break;
 case 39:
 snake.direction[0] = 1;
 snake.direction[1] = 0;
 break;
 case 40:
 snake.direction[1] = 1;
 snake.direction[0] = 0;
 break;
 default:
 ;
 }
 };
 }
 }
 
 //食物
 function Food() {
 this.positions = [[Math.floor(Math.random() * 20) * 20, Math.floor(Math.random() * 20) * 20]]; //随机位置
 this.color = 'Red';
 }
 
 
 //开始执行
 (function () {
 
 var map = new Map();
 var snake = new Snake();
 var food = new Food();
 map.draw(snake);
 map.draw(food);
 snake.obeyOrders();
 var t=0;
 var interval = setInterval(function () { //每0.5s走一步
 map.judge(snake, food);
 snake.move();
 }, 500);
 })()
 </script>
 
 
</body>
 
</html>

效果如图

javascript实现贪吃蛇游戏(娱乐版)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/CYS_zxcvbnm/article/details/108014544