HTML5 canvas遊戲開發實戰 5 : 石頭剪刀布
5.1 遊戲分析
遊戲畫面
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="./js/lufylegend-1.7.6.min.js"></script>
<script type="text/javascript" src="./js/Main.js"></script>
</head>
<body>
<div id="mylegend">loading...</div>
</body>
</html>
5.2 必要的JavaScript知識
隨機數:Math.random()生成0到1的小數,如果需要隨機生成一個5到10之間的整數,
Math.floor(5 + 5*Math.random());
條件分支:if ... else if ... else ... 和switch
5.3 分層實現
整個遊戲介面作為一個層,然後將選擇出拳部分和結果顯示部分分離成為另外兩個層。
5.4 各個層的基本功能
基本畫面顯示:利用靜態類LLoadManage的load函式來一次性讀取多張圖片。
LLoadManage.load($list, $onupdate, $oncomplete);
- $list : 要讀取的圖片陣列
- $onupdate : 每讀取一張圖片後呼叫的函式
- $oncomplete : 讀完所有圖片後呼叫的函式
5.5 出拳
initClickLayer()
5.6 結果判定
init(50, "mylegend", 800, 400, main);
var backLayer;
var resultLayer;
var clickLayer;
var imglist = {};
var imgData = new Array(
{name:"title",path:"./images/title.png"},
{name:"shitou",path:"./images/shitou.png"},
{name:"jiandao",path:"./images/jiandao.png"},
{name:"bu",path:"./images/bu.png"},
);
var showList = new Array();
var nameText, playerNameText;
var selfBitmap,
enemyBitmap,
selfTextAll,
selfTextWin,
selfTextLoss,
selfTextDraw,
win = 0,
loss = 0,
draw = 0;
var checkList = [
[0, 1, -1],
[-1, 0, 1],
[1, -1, 0]
];
function main() {
backLayer = new LSprite();
addChild(backLayer);
loadingLayer = new LoadingSample3();
backLayer.addChild(loadingLayer);
LLoadManage.load(
imgData,
function(progress) {
loadingLayer.setProgress(progress);
console.log(progress);
},
function(result) {
imglist = result;
backLayer.removeChild(loadingLayer);
loadingLayer = null;
gameInit();
}
);
}
function gameInit() {
showList.push(new LBitmapData(imglist["shitou"]));
showList.push(new LBitmapData(imglist["jiandao"]));
showList.push(new LBitmapData(imglist["bu"]));
backLayer.graphics.drawRect(10, '#008800', [0,0,LGlobal.width, LGlobal.heigth], true, '#000000');
//顯示遊戲標題
var titleBitmap = new LBitmap(new LBitmapData(imglist["title"]));
titleBitmap.x = (LGlobal.width - titleBitmap.width)/2;
titleBitmap.y = 10;
backLayer.addChild(titleBitmap);
//玩家方出拳圖片
selfBitmap = new LBitmap(showList[0]);
selfBitmap.x = 400 - selfBitmap.width - 50;
selfBitmap.y = 130;
backLayer.addChild(selfBitmap);
//電腦方出拳圖片
enemyBitmap = new LBitmap(showList[0]);
enemyBitmap.x = 400 + 50;
enemyBitmap.y = 130;
backLayer.addChild(enemyBitmap);
//玩家、電腦名稱設定
playerNameText = new LTextField();
playerNameText.text = "玩家";
playerNameText.weight = "bolder";
playerNameText.color = "#ffffff";
playerNameText.size = 24;
playerNameText.x = selfBitmap.x + (selfBitmap.width - playerNameText.getWidth())/2;
playerNameText.y = 95;
console.log(playerNameText);
backLayer.addChild(playerNameText);
console.log(backLayer);
nameText = new LTextField();
nameText.text = "電腦";
nameText.weight = "bolder";
nameText.color = "#ffffff";
nameText.size = 24;
nameText.x = enemyBitmap.x + (enemyBitmap.width - nameText.getWidth())/2;
nameText.y = 95;
backLayer.addChild(nameText);
//結果顯示層初始化
initResultLayer();
//操作層初始化
initClickLayer();
}
function initResultLayer() {
resultLayer = new LSprite();
resultLayer.graphics.drawRect(4,'#ff8800',[0,0,150,110],true,'#ffffff');
resultLayer.x = 10;
resultLayer.y = 100;
backLayer.addChild(resultLayer);
selfTextAll = new LTextField();
selfTextAll.text = "猜拳次數:0";
selfTextAll.weight = "bolder";
selfTextAll.x = 10;
selfTextAll.y = 20;
resultLayer.addChild(selfTextAll);
selfTextWin = new LTextField();
selfTextWin.text = "勝利次數:0";
selfTextWin.weight = "bolder";
selfTextWin.x = 10;
selfTextWin.y = 40;
resultLayer.addChild(selfTextWin);
selfTextLoss = new LTextField();
selfTextLoss.text = "失敗次數:0";
selfTextLoss.weight = "bolder";
selfTextLoss.x = 10;
selfTextLoss.y = 60;
resultLayer.addChild(selfTextLoss);
selfTextDraw = new LTextField();
selfTextDraw.text = "平局次數:0";
selfTextDraw.weight = "bolder";
selfTextDraw.x = 10;
selfTextDraw.y = 80;
resultLayer.addChild(selfTextDraw);
}
function initClickLayer() {
clickLayer = new LSprite();
clickLayer.graphics.drawRect(4,'#ff8800',[0,0,300,110],true,'#ffffff');
var msgText = new LTextField();
msgText.text = "請出拳:";
msgText.weight = "bolder";
msgText.x = 10;
msgText.y = 10;
clickLayer.addChild(msgText);
var btnShitou = getButton("shitou");
btnShitou.x = 30;
btnShitou.y = 35;
clickLayer.addChild(btnShitou);
btnShitou.addEventListener(LMouseEvent.MOUSE_UP,onclick)
var btnJiandao = getButton("jiandao");
btnJiandao.x = 115;
btnJiandao.y = 35;
clickLayer.addChild(btnJiandao);
btnJiandao.addEventListener(LMouseEvent.MOUSE_UP,onclick)
var btnBu = getButton("bu");
btnBu.x = 200;
btnBu.y = 35;
clickLayer.addChild(btnBu);
btnBu.addEventListener(LMouseEvent.MOUSE_UP,onclick)
clickLayer.x = 250;
clickLayer.y = 275;
backLayer.addChild(clickLayer);
}
//建立按鈕
function getButton(value) {
var btnUp = new LBitmap(new LBitmapData(imglist[value]));
btnUp.scaleX = 0.5;
btnUp.scaleY = 0.5;
var btnOver = new LBitmap(new LBitmapData(imglist[value]));
btnOver.scaleX = 0.5;
btnOver.scaleY = 0.5;
btnOver.x = 2;
btnOver.y = 2;
var btn = new LButton(btnUp, btnOver);
btn.name = value;
return btn;
}
//出拳
function onclick(event, display) {
var selfValue, enemyValue;
if (display.name == "shitou") {
selfValue = 0;
} else if (display.name == "jiandao") {
selfValue = 1;
} else if (display.name == "bu") {
selfValue = 2;
}
enemyValue = Math.floor(Math.random()*3);
selfBitmap.bitmapData = showList[selfValue];
enemyBitmap.bitmapData = showList[enemyValue];
//
var result = checkList[selfValue][enemyValue];
if (result == -1) {
loss += 1;
} else if (result == 1) {
win += 1;
} else {
draw += 1;
}
selfTextWin.text = "勝利次數:" + win;
selfTextLoss.text = "失敗次數:" + loss;
selfTextDraw.text = "平局次數:" + draw;
selfTextAll.text = "猜拳次數:" + (win+loss+draw);
}
相關文章
- Python剪刀石頭布遊戲Python遊戲
- 結對-遊戲《石頭剪刀布》-專案進度遊戲
- 剪刀石頭布程式(Python)Python
- 石頭剪刀布?多人動作競技遊戲《永劫無間》戰鬥系統簡析遊戲
- 程式碼日:剪刀石頭布 (RPS) 專案
- 小熊飛槳練習冊-03石頭剪刀布
- 51微控制器系列(三)51 微控制器遊戲設計 —— 雙人對戰小遊戲(石頭剪刀布)遊戲設計
- 阿里開源HTML5小遊戲開發框架Hilo實戰教程阿里HTML遊戲開發框架
- HTML5中canvas實現小球擊打小方塊遊戲HTMLCanvas遊戲
- 天啦嚕!在家和偶像玩"剪刀石頭布",阿里工程師如何辦到?阿里工程師
- 天啦嚕!在家和偶像玩「剪刀石頭布」,阿里工程師如何辦到?阿里工程師
- html5中canvas元素建立畫布介紹HTMLCanvas
- python之結合if條件判斷和生成隨機數的相關知識,完成石頭剪刀布的遊戲Python隨機遊戲
- 天啦嚕!在家和愛豆玩「剪刀石頭布」,阿里工程師如何辦到?阿里工程師
- HTML5 Canvas(實戰:繪製餅圖2 Tooltip)HTMLCanvas
- 實戰Flash遊戲開發遊戲開發
- HTML5遊戲開發過程中的二三事HTML遊戲開發
- HTML5遊戲開發(四):飛機大戰之顯示場景和元素HTML遊戲開發
- HTML5遊戲開發(五):飛機大戰之讓所有元素動起來HTML遊戲開發
- Canvas中的剪刀手講解與實戰——Android高階UICanvasAndroidUI
- HTML5遊戲開發(二):使用TypeScript編寫程式碼HTML遊戲開發TypeScript
- HTML5 Canvas 詳解HTMLCanvas
- HTML5(五)——Canvas APIHTMLCanvasAPI
- 使用 HTML5 Canvas 實現簽名功能HTMLCanvas
- HTML5遊戲開發(三):使用webpack構建TypeScript應用HTML遊戲開發WebTypeScript
- 《HTML5移動網站與App開發實戰》簡介HTML網站APP
- PAT 1018 錘子剪刀布
- HTML5系列之canvas用法HTMLCanvas
- HTML5畫布-小球碰撞HTML
- HTML5遊戲開發(一):3分鐘建立一個hello worldHTML遊戲開發
- 前端開發入門到實戰:HTML5進階FileReader的使用前端HTML
- 前端開發入門到實戰:html5語義化標籤前端HTML
- 基於 HTML5 Canvas 實現文字動畫特效HTMLCanvas動畫特效
- TP開發的 HTML5頭像擷取案例,會員頭像功能HTML
- html5錄音功能實戰HTML
- html5,css3實戰HTMLCSSS3
- PAT1018錘子剪刀布
- Html5 canvas創意特效合集HTMLCanvas特效
- HTML5怎樣建立畫布?HTML