Skip to content

Commit 57eba1f

Browse files
committed
8.21
1 parent ecc2ced commit 57eba1f

7 files changed

Lines changed: 116 additions & 0 deletions
25.6 MB
Binary file not shown.
File renamed without changes.
49.3 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.

‎骰子随机点数/index1.html‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>骰子游戏</title>
6+
<script src="js/main.js"></script>
7+
</head>
8+
<body onload="init()">
9+
<canvas id="canvas" width="400" height="300" style="border: 1px solid #000;">
10+
你的浏览器不支持
11+
</canvas>
12+
</body>
13+
</html>

‎骰子随机点数/js/main.js‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* Created by NewRed on 2017/8/21.
3+
*/
4+
var cwidth=400;//保存画布的宽度
5+
var cheight=300;//保存画布的高度
6+
var dicex=50;
7+
var dicey=50;//保存骰子的水平与垂直高度
8+
var dicewidth=100;
9+
var diceheight=100;//保存骰子的高度与宽度
10+
var dotrad=6;//保存原点的半径
11+
var ctx;//保存画布上下文变量
12+
function init() {
13+
var ch=1+Math.floor(Math.random()*6);//产生一个1~6之间的随机数
14+
drawface(ch);
15+
}
16+
function drawface(n){
17+
ctx=document.getElementById("canvas").getContext('2d');
18+
ctx.lineWidth=5;
19+
ctx.clearRect(dicex,dicey,dicewidth,diceheight);//清楚上次骰子的空间
20+
ctx.strokeRect(dicex,dicey,dicewidth,diceheight);//画出骰子面的轮廓
21+
ctx.fillStyle="#009966";//设置圆点的颜色
22+
switch(n){
23+
case 1:
24+
draw1();
25+
break;
26+
case 2:
27+
draw2();
28+
break;
29+
case 3:
30+
draw2();
31+
draw1();
32+
break;
33+
case 4:
34+
draw4();
35+
break;
36+
case 5:
37+
draw4();
38+
draw1();
39+
break;
40+
case 6:
41+
draw4();
42+
draw2mid();
43+
break;
44+
}
45+
}
46+
function draw1() {
47+
var dotx;
48+
var doty;
49+
ctx.beginPath();
50+
dotx=dicex+.5*dicewidth;
51+
doty=dicey+.5*diceheight;
52+
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
53+
ctx.closePath();
54+
ctx.fill();
55+
}
56+
function draw2() {
57+
var dotx;
58+
var doty;
59+
ctx.beginPath();
60+
dotx=dicex+3*dotrad;
61+
doty=dicey+3*dotrad;
62+
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
63+
dotx=dicex+dicewidth-3*dotrad;
64+
doty=dicey+diceheight-3*dotrad;
65+
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
66+
ctx.closePath();
67+
ctx.fill();
68+
}
69+
function draw4() {
70+
var dotx;
71+
var doty;
72+
ctx.beginPath();
73+
dotx=dicex+3*dotrad;
74+
doty=dicey+3*dotrad;
75+
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
76+
dotx=dicex+dicewidth-3*dotrad;
77+
doty=dicey+diceheight-3*dotrad;
78+
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
79+
ctx.closePath();
80+
ctx.fill();
81+
ctx.beginPath();
82+
dotx=dicex+3*dotrad;
83+
doty=dicey+diceheight-3*dotrad;
84+
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
85+
dotx=dicex+dicewidth-3*dotrad;
86+
doty=dicey+3*dotrad;
87+
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
88+
ctx.closePath();
89+
ctx.fill();
90+
}
91+
function draw2mid() {
92+
var dotx;
93+
var doty;
94+
ctx.beginPath();
95+
dotx=dicex+3*dotrad;
96+
doty=dicey+.5*diceheight;
97+
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
98+
dotx=dicex+dicewidth-3*dotrad;
99+
doty=dicey+.5*diceheight;
100+
ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
101+
ctx.closePath();
102+
ctx.fill();
103+
}

0 commit comments

Comments
 (0)