Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. var n=28, m=31;
  2. endlessCanvas = true;
  3. var grid=[[1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],[1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1],[1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,0,0,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1],[1,0,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1],[1,0,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1],[1,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,0,1,0,0,0,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1],[1,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,1,0,0,0,0,1],[1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1],[1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,0,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1],[1,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,1,0,0,0,0,1],[1,0,1,1,1,0,1,1,0,1,1,0,1,0,0,0,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1],[1,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,1],[1,0,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1],[1,0,1,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1],[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,0,0,0,1,1,0,1],[1,0,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1],[1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1],[1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]];
  4. var food=[];
  5. var score = 0;
  6. for (let i=0; i<n; ++i){
  7. food[i] = [];
  8. for (let j=0; j<m; ++j){
  9. food[i][j] = 1-grid[i][j];
  10. if (i>=7 && i<=20 && j>=9 && j<=19) food[i][j]=0;
  11. }
  12. }
  13. function drawGrid(){
  14. for (let i=0; i<n; ++i){
  15. for (let j=0; j<m; ++j){
  16. if (grid[i][j]) context.fillStyle = 'blue';
  17. else context.fillStyle = 'black';
  18. context.fillRect(20*i, 20*j, 20, 20);
  19. if (food[i][j]) {
  20. context.fillStyle="yellow";
  21. context.beginPath();
  22. context.arc(20*i+10, 20*j+10, 5, 0, 2*Math.PI);
  23. context.fill();
  24. }
  25. }
  26. }
  27. }
  28.  
  29. class Bot{
  30. constructor(x, y, color){
  31. this.x = x;
  32. this.y = y;
  33. this.color = color;
  34. }
  35. ai(x, y){}
  36. update(playerX, playerY){
  37. let dir = this.ai(playerX, playerY);
  38. let newx=this.x, newy=this.y
  39. if (dir==0) --newx;
  40. if (dir==1) ++newx;
  41. if (dir==2) --newy;
  42. if (dir==3) ++newy;
  43. if (newx>=0 && newx<n && newy>=0 && newy<m && !grid[newx][newy]){
  44. this.x = newx;
  45. this.y = newy;
  46. }
  47. }
  48. draw(){
  49. context.fillStyle = this.color;
  50. context.beginPath();
  51. context.arc(this.x*20+10, this.y*20+10, 10, 0, 2*Math.PI);
  52. context.fill();
  53. }
  54. }
  55. class Ghost extends Bot{
  56. constructor(x, y, color, dx, dy){
  57. super(x, y, color);
  58. this.dx = dx;
  59. this.dy = dy;
  60. }
  61. ai(x, y){
  62. //this.x, this.y
  63. //x, y
  64. let dist = []
  65. for (let i=0; i<n; ++i){
  66. dist[i] = [];
  67. for (let j=0; j<m; ++j){
  68. dist[i][j] = -2;
  69. }
  70. }
  71. dist[x][y] = 0;
  72. let k=0
  73. let cells = [{x: x, y: y}], cells2 = [];
  74. for (k=0; cells.length>0; ++k){
  75. for (let cell of cells){
  76. if (cell.x > 0 && dist[cell.x-1][cell.y]==-2 && grid[cell.x-1][cell.y]==0){
  77. dist[cell.x-1][cell.y] = k+1;
  78. cells2.push({x:cell.x-1, y:cell.y});
  79. }
  80. if (cell.x < n-1 && dist[cell.x+1][cell.y]==-2 && grid[cell.x+1][cell.y]==0){
  81. dist[cell.x+1][cell.y] = k+1;
  82. cells2.push({x:cell.x+1, y:cell.y});
  83. }
  84. if (cell.y > 0 && dist[cell.x][cell.y-1]==-2 && grid[cell.x][cell.y-1]==0){
  85. dist[cell.x][cell.y-1] = k+1;
  86. cells2.push({x:cell.x, y:cell.y-1});
  87. }
  88. if (cell.y < m-1 && dist[cell.x][cell.y+1]==-2 && grid[cell.x][cell.y+1]==0){
  89. dist[cell.x][cell.y+1] = k+1;
  90. cells2.push({x:cell.x, y:cell.y+1});
  91. }
  92. }
  93. cells = cells2;
  94. cells2 = [];
  95. }
  96. let d = dist[this.x][this.y];
  97. if (this.x>0 && dist[this.x-1][this.y] == d-1) return 0;
  98. if (this.x<n-1 && dist[this.x+1][this.y] == d-1) return 1;
  99. if (this.y>0 && dist[this.x][this.y-1] == d-1) return 2;
  100. if (this.y<m-1 && dist[this.x][this.y+1] == d-1) return 3;
  101. }
  102. };
  103.  
  104. class Player extends Bot{
  105. ai(x, y){
  106. if (isKeyPressed[key_left]) return 0;
  107. if (isKeyPressed[key_right]) return 1;
  108. if (isKeyPressed[key_up]) return 2;
  109. if (isKeyPressed[key_down]) return 3;
  110. }
  111. update(x, y){
  112. super.update(x, y);
  113. if (food[this.x][this.y]){
  114. ++score;
  115. food[this.x][this.y] = 0;
  116. }
  117. for (let g of ghost){
  118. if (g.x == player.x && g.y==player.y){
  119. console.log("Game Over", score);
  120. score = 0;
  121. }
  122. }
  123. }
  124. };
  125.  
  126. var ghost = [new Ghost(14, 14, "red", -4, 0), new Ghost(14, 14, "pink", 4, 0), new Ghost(14, 14, "purple", 0, -4), new Ghost(14, 14, "green", 0, 4)];
  127. var player = new Player(14, 23, "white");
  128. var t=0;
  129. function update() {
  130. t++;
  131. if (t%10==0){
  132. player.update();
  133. for (let g of ghost){
  134. var targetX=player.x, targetY=player.y;
  135. let dist = Math.abs(targetX-g.x) + Math.abs(targetY-g.y);
  136. if (targetX+g.dx < n && targetX+g.dx >= 0 && !grid[targetX+g.dx][targetY] && dist>4){
  137. targetX = targetX + g.dx;
  138. }
  139. if (targetY+g.dy < m && targetY+g.dy >= 0 && !grid[targetX][targetY+g.dy] && dist>4){
  140. targetY = targetY + g.dy;
  141. }
  142. g.update(targetX, targetY);
  143. }
  144. }
  145. }
  146. function draw() {
  147. drawGrid();
  148. for (let g of ghost) g.draw();
  149. player.draw();
  150. };
  151. function keyup(key) {
  152.  
  153. // Show the pressed keycode in the console
  154. console.log("Pressed", key);
  155. };
  156. function mouseup() {
  157. // Show coordinates of mouse on click
  158. console.log("Mouse clicked at", mouseX, mouseY);
  159. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement