自己寫的一個簡陋的貪吃蛇游戲,源代碼如下: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>貪吃蛇小游戲</title> 6 <style> 7 *{margin:0; padding:0 ...
自己寫的一個簡陋的貪吃蛇游戲,源代碼如下:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>貪吃蛇小游戲</title> 6 <style> 7 *{margin:0; padding:0;} 8 header { 9 display: block; 10 margin: 0 auto; 11 width: 500px; 12 text-align: center; 13 } 14 header h1 { 15 font-family: Arial; 16 font-weight: bold; 17 } 18 header #newGameButton { 19 display: block; 20 margin: 20px auto; 21 width: 100px; 22 padding: 10px 10px; 23 background-color: #8f7a66; 24 font-family: Arial; 25 color: white; 26 border-radius: 10px; 27 text-decoration: none; 28 } 29 header #newGameButton:hover { 30 background-color: #9f8b77; 31 } 32 header p { 33 font-family: Arial; 34 font-size: 25px; 35 margin: 20px auto; 36 } 37 canvas{ 38 display:block; 39 border:medium double black; 40 margin:4px auto; 41 } 42 </style> 43 </head> 44 <body> 45 <header> 46 <h1>貪吃蛇小游戲</h1> 47 <a href="javascript:newgame();" id="newGameButton">New Game</a> 48 <p>score:<span id="score">0</span></p> 49 </header> 50 <canvas id="canvas"> 51 Your browser doesn't support the <code>canvas</code> element. 52 </canvas> 53 <script> 54 var CANVAS = document.getElementById("canvas"); 55 var CTX = CANVAS.getContext("2d"); 56 var SNAKE = new Array(); //用隊列模擬蛇身 57 var DIR = "right"; //用來控制蛇頭的方向 58 var SIZE = 20; //蛇身的寬度 59 var FOODX = 0; //食物的x坐標 60 var FOODY = 0; //食物的y坐標 61 var HEADX = 0; //蛇頭的x坐標 62 var HEADY = 0; //蛇頭的y坐標 63 var MAXWIDTH = 200; //畫布的高度 64 var MAXHEIGHT = 200; //畫布的寬度 65 var TIME = 400; //蛇的速度 66 var SCORE = 0; //計算玩家分數 67 var interval = null; 68 69 CANVAS.width = MAXWIDTH; 70 CANVAS.height = MAXHEIGHT; 71 72 window.onload = function(){ 73 newgame(); 74 }; 75 document.getElementById("newGameButton").click(function(){ 76 newgame(); 77 }); 78 function newgame(){ 79 SNAKE = []; //用隊列模擬蛇身 80 DIR = "right"; //用來控制蛇頭的方向 81 HEADX = 0; //蛇頭的x坐標 82 HEADY = 0; //蛇頭的y坐標 83 SCORE = 0; 84 window.clearInterval(interval); 85 interval = null; 86 //初始化畫布 87 CTX.clearRect(0, 0, MAXWIDTH, MAXHEIGHT); 88 //畫一條蛇 89 drawSnake(); 90 //放置食物 91 setFood(); 92 //移動蛇 93 interval = window.setInterval(move, TIME); 94 95 } 96 function move(){ 97 switch(DIR){ 98 case "up":HEADY = HEADY-SIZE;break; 99 case "right":HEADX = HEADX+SIZE;break; 100 case "down":HEADY = HEADY+SIZE;break; 101 case "left":HEADX = HEADX-SIZE;break; 102 } 103 if(HEADX>MAXWIDTH-SIZE || HEADY>MAXHEIGHT-SIZE || HEADX<0 || HEADY<0){ 104 alert("你的分數為:"+SCORE+"分,繼續努力吧!失敗原因:碰壁了....."); 105 window.location.reload(); 106 } 107 for(var i=1;i<SNAKE.length;i++){ 108 if(SNAKE[i][0] == SNAKE[0][0] && SNAKE[i][1] == SNAKE[0][1]){ 109 alert("你的分數為:"+SCORE+"分,繼續努力吧!失敗原因:撞到自己了....."); 110 window.location.reload(); 111 } 112 } 113 if(SNAKE.length == MAXWIDTH *MAXHEIGHT){ 114 alert("好厲害!麽麽噠~"); 115 window.location.reload(); 116 } 117 moveIn(HEADX, HEADY);//移動一格 118 } 119 document.onkeydown = function(e) { //改變蛇方向 120 var code = e.keyCode - 37; 121 switch(code){ 122 case 1 : DIR = "up";break;//上 123 case 2 : DIR = "right";break;//右 124 case 3 : DIR = "down";break;//下 125 case 0 : DIR = "left";break;//左 126 } 127 } 128 //=============================畫一條蛇======================================================= 129 function drawSnake(){ 130 CTX.fillStyle = "black"; 131 //畫蛇頭 132 CTX.fillRect (HEADX, HEADY, SIZE, SIZE); 133 SNAKE.push([HEADX, HEADY]); 134 135 //畫蛇身 136 switch(DIR){ 137 case "up": 138 drawBody(HEADX, HEADY + SIZE, HEADX, HEADY + 2 * SIZE); 139 break; 140 case "right": 141 drawBody(HEADX - SIZE, HEADY, HEADX - 2 * SIZE, HEADY); 142 break; 143 case "down": 144 drawBody(HEADX, HEADY - SIZE, HEADX, HEADY - 2 * SIZE); 145 break; 146 case "left": 147 drawBody(HEADX + SIZE, HEADY, HEADX + 2 * SIZE, HEADY); 148 break; 149 } 150 } 151 function drawBody(x1, y1, x2, y2){ 152 CTX.fillRect (x1, y1, SIZE, SIZE); 153 CTX.fillRect (x2, y2, SIZE, SIZE); 154 SNAKE.push([x1, y1]); 155 SNAKE.push([x2, y2]); 156 } 157 //===========================放置食物============================== 158 function setFood(){ 159 do{ 160 FOODX = SIZE * Math.floor(Math.random() * MAXWIDTH / SIZE); 161 FOODY = SIZE * Math.floor(Math.random() * MAXHEIGHT / SIZE); 162 }while(foodInSnake()); 163 CTX.fillStyle = "red"; 164 CTX.fillRect (FOODX, FOODY, SIZE, SIZE); 165 } 166 function foodInSnake(){ 167 for (var i = 0; i < SNAKE.length; i++) { 168 if(FOODX == SNAKE[i][0] && FOODY == SNAKE[i][1]){ 169 return true; 170 } 171 } 172 return false; 173 } 174 //========================================移動一格=========================== 175 function moveIn(x, y){ 176 CTX.fillStyle = "black"; 177 CTX.fillRect(x, y, SIZE, SIZE);//重畫蛇頭 178 //把新蛇頭添加到 SNAKE 數組 179 var newSnake = [[x, y]]; 180 SNAKE = newSnake.concat(SNAKE); 181 182 if(false == eatFood()){//如果沒吃到食物,減少一格蛇尾 183 var snakeTail = SNAKE.pop();//獲得蛇尾位置 184 CTX.clearRect(snakeTail[0], snakeTail[1], SIZE, SIZE);//去掉蛇尾 185 } 186 } 187 function eatFood(){ 188 if(HEADX == FOODX && HEADY == FOODY){ 189 CTX.fillStyle = "block"; 190 CTX.fillRect (FOODX, FOODY, SIZE, SIZE); 191 192 setFood(); 193 SCORE++; 194 //$("#score").text(SCORE); 195 document.getElementById("score").innerHTML = SCORE; 196 return true; 197 } 198 return false; 199 } 200 201 </<