欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript實(shí)現(xiàn)貪吃蛇小游戲思路

 更新時(shí)間:2021年09月26日 10:41:45   作者:難過的新手村  
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)貪吃蛇思路小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

javascript小游戲貪吃蛇實(shí)現(xiàn)思路講解(完整代碼實(shí)現(xiàn)),供大家參考,具體內(nèi)容如下

效果流程

1、首先我們要操作的canvas

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>貪吃蛇</title>
</head>
 
<body>
 <canvas id="canvas"></canvas>   <!-- 我們要操作的canvas -->
 <input type="button" value="開始游戲" /><!-- 開始游戲按鈕 -->
<script>
//獲取元素
var canvas = document.getElementById("canvas"); //找到我們要操作的canvas
var context = canvas.getContext("2d"); //規(guī)定在canvas上操作的環(huán)境為2d
var but = document.getElementsByTagName("input")[0]; //找到開始按鈕
</script>

2、在初始化

canvas.width = 500; //定義canvas寬度
canvas.height = 500; //定義canvas高度
canvas.style.border = "5px solid #000"; //定義canvas邊框
var times = 100; //默認(rèn)時(shí)間200毫秒
var long = 10; //蛇身相對(duì)于步長(zhǎng)的個(gè)數(shù)
var x = y =8;  //蛇的初始坐標(biāo)
var direction = 3;  // 1 上  2 右  3 下 0  左
var size = 8;  //蛇每次移動(dòng)的長(zhǎng)度(步長(zhǎng))
var map  = []; //用來記錄蛇每次移動(dòng)的坐標(biāo) 
var foodx = 0;  //食物的初始X軸坐標(biāo)
var foody = 0;  //食物的初始y軸坐標(biāo)
var onOff = true; 
var foodT = true;
var timer = null;

3、根據(jù)方向控制蛇的坐標(biāo)變化,判斷蛇的坐標(biāo)是否超出canvas邊界,判斷蛇有沒有碰到自己

 //根據(jù)方向控制蛇的坐標(biāo)變化
    switch(direction){
 
        case 1: y = y - size; break; //上
        case 2: x = x + size; break; //右
        case 3: y = y + size; break; //下
        case 0: x = x - size; break; //左
    }
 
    //判斷蛇的坐標(biāo)是否超出canvas邊界,超出則出現(xiàn)碰壁提示,游戲結(jié)束
    if(x>500 || x<0 || y>500 || y<0){
 
//      alert("你碰壁掛了!繼續(xù)努力吧……");
        window.location.reload();   
    }
 
    //判斷蛇有沒有碰到自己,碰到自己游戲結(jié)束 
    for(var i=0; i<map.length; i++){
        if(parseInt( map[i].x ) == x && parseInt( map[i].y ) == y){
//          alert("你碰到自己掛了!繼續(xù)努力吧……");
            window.location.reload(); //重新載入
        }
    }

4、然后繪制蛇

//繪制蛇
    if(map.length>long){ 
        var cl = map.shift();
        context.clearRect(cl['x'],cl['y'],size,size);
    }
    map.push({'x':x,'y':y}); 
    context.fillStyle = "#777"; //填充蛇的顏色
    context.fillRect(x,y,size,size); //繪制蛇

5、判斷食物坐標(biāo)等于蛇的坐標(biāo)時(shí)(蛇吃掉食物)

//判斷食物坐標(biāo)等于蛇的坐標(biāo)時(shí)(蛇吃掉食物)
    if(foodx*8 == x && foody*8 == y ){
        food(); //再次繪制食物
        long++; //蛇的長(zhǎng)度增加
        times = times - 10; //速度加快
        clearInterval(timer);
        onOff = true;
        setTimeout(goto,times); //開始新的一輪
    }

6、確定食物隨機(jī)顯示坐標(biāo),繪制食物

//確定食物隨機(jī)顯示坐標(biāo),繪制食物
function food(){
    foodx = Math.ceil(Math.random()*40); //食物的X軸隨機(jī)坐標(biāo)
    foody = Math.ceil(Math.random()*40); //食物的Y軸隨機(jī)坐標(biāo)    
    context.fillStyle = "mediumvioletred"; //食物的填充顏色 
    context.fillRect(foodx*8,foody*8,8,8); //繪制食物
 
}

7、監(jiān)聽按下方向鍵,獲得蛇前進(jìn)的方向

//監(jiān)聽按下方向鍵,獲得蛇前進(jìn)的方向
document.onkeydown = function(ev){
    var ev = ev || event;
    var cod = ev.keyCode - 37;
 
    switch(cod){
        case 1: direction = 1; break; //向上
        case 2: direction = 2; break; //向右
        case 3: direction = 3; break; //向下
        case 0: direction = 0; break; //向左
    }   
}

完整代碼實(shí)現(xiàn)

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>貪吃蛇</title>
</head>
 
<body>
 <canvas id="canvas"></canvas>   <!-- 我們要操作的canvas -->
 <input type="button" value="開始游戲" /><!-- 開始游戲按鈕 -->
<script>
//獲取元素
var canvas = document.getElementById("canvas"); //找到我們要操作的canvas
var context = canvas.getContext("2d"); //規(guī)定在canvas上操作的環(huán)境為2d
var but = document.getElementsByTagName("input")[0]; //找到開始按鈕
//初始化
canvas.width = 500; //定義canvas寬度
canvas.height = 500; //定義canvas高度
canvas.style.border = "5px solid #000"; //定義canvas邊框
var times = 100; //默認(rèn)時(shí)間200毫秒
var long = 10; //蛇身相對(duì)于步長(zhǎng)的個(gè)數(shù)
var x = y =8;  //蛇的初始坐標(biāo)
var direction = 3;  // 1 上  2 右  3 下 0  左
var size = 8;  //蛇每次移動(dòng)的長(zhǎng)度(步長(zhǎng))
var map  = []; //用來記錄蛇每次移動(dòng)的坐標(biāo) 
var foodx = 0;  //食物的初始X軸坐標(biāo)
var foody = 0;  //食物的初始y軸坐標(biāo)
var onOff = true; 
var foodT = true;
var timer = null;
function star(){
 
    //根據(jù)方向控制蛇的坐標(biāo)變化
    switch(direction){
 
        case 1: y = y - size; break; //上
        case 2: x = x + size; break; //右
        case 3: y = y + size; break; //下
        case 0: x = x - size; break; //左
    }
 
    //判斷蛇的坐標(biāo)是否超出canvas邊界,超出則出現(xiàn)碰壁提示,游戲結(jié)束
    if(x>500 || x<0 || y>500 || y<0){
 
//      alert("你碰壁掛了!繼續(xù)努力吧……");
        window.location.reload();   
    }
 
    //判斷蛇有沒有碰到自己,碰到自己游戲結(jié)束 
    for(var i=0; i<map.length; i++){
        if(parseInt( map[i].x ) == x && parseInt( map[i].y ) == y){
//          alert("你碰到自己掛了!繼續(xù)努力吧……");
            window.location.reload(); //重新載入
        }
    }
 
    //繪制蛇
    if(map.length>long){ 
        var cl = map.shift();
        context.clearRect(cl['x'],cl['y'],size,size);
    }
    map.push({'x':x,'y':y}); 
    context.fillStyle = "#777"; //填充蛇的顏色
    context.fillRect(x,y,size,size); //繪制蛇
 
 
    //判斷食物坐標(biāo)等于蛇的坐標(biāo)時(shí)(蛇吃掉食物)
    if(foodx*8 == x && foody*8 == y ){
        food(); //再次繪制食物
        long++; //蛇的長(zhǎng)度增加
        times = times - 10; //速度加快
        clearInterval(timer);
        onOff = true;
        setTimeout(goto,times); //開始新的一輪
    }
 
 
}
 
//確定食物隨機(jī)顯示坐標(biāo),繪制食物
function food(){
    foodx = Math.ceil(Math.random()*40); //食物的X軸隨機(jī)坐標(biāo)
    foody = Math.ceil(Math.random()*40); //食物的Y軸隨機(jī)坐標(biāo)    
    context.fillStyle = "mediumvioletred"; //食物的填充顏色 
    context.fillRect(foodx*8,foody*8,8,8); //繪制食物
 
}
//開始游戲
function goto(){
    if(onOff){
        timer = setInterval(star,times);
        onOff = false;
    }
    if(foodT){
        food();
        foodT = false;
    }   
}
//點(diǎn)擊按鈕開始游戲開始
but.onclick = goto;//點(diǎn)擊按鈕,開始游戲
 
//監(jiān)聽按下方向鍵,獲得蛇前進(jìn)的方向
document.onkeydown = function(ev){
    var ev = ev || event;
    var cod = ev.keyCode - 37;
 
    switch(cod){
        case 1: direction = 1; break; //向上
        case 2: direction = 2; break; //向右
        case 3: direction = 3; break; //向下
        case 0: direction = 0; break; //向左
    }   
}
</script>
</body>
</html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論