1. [代码][HTML]代码
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Craps game</title>
<script>
var cwidth =400;
var cheight = 300;
var dicex = 50;
var dicey = 50;
var dicewidth = 100;
var diceheight = 100;
var dotard = 6;
var ctx;
var dx;
var dy;
var firstturn = true;
var point;
function throwdice(){
var sum;
var ch = 1+ Math.floor(Math.random()*6);
sum = ch;
dx = dicex;
dy = dicey;
drawface(ch);
dx = dicex +150;
ch = 1+Math.floor(Math.random()*6);
sum +=ch;
drawface(ch);
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.stage.value = "Need follow-up throw.";
document.f.outcome.value=" ";
}
}
else{
switch(sum){
case point:
document.f.outcome.value = "You win!";
document.f.stage.value = "Back to first throw.";
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;
}
}
}
function drawface(n){
ctx = document.getElementById(‘canvas‘).getContext(‘2d‘);
ctx.lineWidth = 5;
ctx.clearRect(dx,dy,dicewidth,diceheight);
ctx.strokeRect(dx,dy,dicewidth,diceheight);
var dotx;
var doty;
ctx.fillStyle = "#009966";
switch(n){
case 1:
draw1();
break;
case 2:
draw2();
break;
case 3:
draw2();
draw1();
break;
case 4:
draw4();
break;
case 5:
draw4();
draw1();
break;
case 6:
draw4();
draw2mid();
break;
}
}
function draw1(){
var dotx;
var doty;
ctx.beginPath();
dotx = dx + .5*dicewidth;
doty = dy + .5*diceheight;
ctx.arc(dotx,doty,dotard,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}
function draw2(){
var dotx;
var doty;
ctx.beginPath();
dotx = dx + 3*dotard;
doty = dy + 3*dotard;
ctx.arc(dotx,doty,dotard,0,Math.PI*2,true);
dotx = dx + dicewidth-3*dotard
doty = dy + diceheight-3*dotard;
ctx.arc(dotx,doty,dotard,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}
function draw4(){
var dotx;
var doty;
ctx.beginPath();
dotx = dx + 3*dotard;
doty = dy + 3*dotard;
ctx.arc(dotx,doty,dotard,0,Math.PI*2,true);
dotx = dx + dicewidth-3*dotard
doty = dy + diceheight-3*dotard;
ctx.arc(dotx,doty,dotard,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
ctx.beginPath();
dotx = dx + 3*dotard;
doty = dy + diceheight-3*dotard;
ctx.arc(dotx,doty,dotard,0,Math.PI*2,true);
dotx = dx + dicewidth-3*dotard
doty = dy + 3*dotard;
ctx.arc(dotx,doty,dotard,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}
function draw2mid(){
var dotx;
var doty;
ctx.beginPath();
dotx = dx + 3*dotard;
doty = dy + .5*deiceheight;
ctx.arc(dotx,doty,dotard,0,Math.PI*2,true);
dotx = dx + dicewidth-3*dotard;
doty = dy + .5*deiceheight;
ctx.arc(dotx,doty,dotard,0,Math.PI*2,true);
ctx.closePath();
ctx.fill();
}
</script>
</head>
<body>
<canvas id="canvas" width="400" height="300">
浏览器不支持HTML5的canvas元素</canvas>
<br />http://www.huiyi8.com/donghua/?
<button onClick="throwdice();">Throw dice</button>
<form name="f">flash
Stage:<input name="stage" value="First Throw"/>
Point:<input name="pv" value=" "/>
Outcome:<input name="outcome" value=" "/>
</form>
</body>
</html>
相关文章
- 10-03给倒水小游戏写的暴力破解exe
- 10-03使用 HTML5 Canvas 标签的贪吃蛇游戏
- 10-03为什么不用C++写游戏(聪明的程序员不用C++折磨自己)(这些工作,QT都替开发者解决了,C++没有根类导致太多的问题,也没有字符串类)
- 10-03开源一个golang写的游戏服务器框架
- 10-03C语言从入门到入土(入门篇)如何让朋友也能玩自己写的游戏(.exe文件如何附带静态链接库)
- 10-03python学习笔记 ——python写的猜数字游戏 002
- 10-03html5录音支持pc和Android、ios部分浏览器,微信也是支持的,JavaScript getUserMedia
- 10-03用Dart写的黑白棋游戏
- 10-03css+javascript 写的HTML5 微信端输入支付密码键盘
- 10-03C语言写的一个贪吃蛇小游戏(windows系统)