-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchingGame.pde
389 lines (350 loc) · 8.99 KB
/
matchingGame.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
Matching Game
Made for Space Apps 2019
Made by: Alissa, Donovan, Fahad, and Lex
*/
final int xamt = 8;
final int yamt = 4;
Card[][] deck = new Card[xamt][yamt];
Card clicked = null;
Card curClicked = null;
int timesClicked = 0;
boolean haveFound = false;
boolean haveWon = false;
PImage img;
int backg = 17;
int score = 0;
int p1score = 0;
int p2score = 0;
int highscore = 1000;
String player1 = "";
String player2 = "";
int players = -1;
boolean isPlayer1Done = false;
boolean isStarting = true;
boolean shownRules = false;
int curPlayer = 1;
int roundsPlayed = 1;
void setup(){
fullScreen();
textAlign(CENTER);
fill(127);
// make all the cards
for(int i = 0; i < xamt; i++){
for(int j = 0; j < yamt; j++){
deck[i][j] = new Card(i * (width / xamt), j * (height / yamt));
}
}
// assign images to cards
shuffle();
}
void draw(){
// screen for selecting how many players
if (players == -1){
background(127);
strokeWeight(3.5);
line(width/2,0,width/2,height);
fill(0);
textSize(72);
text("1 Player", width/4, height/2);
text("2 Players", 3 * width/4, height/2);
textSize(36);
text("(click here)", width/4, height/2 + 75);
text("(click here)", 3 * width/4, height/2 + 75);
return;
}
// main display
if (!isStarting){
if (shownRules){
// load background image
PImage back = loadImage("backimages/img" + backg + ".jpg");
back.resize(width,height);
background(back);
// shows all the cards
strokeWeight(1);
for(int i = 0; i < xamt; i++){
for(int j = 0; j < yamt; j++){
deck[i][j].show(xamt,yamt);
}
}
if (timesClicked == 0 && players == 2 && !haveWon){
textSize(72);
fill(255);
if (curPlayer == 1){
text(player1, width/2, height/2);
}
else {
text(player2, width/2, height/2);
}
}
} else {
showRules();
}
}
else if (!isPlayer1Done){ // entering player 1 name
background(127);
fill(255);
textSize(72);
text("Player 1, enter your name:", width/2, height/2 - 25);
text(player1 + "_", width/2, height/2 + 50);
}
else { // entering player 2 name
background(127);
fill(255);
textSize(72);
text("Player 2, enter your name:", width/2, height/2 - 25);
text(player2 + "_", width/2, height/2 + 50);
}
if(haveWon){ // load end screen for correct version
if (players == 1){
onePlayerEnd();
}
if (players == 2){
twoPlayerEnd();
}
text("Press SPACE to restart", width/2, height - 75);
textSize(32);
text("Press < or > to change the image", width/2, height - 135);
}
}
void keyPressed(){
// getting player names
if (isStarting){
if (!isPlayer1Done){
player1 = getName(player1);
}
else{
player2 = getName(player2);
}
}
else if (key == 119){ // w (cheat to win)
haveWon = true;
if (score < highscore){
highscore = score;
}
for(int i = 0; i < xamt; i++){
for(int j = 0; j < yamt; j++){
deck[i][j].found = true;
}
}
}
else if ((key == 114 || key == 82) && !haveWon){ // R/r to show rules
if (shownRules == true){
shownRules = false;
}
else {
shownRules = true;
}
}
endScreen(); // allows switching images at end
}
void mouseClicked(){
if(players == -1){ // selecting 1 or 2 players
if(mouseX <= (width/2)){
players = 1;
isStarting = false;
}
else {
players = 2;
}
return;
}
if(timesClicked != 2){ // runs before 3rd click
curClicked = deck[mouseX/(width/xamt)][mouseY/(height/yamt)];
if (curClicked.isFlipped || curClicked.found){ // don't do anything if it's already found/flipped
return;
}
}
timesClicked++;
if (timesClicked == 1){ // first click: flip and assign
curClicked.flip();
clicked = curClicked;
addColor();
}
else if (timesClicked == 2){ // second click: flip and compare
curClicked.flip();
addColor();
if (clicked.pimg == curClicked.pimg){
haveFound = true;
if(curPlayer == 1){
p1score++;
}
else {
p2score++;
}
}
}
else { // third click: checking for found cards/winning, unflip cards
timesClicked = 0;
if(haveFound){ // if we found a match in 2nd click
clicked.found = true;
curClicked.found = true;
haveFound = false;
if (!haveWon){ // check if we won
for(int i = 0; i < xamt; i++){
for(int j = 0; j < yamt; j++){
if(!deck[i][j].found){
return;
}
}
}
}
haveWon = true; // only runs when we won
if (score < highscore){
highscore = score;
}
}
if(!haveWon){
score++;
curPlayer = 3 - curPlayer;
}
// unflip all cards (on third click)
for(int i = 0; i < xamt; i++){
for(int j = 0; j < yamt; j++){
deck[i][j].isFlipped = false;
}
}
}
}
void restart(){ // runs on spacebar
if (!haveWon){ // non
return;
}
roundsPlayed++;
// reset cards
for(int i = 0; i < xamt; i++){
for(int j = 0; j < yamt; j++){
deck[i][j].pimg = null;
deck[i][j].found = false;
deck[i][j].r = 127;
deck[i][j].b = 127;
}
}
// shuffling deck
shuffle();
// reset data
score = 0;
haveWon = false;
if ((roundsPlayed % 2) == 0){
curPlayer = 2;
}
else {
curPlayer = 1;
}
// sets background back to Earth
backg = 17;
}
void shuffle(){
// assigns images to cards
for (int i = 1; i <= 16; i++){
img = loadImage("images/img" + i + ".jpg");
Card picked;
for (int j = 0; j < 2; j++){
do {
picked = deck[floor(random(xamt))][floor(random(yamt))];
} while (picked.pimg != null);
picked.pimg = img;
}
}
}
void endScreen(){
// spacebar
if (key == 32){
restart();
}
// arrow keys change image
if (keyCode == LEFT){
backg--;
if (backg < 1){
backg = 17;
}
}
else if (keyCode == RIGHT){
backg++;
if(backg > 17){
backg = 1;
}
}
}
void onePlayerEnd(){ // endscreen for a one player game
fill(255);
textSize(72);
text("Congrats!", width/2, 85);
textSize(64);
text("Score: " + score, width/2, height/2 - 25 );
text("Highscore: " + highscore, width/2, height/2 + 25);
text("Rounds: " + roundsPlayed, width/2, height - 255);
}
void twoPlayerEnd(){ // endscreen for a two player game
fill(255);
textSize(72);
text(player1, width/6, 85);
text(player2, 5 * width/6, 85);
textSize(64);
text("Pairs: " + p1score, width/6, 160);
text("Pairs: " + p2score, 5 * width/6, 160);
// find winner
if (p1score > p2score){
text("Congrats", width/2, height/2 - 85);
text(player1 + "!", width/2, height/2 -25);
}
else if (p2score > p1score) {
text("Congrats", width/2, height/2 - 85);
text(player2 + "!", width/2, height/2 -25);
}
else {
text("Congrats!", width/2, height/2 - 85);
}
text("Total Turns: " + score, width/2, height - 285);
text("Combined Highscore: " + highscore, width/2, height - 225);
text("Rounds: " + roundsPlayed, width/2, 85);
}
String getName(String player){ // assigns input to player string
if (((key >= 65 && key <= 90) || (key >= 97 && key <= 122)) && player.length() < 10){
player += key; // only allows lower and upper case letters
}
if (key == 8 && player.length() > 0){ // removes last letter from string
player = player.substring(0,player.length()-1);
}
if (key == 10){ // runs when enter is hit
if (player.length() == 0) { // sets default names
if(isPlayer1Done){
player = "Player 2";
}
else {
player = "Player 1";
}
}
if (isPlayer1Done){
isStarting = false;
}
else {
isPlayer1Done = true;
}
}
return player;
}
void addColor(){
if(players == 2){
if(curClicked.r < 245 && curPlayer == 1) { // adds red to cards player 1 clicks
curClicked.r += 10;
}
if(curClicked.b < 245 && curPlayer == 2) { // adds blue to cards player 2 clicks
curClicked.b += 10;
}
}
else {
if(curClicked.r < 245){
curClicked.r += 10; // adds red during 1 player games
}
}
}
void showRules(){
// displays the rules image when r is pressed
PImage rules = loadImage("images/rules.jpg");
rules.resize(width,height);
background(rules);
textSize(40);
fill(0);
text("Press [R/r] to open or close this page at any time", width/2, height - 100);
}