-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_demo.js
More file actions
119 lines (98 loc) · 2.45 KB
/
coin_demo.js
File metadata and controls
119 lines (98 loc) · 2.45 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const coinArray = [];
let numCoins = 8;
const w = 80;
const gap = 20;
let cost = 0;
let selected = false;
let coinSelected = -1;
let resetButton;
let moreButton;
let lessButton;
function setup() {
createCanvas(windowWidth, windowHeight);
const start = width/2-(numCoins-1)*(w + gap)/2;
for(i = 0; i < numCoins; i++) {
coinArray[i] = new Coin(i+1, start+i*(w+gap), height/2, w);
}
resetButton = createButton('Reset');
resetButton.position(30, height-100);
resetButton.mousePressed(reset);
moreButton = createButton('More Coins');
moreButton.position(200, height-100);
moreButton.mousePressed(more);
lessButton = createButton('Fewer Coins');
lessButton.position(100, height-100);
lessButton.mousePressed(less);
}
function draw() {
background('white');
textAlign(LEFT, TOP);
textSize(64);
text('Cost: $' + cost, 50, 50);
textSize(32);
for(i = 0; i < numCoins; i++) {
coinArray[i].update();
}
}
function more() {
numCoins += 1;
reset();
}
function less() {
if(numCoins > 1) {
numCoins -= 1;
}
reset();
}
function reset() {
cost = 0;
selected = false;
setup();
}
function clearInfo() {
selected = false;
for(i = 0; i < numCoins; i++) {
coinArray[i].clearCost();
coinArray[i].noHighlight();
}
}
function mouseClicked() {
let clickedOnCoin = false;
for(i = 0; i < numCoins; i++) {
if(coinArray[i].isMouseClose()) {
clickedOnCoin = true;
if (selected) {
if(coinArray[i].showCost) {
selected = false;
cost += coinArray[i].cost;
temp = coinArray[coinSelected].num;
coinArray[coinSelected].setNum(coinArray[i].num);
coinArray[i].setNum(temp);
for(i = 0; i < numCoins; i++) {
coinArray[i].clearCost();
coinArray[i].noHighlight();
}
}
} else {
selected = true;
coinSelected = i;
coinArray[i].highlight();
if (i+1 < numCoins) {
coinArray[i+1].setCost(1);
}
if (i-1 >= 0) {
coinArray[i-1].setCost(1);
}
if (i+4 < numCoins) {
coinArray[i+4].setCost(0);
}
if (i-4 >= 0) {
coinArray[i-4].setCost(0);
}
}
}
}
if(!clickedOnCoin) {
clearInfo();
}
}