-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcheckers.pde
151 lines (131 loc) · 4.58 KB
/
checkers.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// generates a chess/checkers board with alternating colors based on mouse position
// for storing Java shapes in lists
import java.awt.geom.Point2D;
import java.awt.Rectangle;
// grid to display on-screen
int rows = 8;
int columns = 8;
// size of screen - must be specified in size command (w x h)
// colors for the grid squares
int whiteColorR = 255;
int whiteColorG = 250;
int whiteColorB = 237;
int blackColorR = 108;
int blackColorG = 78;
int blackColorB = 0;
// background color
color backgroundColor = color(35);
// misc. (frame rate, anti-aliasing)
int frame_rate = 45;
boolean anti_aliasing = true;
// preset variables
float cellH = h / rows;
float cellW = w / columns;
float cellAve = (cellH + cellW) / 2;
int cells = rows * columns / 2;
boolean light = true;
color whiteColor = color(whiteColorR, whiteColorG, whiteColorB);
color blackColor = color(blackColorR, blackColorG, blackColorB);
int whiteSquaresPointer = 0;
Point2D whiteSquares[] = new Point2D[cells];
Rectangle whiteSquares2[] = new Rectangle[cells];
int whiteSquareColors[][] = new int[cells][3];
int blackSquaresPointer = 0;
Point2D blackSquares[] = new Point2D[cells];
Rectangle blackSquares2[] = new Rectangle[cells];
int blackSquareColors[][] = new int[cells][3];
void setup(){
background(backgroundColor);
size(300, 300);
if(anti_aliasing)
smooth();
frameRate(frame_rate);
// create the grid of squares
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
// current x and y position
float x = (float) j * cellW;
float y = (float) i * cellH;
// operations to create each type of square
// store data for later (position, color)
if(light){
int pp = whiteSquaresPointer;
whiteSquares[pp] = new Point2D.Float(x, y);
whiteSquares2[pp] = new Rectangle((int) x, (int) y, (int) cellW, (int) cellH);
whiteSquareColors[pp] = new int[] {whiteColorR, whiteColorG, whiteColorB};
fill(whiteColor);
whiteSquaresPointer++;
}
else{
int pp = blackSquaresPointer;
blackSquares[pp] = new Point2D.Float(x, y);
blackSquares2[pp] = new Rectangle((int) x, (int) y, (int) cellW, (int) cellH);
blackSquareColors[pp] = new int[] {blackColorR, blackColorG, blackColorB};
fill(blackColor);
blackSquaresPointer++;
}
// draw square
rect(x, y, cellW, cellH);
// alternate colors between columns
light = !light;
}
// alternate colors between rows
light = !light;
}
}
void draw(){
// color every white square
for(int i = 0; i < whiteSquaresPointer; i++){
whiteColorR = whiteSquareColors[i][0];
whiteColorG = whiteSquareColors[i][1];
whiteColorB = whiteSquareColors[i][2];
// if hovering over, make square darker
if(whiteSquares2[i].contains(mouseX, mouseY)){
if(whiteColorR > blackColorR)
whiteColorR -= 10;
if(whiteColorG > blackColorG)
whiteColorG -= 10;
if(whiteColorB > blackColorB)
whiteColorB -= 10;
// otherwise, return square to original color
}else{
if(whiteColorR < red(whiteColor))
whiteColorR += 2;
if(whiteColorG < green(whiteColor))
whiteColorG += 2;
if(whiteColorB < blue(whiteColor))
whiteColorB += 2;
}
// save the data and paint the color on the square
whiteSquareColors[i] = new int[] {whiteColorR, whiteColorG, whiteColorB};
fill(whiteColorR, whiteColorG, whiteColorB);
rect((float) whiteSquares[i].getX(), (float) whiteSquares[i].getY(), cellW, cellH);
}
// color every black square
for(int i = 0; i < blackSquaresPointer; i++){
blackColorR = blackSquareColors[i][0];
blackColorG = blackSquareColors[i][1];
blackColorB = blackSquareColors[i][2];
// if hovering over, make square lighter
if(blackSquares2[i].contains(mouseX, mouseY)){
if(blackColorR < whiteColorR)
blackColorR += 10;
if(blackColorG < whiteColorG)
blackColorG += 10;
if(blackColorB < whiteColorB)
blackColorB += 10;
// otherwise, return square to original color
}else{
if(blackColorR > red(blackColor))
blackColorR -= 2;
if(blackColorG > green(blackColor))
blackColorG -= 2;
if(blackColorB > blue(blackColor))
blackColorB -= 2;
}
// save the data and paint the color on the square
blackSquareColors[i] = new int[] {blackColorR, blackColorG, blackColorB};
fill(blackColorR, blackColorG, blackColorB);
rect((float) blackSquares[i].getX(), (float) blackSquares[i].getY(), cellW, cellH);
}
}