推箱子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#app{
}
*{
margin: 0;
padding: 0;
}
#app span{
width: 20px;
height: 20px;
display: inline-block;
}
.person{
background: red;
}
.kong{
background: white;
}
.shitu{
background: #8c8c8c;
}
.yellow{
background: yellow;
}
.xiaozi{
background: burlywood;
}
#context{
position: absolute;
right: 10%;
top: 25%;
font-size: 50px;
color: #00FF00;
}
</style>
</head>
<body onkeydown="doKeyDown(event)">
<div id="app"></div>
<canvas id="canvas"></canvas>
<div id="context"></div>
</body>
<script>
window.onload=function(){
load(levels[cuindex],true);
}
let x,y,flag=0,xian;
let maxcount,cucount=0;
let cuindex=0;
let conll;
let map = new Map();
let map2 = new Map();
map.set(0,"<span class=\"kong\"></span>");
map.set(1,"<span class=\"shitu\"></span>");
map.set(2,"<span class=\"yellow\"></span>");
map.set(3,"<span class=\"xiaozi\"></span>");
map.set(4,"<span class=\"person\"></span>");
function dian(x,y,key) {
let arr;
switch (key) {
case 37://左鍵頭 0
arr=[x,y-1];
break;
case 38://上鍵頭 1
arr=[x-1,y];
break;
case 39://右箭頭 2
arr=[x,y+1];
break;
case 40://下箭頭 3
arr=[x+1,y];
break;
}
return arr;
}
function doKeyDown(event){
if(event.keyCode%37<4){
xian=event.keyCode;
let dian1 = dian(x,y,xian);
move(dian1[0],dian1[1]);
}
}
function move(cx,cy) {
switch (levels[cuindex][cx][cy]) {
case 0:
case 2:
flag=levels[cuindex][cx][cy];
levels[cuindex][cx][cy]=4;
if(map2.get(x+","+y))
levels[cuindex][x][y]=2;
else{
if(flag!==2){
levels[cuindex][x][y]=flag;
}else{
levels[cuindex][x][y]=0;
}
}
x=cx;
y=cy;
load(levels[cuindex]);
break;
case 3 :
let a = dian(cx,cy,xian);
switch (levels[cuindex][a[0]][a[1]]) {
case 0:
case 2:
if(levels[cuindex][a[0]][a[1]]===2){
cucount++;
}
if(map2.get(cx+","+cy)){
cucount--;
}
levels[cuindex][a[0]][a[1]]=3;
levels[cuindex][cx][cy]=4;
levels[cuindex][x][y]=flag;
x=cx;
y=cy;
load(levels[cuindex]);
break;
}
break;
}
if(cucount===maxcount){
levels[cuindex]=conll;
load(levels[++cuindex],true);
}
else
info();
}
function info() {
document.getElementById("context").innerText="當前關卡"+cuindex+"完成了的"+cucount+"還有"+(maxcount-cucount);
}
function load(arr,flag){
let byId = document.getElementById("app");
byId.innerHTML="";
if(flag) {
conll=[arr.length];
maxcount=0;
cucount=0;
map2.clear();
info();
}
for (let i=0;i<arr.length;i++){
byId.innerHTML+="<div>"
for (let j=0;j<arr[i].length;j++){
byId.innerHTML+=map.get(arr[i][j]);
if(flag) {
if(!conll[i])
conll[i]=[];
conll[i][j]=arr[i][j];
if(arr[i][j]===4){
x=i;
y=j;
}
if(arr[i][j]===2){
map2.set(i+","+j,true);
maxcount++;
}
}
}
byId.innerHTML+="</div>"
}
}
let levels=[];
levels[0]=[
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,2,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,1,1,1,1,0,0,0,0],
[0,0,0,0,1,1,1,3,0,3,2,1,0,0,0,0],
[0,0,0,0,1,2,0,3,4,1,1,1,0,0,0,0],
[0,0,0,0,1,1,1,1,3,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,2,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]];
lev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,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]];
levels[3]=[
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0],
[0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,1,4,3,0,1,0,0,0,0,0,0,0],
[0,0,0,0,1,1,3,0,1,1,0,0,0,0,0,0],
[0,0,0,0,1,1,0,3,0,1,0,0,0,0,0,0],
[0,0,0,0,1,2,3,0,0,1,0,0,0,0,0,0],
[0,0,0,0,1,2,2,5,2,1,0,0,0,0,0,0],
[0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]];
</script>
</html>
貪吃蛇
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
#contie span{
width: 20px;
height: 20px;
display: inline-block;
}
.person{
background: red;
}
.kong{
background: #bce8f1;
}
.shitu{
background: #8c8c8c;
}
.yellow{
background: yellow;
}
.xiaozi{
background: burlywood;
}
#context{
position: absolute;
right: 10%;
top: 25%;
font-size: 50px;
color: #00FF00;
}
</style>
</head>
<body onkeydown="doKeyDown(event)">
<div id="contie">
</div>
</body>
<script>
let arr=[];
let max=9;
let xiaosize=2;
let point=[{x:1,y:1},{x:1,y:2}];
let eat={x:5,y:5};
let dom;
let postion=39;
let map = new Map();
map.set(0,"<span class=\"kong\"></span>");
map.set(1,"<span class=\"shitu\"></span>");
map.set(2,"<span class=\"yellow\"></span>");
initial();
function initial() {
dom=document.getElementById("contie");
for (let i=0;i<max;i++){
arr[i]=[];
for (let j=0;j<max;j++)
arr[i][j]=0;
}
for (let i=0;i<point.length;i++){
arr[point[i].x][point[i].y]=2;
}
arr[eat.x][eat.y]=1;
playgame();
}
function xuanyan() {
dom.innerHTML="";
for (let i=0;i<max;i++){
for (let j=0;j<max;j++){
dom.innerHTML+=map.get(arr[i][j]);
}
dom.innerHTML+="<br>";
}
}
function playgame() {
xuanyan();
dom.hello=setInterval(function () {
arr[point[0].x][point[0].y]=0;
for (let i=0;i<point.length;i++){
if(i+1<point.length){
point[i].x=point[i+1].x;
point[i].y=point[i+1].y;
}else{
switch (postion) {
case 37:
point[xiaosize-1].y--;
break;
case 38:
point[xiaosize-1].x--;
break;
case 39:
point[xiaosize-1].y++;
break;
case 40:
point[xiaosize-1].x++;
break;
}
if(point[xiaosize-1].x<0||point[xiaosize-1].x>=max||point[xiaosize-1].y<0||point[xiaosize-1].y>=max){
clearInterval(dom.hello);
return alert("撞牆了");
}
if(f(point[xiaosize-1].x,point[xiaosize-1].y,xiaosize-1)){
clearInterval(dom.hello);
return alert("吃到自己了");
}
}
}
if(arr[point[xiaosize-1].x][point[xiaosize-1].y]===1){
point.push({x:0,y:0});
for (let i=xiaosize-1;i>=0;i--){
point[i+1].x=point[i].x;
point[i+1].y=point[i].y;
}
xiaosize++;
if(point[1].x===point[2].x){
point[0].x=point[1].x;
point[0].y= point[1].y<point[2].y?point[1].y-1:point[1].y+1;
}else{
point[0].y=point[1].y;
point[0].x= point[1].x<point[2].x?point[1].x-1:point[1].x+1;
}
do {
eat.x = parseInt(Math.random()*max);
eat.y = parseInt(Math.random()*max);
} while(f(eat.x,eat.y))
arr[eat.x][eat.y]=1;
arr[point[0].x][point[0].y]=2;
arr[point[xiaosize-1].x][point[xiaosize-1].y]=2;
}else
arr[point[xiaosize-1].x][point[xiaosize-1].y]=2;
xuanyan();
},300)
}
function doKeyDown(event){
if(event.keyCode>36&&event.keyCode<41)
postion=event.keyCode;
}
function f(x,y,flag) {
for (let i=0;i< (flag!=null?flag:point.length);i++) {
if(point[i].x===x&&point[i].y===y)
return true;
}
return false;
}
</script>
</html>
簡單的飛機大戰
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
#contie span{
width: 20px;
height: 20px;
display: inline-block;
}
.person{
background: red;
}
.kong{
background: #c1e2b3;
}
.shitu{
background: #8c8c8c;
}
.yellow{
background: yellow;
}
.xiaozi{
background: burlywood;
}
#context{
position: absolute;
right: 10%;
top: 25%;
font-size: 50px;
color: #00FF00;
}
</style>
</head>
<body onkeydown="doKeyDown(event)">
<div id="contie">
</div>
<div id="context"></div>
</body>
<script>
let arr=[];
let two=[];
let max=18;
let point=[{x:max-1,y:max/2}];
let dom,dom2;
let zidan;
let diren;
let direnzidan;
let dirensize=0;
let xiaom=0;
let zidians=0;
let xielang=4;
let map = new Map();
map.set(0,"<span class=\"kong\"></span>");
map.set(1,"<span class=\"shitu\"></span>");
map.set(2,"<span class=\"yellow\"></span>");
map.set(3,"<span class=\"xiaozi\"></span>");
map.set(4,"<span class=\"person\"></span>");
function initial() {
dom=document.getElementById("contie");
dom2=document.getElementById("context");
zidan=new Linklist();
diren=new Linklist();
direnzidan=new Linklist();
for (let i=0;i<max;i++){
arr[i]=[];
two[i]=[];
for (let j=0;j<max;j++){
two[i][j]=0;
arr[i][j]=0;
}
}
for (let i=0;i<point.length;i++){
arr[point[i].x][point[i].y]=1;
}
playgame();
}
function creatediren() {
diren.add(new Node(0,parseInt(Math.random()*max),2));
}
function dirmove(node) {
node.y=postion123(node.y);
node.x=postion123(node.x);
if(two[node.x][node.y]===3){
let s = zidan.search(node.x,node.y);
s.x=0;
two[node.x][node.y]=0;
node.x=max;
dirensize--;
xiaom++;
info();
}else
if(Math.random()>0.2){
direnzidan.add(new Node(node.x+1,node.y,4));
}
}
function info(str) {
dom2.innerText="共打了"+zidians+"子彈 幹掉"+xiaom+"敵人"+xielang+"血量"+(str?str:"");
}
function postion123(x) {
switch (x) {
case 0:
return 1;
case max:
return max-1;
default:
return x+(Math.random()>0.5?1:-1);
}
}
function xuanyan() {
dom.innerHTML="";
for (let i=0;i<max;i++){
for (let j=0;j<max;j++){
if(two[i][j]!==0){
dom.innerHTML+=map.get(two[i][j]);
}else
dom.innerHTML+=map.get(arr[i][j]);
}
dom.innerHTML+="<br>";
}
}
function playgame() {
info();
xuanyan();
let reomve={zidan:0,dzidan:0,diren:0}
dom.hello=setInterval(function () {
if(dirensize<3){
creatediren();
dirensize++;
}
zidan.Gaidian(zidan,reomve.zidan,(s)=>{
two[s.x][s.y]=0;
if(s.x>=1){
two[s.x-1][s.y]=s.data;
s.x--;
}else{
reomve.zidan++;
}
},0);
diren.Gaidian(diren,reomve.diren,(s)=>{
if(s.x<max){
arr[s.x][s.y]=0;
dirmove(s);
if(s.x<max)
arr[s.x][s.y]=s.data;
}else{
reomve.diren++;
}
},max);
direnzidan.Gaidian(direnzidan,reomve.dzidan,(s)=>{
if(s.x<max){
two[s.x][s.y]=0;
s.x++;
if(s.x<max){
two[s.x][s.y]=s.data;
if( arr[s.x][s.y]===1){
xielang--;
info();
if(xielang===0){
clearInterval(dom.hello);
info("gameover");
}
}
}
}else{
reomve.dzidan++;
}
},max);
xuanyan();
},50)
}
function doKeyDown(event){
if(event.keyCode>36&&event.keyCode<41){
postion=event.keyCode;
let dian1 = dian(point[0].x,point[0].y,postion);
if(dian1[0]<0||dian1[0]>=max||dian1[1]<0||dian1[1]>=max){
}else{
arr[point[0].x][point[0].y]=0;
point[0].x=dian1[0];
point[0].y=dian1[1];
arr[point[0].x][point[0].y]=1;
}
}else if(event.keyCode===32){
zidan.add(new Node(point[0].x-1,point[0].y,3));
zidians++;
info();
}
}
function dian(x,y,key) {
let arr;
switch (key) {
case 37://左鍵頭 0
arr=[x,y-1];
break;
case 38://上鍵頭 1
arr=[x-1,y];
break;
case 39://右箭頭 2
arr=[x,y+1];
break;
case 40://下箭頭 3
arr=[x+1,y];
break;
}
return arr;
}
class Node{
constructor(x,y,data,next) {
this.x=x;
this.y=y;
this.data=data;
this.next=next;
}
}
class Linklist{
constructor() {
this.root=null;
this.last=null;
}
add(node){
if(this.root==null){
this.root=node;
this.last=node;
}else{
this.last.next=node;
this.last=this.last.next;
}
}
search(x,y){
let s=this.root;
while(s!=null){
if(s.x===x&&s.y===y) return s;
s=s.next;
}
}
remove(a,val){
for(let i=0;i<a;i++)
this.root=this.deleteNode(this.root,val);
let s = this.root;
while(s!=null){
if(s.next==null) break;
s=s.next;
}
this.last=s;
}
deleteNode(head, val) {
if(head==null) return null;
if(head.x ===val){
return head.next;
}
if(head.next==null) return head;
if(head.next.x === val){
head.next = head.next.next;
}else{
head.next=this.deleteNode(head.next,val);
}return head;
};
Gaidian(node,xx,F,val){
let s = node.root;
xx=0;
while(s!=null){
F(s);
s=s.next;
}
if(xx>10){
node.remove(xx,val);
}
}
}
initial();
</script>
</html>