当前位置: 代码网 > it编程>编程语言>Javascript > 使用JavaScript做一款无框架浏览器直接运行的益智类数字棋牌小游戏

使用JavaScript做一款无框架浏览器直接运行的益智类数字棋牌小游戏

2024年05月15日 Javascript 我要评论
游戏界面一、游戏效果展示二、游戏逻辑与交互设计 由于技术有限不能做太复杂的游戏,所以做个休闲数字游戏是比较合适的,下手之前做了挺多准备,包括思考游戏逻辑、收集和制作游戏图片、音乐素材等,最后确定要

游戏界面

 一、游戏效果展示

二、游戏逻辑与交互设计

       由于技术有限不能做太复杂的游戏,所以做个休闲数字游戏是比较合适的,下手之前做了挺多准备,包括思考游戏逻辑、收集和制作游戏图片、音乐素材等,最后确定要完成一下几个游戏必备的内容。

       (一)游戏逻辑

游戏玩法就是在原来“井字过三关”游戏的基础上加上数字计算,三连子后同时计算个位数大小,谁大就谁赢。

       (二)人机交互

游戏为双人游戏,分为蓝方(ai)红方(玩家),双方一开始各自获得5个随机分配的棋子,棋子包含个位数字。抛骰子后,玩家按先后顺序拖动棋子放入棋盘中。每次游戏都可实时计算得分,每盘游戏结束后记录得分,具有历史排行榜功能。

       (三)游戏界面设计

游戏以卡通为主,收集制作素材尽量为可爱风格。支持英语、日语、简体中文、繁体中文。

       (四)游戏音乐设计

游戏要有背景音乐、下棋声音、胜负声音、并可以按需关闭声音。

二、游戏制作过程

(一)游戏资源收集和制作

先在纸上画出草图,然后网上收集素材制作棋盘、棋子等贴图,最后全部放在一个img文件夹并命名编号,用于后续javascript调用。

(二)游戏布局

使用html+css代码,将img文件夹中的贴图元素按设计的布局放置在对应的位置中,并设置z-index属性,用于控制元素的显示与隐藏。html代码如下:

<!doctype html>
 
<html>
 
<head>
	<meta charset="utf-8"/>
	<meta name="format-detection"content="telephone=no"/>
	<meta name="msapplication-tap-highlight"content="no"/>
	<meta name="viewport"content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
	<meta http-equiv="content-security-policy"content="default-src * 'unsafe-inline'; style-src 'self' 'unsafe-inline'; media-src *"/>
<link href="css/gamepage.css" rel="external nofollow" rel="stylesheet"type="text/css"/>
<script src="js/jquery.js"type="text/javascript"></script>
<script src="js/gamepage.js"type="text/javascript"></script>
<title>开心连连算【#9】</title>
</head>
 
<body>
	<div id="tool" >
		<div id="allcandy"  ></div>
		<div id="allcandy_text"></div>
		<div id="sound"class="sound"onclick="pauseaudio()"></div>
		<div id="rank"onclick="showrank()"></div>
	</div>
	<div id="info_c"></div>
		<div id="info">
			<div id="block">
				<div id="block0"class="info_block_style">
					<div id="block0_text"></div>
				</div>
				<div id="block1"class="info_block_style">
					<div id="block1_img"class="info_img_style"></div>
					<div id="block1_text"></div>
				</div>
				<div id="block2"class="info_block_style">
					<div id="block2_img"class="info_img_style"></div>
					<div id="block2_text"></div>
				</div>			
				<div id="block3"class="info_block_style">
					<div id="block3_img"class="info_img_style"></div>
					<div id="block3_text"></div>
				</div>	
				<div id="block4"class="info_block_style">
					<div id="block4_img"class="info_img_style"></div>
					<div id="block4_text"></div>
				</div>
				<div id="info_close"onclick="closeinfo()"></div>
			</div>
		</div>
	<div id="rankpage">
		<div id="rank0"></div>
		<div id="rank1"></div>
		<div id="rank2"></div>
		<div id="rank3"></div>
		<div id="rankclose"onclick="closerank()"></div>
	</div>
	<div id="bg1">
		<button id="desktoptest" onclick="desktoptest()">使用浏览器调试点击这里</button>
		<div id="mainpage1">
			<div id="icon6"class="bo1"><div id="gamehead" ></div></div>
		</div>
		
		<div id="choosepage1">
			<div id="start"class="btn_font"onclick="initgame()"></div>
			<div id="help"class="btn_font"onclick="showinfo()"></div>
			<div id="language" class="btn_font">
				<select id="select1" class="select_language btn_font" name="select1" onchange="switchlanguage()">
					<option value="en-us">english</option>
					<option value="zh-cn">中文(简体)</option>
					<option value="zh-tw">中文(繁體)</option>
					<option value="ja-jp">日本語</option>
				</select>
			</div>
		</div>
		
	</div>
	<div id="bg2">
		<div id="mainpage">
			<div id="gamepage"data-role="content">
 
				<div id="ai"></div>
 
				<div id="qipan">
					<div id="qp_1">
						<div  id="1qp"class="qp"></div>
					</div>
					<div id="qp_2">
						<div  id="2qp"class="qp"></div>
					</div>
					<div id="qp_3">
						<div  id="3qp"class="qp"></div>
					</div>
					<div id="qp_4">
						<div  id="4qp"class="qp"></div>
					</div>
					<div id="qp_5">
						<div  id="5qp"class="qp"></div>
					</div>
					<div id="qp_6">
						<div  id="6qp"class="qp"></div>
					</div>
					<div id="qp_7">
						<div  id="7qp"class="qp"></div>
					</div>
					<div id="qp_8">
						<div  id="8qp"class="qp"></div>
					</div>
					<div id="qp_9">
						<div  id="9qp"class="qp"></div>
					</div>
					<div id="h1_1">
						<div id="1a"class="ha1" ></div>
					</div>
					<div id="h1_2">
						<div id="2a"class="ha1"></div>
					</div>
					<div id="h1_3">
						<div id="3a"class="ha1"></div>
					</div>
					<div id="h1_4">
						<div id="4a"class="ha1"></div>
					</div>
					<div id="h1_5">
						<div id="5a"class="ha1"></div>
					</div>
					<div id="h2_1" >
						<div id="1p"class="ha2"></div>
					</div>
					<div id="h2_2" >
						<div id="2p"class="ha2"></div>
					</div>
					<div id="h2_3" >
						<div id="3p"class="ha2"></div>
					</div>
					<div id="h2_4" >
						<div id="4p"class="ha2"></div>
					</div>
					<div id="h2_5" >
						<div id="5p"class="ha2"></div>
					</div>
				</div>
				<!--bug-->
				<div id='result'>
					
					<div id="candy"class="co1">0</div>
					<div id="trophy"class="co2">0</div>
					<div id="result_text"></div>
					<div id="result_close" onclick="closeresult()"></div>
					<div id="result_head"></div>
				</div>
				<div id='scorepanel'>
					<div id="scorepanel_c"></div>
					<div id="scorepanel_text"></div>
					<div id="player1_score">0</div>
					<div id="vs">vs</div>
					<div id="player2_score">0</div>
				</div>
				
				<div id="finger"></div>
				<div id="finger_frame"></div>
				<div id="nextstep" onclick="nextstep()"></div>
				<div id="nevershow" onclick="nevershow()"></div>
				<div id="tutorial"></div>
 
				<div id="destiny_panel"></div>
 
				<div id="destiny">
					<div id="destiny_text"></div>
				</div>
			</div>
			<div id="choosepage">
				<div id="restart" class="btn_font" onclick="initgame()"></div>
				<div id="comeback" class="btn_font" onclick="comeback()"></div>
			</div>
		</div>
	</div>
	<div id="tips">
		<div id="tips_c"></div>
		<div id="tips_icon"></div>
		<div id="tips_text"></div>
	</div>
	<div id="share_page">
		<div id="share_page_c"></div>
		<div id="share_friend" onclick="sharescore(1)"></div>
		<div id="share_friends" onclick="sharescore(2)"></div>
		<div id="share_text"></div>
	</div>
</body>
</html>

        css的代码如下,主要用于设置共同的样式属性和部分元素的移动、弹出、消失动画。

* { -webkit-tap-highlight-color: rgba(0, 0, 0, 0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */ }
body { -webkit-touch-callout: none;                /* prevent callout to copy image, etc when tap to hold */ -webkit-text-size-adjust: none;             /* prevent webkit from resizing text to fit */ -webkit-user-select: none;                  /* prevent copy paste, to allow, change 'none' to 'text' */ overflow: hidden; }
 
/*主页css--------------------------------------------*/
#icon6 { background-image: url(../img/icon6.png); position: absolute; top: 110px; left: 50%; margin-left: -100px; width: 201px; height: 130px; }
#gamehead { text-align: center; line-height: 130px; font-weight: bolder; font-size: 20px; color: #fff; text-shadow: 0px 0px 10px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#select1 { appearance: none; -webkit-appearance: none; background: no-repeat; border: none; }
 
/*主页游戏按钮*/
#choosepage1 { bottom: 250px; width: 143px; left: 50%; margin-left: -71px; position: absolute; }
#start { top: 0px; position: absolute; background-image: url(../img/bt_1.png); width: 143px; height: 55px; }
#help { top: 60px; position: absolute; background-image: url(../img/bt_2.png); width: 143px; height: 55px; }
#language { top: 120px; position: absolute; background-image: url(../img/bt_3.png); width: 143px; height: 55px; }
.btn_font { text-align: center; line-height: 55px; color: #ffffff; text-shadow: 0 0 5px #000; font-size: 15px; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
 
/*游戏背景*/
#bg1 { background-size: 100% 100%; position: absolute; top: 0px; left: 0px; background-image: url(../img/bg_2.png); width: 100%; height: 100%; z-index: 999; }
#bg2 { background-size: 100% 100%; position: absolute; top: 0px; left: 0px; background-image: url(../img/bg_2.png); width: 100%; height: 100%; }
 
/*游戏简介*/
#info { top: 0px; left: 0px; width: 100%; height: 100%; position: absolute; z-index: 1000; font-size: 13px; visibility: hidden; }
#info_c { top: 0px; left: 0px; width: 100%; height: 100%; position: absolute; z-index: 1000; background-color: #666; opacity: 0.9; visibility: hidden; }
#info_text { color: #fff; }
#info_close { background-image: url(../img/close.png); position: absolute; width: 50px; height: 50px; bottom: 0px; right: 0px; }
.info_block_style { color: #ffffff; text-shadow: 0px 0px 2px #000; font-size: 13px; font-weight: bold; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
.info_img_style { border-radius: 30px; }
.block_font { color: #fff; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#block { position: absolute; width: 300px; height: 420px; top: 30px; left: 50%; margin-left: -150px; text-align: center; }
#block0 { position: absolute; width: 300px; height: 40px; top: 0px; left: 50%; margin-left: -150px; font-size: 15px; text-align: center; }
#block1 { position: absolute; width: 300px; height: 40px; top: 50px; left: 50%; margin-left: -150px; text-align: left; }
#block2 { position: absolute; width: 300px; height: 40px; top: 140px; left: 50%; margin-left: -150px; text-align: left; }
#block3 { position: absolute; width: 300px; height: 40px; top: 230px; left: 50%; margin-left: -150px; text-align: left; }
#block4 { position: absolute; width: 300px; height: 40px; top: 320px; left: 50%; margin-left: -150px; text-align: left; }
#block1_img { position: absolute; top: 40px; width: 160px; height: 50px; right: 5px; background-image: url(../img/tips1.png); }
#block2_img { position: absolute; top: 40px; width: 160px; height: 50px; right: 5px; background-image: url(../img/tips2.png); }
#block3_img { position: absolute; top: 40px; width: 160px; height: 50px; right: 5px; background-image: url(../img/tips3.png); }
#block4_img { position: absolute; top: 40px; width: 160px; height: 50px; right: 5px; background-image: url(../img/tips4.png); }
 
/*游戏css----------------------------------------------------------*/
 
#gameheader { height: 50px; }
 
/*电脑样式*/
#ai { position: absolute; top: 0px; left: 0px; width: 60px; height: 91px; z-index: 996; }
 
/*提示样式*/
#tips { position: absolute; top: 230px; left: 0px; width: 100%; height: 69px; z-index: 1000; visibility: hidden; }
#tips_c { position: absolute; bottom: 0px; left: 0px; width: 100%; height: 69px; z-index: 996; background-color: #999; opacity: 0.6; }
#tips_icon { position: absolute; bottom: 0px; right: 0px; width: 69px; height: 69px; z-index: 1001; background-image: url(../img/tips_2.png); }
#tips_text { top: 20px; position: absolute; width: 250px; left: 50%; margin-left: -125px; height: 49px; text-align: center; color: #ffffff; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; z-index: 999; }
#destiny_panel { top: 0px; left: 0px; position: absolute; width: 100%; height: 100%; background-color: #000; opacity: 0.5; z-index: 995;visibility: hidden;}
#tutorial{ top: 0px; left: 0px; position: absolute; width: 100%; height: 100%; background-color: #000; opacity: 0.5; z-index: 995;visibility: hidden;}
/*先后手样式*/
#destiny { background-image: url(../img/coin_2.png); left: 50%; margin-left: -55px; position: absolute; width: 109px; height: 112px; top: 200px; color: #000; text-align: center; font-weight: bolder; line-height: 112px; z-index: 997; }
#destiny_text { color: #ffffff; text-shadow: 0 0 5px #000; font-size: 20px; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#finger{background-image: url(../img/finger.png);background-repeat:no-repeat;position: absolute;width: 200px;height: 110px;z-index: 996;font-size: 13px; color: #ffffff;font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;visibility: hidden;}
#finger_frame{z-index: 996;top: 180px; width: 300px; left: 50%; margin-left: -150px;position:absolute;width:300px;height: 130px;border:4px solid;border-radius:15px;font-size: 12px;color: #fff;font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;visibility: hidden;text-indent:20px;}
#nextstep{ bottom:70px;right:20px; position:absolute;width:100px;height: 40px;z-index: 996;font-size: 15px;text-align: center; color: #ffffff;background-color: #999;border-radius:10px;font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;}
#nevershow{bottom:70px;right:130px; position:absolute;width:100px;height: 40px;z-index: 996;font-size: 15px;text-align: center; color: #ffffff;background-color: #999;border-radius:10px;font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;}
/*状态栏样式*/
#tool { width: 100%; height: 35px; top: 0px; left: 0px; position: absolute; z-index: 1000; }
#scorepanel { position: absolute; top: 0px; left: 0px; width: 90px; height: 40px;left: 50%; margin-left: -45px; z-index: 997; }
#scorepanel_c { position: absolute; top: 0px; left: 0px; width: 90px; height: 40px;background-color: #999;border-radius:0px 0px 5px 5px;opacity: 0.6;}
#scorepanel_text{text-align: center; position: absolute; top: 0px; width: 90px; height: 12px; left: 50%; margin-left: -45px; color: #ffffff; font-size: 12px; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;}
#player1_score { text-align: center; position: absolute; bottom: 0px; width: 30px; height: 18px; left: 50%; margin-left: -45px; background-color: #6cf; color: #ffffff; font-size: 15px; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;border-radius:0px 0px 0px 5px;}
#vs            { text-align: center; position: absolute; bottom: 0px; width: 30px; height: 18px; left: 50%; margin-left: -15px; color: #ffffff; font-size: 15px;  text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#player2_score { text-align: center; position: absolute; bottom: 0px; width: 30px; height: 18px; left: 50%; margin-left: 15px; color: #ffffff; background-color: #f33; font-size: 15px; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;border-radius:0px 0px 5px 0px;}
#rank { background-image: url(../img/rank.png); position: absolute; width: 44px; height: 30px; top: 2px; right: 47px; }
#allcandy {line-height: 30px; background-repeat: no-repeat; background-image: url(../img/candy_mini.png); position: absolute; width: 80px; height: 30px; top: 2px; left: 0px;text-align: right; font-size: 15px; color: #fff; text-shadow: 0 0 5px #000;  font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;}
#allcandy_text { line-height: 30px; position: absolute; width: 60px; height: 30px; top: 2px; left: 80px; font-size: 15px; color: #fff; text-shadow: 0 0 5px #000;  font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;}
#winner { background-color: #00fa9a; color: #fff; font-weight: lighter; }
.sound { background-image: url(../img/sound.png); position: absolute; top: 2px; width: 30px; height: 30px; right: 10px; }
.sound_shutdown { background-image: url(../img/sound_shutdown.png); position: absolute; top: 2px; width: 30px; height: 30px; right: 10px; }
 
/*游戏结果*/
#result { background-repeat: no-repeat; background-image: url(../img/rankpage2.png); top: 90px; left: 50%; width: 184px; height: 390px; position: absolute; margin-left: -92px; z-index: 996; text-align: center; }
#result_head{background-color: #fff; position: absolute; right: 0px; width: 100px;height: 30px;color: #ff0000;  font-size: 20px;font-weight: bolder; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif;border:5px solid;border-radius:25px;}
#candy { position: absolute; width: 172px; height: 68px; top: 40px; left: 50px; line-height: 68px; color: #fff; text-indent: 50px; background-image: url(../img/rankp4.png); font-size: 15px; color: #ffffff; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#trophy { position: absolute; width: 172px; height: 68px; top: 110px; left: 50px; line-height: 68px; color: #fff; text-indent: 50px; background-image: url(../img/rankp5.png); font-size: 15px; color: #ffffff; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#result_text { position: absolute; top: 178px; margin-left: 10px; text-align: left; color: #ffffff; text-shadow: 0 0 5px #000; font-size: 15px; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#result_close { background-image: url(../img/close.png); position: absolute; width: 50px; height: 50px; bottom: 75px; right: 0px; }
#share_page { position: absolute; bottom: 70px; height: 70px; left: 50%; width: 120px; margin-left: -60px;z-index: 999;visibility: hidden; }
#share_page_c { background-color: #999; position: absolute; bottom: 0px; height: 70px; left: 0px; width: 120px;border-radius: 10px; opacity: 0.6;}
#share_friend { background-image: url(../img/friend.png); position: absolute; top: 20px; left: 5px; width: 50px; height: 50px; }
#share_friends { background-image: url(../img/friends.png); position: absolute; top: 20px; right: 5px; width: 50px; height: 50px; }
#share_text { position: absolute; top: 0px; left: 0px; width: 120px; height: 20px; text-align: center; color: #ffffff; text-shadow: 0 0 5px #000; font-size: 10px; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
 
/*游戏选项*/
#restart { position: absolute; top: 0px; left: 0px; width: 143px; background-image: url(../img/bt_1.png); height: 55px; }
#comeback { position: absolute; top: 60px; left: 0px; width: 143px; background-image: url(../img/bt_3.png); height: 55px; }
.comeback { color: #ffffff; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#choosepage { top: 230px; width: 143px; left: 50%; margin-left: -71px; position: absolute; text-align: center; height: 55px; z-index: 998; visibility: hidden; }
.choosepage_btn { position: absolute; right: 20px; line-height: 55px; font-size: large; text-decoration: none; }
 
/*游戏棋盘*/
#gamepage { color: #ffffff; visibility: hidden; }
#qipan { position: absolute; top: 50px; width: 300px; left: 50%; margin-left: -150px; text-align: center; line-height: 30px; height: 450px; }
.qp { position: absolute; width: 100px; height: 113px; }
.qp_w { position: absolute; width: 80px; height: 90px; background-image: url(../img/qp_w_mini.png); }
#qp_1 { background-image: url(../img/qp_mini.png); position: absolute; top: 50px; left: 10px; width: 80px; height: 90px; }
#qp_2 { background-image: url(../img/qp_mini.png); position: absolute; top: 50px; left: 110px; width: 80px; height: 90px; }
#qp_3 { background-image: url(../img/qp_mini.png); position: absolute; top: 50px; left: 210px; width: 80px; height: 90px; }
#qp_4 { background-image: url(../img/qp_mini.png); position: absolute; top: 163px; left: 10px; width: 80px; height: 90px; }
#qp_5 { background-image: url(../img/qp_mini.png); position: absolute; top: 163px; left: 110px; width: 80px; height: 90px; }
#qp_6 { background-image: url(../img/qp_mini.png); position: absolute; top: 163px; left: 210px; width: 80px; height: 90px; }
#qp_7 { background-image: url(../img/qp_mini.png); position: absolute; top: 276px; left: 10px; width: 80px; height: 90px; }
#qp_8 { background-image: url(../img/qp_mini.png); position: absolute; top: 276px; left: 110px; width: 80px; height: 90px; }
#qp_9 { background-image: url(../img/qp_mini.png); position: absolute; top: 276px; left: 210px; width: 80px; height: 90px; }
 
/*手牌样式*/
.ha1 { line-height: 50px; color: #ffffff; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; font-size: 28px;text-align: center;width: 70px; height: 62px;}
.ha2 { line-height: 50px; color: #ffffff; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; font-size: 28px;text-align: center;width: 70px; height: 62px;}
#h2_1 { background-image: url(../img/hand_p1.png); position: absolute; bottom: 15px; left: 5px; width: 70px; height: 62px; }
#h2_2 { background-image: url(../img/hand_p1.png); position: absolute; bottom: 15px; left: 60px; width: 70px; height: 62px; }
#h2_3 { background-image: url(../img/hand_p1.png); position: absolute; bottom: 15px; left: 115px; width: 70px; height: 62px; }
#h2_4 { background-image: url(../img/hand_p1.png); position: absolute; bottom: 15px; left: 170px; width: 70px; height: 62px; }
#h2_5 { background-image: url(../img/hand_p1.png); position: absolute; bottom: 15px; left: 225px; width: 70px; height: 62px; }
#h1_1 { background-image: url(../img/hand_a1.png); position: absolute; top: 0px; left: 5px; width: 70px; height: 62px; }
#h1_2 { background-image: url(../img/hand_a1.png); position: absolute; top: 0px; left: 60px; width: 70px; height: 62px; }
#h1_3 { background-image: url(../img/hand_a1.png); position: absolute; top: 0px; left: 115px; width: 70px; height: 62px; }
#h1_4 { background-image: url(../img/hand_a1.png); position: absolute; top: 0px; left: 170px; width: 70px; height: 62px; }
#h1_5 { background-image: url(../img/hand_a1.png); position: absolute; top: 0px; left: 225px; width: 70px; height: 62px; }
 
.hand_p1_class{background-image: url(../img/hand_p1.png); position: absolute;top:0px;left:0px;width: 70px; height: 62px;z-index: 996;}
.hand_a1_class{background-image: url(../img/hand_a1.png); position: absolute;top:0px;left:0px;width: 70px; height: 62px;z-index: 996;}
/*游戏排行榜*/
#rankpage { background-image: url(../img/rankpage2.png); top: 100px; left: 50%; width: 184px; height: 290px; position: absolute; margin-left: -92px; z-index: 1000; visibility: hidden; }
#rankclose { background-image: url(../img/close.png); position: absolute; width: 50px; height: 50px; bottom: 0px; right: 0px; }
#rank0{position: absolute; width: 100px; height: 68px; top: 5px; margin-left: -50px;left: 50%; text-align: center;font-size: 16px; color: #ffffff; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#rank1 { position: absolute; width: 172px; height: 68px; top: 45px; left: 50px; line-height: 68px; text-align: center; background-image: url(../img/rankp1.png); font-size: 16px; color: #ffffff; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#rank2 { position: absolute; width: 172px; height: 68px; top: 115px; left: 50px; line-height: 68px; text-align: center; background-image: url(../img/rankp2.png); font-size: 16px; color: #ffffff; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
#rank3 { position: absolute; width: 172px; height: 68px; top: 185px; left: 50px; line-height: 68px; text-align: center; background-image: url(../img/rankp3.png); font-size: 16px; color: #ffffff; text-shadow: 0 0 5px #000; font-family: "arial", "microsoft yahei", "黑体", "宋体", sans-serif; }
 
#desktoptest{position: absolute;left: 0px; top:50px;visibility: hidden;}
 
/*上下浮动动画*/
@keyframes updown {
	from { transform: translate(0px, 0px); }
	to { transform: translate(0px, -10px); }
}
@-webkit-keyframes updown {
	from { -webkit-transform: translate(0px, 0px); }
	to { -webkit-transform: translate(0px, -10px); }
}
.bo { animation: updown 0.5s linear 0s infinite alternate; -webkit-animation: updown 0.5s linear 0s infinite alternate; }
.bo1 { animation: updown 1s linear 0s infinite alternate; -webkit-animation: updown 1s linear 0s infinite alternate; }
 
/*抛硬币动画*/
@keyframes flipcoin {
	0% { transform: rotatey(0deg); }
	50% { transform: rotatey(1200deg); }
	100% { transform: rotatey(1800deg); }
}
@-webkit-keyframes flipcoin {
	0% { -webkit-transform: rotatey(0deg); }
	50% { -webkit-transform: rotatey(1200deg); }
	100% { -webkit-transform: rotatey(1800deg); }
}
.flip { animation: flipcoin 1.5s linear 0s forwards; -webkit-animation: flipcoin 1.5s linear 0s forwards; }
 
/*滑动出现动画*/
@keyframes translatel {
	from { transform: translate(0px, 0px); }
	to { transform: translate(-44px, 0px); }
}
@-webkit-keyframes translatel {
	from { -webkit-transform: translate(0px, 0px); }
	to { -webkit-transform: translate(-44px, 0px); }
}
.co1 { animation: translatel 0.5s linear 0s  forwards; -webkit-animation: translatel 0.5s linear 0s  forwards; }
.co2 { animation: translatel 0.5s linear 0.5s  forwards; -webkit-animation: translatel 1s linear 0s  forwards; }
.co3 { animation: translatel 0.5s linear 1s  forwards; -webkit-animation: translatel 1.5s linear 0s  forwards; }
 
/*win,lose动画*/
@keyframes zoom {
	from { transform: scale(3, 2) rotate(0deg);}
	to { transform: scale(1, 1) rotate(20deg); }
}
@-webkit-keyframes zoom {
	from { -webkit-transform: scale(3, 2) rotate(0deg); 
	 }
	to { -webkit-transform: scale(1, 1) rotate(20deg); 
	 }
}
#result_head { animation: zoom 0.5s linear 0s forwards; -webkit-animation: zoom 0.5s linear 0s forwards; }
.pageturn { animation: zoom 0.5s linear 0s forwards; -webkit-animation: zoom 0.5s linear 0s forwards; }
 
/*交换动画*/
@keyframes swing {
	from { transform: translate(-5px, 0px); }
	to { transform: translate(5px, 0px); }
}
@-webkit-keyframes swing {
	from { -webkit-transform: translate(-5px, 0px); }
	to { -webkit-transform: translate(5px, 0px); }
}
.toswing { animation: swing 0.1s linear 0s 8 alternate forwards; -webkit-animation: swing 0.1s linear 0s 8 alternate forwards; }
 
@keyframes touchtomove {
	from { transform: translate(0px, 0px); }
	to { transform: translate(50px, -100px); }
}
@-webkit-keyframes touchtomove {
	from { -webkit-transform: translate(0px, 0px); }
	to { -webkit-transform: translate(50px, -100px); }
}
.touchmove { animation: touchtomove 2s linear 0s infinite ; -webkit-animation: touchtomove 2s linear 0s infinite ; }

(三)游戏响应开发

        1.通过javascript.addeventlistener()div棋子元素添加选中、拖动、放入,触发对应mousedown、mousemove、mouseup事件的响应函数。

        为div元素添加事件响应函数,代码如下:

document.getelementbyid("h2_" + i).addeventlistener('mouseup', outmouse);
document.getelementbyid("h2_" + i).addeventlistener('mousemove', movemouse);
document.getelementbyid("h2_" + i).addeventlistener('mousedown', selectmouse);

        为div元素实现拖放效果,代码如下:

//玩家棋子拖拽移动回调函数
function movemouse(e) {
	
	if (isdrag) {
		var n = thx + e.pagex- hx;
		var m = thy + e.pagey - hy;
		$("#h2_" + parseint(e.target.id)).css({
			"left": n,
			"top": m
		});
 
		hx1 = e.pagex;
		hy1 = e.pagey;
		return false;
	}
}

        2.使用audio元素,为游戏添加音效,载入游戏音频文件、播放音频代码如下:

btn_media = document.createelement("audio");
btn_media.src = "media/btn09.mp3";
btn_media.play()

        3.使用二维数组存放双方棋子,用于判断双方下棋后剩余棋子的情况,a[][0]代表手牌数值,a[][1]代表手牌状态,主要代码如下:

//玩家与电脑的手牌,a[][0]代表手牌数值,a[][1]代表手牌状态
var aihand = new array([5][2]);
var playerhand = new array([5][2]);
for (var k = 0; k < 5; k++) {
	aihand[k] = new array();
	for (var j = 0; j < 2; j++) {
		aihand[k][j] = "";
	}
}
for (var k = 0; k < 5; k++) {
	playerhand[k] = new array();
	for (var j = 0; j < 2; j++) {
		playerhand[k][j] = "";
	}
}

        4.使用二维数组存放棋局,用于判断实时得分以及控制ai下棋,主要代码如下:

//记录手牌的棋盘
var gamechessboardforhand = new array([3][3]);
 
for (var k = 0; k < 3; k++) {
	gamechessboardforhand[k] = new array();
	for (var j = 0; j < 3; j++) {
		gamechessboardforhand[k][j] = 0;
	}
}

        5.编写简单ai算法,自动下棋,目前原理只是有空位就放。

// ai思考,返回ai的操作airequest
function discard() {
	var airequest = new array();
 
	//复制棋盘
	var clonechessboard = new array([3][3]);
 
	for (var k = 0; k < 3; k++) {
		clonechessboard[k] = new array();
		for (var j = 0; j < 3; j++) {
			clonechessboard[k][j] = 0;
		}
	}
 
	for (var k = 0; k < 3; k++) {
		for (var j = 0; j < 3; j++) {
			clonechessboard[k][j] = parseint(gamechessboard[k][j]);
		}
	}
 
	airequest[0] = -1;
	airequest[1] = -1;
	airequest[2] = -1;
 
	//模拟玩家走一步
	var minigamestate = 9;
	for (var k = 0; k < 5; k++) {
		//如果玩家手牌可以出
		if (aihand[k][1] > 0) {
			for (var i = 0; i < 3; i++) {
 
				for (var j = 0; j < 3; j++) {
					if (clonechessboard[i][j] == 0) {
						clonechessboard[i][j] = aihand[k][0] + 1000;
						var gamestate = judgegamecong(clonechessboard);
						//console.log(gamestate);
						if (gamestate[0] % 10 < gamestate[2] % 10) {
							airequest[0] = i;
							airequest[1] = j;
							if ((gamestate[2] % 10) <= minigamestate && (gamestate[2] % 10) != -1) {
								minigamestate = gamestate[2] % 10;
								airequest[2] = k;
							}
							//airequest[2]=k; bug1用了玩家的手牌位置来出
						}
						clonechessboard[i][j] = 0;
 
					}
				}
			}
		}
	}
 
	var enablechessboard = new array();
	//没有找到解则
	if (airequest[0] == -1) {
		for (var i = 0; i < 3; i++) {
			for (var j = 0; j < 3; j++) {
				if (clonechessboard[i][j] == 0) {
					enablechessboard.push(i * 3 + j);
					//airequest[0] = i;
					//airequest[1] = j;
				}
			}
		}
		var randomnum = getrandom(enablechessboard.length - 1);
		var i = math.floor(enablechessboard[randomnum] / 3);
		var j = enablechessboard[randomnum] % 3;
		console.log(randomnum, i, j, randomnum, enablechessboard.length);
		airequest[0] = i;
		airequest[1] = j;
 
		for (var i = 0; i < 5; i++) {
			if (aihand[i][1] > 0) {
				airequest[2] = i;
			}
		}
	}
 
	return airequest;
}

        6.判断输赢,把结果存入全局变量gamestate中,主要代码如下:

// 判断游戏胜负
function judgegamecong(chessboard) {
 
	var judgearray = new array([8][3]);
 
	//gamestate[0-3]分别代表电脑得分、电脑三连子位置、玩家得分、玩家三连子位置
	var gamestate = new array(-1, -1, -1, -1);
	//将棋盘横竖、对角线8个数组存入judgearray
	for (var g = 0; g < 3; g++) {
		judgearray[g] = new array();
		for (var j = 0; j < 3; j++) {
			judgearray[g][j] = chessboard[g][j];
		}
	}
	for (var g = 3, k = 0; g < 6; g++, k++) {
		judgearray[g] = new array();
		for (var j = 0; j < 3; j++) {
			judgearray[g][j] = chessboard[j][k];
		}
	}
 
	judgearray[6] = new array();
	judgearray[6][0] = chessboard[0][0];
	judgearray[6][1] = chessboard[1][1];
	judgearray[6][2] = chessboard[2][2];
 
	judgearray[7] = new array();
	judgearray[7][0] = chessboard[0][2];
	judgearray[7][1] = chessboard[1][1];
	judgearray[7][2] = chessboard[2][0];
 
	//遍历judgearray,并将最高分与数组位置存入gamestate,其中gamestate[0-3]分别代表电脑得分、电脑三连子位置、玩家得分、玩家三连子位置
	for (var i = 0; i < 8; i++) {
 
		var x = parseint(judgearray[i][0] / 1000);
		var y = parseint(judgearray[i][1] / 1000);
		var z = parseint(judgearray[i][2] / 1000);
		var a = parseint(judgearray[i][0]) + parseint(judgearray[i][1]) + parseint(judgearray[i][2]);
 
		if ((x == y) && (x == z) && (x != 0)) {
			if (x == 1) {
				if ((a % 10) >= (gamestate[0] % 10)) {
					gamestate[0] = a;
					gamestate[1] = i;
				}
			} else {
				if ((a % 10) >= (gamestate[2] % 10)) {
					gamestate[2] = a;
					gamestate[3] = i;
				}
			}
		}
 
	}
	// console.log(gamestate);
	return gamestate;
 
}

到此这篇关于使用javascript做一款无框架浏览器直接运行的益智类数字棋牌小游戏的文章就介绍到这了,更多相关javascript益智类数字棋牌小游戏内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com