游戏一:dice

.html file

<!doctype html>
<html>
	<head>
		
		<script type = "text/javascript" src = "dice.js"></script>
		
		<title>The Dice Game</title>
		
		
	</head>
	<body>
		<button type = "button" onclick = "Init()">Click Me Now</button>
		<canvas id = "canvas" width = "400" height = "300">
			Your browser doesn't support the HTML5 element canvas.
		</canvas>
		
		<form name = "f" onSubmit = "throwdice();">
			Stage : <input name = "stage" value = "First Throw" />
			Point : <input name = "pv" value = "   " />
			Outcome : <input name = "outcome" value = "    " />
			<input type = "submit" value = "Throw Dice" />
		</form><br />
		
	</body>
</html>


.js file

var cwidth = 400;
var cheight = 300;
var dicex = 50;
var dicey = 50;
var dicewidth = 100;
var diceheight = 100;
var dotrad = 6;  //点的半径
var ctx;
var firstturn = true;
var point;


function Draw1()
{
	var dotx;	//点的 X
	var doty;	//点的 Y
	ctx.beginPath();
	dotx = dicex + 0.5 * dicewidth;
	doty = dicey + 0.5 * diceheight;
	ctx.arc(dotx, doty, dotrad, 0, Math.PI*2, true);
	ctx.closePath();
	ctx.fill();
	
}

function Draw2()
{
	var dotx;	//点的 X
	var doty;	//点的 Y
	ctx.beginPath();
	dotx = dicex + 3 * dotrad;
	doty = dicey + 3 * dotrad;
	ctx.arc(dotx, doty, dotrad, 0, Math.PI*2, true);
	dotx = dicex + dicewidth - 3 * dotrad;
	doty = dicey + diceheight - 3 * dotrad;
	ctx.arc(dotx, doty, dotrad, 0, Math.PI*2, true);
	ctx.closePath();
	ctx.fill();
}

function Draw4()
{
	var dotx;	//点的 X
	var doty;	//点的 Y
	ctx.beginPath();
	dotx = dicex + 3 * dotrad;
	doty = dicey + 3 * dotrad;
	ctx.arc(dotx, doty, dotrad, 0, Math.PI*2, true);
	dotx = dicex + dicewidth - 3 * dotrad;
	doty = dicey + diceheight - 3 * dotrad;
	ctx.arc(dotx, doty, dotrad, 0, Math.PI*2, true);
	dotx = dicex + 3 * dotrad;
	doty = dicey + diceheight - 3 * dotrad;
	ctx.arc(dotx, doty, dotrad, 0, Math.PI*2, true);
	dotx = dicex + dicewidth - 3 * dotrad;
	doty = dicey + 3 * dotrad;
	ctx.arc(dotx, doty, dotrad, 0, Math.PI*2, true);
	ctx.closePath();
	ctx.fill();
}

function Draw2mid()
{
	var dotx;
	var doty;
	ctx.beginPath();
	dotx = dicex + 3 * dotrad;
	doty = dicey + 0.5 * diceheight;
	ctx.arc(dotx, doty, dotrad, 0, Math.PI*2, true);
	dotx = dicex + dicewidth - 3*dotrad;
	doty = dicey + 0.5 * diceheight;
	ctx.arc(dotx, doty, dotrad, 0, Math.PI*2, true);
	ctx.closePath();
	ctx.fill();
}



//画图
function Drawface( n )
{
	
	ctx = document.getElementById("canvas").getContext("2d");
	ctx.lineWidth = 5;
	ctx.clearRect(dicex, dicey, dicewidth, diceheight);
	ctx.strokeRect(dicex, dicey, dicewidth, diceheight);
	ctx.fillStyle = "#009966";
	switch(n){
		case 1:
			Draw1();
			break;
		case 2:
			Draw2();
			break;
		case 3:
			Draw1();
			Draw2();
			break;
		case 4:
			Draw4();
			break;
		case 5:
			Draw4();
			Draw1();
			break;
		case 6:
			Draw4();
			Draw2mid();
			break;
	}
}


//初始化
function Init()
{
	var ch1,ch2;
	var sum;
	ch1 = 1 + Math.floor(Math.random()*6);
	sum = ch1;
	dicex = 50;
	Drawface(ch1);
	
	dicex = 200;
	ch2 = 1 + Math.floor(Math.random()*6);
	sum = ch1 + ch2;
	if(firstturn){
		switch(sum){
			case 7:
			case 11:
				document.f.outcome.value = "You win!";
				break;
			case 2:
			case 3:
			case 12:
				document.f.outcome.value = "You lose!";
				break;
			default:
				point = sum;
				document.f.pv.value = point;
				firstturn = false;
				document.f.state.value = "Need follow-up throw.";
				document.outcome.value = "   ";
		}
	}else{
		switch(sum){
			case point:
				document.f.outcome.value = "You win!";
				document.f.stage.value = "Back to first thorw.";
				document.f.pv.value = "   ";
				firstturn = true;
				break;
			case 7:
				document.f.outcome.value = "You lose!";
				document.f.stage.value = "Back to first throw.";
				document.f.pv.value = "   ";
				firstturn = true;
		}
	}
	Drawface(ch2);
	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值