html5游戏开发-简单老虎机

原创 2011年11月29日 08:44:02

本游戏使用的是html5的canvas,运行游戏需要浏览器支持html5。

使用开源引擎:lufylegend.js,

lufylegend.js引擎包内包含这个demo,请直接下载lufylegend.js引擎,查看引擎包内源码

lufylegend.js引擎下载地址

http://lufylegend.com/lufylegend


游戏截图


游戏测试地址

http://fsanguo.comoj.com/html5/slot/index.html

游戏结构

index.html

js文件夹|---Main.js

         |---Reel.js

images文件夹|--图片



游戏代码:

Main.js

init(50,"mylegend",600,600,main);

var loadingLayer;
var backLayer;
var stopLayer;
var startLayer;
var loadIndex = 0;
var imglist = {};
var btnup,btndown,btnleft,btnright;
var imgData = new Array();

var mapImgList = new Array();
var mapmoveflag = "";
var MOVE_STEP = 10;

var combination = new Array([1,1,5], [1,2,4], [1,5,1], [2,1,4], [2,3,3], [2,4,1], [2,5,4], [3,1,2], [3,4,3], [3,5,5], [4,1,2], [4,2,3], [4,5,1], [4,5,5], [5,1,1], [5,2,4], [5,3,2], [5,5,1], [1,1,1], [1,1,1]);
var reels = new Array();
var kakes = new Array();
//停止ボタン参照用配列
var stopBtn = new Array();
var start;
var win;
function main(){
	imgData.push({name:"stop_up",path:"./images/slot_stop_up.png"});
	imgData.push({name:"stop_over",path:"./images/slot_stop_over.png"});
	imgData.push({name:"start",path:"./images/slot_start.jpg"});
	imgData.push({name:"kake",path:"./images/slot_kake.png"});
	imgData.push({name:"slot_back",path:"./images/slot_back.jpg"});
	imgData.push({name:"slot_ok",path:"./images/slot_ok.png"});
	imgData.push({name:"item1",path:"./images/1.png"});
	imgData.push({name:"item2",path:"./images/2.png"});
	imgData.push({name:"item3",path:"./images/3.png"});
	imgData.push({name:"item4",path:"./images/4.png"});
	imgData.push({name:"item5",path:"./images/5.png"});
	imgData.push({name:"item6",path:"./images/6.png"});
	loadingLayer = new LSprite();
	loadingLayer.graphics.drawRect(1,"black",[50, 200, 200, 20],true,"#ffffff");
	addChild(loadingLayer);
	loadImage();
}
function loadImage(){
	if(loadIndex >= imgData.length){
		removeChild(loadingLayer);
		legendLoadOver();
		gameInit();
		return;
	}
	loader = new LLoader();
	loader.addEventListener(LEvent.COMPLETE,loadComplete);
	loader.load(imgData[loadIndex].path,"bitmapData");
}
function loadComplete(event){
	loadingLayer.graphics.clear();
	loadingLayer.graphics.drawRect(1,"black",[50, 200, 200, 20],true,"#ffffff");
	loadingLayer.graphics.drawRect(1,"black",[50, 203, 200*(loadIndex/imgData.length), 14],true,"#000000");
	imglist[imgData[loadIndex].name] = loader.content;
	loadIndex++;
	loadImage();
}
function gameInit(event){
	var i,j,bitmap,bitmapdata,childmap;
	
	backLayer = new LSprite();
	addChild(backLayer);

	bitmapdata = new LBitmapData(imglist["slot_back"]);
	bitmap = new LBitmap(bitmapdata);
	backLayer.addChild(bitmap);
	
	stopLayer = new LSprite();
	addChild(stopLayer);
	for(i=0;i<3;i++){
		var reel = new Reel(combination,i);
		reel.x = 150 * i + 90;
		reel.y = 225;
		reels.push(reel);
		addChild(reel);
		var kake = new LBitmap(new LBitmapData(imglist["kake"]));
		kake.x = 150 * i + 90;
		kake.y = 225;
		kakes.push(kake);
		addChild(kake);
		var stop = new LButton(new LBitmap(new LBitmapData(imglist["stop_up"])),new LBitmap(new LBitmapData(imglist["stop_over"])));
		stop.x = 150 * i + 110;
		stop.y = 490;
		stop.index = i;
		stopBtn.push(stop);
		stop.visible = false;
		stop.addEventListener(LMouseEvent.MOUSE_UP, stopevent);
		addChild(stop);
	}

	startLayer = new LSprite();
	addChild(startLayer);
	start = new LButton(new LBitmap(new LBitmapData(imglist["start"])),new LBitmap(new LBitmapData(imglist["start"])));
	start.x = 55;
	start.y = 450;
	startLayer.addChild(start);
	start.addEventListener(LMouseEvent.MOUSE_UP, onmouseup);
	
	
	win = new LButton(new LBitmap(new LBitmapData(imglist["slot_ok"])),new LBitmap(new LBitmapData(imglist["slot_ok"])));
	startLayer.addChild(win);
	win.visible = false;
	win.addEventListener(LMouseEvent.MOUSE_UP, winclick);
	
	backLayer.addEventListener(LEvent.ENTER_FRAME,onframe);
}
function onframe(){
	var i;
	for(i=0;i<3;i++){
		reels[i].onframe();
	}
}
function stopevent(event,currentTarget){
	reels[currentTarget.index].stopFlag = true;
}
function onmouseup(event){
	var i;
	var stopNum = Math.floor(Math.random()*(combination.length/3));
	start.visible = false;
	for(i=0;i<3;i++){
		stopBtn[i].visible = true;
		reels[i].startReel = true;
		reels[i].stopFlag = false;
		reels[i].stopNum = stopNum;
	}
}
function winclick(){
	win.visible = false;
	start.visible = true;
}
function checkWin(){
	var i;
	var allstop = 0;
	for(i=0;i<3;i++){
		if(!reels[i].startReel)allstop++;
	}
	if(allstop >= 3){
		for(i=0;i<3;i++){
			stopBtn[i].visible = false;
		}
		
		if(reels[0].stopNum >= 19){
			win.visible = true;
		}else{
			start.visible = true;
		}
	}
}

Reel.js

function Reel(combination,index){
	base(this,LSprite,[]);
	var self = this;

	//-------------------------------------------
	//実行側から操作可能なプロパティの初期設定
	//-------------------------------------------
	self.maxSpeed = 70;
	self.minSpeed = 10;
	self.currentNum = 1;
	self.stopNum = 0;
	self.maxNum = 6;
	self.speedUpStep = 2;
	self.speedDownStep = 2;
	self.combination = combination;
	self.stopFlag = true;
	self.currentSpeed = 0;
	self.startReel = false;
	self.index = index;
	//-------------------------------------------
	//準備
	//-------------------------------------------
	self.reels = [];
	self.indexs = [0,0,0,0];
	self.reels.push(new LBitmap(self.getReel()));
	self.reels.push(new LBitmap(self.getReel()));
	self.reels.push(new LBitmap(self.getReel()));
	self.reels.push(new LBitmap(self.reels[0].bitmapData));
	
	
	var i,sy;
	self.reels[0].height = 60;
	self.reels[0].bitmapData.height = self.reels[0].height;
	self.reels[0].bitmapData.setCoordinate(0,80-self.reels[0].height);
	self.reels[2].height = 60;
	self.reels[2].bitmapData.height = self.reels[2].height;
	self.reels[3].visible = false;
	sy = 0;
	for(i=0;i<self.reels.length;i++){
		self.reels[i].y = sy;
		sy += self.reels[i].height;
		self.addChild(self.reels[i]);
	}
	//self.startReel = true;
	//self.stopFlag = false;
}
Reel.prototype.onframe = function (){
	var self = this;

	if(self.startReel)self.wheel();
};
Reel.prototype.getReel = function (){
	var self = this;
	if(self.currentNum > self.maxNum)self.currentNum = 1;
	self.indexs[0] = self.currentNum;

	self.indexs.pop();
	self.indexs.unshift(self.currentNum);
	var nextReel = new LBitmapData(imglist["item"+self.currentNum++]);
	return nextReel;
};
Reel.prototype.wheel = function (){
	var self = this;
	
	//回転速度の調節
	if (self.stopFlag) {
		//スピードダウン
		if (self.currentSpeed > self.minSpeed) {
			self.currentSpeed -= self.speedDownStep;
		} else {
			self.currentSpeed = self.minSpeed;
		}
	} else {
		//スピードアップ
		if (self.currentSpeed < self.maxSpeed) {
			self.currentSpeed += self.speedUpStep;
		} else {
			self.currentSpeed = self.maxSpeed;
		}
	}
	if(self.stopFlag && self.currentSpeed <= self.minSpeed && self.indexs[1] == self.combination[self.stopNum][self.index] && self.reels[1].y + self.currentSpeed > 60){
		self.currentSpeed = 60 - self.reels[1].y; 
		self.startReel = false;
		
	}
	self.setY();
	if(!self.startReel)checkWin();
};
Reel.prototype.setY = function(){
	var self = this;
	self.reels[1].y += self.currentSpeed;
	if(self.reels[1].y + self.reels[1].height > 200){
		self.reels[1].height = 200 - self.reels[1].y;
		self.reels[1].bitmapData.height = self.reels[1].height;
	}
	if(self.reels[1].y > 80){
		self.reels[0].height = 80;
		self.reels[0].y = self.reels[1].y - 80;
	}else{
		self.reels[0].height = self.reels[1].y;
		self.reels[0].y = 0;
	}
	self.reels[0].bitmapData.height = self.reels[0].height;
	self.reels[0].bitmapData.setCoordinate(0,80-self.reels[0].height);
	
	self.reels[2].y = self.reels[1].y + self.reels[1].height;
	
	if(self.reels[2].y > 200){
		self.reels[2].visible = false;
	}else if(self.reels[2].y + 80 > 200){
		self.reels[2].height = 200 - self.reels[2].y;
		self.reels[2].bitmapData.height = self.reels[2].height;
	}else{
		self.reels[3].y = self.reels[2].y + self.reels[2].height;
		if(self.reels[3].y < 200){
			self.reels[3].height = 200 - self.reels[3].y;
			self.reels[3].bitmapData.height = self.reels[3].height;
		}
	}
	
	if(self.reels[0].y > 0){
		var child = self.reels.pop();
		child.bitmapData = self.getReel();
		child.visible = true;
		self.reels.unshift(child);
		child.y = 0;
		child.height = self.reels[1].y;
		child.bitmapData.height = child.height;
		child.bitmapData.setCoordinate(0,80-child.height);
	}
	if(self.reels[3].y >= 200){
		self.reels[3].visible = false;
	}
};
index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>slot</title>	
<meta name="viewport" content="width=480,initial-scale=0.5" />
<script type="text/javascript" src="../legend/legend.js"></script> 
<script type="text/javascript" src="./js/Reel.js"></script> 
<script type="text/javascript" src="./js/Main.js"></script> 
</head>
<body>
<div id="mylegend">loading……</div>

</body>
</html>


HTML5应用——欢乐老虎机

在上面一篇博文中,我介绍了HTML5应用的简易播放器,这篇博文中介绍一个比较复杂的HTML5游戏-老虎机。 寒假在家玩老虎机输了些许钱,所以自己就萌生了写个老虎机的游戏。开始打算用Visual C++...

java例程练习(Circle类和Point类柔和练习)

public class Test { public static void main(String[] args) { Circle c1 = new Circle(new Point(1.0...

游戏开发随手记:Cocos2d-html5:目录结构简单介绍

ocos2d-html5引擎下载地址: https://github.com/cocos2d/cocos2d-html5.git 下载下来后,用WebStorm工具打开,目录如下图 整个引擎...

[HTML5游戏开发]简单的《找不同汉字版》,来考考你的眼力吧

本次游戏开发需要用到lufylegend.js开源游戏引擎,版本我用的是1.5.2(现在最新的版本是1.6.0)。   引擎下载的位置:http://lufylegend.googlecode.co...

HTML5面向对象的游戏开发简单实例总结

在阅读一本HTML5游戏开发相关书籍时发现一个很好的例子,通过这个例子可以对面向对象的开发进行更深入的理解。这个对象要实现的是:将一个CSS sprite中的图像绘制到canvas中。首先创建一个Sp...
  • fareise
  • fareise
  • 2016年07月15日 22:37
  • 3127

[HTML5游戏开发]简单的《找没有同汉字版〗爆去考考您狄综力吧

[color=ize:18px]一,筹办工做   本次游戏开发需求用到lufylegend.js开源游戏引擎,版本我用的是1.5.2(如今最新的版本是1.6.0)。   引擎下载的位置:http:...

html5游戏开发-愤怒的小鸟-开源讲座(一)-跳入弹出的小鸟

愤怒的小鸟是一款人气火爆的益智游戏,现在我试着用lufylegend库件和Box2dWeb物理引擎来看看在html5中如何制作此类经典的物理游戏。准备工作一首先,你需要下载lufylegend库件1....

Cocos2d-html5入门 (跨平台游戏开发)

cocos2d-js是cocos2d-x的JavaScript版本,真正跨全平台的游戏引擎,采用原生JavaScript语言,可发布到包括Web平台,iOS,Android,Windows Phone...

Cocos2d-x jsb + Cocos2d-html5 跨平台游戏开发(二)—— 遇到的坑

上一篇blog里总结了选择cocos2d-x javascript binding(后面简称jsb)和cocos2d-html5(后面简称h5)技术开发的原因,这篇总结一下半年来遇到的一些问题。希望能...

HTML5+JS游戏开发模块----canvas图片拖放

边学边做笔记,现在游戏开发进入下一个阶段,canvas图片拖放模块,如果要做塔防类游戏,少不了拖放炮塔之类的。HTML5提供拖放标签,draggable,ondrop,ondragover,ondra...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:html5游戏开发-简单老虎机
举报原因:
原因补充:

(最多只允许输入30个字)