蘑菇與熊游戲開發第三回(讓熊動起來)
作者:admin
一、先定義全局變量、二、定義熊、三、描繪熊在畫布上,注意上面的熊是不動的,為什么呢,因為x,y軸和角度沒變,因此我們再加上改變x,y和角度的代碼。
這一回我們讓熊動起來。
預期達到效果:http://www.html5china.com/html5games/mogu/index2.html
一、先定義全局變量
- var bearEyesClosedImg = new Image();//閉著眼睛的熊熊
- var horizontalSpeed = 2;//水平速度
- var verticalSpeed = -2; //垂直速度,開始肯定是要向上飄,所以要負數
- var bearAngle = 2;//熊旋轉的速度
二、定義熊
首先定義一只公用熊
- //定義動物熊 Animal 繼承 游戲對象GameObject
- function Animal() {};
- Animal.prototype = new GameObject();//游戲對象GameObject
- Animal.prototype.angle = 0;//旋轉的角度,(用來改變熊的旋轉速度)
定義我們使用的熊
- //定義熊實例
- var animal = new Animal();
初始化熊
- bearEyesClosedImg.src = "images/bear_eyesclosed.png";//閉著眼睛的
- animal.image = bearEyesClosedImg;//熊圖片
- animal.x = parseInt(screenWidth/2);//x坐標
- animal.y = parseInt(screenHeight/2); //y坐標
三、描繪熊在畫布上
因為熊是相對移動的,所以我們要加一個基準
- //以當前熊的中心位置為基準
- ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));
- //描繪熊
- ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));
但是熊要旋轉啊,好的,想要改變它的角度,上面的代碼中加入旋轉
- //以當前熊的中心位置為基準
- ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));
- //根據當前熊的角度輪換
- ctx.rotate(animal.angle * Math.PI/180);
- //描繪熊
- ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));
上面的熊是不動的,為什么呢,因為x,y軸和角度沒變,因此我們再加上改變x,y和角度的代碼,于是上面的代碼變成了
- //改變移動動物X和Y位置
- animal.x += horizontalSpeed;
- animal.y += verticalSpeed;
- //改變翻滾角度
- animal.angle += bearAngle;
- //以當前熊的中心位置為基準
- ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));
- //根據當前熊的角度輪換
- ctx.rotate(animal.angle * Math.PI/180);
- //描繪熊
- ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));
到現目前為止熊已經能滾動和移動了,最終代碼如下:
- <!DOCTYPE>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>蘑菇動起來-html5中文網</title>
- <!-- 要記得引用jquery-1.4.2.js -->
- <script type="text/javascript" src="./js/jquery-1.4.2.js"></script>
- <script type="text/javascript" >
- //全局變量
- var backgroundForestImg = new Image();//森林背景圖
- var mushroomImg = new Image();//蘑菇
- var bearEyesClosedImg = new Image();//閉著眼睛的熊熊
- var ctx;//2d畫布
- var screenWidth;//畫布寬度
- var screenHeight;//畫布高度
- var speed = 2;//不變常量,從新開始的速度
- var horizontalSpeed = speed;//水平速度,隨著熊的碰撞會發生改變
- var verticalSpeed = -speed; //垂直速度,開始肯定是要向上飄,所以要負數,隨著熊的碰撞會發生改變
- var bearAngle = 2;//熊旋轉的速度
- //公用 定義一個游戲物體戲對象
- function GameObject()
- {
- this.x = 0;
- this.y = 0;
- this.image = null;
- }
- //定義蘑菇Mushroom 繼承游戲對象GameObject
- function Mushroom() {};
- Mushroom.prototype = new GameObject();//游戲對象GameObject
- //蘑菇實例
- var mushroom = new Mushroom(); //循環描繪物體
- //定義動物熊 Animal 繼承 游戲對象GameObject
- function Animal() {};
- Animal.prototype = new GameObject();//游戲對象GameObject
- Animal.prototype.angle = 0;//動物的角度,目前中(即作為動物它在屏幕上旋轉退回)
- //定義熊實例
- var animal = new Animal();
- function GameLoop()
- {
- //清除屏幕
- ctx.clearRect(0, 0, screenWidth, screenHeight);
- ctx.save();
- //繪制背景
- ctx.drawImage(backgroundForestImg, 0, 0);
- //繪制蘑菇
- ctx.drawImage(mushroom.image, mushroom.x, mushroom.y);
- //繪制熊
- //改變移動動物X和Y位置
- animal.x += horizontalSpeed;
- animal.y += verticalSpeed;
- //改變翻滾角度
- animal.angle += bearAngle;
- //以當前熊的中心位置為基準
- ctx.translate(animal.x + (animal.image.width/2), animal.y + (animal.image.height/2));
- //根據當前熊的角度輪換
- ctx.rotate(animal.angle * Math.PI/180);
- //描繪熊
- ctx.drawImage(animal.image, - (animal.image.width/2), - (animal.image.height/2));
- ctx.restore();
- }
- //加載圖片
- function LoadImages()
- {
- mushroomImg.src = "images/mushroom.png";//蘑菇
- backgroundForestImg.src = "images/forest1.jpg";//森林背景圖
- bearEyesClosedImg.src = "images/bear_eyesclosed.png";//閉著眼睛的
- mushroom.image = mushroomImg;
- animal.image = bearEyesClosedImg;
- }
- //事件處理
- function AddEventHandlers()
- {
- //鼠標移動則蘑菇跟著移動
- $("#container").mousemove(function(e){
- mushroom.x = e.pageX - (mushroom.image.width/2);
- });
- }
- //初始化
- $(window).ready(function(){
- AddEventHandlers();//添加事件
- LoadImages();
- ctx = document.getElementById('canvas').getContext('2d'); //獲取2d畫布
- screenWidth = parseInt($("#canvas").attr("width")); //畫布寬度
- screenHeight = parseInt($("#canvas").attr("height"));
- //初始化蘑菇
- mushroom.x = parseInt(screenWidth/2);// 蘑菇X坐標
- mushroom.y = screenHeight - 40;//蘑菇Y坐標
- //初始化熊
- animal.x = parseInt(screenWidth/2);
- animal.y = parseInt(screenHeight/2);
- setInterval(GameLoop, 10);
- });
- </script>
- </head>
- <body>
- <div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">
- <canvas id="canvas" width="480" height="320" >
- 瀏覽器不支持html5,<a target="_blank" href="http://www.html5china.com/help/browser.html">請下載</a>支持html5的瀏覽器來觀看
- </canvas>
- </div>
- </body>
- </html>
第三回就講到這了,第四回講熊碰撞邊界和蘑菇的事件
原文鏈接:http://www.html5china.com/course/20110101_899.html
【編輯推薦】
責任編輯:張偉
來源:
HTML5China