forked from APCSLowell/Dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dice.pde
62 lines (56 loc) · 1.01 KB
/
Dice.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
void setup() {
size(300, 300);
noLoop();
}
void draw() {
background(255);
int total = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
Die die = new Die(50 + i * 60, 50 + j * 60);
die.roll();
die.show();
total += die.value;
}
}
fill(0);
textSize(24);
text("Total: " + total, 100, 250);
}
void mousePressed() {
redraw();
}
class Die {
int x, y;
int size;
int value;
Die(int x, int y) {
this.x = x;
this.y = y;
this.size = 50;
roll();
}
void roll() {
value = (int)(Math.random() * 6) + 1;
}
void show() {
fill(255);
rect(x, y, size, size);
fill(0);
if (value == 1 || value == 3 || value == 5) {
ellipse(x + size/2, y + size/2, 10, 10);
}
if (value >= 2) {
ellipse(x + size/4, y + size/4, 10, 10);
ellipse(x + 3*size/4, y + 3*size/4, 10, 10);
}
if (value >= 4) {
ellipse(x + 3*size/4, y + size/4, 10, 10);
ellipse(x + size/4, y + 3*size/4, 10, 10);
}
if (value == 6) {
ellipse(x + size/4, y + size/2, 10, 10);
ellipse(x + 3*size/4, y + size/2, 10, 10);
}
}
}