100+行HMTL5代码实现flappy bird

来源:互联网 发布:手机壁纸软件 编辑:程序博客网 时间:2024/05/12 09:02

http://blog.csdn.net/ljhcsd455/article/details/19352665


最近开始学习HTML5,刚好用最近正火的游戏flappy bird来练手。

效果如下:


JS代码实现如下:


[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1.   
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. function Bird(x, y, image){  
  2.     this.x = x,  
  3.     this.y = y,  
  4.     this.width = 0,  
  5.     this.height = 0,  
  6.     this.image = image;  
  7.     this.draw = function(context, state){  
  8.         if(state == "up")  
  9.             context.drawImage(this.image, this.width, 0, this.width, this.height, this.x, this.y, this.width, this.height);  
  10.         else  
  11.             context.drawImage(this.image, 0, 0, this.width, this.height, this.x, this.y, this.width, this.height);  
  12.     }  
  13. };  
  14.   
  15. function Obstacle(x, y, width, height){  
  16.     this.x = x,  
  17.     this.y = y,  
  18.     this.width = width,  
  19.     this.height = height,  
  20.     this.draw = function(context){  
  21.         context.fillRect(this.x, this.y, this.width, this.height);  
  22.     }  
  23. };  
  24.   
  25. window.onload = function() {  
  26.     var canvas = document.getElementById("canvas");  
  27.     var c=canvas.getContext("2d");  
  28.     c.font = "24pt 宋体";  
  29.   
  30.     var touch = false;  
  31.     var gameOver = false;  
  32.   
  33.     var startX = 100;   //起始位置  
  34.     var startY = 100;  
  35.     var obsDistance = 150;  //上下连个障碍物距离  
  36.     var obsSpeed = 2;  //障碍物移动速度  
  37.     var obsInterval = 2000; //制造障碍物间隔ms  
  38.     var upSpeed = 5;    //上升速度  
  39.     var downSpeed = 5;  //下降速度  
  40.     var score = 0;  //得分  
  41.   
  42.     //主角  
  43.     var image = new Image()  
  44.     var bird = new Bird(startX, startY, image);  
  45.     image.src = "img/test01.png";  
  46.     image.onload = function(){  
  47.         bird.width = image.width/2;  
  48.         bird.height = image.height;  
  49.     };  
  50.   
  51.     //障碍物  
  52.     var obsList = new Array();  
  53.     var h = 100;  
  54.     var h2 = canvas.height- h - obsDistance;  
  55.     var obs1 = new Obstacle(canvas.width, 0, 50, h);  
  56.     var obs2 = new Obstacle(canvas.width, canvas.height-h2, 50, h2);  
  57.     obsList.push(obs1);  
  58.     obsList.push(obs2);  
  59.   
  60.     canvas.onmousedown = function() {  
  61.         touch = true;  
  62.     }  
  63.     canvas.onmouseup = function(){  
  64.         touch = false;  
  65.     };  
  66.   
  67.     //游戏更新  
  68.     var updateTimer = setInterval(function(){  
  69.         //计分  
  70.         if(score == 0 && obs1.x+obs1.width < startX) {  
  71.             score = 1;  
  72.             var scoreTimer = setInterval(function(){  
  73.                 if(gameOver) {  
  74.                     clearInterval(scoreTimer);  
  75.                     return;  
  76.                 }  
  77.                 score++;  
  78.             }, obsInterval);  
  79.         }  
  80.   
  81.         //碰撞检测  
  82.         if(bird.y<0 || bird.y > canvas.height-bird.height)  
  83.             gameOver = true;  
  84.         else {  
  85.             var p = [{x:bird.x, y:bird.y}, {x:bird.x+bird.width, y:bird.y},  
  86.                 {x:bird.x, y:bird.y+bird.height}, {x:bird.x+bird.width, y:bird.y+bird.height}];  
  87.             for(var i = 0; i < obsList.length; i++) {  
  88.                 for(var j = 0; j < 4; j++)  
  89.                     if(p[j].x >= obsList[i].x && p[j].x <= obsList[i].x+obsList[i].width && p[j].y >= obsList[i].y && p[j].y <= obsList[i].y+obsList[i].height) {  
  90.                         gameOver = true;  
  91.                         break;  
  92.                     }  
  93.                 if(gameOver)  
  94.                     break;  
  95.             }  
  96.         }  
  97.         if(gameOver) {  
  98.             clearInterval(updateTimer);  
  99.             c.fillStyle = "#ff0000";  
  100.             c.fillText("Game Over !", canvas.width/4, canvas.height/2);  
  101.             return;  
  102.         }  
  103.   
  104.         //清屏  
  105.         c.fillStyle = "#000000";  
  106.         c.fillRect(0, 0, 500, 500);  
  107.   
  108.         //绘制障碍物  
  109.         c.fillStyle = "#00ff00";  
  110.         for(var i = 0; i < obsList.length; i++) {  
  111.             obsList[i].x -= obsSpeed;  
  112.             obsList[i].draw(c);  
  113.         }  
  114.   
  115.         //检测触摸  
  116.         if(touch) {  
  117.             bird.y -= upSpeed;  
  118.             bird.draw(c, "up");  
  119.         }  
  120.         else {  
  121.             bird.y += downSpeed;  
  122.             bird.draw(c, "down");  
  123.         }  
  124.   
  125.         //显示分数  
  126.         c.fillStyle = "#ffffff";  
  127.         c.fillText("score : " + score, 10, 30);  
  128.     }, 20);  
  129.   
  130.     //制造障碍物  
  131.     var obsTimer = setInterval(function(){  
  132.         if(gameOver) {  
  133.             clearInterval(obsTimer);  
  134.             return;  
  135.         }  
  136.   
  137.         var h = Math.floor(Math.random()*(canvas.height-obsDistance-20)+20);  
  138.         var h2 = canvas.height - h - obsDistance;  
  139.         var obs1 = new Obstacle(canvas.width, 0, 50, h);  
  140.         var obs2 = new Obstacle(canvas.width, canvas.height-h2, 50, h2);  
  141.         obsList.push(obs1);  
  142.         obsList.push(obs2);  
  143.   
  144.         //移除越界障碍物  
  145.         if(obsList[0].x < -obsList[0].width)  
  146.             obsList.splice(0, 2);  
  147.     }, obsInterval);  
  148. }; 
0 0
原创粉丝点击