tilegame.pde 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ArrayList<Tile> tiles = new ArrayList<Tile>();
  2. ArrayList<Tile> moving = new ArrayList<Tile>();
  3. boolean hasWon = false;
  4. boolean allValidPositions = false;
  5. void setup() {
  6. size(480, 800);
  7. int tileSize = 80;
  8. for (int y = 0; y < height; y+=tileSize) {
  9. for (int x = 0; x < width; x+=tileSize) {
  10. //println("[" + x + "," + y + "]: " + randomRotation);
  11. Tile tmp = new Tile(x, y, tileSize);
  12. tmp.shuffle();
  13. tiles.add(tmp);
  14. }
  15. }
  16. noStroke();
  17. }
  18. void draw() {
  19. background(128, 128, 128);
  20. allValidPositions = true;
  21. for (Tile tile : tiles) {
  22. if (!hasWon) {
  23. tile.update();
  24. }
  25. tile.draw();
  26. if (tile.getRotation() != 0) {
  27. allValidPositions = false;
  28. }
  29. }
  30. if (allValidPositions) {
  31. hasWon = true;
  32. textSize(48);
  33. String message = "You Win!";
  34. float offset = textWidth(message);
  35. float ascent = textAscent();
  36. fill(255, 255, 255);
  37. stroke(0, 0, 0);
  38. rect(width / 4, (height / 2) - ascent * 2, width / 2, ascent *2);
  39. fill(0, 0, 0);
  40. text(message, (width - offset) / 2, (height - ascent) / 2);
  41. }
  42. }
  43. void shuffleTiles() {
  44. for (Tile tile : tiles) {
  45. tile.shuffle();
  46. }
  47. }
  48. void mouseReleased() {
  49. if (allValidPositions && hasWon) {
  50. allValidPositions = false;
  51. hasWon = false;
  52. shuffleTiles();
  53. } else {
  54. for (Tile tile : tiles) {
  55. if (tile.isHovering(mouseX, mouseY)) {
  56. tile.setRotationTarget(tile.getRotationTarget() + 90);
  57. moving.add(tile);
  58. }
  59. }
  60. for (Tile tile : moving) {
  61. tiles.remove(tile);
  62. tiles.add(tile);
  63. }
  64. moving.clear();
  65. }
  66. }
  67. class Tile {
  68. private PVector position = new PVector();
  69. private PVector size = new PVector();
  70. private color tileColor;
  71. private int rotation;
  72. private int rotationTarget;
  73. private color lineColor;
  74. private int rotationSpeed;
  75. public Tile(int x, int y, int tileSize) {
  76. this.position.x = x;
  77. this.position.y = y;
  78. this.size.x = tileSize;
  79. this.size.y = tileSize;
  80. this.tileColor = color(255, 255, 255);
  81. this.rotation = 0;
  82. this.rotationTarget = 0;
  83. this.lineColor = color(255, 255, 0);
  84. this.rotationSpeed = 5;
  85. }
  86. public void update() {
  87. if (this.isHovering(mouseX, mouseY)) {
  88. this.setColor(color(0, 255, 0));
  89. } else {
  90. this.tileColor = color(100, 100, 100);
  91. }
  92. if (this.isAnimating()) {
  93. this.rotation += this.rotationSpeed;
  94. this.tileColor = color(255, 0, 0);
  95. }
  96. }
  97. public void draw() {
  98. noStroke();
  99. fill(this.tileColor);
  100. pushMatrix();
  101. translate(this.position.x + this.size.x / 2, this.position.y + this.size.y / 2);
  102. if (this.rotation >= 360) {
  103. this.rotation = 0;
  104. }
  105. rotate(this.rotation * (PI/180));
  106. rect(-this.size.x / 2, -this.size.y / 2, this.size.x, this.size.y);
  107. stroke(this.lineColor);
  108. line(0, 0, this.size.x / 2, 0);
  109. popMatrix();
  110. }
  111. public boolean isHovering(int mouseX, int mouseY) {
  112. if (mouseX >= this.position.x && mouseX < this.position.x + this.size.x) {
  113. //hovering within x
  114. if (mouseY >= this.position.y && mouseY < this.position.y + this.size.y) {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. public void setColor(int newColor) {
  121. this.tileColor = newColor;
  122. }
  123. public void addRotation(int newRotation) {
  124. this.rotation += newRotation;
  125. }
  126. public void setRotationTarget(int newTarget) {
  127. this.rotationTarget = newTarget;
  128. while (this.rotationTarget >= 360) {
  129. this.rotationTarget -= 360;
  130. if (this.rotationTarget <= 0) {
  131. this.rotationTarget = 0;
  132. }
  133. }
  134. }
  135. public int getRotationTarget() {
  136. return this.rotationTarget;
  137. }
  138. public boolean isAnimating() {
  139. return this.rotation != this.rotationTarget;
  140. }
  141. public int getRotation() {
  142. return this.rotation;
  143. }
  144. public void shuffle() {
  145. int randomRotation = 90 * (int)random(0, 4);
  146. this.rotation = randomRotation;
  147. this.rotationTarget = randomRotation;
  148. }
  149. }