From fb16a90e4cd5feef5814f2749d454aaf5336139c Mon Sep 17 00:00:00 2001
From: Jerry Chan 29 <791603901@qq.com>
Date: Sat, 30 Jan 2021 11:26:26 +0800
Subject: [PATCH] =?UTF-8?q?snake=5F14=5Fhtml4:=20=E4=B8=8A=E4=BC=A014?=
=?UTF-8?q?=E5=B9=B4=E7=94=A8=E5=8E=9F=E7=94=9FHTML4+JavaScript=E5=BC=80?=
=?UTF-8?q?=E5=8F=91=E7=9A=84=E8=B4=AA=E5=90=83=E8=9B=87=E5=B0=8F=E6=B8=B8?=
=?UTF-8?q?=E6=88=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
sanke_14_html4/README.md | 6 +
sanke_14_html4/help.html | 16 ++
sanke_14_html4/index.html | 27 ++++
sanke_14_html4/snake.js | 302 ++++++++++++++++++++++++++++++++++++++
4 files changed, 351 insertions(+)
create mode 100644 sanke_14_html4/README.md
create mode 100644 sanke_14_html4/help.html
create mode 100644 sanke_14_html4/index.html
create mode 100644 sanke_14_html4/snake.js
diff --git a/sanke_14_html4/README.md b/sanke_14_html4/README.md
new file mode 100644
index 0000000..fc7a3a0
--- /dev/null
+++ b/sanke_14_html4/README.md
@@ -0,0 +1,6 @@
+# snake_14_html4
+该目录下为29大约在2014年(似乎是)高考结束的暑假使用原生HTML4+JavaScript(没有使用任何前端库以及HTML5标签)开发的贪吃蛇小游戏,直接双击用浏览器打开index.html即可玩耍。
+
+目前仅可在桌面端浏览器中游戏,暂不支持移动端。
+
+有在线版本,不需要把仓库下到本地就可以玩耍:http://29studio.cn/snake/
diff --git a/sanke_14_html4/help.html b/sanke_14_html4/help.html
new file mode 100644
index 0000000..6ea66e1
--- /dev/null
+++ b/sanke_14_html4/help.html
@@ -0,0 +1,16 @@
+
+
+
+
+ 帮助
+
+
+
+
+ 操作说明:点击“New Game”开始游戏,按“↑”“↓”“←”“→”键让蛇移动,
+点击“Pause/Resume”或按空格键可以暂停或继续游戏,点击“Stop”可以结束当前游戏局。
+ 小贴士:按住某方向键不动可以让蛇加速~~蛇移动到墙边上可以从对面的墙穿出来。
+
+
+
+
diff --git a/sanke_14_html4/index.html b/sanke_14_html4/index.html
new file mode 100644
index 0000000..fc11b42
--- /dev/null
+++ b/sanke_14_html4/index.html
@@ -0,0 +1,27 @@
+
+
+
+
+
+ Jerry Chan 29
+
+
+
+
+ 29的首页正在开发中,先玩一下29开发的JS版贪吃蛇放松一下吧~
+ 29's homepage is under develop, you can play the JavaScript
+ version of the "snake" game that 29 developed and have a rest! :)
+
+
+
Initializing, please wait...
+
+
+
+
+
+
+ 帮助 (help)
+
+
+
+
diff --git a/sanke_14_html4/snake.js b/sanke_14_html4/snake.js
new file mode 100644
index 0000000..06903bd
--- /dev/null
+++ b/sanke_14_html4/snake.js
@@ -0,0 +1,302 @@
+//snake.js
+
+function CreatePoint (x,y)
+{
+var pt = new Object;
+pt.x = x;
+pt.y = y;
+return pt;
+}
+
+var snake = new Array();
+var input = new Array(0);
+var map = new Array (20);
+var food = CreatePoint (0,0);
+var cur_dir;
+var bFast = false,bPause = false,bStart = false;
+var iTimer,nTimerCount = 0;
+var score;
+var i,j;
+
+for (i = 0;i < 20;i ++) map[i] = new Array (15);
+
+function CreateTimer (delay)
+{
+iTimer = window.setInterval ("GameLoop()",delay);
+nTimerCount ++;
+}
+
+function KillTimer ()
+{
+window.clearInterval (iTimer);
+nTimerCount --;
+}
+
+function NewGame ()
+{
+if (bStart && !bPause) KillTimer();
+
+snake.length = 3;
+snake[0] = CreatePoint (10,10);
+snake[1] = CreatePoint (11,10);
+snake[2] = CreatePoint (12,10);
+
+input = new Array (0);
+
+for (i = 0;i < 20;i ++)
+for (j = 0;j < 15;j ++)
+map[i][j] = 0;
+
+cur_dir = 37;
+score = 0;
+bFast = false;
+bPause = false;
+RandFood();
+bStart = true;
+
+CreateTimer(500);
+PrintGame();
+}
+
+function RandFood ()
+{
+var pt = CreatePoint(0,0);
+
+do
+{
+pt.x = Math.floor (Math.random() * 20);
+pt.y = Math.floor (Math.random() * 15);
+}
+while (CheckCover (pt,0))
+
+food = pt;
+}
+
+function CheckCover (pt,num)
+{
+for (i = num;i < snake.length;i ++)
+{
+if (pt.x == snake[i].x && pt.y == snake[i].y)
+return true;
+}
+
+return false;
+}
+
+function GamePause ()
+{
+if (bStart)
+{
+if (bPause)
+{
+CreateTimer (500);
+bPause = false;
+}
+else
+{
+KillTimer();
+bPause = true;
+}
+}
+}
+
+function StopGame ()
+{
+if (!bPause && bStart) KillTimer ();
+PrintBlank();
+bStart = false;
+}
+
+
+function SpacePress ()
+{
+if (window.event.keyCode == 32) GamePause ();
+}
+
+function PushKey (num1,num2)
+{
+if (input.length == 0 && (cur_dir == num1 || cur_dir == num2))
+input.push (window.event.keyCode);
+else if (input.length > 0)
+{
+if (input[input.length - 1] == num1 || input[input.length - 1] == num2)
+input.push (window.event.keyCode);
+}
+}
+
+function ChnDir ()
+{
+if (bStart && !bPause)
+{
+if (window.event.keyCode == cur_dir && input.length == 0 && !bFast)
+{
+KillTimer();
+CreateTimer (50);
+bFast = true;
+}
+else
+{
+if (bFast && (window.event.keyCode != cur_dir || input.length > 0))
+{
+KillTimer();
+CreateTimer (500);
+bFast = false;
+}
+
+switch (window.event.keyCode)
+{
+case 37:
+PushKey (38,40);
+break;
+
+case 38:
+PushKey (37,39);
+break;
+
+case 39:
+PushKey (38,40);
+break;
+
+case 40:
+PushKey (37,39);
+break;
+}
+}
+}
+}
+
+function SlowDown ()
+{
+if (bStart && bFast && window.event.keyCode >= 37 && window.event.keyCode <= 40)
+{
+KillTimer ();
+CreateTimer (500);
+bFast = false;
+}
+}
+
+function SnakeMove (x,y)
+{
+var pt = CreatePoint (0,0);
+
+pt.x = snake[0].x + x;
+pt.y = snake[0].y + y;
+
+if (pt.x < 0) pt.x = 19;
+else if (pt.y < 0) pt.y = 14;
+else if (pt.x >= 20) pt.x = 0;
+else if (pt.y >= 15) pt.y = 0;
+
+snake.unshift (pt);
+}
+
+function GameLoop ()
+{
+if (input.length > 0) cur_dir = input.shift();
+
+switch (cur_dir)
+{
+case 37:
+SnakeMove (-1,0);
+break;
+
+case 38:
+SnakeMove (0,-1);
+break;
+
+case 39:
+SnakeMove (1,0);
+break;
+
+case 40:
+SnakeMove (0,1);
+break;
+}
+
+if (snake[0].x == food.x && snake[0].y == food.y)
+{
+RandFood();
+score++;
+}
+else snake.pop();
+
+if (CheckCover (snake[0],3))
+{
+KillTimer();
+window.alert ("Game End! \nYour score: " + score);
+PrintBlank();
+bStart = false;
+}
+else PrintGame();
+}
+
+function PrintBlank ()
+{
+var id = document.getElementById ("Main");
+var str = "";
+
+str += "\n\n";
+
+for (j = 0;j < 15;j ++)
+{
+str += "\n";
+for (i = 0;i < 20;i ++) str += "   | \n";
+str += "
\n";
+}
+
+str += "
\n";
+
+id.innerHTML = str;
+}
+
+function PrintGame ()
+{
+var id = document.getElementById ("Main");
+var str = "";
+
+for (i = 0;i < 20;i ++)
+for (j = 0;j < 15;j ++)
+map[i][j] = 0;
+
+map[snake[0].x][snake[0].y] = 1;
+
+for (i = 1;i < snake.length;i ++)
+map[snake[i].x][snake[i].y] = 2;
+
+map[food.x][food.y] = 3;
+
+str += "\n\n";
+
+for (j = 0;j < 15;j ++)
+{
+str += "\n";
+
+for (i = 0;i < 20;i ++)
+{
+str += "   | \n";
+}
+
+str += "
\n";
+}
+
+str += "
\n";
+
+id.innerHTML = str;
+}