成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

蘑菇與熊游戲開(kāi)發(fā)第三回(讓熊動(dòng)起來(lái))

開(kāi)發(fā) 前端 游戲開(kāi)發(fā)
一、先定義全局變量、二、定義熊、三、描繪熊在畫布上,注意上面的熊是不動(dòng)的,為什么呢,因?yàn)閤,y軸和角度沒(méi)變,因此我們?cè)偌由细淖儀,y和角度的代碼。

這一回我們讓熊動(dòng)起來(lái)。

預(yù)期達(dá)到效果:http://www.html5china.com/html5games/mogu/index2.html

一、先定義全局變量

  1. var bearEyesClosedImg = new Image();//閉著眼睛的熊熊       
  2. var horizontalSpeed = 2;//水平速度      
  3. var verticalSpeed = -2; //垂直速度,開(kāi)始肯定是要向上飄,所以要負(fù)數(shù)      
  4. var bearAngle = 2;//熊旋轉(zhuǎn)的速度    

二、定義熊

首先定義一只公用熊

  1. //定義動(dòng)物熊 Animal 繼承 游戲?qū)ο驡ameObject      
  2. function Animal() {};      
  3. Animal.prototype = new GameObject();//游戲?qū)ο驡ameObject      
  4. Animal.prototype.angle = 0;//旋轉(zhuǎn)的角度,(用來(lái)改變熊的旋轉(zhuǎn)速度)    

定義我們使用的熊

  1. //定義熊實(shí)例       
  2. var animal = new Animal();    

初始化熊 

  1. bearEyesClosedImg.src = "images/bear_eyesclosed.png";//閉著眼睛的      
  2. animal.image = bearEyesClosedImg;//熊圖片      
  3. animal.x = parseInt(screenWidth/2);//x坐標(biāo)      
  4. animal.y = parseInt(screenHeight/2); //y坐標(biāo)    

三、描繪熊在畫布上

因?yàn)樾苁窍鄬?duì)移動(dòng)的,所以我們要加一個(gè)基準(zhǔn)

  1. //以當(dāng)前熊的中心位置為基準(zhǔn)      
  2. ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  3. //描繪熊      
  4. ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));    

但是熊要旋轉(zhuǎn)啊,好的,想要改變它的角度,上面的代碼中加入旋轉(zhuǎn)

  1. //以當(dāng)前熊的中心位置為基準(zhǔn)      
  2. ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  3. //根據(jù)當(dāng)前熊的角度輪換      
  4. ctx.rotate(animal.angle * Math.PI/180);      
  5. //描繪熊      
  6. ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));    

上面的熊是不動(dòng)的,為什么呢,因?yàn)閤,y軸和角度沒(méi)變,因此我們?cè)偌由细淖儀,y和角度的代碼,于是上面的代碼變成了

  1. //改變移動(dòng)動(dòng)物X和Y位置      
  2. animal.x += horizontalSpeed;      
  3. animal.y += verticalSpeed;      
  4. //改變翻滾角度      
  5. animal.angle += bearAngle;      
  6. //以當(dāng)前熊的中心位置為基準(zhǔn)      
  7.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  8. //根據(jù)當(dāng)前熊的角度輪換      
  9.     ctx.rotate(animal.angle * Math.PI/180);      
  10. //描繪熊      
  11.     ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));    

到現(xiàn)目前為止熊已經(jīng)能滾動(dòng)和移動(dòng)了,最終代碼如下:

  1. <!DOCTYPE>        
  2. <html>        
  3. <head>        
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />        
  5. <title>蘑菇動(dòng)起來(lái)-html5中文網(wǎng)</title>        
  6. <!-- 要記得引用jquery-1.4.2.js -->     
  7. <script type="text/javascript" src="./js/jquery-1.4.2.js"></script>        
  8. <script type="text/javascript" >        
  9.     //全局變量         
  10.     var backgroundForestImg = new Image();//森林背景圖         
  11.     var mushroomImg = new Image();//蘑菇       
  12.     var bearEyesClosedImg = new Image();//閉著眼睛的熊熊       
  13.     var ctx;//2d畫布         
  14.     var screenWidth;//畫布寬度         
  15.     var screenHeight;//畫布高度       
  16.     var speed = 2;//不變常量,從新開(kāi)始的速度        
  17.     var horizontalSpeed = speed;//水平速度,隨著熊的碰撞會(huì)發(fā)生改變      
  18.     var verticalSpeed = -speed; //垂直速度,開(kāi)始肯定是要向上飄,所以要負(fù)數(shù),隨著熊的碰撞會(huì)發(fā)生改變      
  19.     var bearAngle = 2;//熊旋轉(zhuǎn)的速度      
  20.     //公用 定義一個(gè)游戲物體戲?qū)ο?nbsp;        
  21.     function GameObject()         
  22.     {         
  23.         this.x = 0;         
  24.         this.y = 0;         
  25.         this.image = null;         
  26.     }         
  27.              
  28.     //定義蘑菇Mushroom 繼承游戲?qū)ο驡ameObject         
  29.     function Mushroom() {};         
  30.     Mushroom.prototype = new GameObject();//游戲?qū)ο驡ameObject         
  31.     //蘑菇實(shí)例         
  32.     var mushroom = new Mushroom();        //循環(huán)描繪物體        
  33.            
  34.     //定義動(dòng)物熊 Animal 繼承 游戲?qū)ο驡ameObject      
  35.     function Animal() {};      
  36.     Animal.prototype = new GameObject();//游戲?qū)ο驡ameObject      
  37.     Animal.prototype.angle = 0;//動(dòng)物的角度,目前中(即作為動(dòng)物它在屏幕上旋轉(zhuǎn)退回)      
  38.     //定義熊實(shí)例       
  39.     var animal = new Animal();      
  40.           
  41.     function GameLoop()         
  42.     {         
  43.         //清除屏幕         
  44.         ctx.clearRect(0, 0, screenWidth, screenHeight);         
  45.         ctx.save();         
  46.         //繪制背景         
  47.         ctx.drawImage(backgroundForestImg, 0, 0);         
  48.         //繪制蘑菇         
  49.         ctx.drawImage(mushroom.image, mushroom.x, mushroom.y);       
  50.         //繪制熊      
  51.         //改變移動(dòng)動(dòng)物X和Y位置      
  52.         animal.x += horizontalSpeed;      
  53.         animal.y += verticalSpeed;      
  54.         //改變翻滾角度      
  55.         animal.angle += bearAngle;      
  56.         //以當(dāng)前熊的中心位置為基準(zhǔn)      
  57.         ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));      
  58.         //根據(jù)當(dāng)前熊的角度輪換      
  59.         ctx.rotate(animal.angle * Math.PI/180);      
  60.         //描繪熊      
  61.         ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));      
  62.         ctx.restore();      
  63.         }         
  64.     //加載圖片         
  65.     function LoadImages()         
  66.     {         
  67.         mushroomImg.src = "images/mushroom.png";//蘑菇         
  68.         backgroundForestImg.src = "images/forest1.jpg";//森林背景圖        
  69.         bearEyesClosedImg.src = "images/bear_eyesclosed.png";//閉著眼睛的      
  70.               
  71.         mushroom.image = mushroomImg;         
  72.         animal.image = bearEyesClosedImg;      
  73.     }       
  74.     //事件處理         
  75.     function AddEventHandlers()         
  76.     {         
  77.         //鼠標(biāo)移動(dòng)則蘑菇跟著移動(dòng)         
  78.         $("#container").mousemove(function(e){         
  79.             mushroom.x = e.pageX - (mushroom.image.width/2);         
  80.         });          
  81.                  
  82.     }       
  83.     //初始化         
  84.     $(window).ready(function(){          
  85.         AddEventHandlers();//添加事件        
  86.         LoadImages();                 
  87.         ctx = document.getElementById('canvas').getContext('2d'); //獲取2d畫布            
  88.         screenWidth = parseInt($("#canvas").attr("width")); //畫布寬度       
  89.         screenHeight = parseInt($("#canvas").attr("height"));         
  90.         //初始化蘑菇      
  91.         mushroom.x = parseInt(screenWidth/2);// 蘑菇X坐標(biāo)        
  92.         mushroom.y = screenHeight - 40;//蘑菇Y(jié)坐標(biāo)         
  93.         //初始化熊      
  94.         animal.x = parseInt(screenWidth/2);      
  95.         animal.y = parseInt(screenHeight/2);       
  96.         setInterval(GameLoop, 10);         
  97.     });         
  98.        
  99.         
  100. </script>        
  101. </head>        
  102.         
  103. <body>        
  104.     <div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">        
  105.         <canvas id="canvas" width="480" height="320" >       
  106.         瀏覽器不支持html5,<a target="_blank" href="http://www.html5china.com/help/browser.html">請(qǐng)下載</a>支持html5的瀏覽器來(lái)觀看       
  107.         </canvas>        
  108.     </div>        
  109.        </body>        
  110. </html>       

第三回就講到這了,第四回講熊碰撞邊界和蘑菇的事件

原文鏈接:http://www.html5china.com/course/20110101_899.html

【編輯推薦】

  1. 蘑菇與熊游戲開(kāi)發(fā)***回(游戲分析)
  2. 蘑菇與熊游戲開(kāi)發(fā)第二回(讓蘑菇動(dòng)起來(lái))
  3. 蘑菇與熊游戲開(kāi)發(fā)第四回(熊碰撞邊界處理)
  4. 蘑菇與熊游戲開(kāi)發(fā)第五回(熊碰撞蘑菇處理)
  5. 蘑菇與熊游戲開(kāi)發(fā)第六回(繪制獎(jiǎng)品)
  6. 蘑菇與熊游戲開(kāi)發(fā)第七回(熊碰到獎(jiǎng)品處理)
  7. 蘑菇與熊游戲開(kāi)發(fā)第八回(完善游戲)
責(zé)任編輯:張偉 來(lái)源: HTML5China
相關(guān)推薦

2012-05-21 10:45:30

HTML5

2012-05-21 13:18:12

HTML5

2012-05-21 13:11:51

HTML5

2012-05-21 13:32:45

HTML5

2012-05-21 14:08:21

HTML5

2012-05-21 10:40:13

HTML5

2012-05-21 13:25:49

HTML5

2012-09-03 09:21:51

2010-09-08 09:48:56

Gif播放教程Android

2020-11-16 11:50:21

Python代碼命令

2009-06-19 11:18:51

Factory BeaSpring配置

2022-06-07 09:00:32

PythonAI靜態(tài)圖片

2013-05-27 15:35:18

用友UAP移動(dòng)應(yīng)用移動(dòng)平臺(tái)

2019-05-21 14:18:09

PygamePython編程語(yǔ)言

2011-06-01 14:51:54

jQuery

2021-09-26 09:23:01

GC算法垃圾

2010-05-21 11:03:51

統(tǒng)一通信系統(tǒng)

2011-09-15 17:36:29

Android應(yīng)用Call Cartoo動(dòng)畫

2018-07-26 13:53:27

2014-03-21 09:52:29

jQuery動(dòng)畫插件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 久久一级| 中文字幕视频在线看5 | 天天视频成人 | 国产精品久久久久久久免费大片 | 婷婷丁香在线视频 | 免费观看一级毛片 | 国产精品视频一区二区三区 | 免费国产视频在线观看 | 免费黄色录像视频 | 成人羞羞国产免费视频 | 午夜精品福利视频 | 国产女人与拘做受视频 | 波霸ol一区二区 | 欧美国产一区二区三区 | 久久久久久艹 | 一道本在线| 亚洲成人在线免费 | 久久机热| 国产亚洲精品久久久久久牛牛 | www四虎影视 | 亚洲欧美国产毛片在线 | 可以免费观看的av片 | 在线观看久草 | 国产精品久久久久久久久久久免费看 | 久久专区| 亚洲精品日韩欧美 | 成人免费观看男女羞羞视频 | 日韩一区精品 | 青青草这里只有精品 | 又黄又爽的网站 | 国产精品久久久久久吹潮 | 欧美一区二区成人 | 国产精品激情在线 | 2018国产大陆天天弄 | 国产精品视频999 | 欧美aaa一级片 | 桃色五月 | 成人国产在线视频 | 国内自拍偷拍 | 日韩精品一区二区三区视频播放 | 国产美女自拍视频 |