js 實現貪吃蛇遊戲效果

2021-10-03 15:17:51 字數 1097 閱讀 4061

用div模擬遊戲畫面範圍,食物,以及貪吃蛇。限定食物出現的時機(為隨機出現。俺不能超出畫面範圍。限定貪吃蛇遊走範圍)限定遊戲規則:

1,貪吃蛇,超出畫面範圍遊戲結束。

2,貪吃蛇,撞到自己遊戲結束。

class map

createele()px;height:$px;background:$;margin:0 auto;position:relative;border:solid 10px black;`;}}

class food

createele()px;height:$px;background:$;position:absolute;left:$px;top:$px;`;

}// 實物出現是隨機的位置

randompos()

}class snake,,];

this.d = "right";

this.createele();

}// 引入貪吃蛇,並迴圈得到。貪吃蛇最初每個部位(也就是組成貪吃蛇所有div)

createele(),300);

}move()

switch(this.d)

if(this.body[0].x < 0 || this.body[0].y < 0 || this.body[0].x > ((m.w-this.w) / this.w) || this.body[0].y > ((m.h-this.h) / this.h))

if(this.body[0].x === f.x && this.body[0].y === f.y))

f.randompos();

}for(var i=1;i}

this.createele();

}direct(type)}}

function random(a,b)

function randomcolor(),$,$)`

}var m = new map();

var f = new food();

f.randompos();

var s = new snake();

document.onkeydown = function(eve)

JS實現簡單的遊戲貪吃蛇

doctype html html head meta charset utf 8 title document title head body body script 貪吃蛇 鍵盤的方向鍵,控制蛇的方向,碰撞食物,實現增加長度的效果,撞到牆壁或自身,遊戲結束 分析 地圖 提供邊界 食物 隨機出現,...

貪吃蛇遊戲

貪吃蛇遊戲 結構化程式設計 c語言程式設計 重要的的是結構化的程式設計思想 include include include include include include define field width 300 就做20個格仔的 define field height 300 define f...

貪吃蛇遊戲

include include include include include 使用當前時間做種子 enum dir 列舉型別enum dir 圍牆 class fencef 定義物件 畫框框 void fence initfence 顯示框框 void fence outputf int snak...