A match-3 game. https://gelatolabs.xyz/almostbejeweled/

almostBejeweled.pde 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. final int WHITE = 0;
  2. final int PURPLE = 1;
  3. final int YELLOW = 2;
  4. final int BLUE = 3;
  5. final int ORANGE = 4;
  6. final int RED = 5;
  7. final int GREEN = 6;
  8. int sketchWidth = 1024;
  9. int sketchHeight = 1280;
  10. int mx;
  11. int my;
  12. PImage bg;
  13. PImage guide;
  14. PImage[] gemImgs = new PImage[7];
  15. Gem board[][] = new Gem[8][7];
  16. int level = 1;
  17. int score = 0;
  18. int scoreNeeded = 10000;
  19. int multiplier = 1;
  20. int moves = 20;
  21. int bestGem = int(random(3, 7));
  22. do
  23. int worstGem = int(random(3, 7));
  24. while (worstGem == bestGem);
  25. void setup() {
  26. bg = loadImage("img/background.svg");
  27. guide = loadImage("img/guide.svg");
  28. gemImgs[0] = loadImage("img/gem0.svg");
  29. gemImgs[1] = loadImage("img/gem1.svg");
  30. gemImgs[2] = loadImage("img/gem2.svg");
  31. gemImgs[3] = loadImage("img/gem3.svg");
  32. gemImgs[4] = loadImage("img/gem4.svg");
  33. gemImgs[5] = loadImage("img/gem5.svg");
  34. gemImgs[6] = loadImage("img/gem6.svg");
  35. PFont font;
  36. font = loadFont("FFScala.ttf");
  37. textSize(48);
  38. for (int i = 0; i < 8; i++)
  39. for (int j = 0; j < 7; j++)
  40. board[i][j] = new Gem(i*128, j*128, board);
  41. checkMatches();
  42. score = 0;
  43. multiplier = 1;
  44. moves = 20;
  45. bestGem = int(random(3, 7));
  46. do
  47. worstGem = int(random(3, 7));
  48. while (worstGem == bestGem);
  49. }
  50. void draw() {
  51. mx = sketchWidth / width * mouseX;
  52. my = sketchHeight / height * mouseY;
  53. background(0);
  54. set(0, 0, bg);
  55. for (int i = 0; i < 8; i++)
  56. for (int j = 0; j < 7; j++) {
  57. board[i][j].update();
  58. board[i][j].display();
  59. }
  60. fill(0);
  61. rect(0, 896, 1024, 384);
  62. fill(255);
  63. text("Score: " + score + "/" + scoreNeeded, 50, 1000);
  64. text("Multiplier: " + multiplier, 50, 1100);
  65. text("Moves: " + moves, 50, 1050);
  66. text("Level: " + level, 50, 1150);
  67. switch(bestGem) {
  68. case 0: text("Best gem: white", sketchWidth / 2, 1000); break;
  69. case 1: text("Best gem: purple", sketchWidth / 2, 1000); break;
  70. case 2: text("Best gem: yellow", sketchWidth / 2, 1000); break;
  71. case 3: text("Best gem: blue", sketchWidth / 2, 1000); break;
  72. case 4: text("Best gem: orange", sketchWidth / 2, 1000); break;
  73. case 5: text("Best gem: red", sketchWidth / 2, 1000); break;
  74. case 6: text("Best gem: green", sketchWidth / 2, 1000); break;
  75. }
  76. switch(worstGem) {
  77. case 0: text("Worst gem: white", sketchWidth / 2, 1050); break;
  78. case 1: text("Worst gem: purple", sketchWidth / 2, 1050); break;
  79. case 2: text("Worst gem: yellow", sketchWidth / 2, 1050); break;
  80. case 3: text("Worst gem: blue", sketchWidth / 2, 1050); break;
  81. case 4: text("Worst gem: orange", sketchWidth / 2, 1050); break;
  82. case 5: text("Worst gem: red", sketchWidth / 2, 1050); break;
  83. case 6: text("Worst gem: green", sketchWidth / 2, 1050); break;
  84. }
  85. }
  86. void mouseReleased() {
  87. for (int i = 0; i < 8; i++)
  88. for (int j = 0; j < 7; j++) {
  89. board[i][j].othersHeld = false;
  90. if (board[i][j].held) {
  91. board[i][j].held = false;
  92. board[i][j].x = board[i][j].sx;
  93. board[i][j].y = board[i][j].sy;
  94. if (int(mx / 128) * 128 == board[i][j].sx || int(my / 128) * 128 == board[i][j].sy) {
  95. int[][] tempBoard = new int[8][7];
  96. for (int l = 0; l < 8; l++)
  97. for (int m = 0; m < 7; m++)
  98. tempBoard[l][m] = board[l][m].gem;
  99. int tempGem = board[i][j].gem;
  100. if (int(mx / 128) * 128 > board[i][j].sx)
  101. for (int k = i; k < int(mx / 128); k++)
  102. board[k][j].gem = board[k+1][j].gem;
  103. else if (int(mx / 128) * 128 < board[i][j].sx)
  104. for (int k = i; k > int(mx / 128); k--)
  105. board[k][j].gem = board[k-1][j].gem;
  106. else if (int(my / 128) * 128 > board[i][j].sy)
  107. for (int k = j; k < int(my / 128); k++)
  108. board[i][k].gem = board[i][k+1].gem;
  109. else
  110. for (int k = j; k > int(my / 128); k--)
  111. board[i][k].gem = board[i][k-1].gem;
  112. board[int(mx / 128)][int(my / 128)].gem = tempGem;
  113. if (!checkMatches()) {
  114. for (l = 0; l < 8; l++)
  115. for (m = 0; m < 7; m++)
  116. board[l][m].gem = tempBoard[l][m];
  117. } else {
  118. moves--;
  119. if (moves == 0) {
  120. level = 1;
  121. scoreNeeded = 10000
  122. println("Out of moves!");
  123. setup();
  124. }
  125. }
  126. if (score >= scoreNeeded) {
  127. level++;
  128. scoreNeeded += 2500;
  129. println("You win!");
  130. setup();
  131. }
  132. }
  133. }
  134. }
  135. }
  136. class Gem {
  137. int x, y, sx, sy;
  138. int w = 128;
  139. int h = 128;
  140. int gem = int(random(0, 7));
  141. boolean held = false;
  142. boolean othersHeld = false;
  143. Gem[][] others;
  144. Gem(int ix, int iy, Gem[][] o) {
  145. x = ix;
  146. y = iy;
  147. sx = ix;
  148. sy = iy;
  149. others = o;
  150. }
  151. void update() {
  152. for (int i = 0; i < 8; i++)
  153. for (int j = 0; j < 7; j++) {
  154. if (others[i][j].held)
  155. othersHeld = true;
  156. }
  157. if (!othersHeld && overRect(x, y, w, h) && mousePressed)
  158. held = true;
  159. if (held) {
  160. x = mx - w/2;
  161. y = my - h/2;
  162. }
  163. }
  164. void display() {
  165. if (held)
  166. set(sx-896, sy-768, guide);
  167. image(gemImgs[gem], x, y, w, h);
  168. }
  169. }
  170. boolean overRect(int x, int y, int w, int h) {
  171. return (mx >= x && mx <= x+w && my >= y && my <= y+h);
  172. }
  173. boolean checkMatches() {
  174. boolean match = false;
  175. for (int i = 0; i < 8; i++)
  176. for (int j = 0; j < 7; j++) {
  177. if (i < 6) if (board[i+1][j].gem == board[i][j].gem && board[i+2][j].gem == board[i][j].gem) {
  178. if (i < 5) if (board[i+3][j].gem == board[i][j].gem) {
  179. if (i < 4) if (board[i+4][j].gem == board[i][j].gem) {
  180. doMatch(board[i][j].gem);
  181. board[i+4][j].gem = -1;
  182. }
  183. doMatch(board[i][j].gem);
  184. board[i+3][j].gem = -1;
  185. }
  186. doMatch(board[i][j].gem);
  187. board[i][j].gem = -1;
  188. board[i+1][j].gem = -1;
  189. board[i+2][j].gem = -1;
  190. match = true;
  191. }
  192. if (j < 5) if (board[i][j+1].gem == board[i][j].gem && board[i][j+2].gem == board[i][j].gem) {
  193. if (j < 4) if (board[i][j+3].gem == board[i][j].gem) {
  194. if (j < 3) if (board[i][j+4].gem == board[i][j].gem) {
  195. doMatch(board[i][j].gem);
  196. board[i][j+4].gem = -1;
  197. }
  198. doMatch(board[i][j].gem);
  199. board[i][j+3].gem = -1;
  200. }
  201. doMatch(board[i][j].gem);
  202. board[i][j].gem = -1;
  203. board[i][j+1].gem = -1;
  204. board[i][j+2].gem = -1;
  205. match = true;
  206. }
  207. }
  208. for (int i = 0; i < 8; i++)
  209. for (int j = 0; j < 7; j++)
  210. if (board[i][j].gem == -1) {
  211. for (int k = j; k > 0; k--)
  212. board[i][k].gem = board[i][k-1].gem;
  213. board[i][0].gem = int(random(0, 7));
  214. checkMatches();
  215. }
  216. return(match);
  217. }
  218. void doMatch(int color) {
  219. if (color == PURPLE)
  220. score -= 500;
  221. else if (color == YELLOW)
  222. moves += 2;
  223. else if (color == WHITE)
  224. multiplier += 0.25;
  225. else if (color == bestGem)
  226. score += 200 * multiplier;
  227. else if (color == worstGem)
  228. score += 50 * multiplier;
  229. else
  230. score += 100 * multiplier;
  231. score = int(score);
  232. }