贪吃蛇游戏
一直喜欢玩贪吃蛇,又想联系前端,感觉用js写网页版小游戏还蛮好的。 因此就去万能的b站找了个视频,就学着写了贪吃蛇的小游戏,自己玩起来感觉还算ok。
效果图片
效果
开始暂停
刷新随机产生食物位置
撞墙
代码实现
index.html
<!Doctype html>
<html lang="en">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<head>
<meta charset="utf-8">
<title>贪吃蛇小游戏
</title>
<link rel="stylesheet" href="css/main.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
<div id="main">
<h1>贪吃蛇小游戏
</h1>
<input class="btn btn-begin" type="button" value="开始游戏" id="begin">
<input class="btn btn-pause" type="button" value="暂停游戏" id="pause">
<span class="gtitle">第
<span id="gnum">1
</span> 关
</span>
<script type="text/javascript" src="js/game.js"></script>
</div>
</body>
</html>
main.css
body{
margin: 0px
;
padding: 0px
;
}
#main{
margin: 100px
;
}
.gtitle{
font-size:25px
;
font-weight: bold
;
}
#gnum{
font-size: 50px
;
color:#8B0000
;
}
.btn{
cursor: pointer
;
width: 100px
;
height: 40px
;
border-radius: 10px
;
color: #fff
;
}
.btn-begin{
background: lightseagreen
;
}
.btn-begin:hover{
background: cadetblue
;
font-weight: bold
;
}
.btn-pause{
background: brown
;
}
.btn-pause:hover{
background: orangered
;
font-weight: bold
;
}
game.js
var main
=documeant
.getElementById('main');
var showcanvas
= true;
function Map(atom
,xnum
,ynum
) {
this.atom
=atom
;
this.xnum
=xnum
;
this.ynum
=ynum
;
this.canvas
=null;
this.create=function () {
this.canvas
=document
.createElement('div');
this.canvas
.style
.cssText
='position:relative;top:40px;border:1px solid #808080;background:#eee;';
this.canvas
.style
.width
=this.atom
* this.xnum
+'px';
this.canvas
.style
.height
=this.atom
* this.ynum
+'px';
main
.appendChild(this.canvas
);
if(showcanvas
){
for(var i
=0;i
<xnum
;i
++){
for(var j
=0;j
<ynum
;j
++){
var a
=document
.createElement('div');
a
.style
.cssText
='border:1px solid #808080';
a
.style
.width
=this.atom
+'px';
a
.style
.height
=this.atom
+'px';
a
.style
.background
='#D3D3D3';
this.canvas
.appendChild(a
);
a
.style
.position
='absolute';
a
.style
.left
=i
* this.atom
+'px';
a
.style
.top
=j
* this.atom
+'px';
}
}
}
}
}
function Food(map
) {
this.width
=map
.atom
;
this.height
=map
.atom
;
this.bgcolor
='rgb('+Math
.floor(Math
.random()*200)+','+Math
.floor(Math
.random()*200)+','+Math
.floor(Math
.random()*200)+')';
this.x
=Math
.floor(Math
.random()*map
.xnum
);
this.y
=Math
.floor(Math
.random()*map
.ynum
);
this.flag
=document
.createElement('div');
this.flag
.style
.width
=this.width
+'px';
this.flag
.style
.height
=this.height
+'px';
this.flag
.style
.background
=this.bgcolor
;
this.flag
.style
.borderRadius
='50%';
this.flag
.style
.position
='absolute';
this.flag
.style
.left
=this.x
*this.width
+'px';
this.flag
.style
.top
=this.y
*this.height
+'px';
map
.canvas
.appendChild(this.flag
);
}
function Snake(map
) {
this.width
=map
.atom
;
this.height
=map
.atom
;
this.direction
='right';
this.body
=[
{x
:2,y
:0},
{x
:1,y
:0},
{x
:0,y
:0}
];
this.display = function () {
for(var i
=0;i
<this.body
.length
;i
++){
if(this.body
[i
].x
!=null){
var s
=document
.createElement('div');
this.body
[i
].flag
=s
;
s
.style
.width
=this.width
+'px';
s
.style
.height
=this.height
+'px';
s
.style
.borderRadius
='50%';
s
.style
.background
='rgb('+Math
.floor(Math
.random()*200)+','+Math
.floor(Math
.random()*200)+','+Math
.floor(Math
.random()*200)+')';
s
.style
.position
='absolute';
s
.style
.left
=this.body
[i
].x
* this.width
+'px';
s
.style
.top
=this.body
[i
].y
* this.height
+'px';
map
.canvas
.appendChild(s
);
}
}
}
this.run=function () {
for(var i
=this.body
.length
-1;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 'left':this.body
[i
].x
-=1;break;
case 'right':this.body
[i
].x
+=1;break;
case 'up':this.body
[i
].y
-=1;break;
case 'down':this.body
[i
].y
+=1;break;
}
if(this.body
[0].x
==food
.x
&& this.body
[0].y
==food
.y
){
this.body
.push({x
:null,y
:null,flag
:null});
map
.canvas
.removeChild(food
.flag
);
food
=new Food(map
);
}
if(this.body
[0].x
<0 || this.body
[0].x
>map
.xnum
-1||this.body
[0].y
<0 || this.body
[0].y
>map
.ynum
-1){
clearInterval(timer
);
alert('撞墙了');
restart(map
,this);
return false;
}
for(var i
=4;i
<this.body
.length
;i
++){
if(this.body
[0].x
==this.body
[i
].x
&& this.body
[0].y
==this.body
[i
].y
){
clearInterval(timer
);
alert('撞到自己了');
restart(map
,this);
return false;
}
}
for(var i
=0; i
<this.body
.length
; i
++){
if(this.body
[i
].flag
!= null) {
map
.canvas
.removeChild(this.body
[i
].flag
);
}
}
this.display();
}
}
function restart(map
,snake
) {
for (var i
= 0; i
< snake
.body
.length
; i
++) {
map
.canvas
.removeChild(snake
.body
[i
].flag
);
}
snake
.body
= [
{x
: 2, y
: 0},
{x
: 1, y
: 0},
{x
: 0, y
: 0}
];
snake
.direction
= 'right';
snake
.display();
map
.canvas
.removeChild(food
.flag
);
food
= new Food(map
);
}
function Level() {
this.num
=1;
this.speed
=300;
this.slength
=8;
this.set=function () {
this.num
++;
if(this.speed
<=50){
this.speed
=50;
}else{
this.speed
-=50;
}
this.slength
+=20;
this.display();
start();
}
this.display=function () {
$('#gunm').innerHtml
=this.num
;
}
}
var level
=new Level();
level
.display();
var map
=new Map(20,40,20);
map
.create();
var food
= new Food(map
);
var snake
= new Snake(map
);
snake
.display();
window
.onkeydown=function (event
) {
var ev
=event
||window
.event
;
switch(event
.keyCode
){
case 38://上
if(snake
.direction
!='down'){
snake
.direction
='up';
}
break;
case 40://下
if(snake
.direction
!='up'){
snake
.direction
='down';
}
break;
case 37://左
if(snake
.direction
!='right'){
snake
.direction
='left';
}
break;
case 39://右
if(snake
.direction
!='left'){
snake
.direction
='right';
}
break;
}
}
var timer
;
function start() {
clearInterval(timer
);
timer
=setInterval(function () {
snake
.run();
},level
.speed
);
}
$('#begin').click(function(){
start();
});
$('#pause').click(function(){
clearInterval(timer
);
});
引用
b站视频 ☺️
转载请注明原文地址:https://ipadbbs.8miu.com/read-53650.html