Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. var cells = []; // This means array!
  2. var w = 10;
  3. var cols, rows;
  4.  
  5. function setup() {
  6.  
  7. createCanvas(200, 200);
  8. cols = width / w; //so code doesn't redraw itself
  9. rows = height / w;
  10. createGrid();
  11.  
  12. for (var i = 0; i < cols; i++) {
  13. for (var j = 0; j < rows; j++) {
  14. cells[i][j].display();
  15. }
  16. }
  17. }
  18.  
  19.  
  20. function createGrid() {
  21.  
  22. for (var i = 0; i < cols; i++) {
  23.  
  24. cells[i] = [];
  25.  
  26. for (var j = 0; j < rows; j++) {
  27.  
  28. cells[i][j] = {
  29. x: i * w,
  30. y: j * w,
  31. w: w,
  32. h: w,
  33. dir: floor(random(4)),
  34.  
  35. display: function() {
  36. fill(255);
  37. rect(this.x, this.y, this.w, this.h);
  38.  
  39. if (this.dir == 1) {
  40.  
  41. line(this.x, this.y, this.x + 10, this.y + 10);
  42.  
  43. } else if (this.dir == 2) {
  44.  
  45. line(this.x + 10, this.y, this.x, this.y + 10);
  46.  
  47. } else if (this.dir == 3) {
  48.  
  49. line(this.x + 10, this.y, this.x, this.y + 10);
  50. line(this.x, this.y, this.x + 10, this.y + 10);
  51.  
  52. } else {
  53. rect(this.x, this.y, this.w, this.h);
  54. }
  55.  
  56. //line changes
  57.  
  58. if (mouseX >= this.x && mouseX < this.x + 10 && mouseY >= this.y && mouseY < this.y + 10) {
  59.  
  60.  
  61.  
  62. fill(255);
  63. rect(this.x, this.y, this.w, this.h);
  64. if (this.dir == 2) {
  65.  
  66. line(this.x, this.y, this.x + 10, this.y + 10);
  67.  
  68. } else if (this.dir == 3) {
  69.  
  70. line(this.x + 10, this.y, this.x, this.y + 10);
  71.  
  72. } else if (this.dir == 1) {
  73.  
  74. rect(this.x, this.y, this.w, this.h);
  75.  
  76. } else {
  77.  
  78. line(this.x + 10, this.y, this.x, this.y + 10);
  79. line(this.x, this.y, this.x + 10, this.y + 10);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87.  
  88.  
  89.  
  90. function draw() {
  91. //create array of cells
  92. background(255);
  93. for (var i = 0; i < cols; i++) {
  94. for (var j = 0; j < rows; j++) {
  95. cells[i][j].display();
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement