前端学习(1680):前端系列实战课程之创建和显示蛇
生活随笔
收集整理的這篇文章主要介紹了
前端学习(1680):前端系列实战课程之创建和显示蛇
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>游戲初始化界面</title><style>body {margin: 0;padding: 0;}#main {margin: 100px;}.btn {width: 100px;height: 40px;}</style>
</head><body><div id="main"><!-- 按鈕 --><input class="btn" type="button" value="開始游戲" id="begin"><input class="btn" type="button" value="暫停游戲" id="pause"></div><!-- 貪吃蛇游戲設(shè)計(jì) --><script>var main = document.getElementById('main');/* 畫布格子是否開啟 */var showcanvas = true;/* atom 原子大小 xnum橫向原子數(shù)量 ynum縱向原子數(shù)量 */function Map(atom, xnum, ynum) {this.atom = atom;this.xnum = xnum;this.ynum = ynum;//聲明畫布this.canvas = null;//第二部分 創(chuàng)建畫布方法this.create = function() {this.canvas = document.createElement('div');this.canvas.style.cssText = 'position:relative;top:40px;border:1px solid red;background:#FAFAFA';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 x = 0; x < xnum; x++) {for (var y = 0; y < ynum; y++) {var a = document.createElement('div');a.style.cssText = "border:1px solid yellow";a.style.width = this.atom + 'px';a.style.height = this.atom + 'px';a.style.backgroundColor = 'green';this.canvas.appendChild(a);a.style.position = 'absolute';a.style.left = x * this.atom + 'px';a.style.top = y * this.atom + 'px';}}}}}/* 創(chuàng)建蛇 */function Snake(map) {//設(shè)置寬度高度this.width = map.atom;this.height = map.atom;/* 蛇的方向 */this.direction = 'right';this.body = [{x: 2,y: 0}, //第一點(diǎn){x: 1,y: 0}, //第二點(diǎn){x: 0,y: 0} //第三點(diǎn)];//顯示蛇this.display = function() {for (var i = 0; i < this.body.length; i++) {//當(dāng)吃到食物時(shí)候 x==null 不能新建 不然會(huì)在00處新建一個(gè)if (this.body[i].x != null) {var s = document.createElement('div');//將蛇的節(jié)點(diǎn)保存到狀態(tài)變量中 方便刪除使用this.body[i].flag = s;console.log(1);//設(shè)置蛇的樣式s.style.width = this.width + 'px';s.style.height = this.height + 'px';s.style.backgroundColor = "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);}}}}var map = new Map(20, 40, 20);//創(chuàng)建畫布map.create();//構(gòu)造食物var food = new Food(map);//構(gòu)建蛇var snake = new Snake(map);snake.display();/* 第三部分 創(chuàng)建食物 map地圖對(duì)象 */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.backgroundColor = 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);}var timer; //變量可以提升/* 第一部分 *//* 第一部分開始 */document.getElementById('begin').onclick = function() {clearInterval(timer);timer = setInterval(function() {}, 300)}/*第一部分 暫停 */document.getElementById('begin').onclick = function() {clearInterval(timer);timer = setInterval(function() {}, 300)}</script>
</body></html>
運(yùn)行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的前端学习(1680):前端系列实战课程之创建和显示蛇的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Android】超级终端命令
- 下一篇: 前端学习(1612):oracle简介