Guest User

Untitled

a guest
Jul 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Variables */
  2. var canvas;
  3. var context;
  4.  
  5. var player_direction;
  6. var x;
  7. var y;
  8. var colour;
  9. var nick;
  10.  
  11. var gamestarted = false;
  12.  
  13. var MAZE_WIDTH;
  14. var MAZE_HEIGHT;
  15.  
  16. var img_maze;
  17. var img_wars;
  18.  
  19. var health = 10;
  20.  
  21. var mazedata; // Variable to store maze data as a bool[][]
  22.  
  23. var brick; // Brick image
  24.  
  25. /* Gameplay */
  26.  
  27. function checkKey(e) {
  28.     e = e || window.event;
  29.     if (e.keyCode==32)
  30.         fire();
  31.     if (e.keyCode==37)
  32.         rotateLeft();
  33.     if (e.keyCode==38)
  34.         moveForward();
  35.     if (e.keyCode==39)
  36.         rotateRight();
  37.     if (e.keyCode==40)
  38.         moveBackward();
  39.     if (e.keyCode==13)
  40.         startGame();
  41.     if (e.keyCode==27)
  42.         quitGame();
  43. }
  44.  
  45. document.onkeydown = checkKey;
  46.  
  47. function updateHealth(amount) {
  48.     health = amount;
  49.     if (health>0)
  50.         document.title = "Maze Wars - Health: "+health;
  51.     else
  52.         document.title = "Maze Wars";
  53. }
  54.  
  55. function drawSplash() {
  56.     /* Reset canvas */
  57.     canvas.width = canvas.width;
  58.    
  59.     /* Background */
  60.     context.fillStyle = "#000000";  
  61.     context.fillRect(0,0,500,500);
  62.    
  63.     /* Draw text */
  64.     context.drawImage(img_maze,154,160);
  65.     context.drawImage(img_wars,154,280);
  66.    
  67.     /* List Instructions */
  68.     document.getElementById('players').innerHTML = '<font color="#FFFFFF"><b>Instructions - Enter: Start Game, Space: Fire Lazer, Up/Down/Left/Right: Move Character</b></font>';
  69. }
  70.  
  71. function init() {
  72.     canvas = document.getElementById("canvas");
  73.     context = canvas.getContext("2d");
  74.    
  75.     var logindiv = document.getElementById("login_form");
  76.     var gamediv = document.getElementById("game_container");
  77.    
  78.     logindiv.style.display = "none";
  79.     gamediv.style.display = "block";
  80.    
  81.     brick = new Image();
  82.     brick.src = "brick.jpg";
  83.    
  84.     img_maze = new Image();
  85.     img_maze.src = "maze.png";
  86.     img_wars = new Image();
  87.     img_wars.src = "wars.png";
  88.    
  89.     drawSplash();
  90. }
  91.  
  92. function startGame() {
  93.     gamestarted = true;
  94.     updateHealth(10);
  95.     sendStart();
  96. }
  97.  
  98. function drawfire(firex,firey,direction) {
  99.     if (direction==1)
  100.         direction = 3;
  101.     else if (direction==3)
  102.         direction = 1;
  103.    
  104.     context.fillStyle = "#FF0000";
  105.     context.beginPath();
  106.     context.moveTo(255-((x-firex)*10),255-((y-firey)*10));
  107.     context.arc(255-((x-firex)*10),255-((y-firey)*10),35,Math.PI*0.5*direction-0.18,Math.PI*0.5*direction+0.18);
  108.     context.lineTo(255-((x-firex)*10),255-((y-firey)*10));
  109.     context.closePath();
  110.     context.fill();
  111. }
  112.  
  113. function checkBlock(x,y) {
  114.     if (x<0)
  115.         x+=MAZE_WIDTH;
  116.     if (y<0)
  117.         y+=MAZE_HEIGHT;
  118.     if (x>=MAZE_WIDTH)
  119.         x-=MAZE_WIDTH;
  120.     if (y>=MAZE_HEIGHT)
  121.         y-=MAZE_HEIGHT;
  122.     return (mazedata[y][x]=='1');
  123. }
  124.  
  125. function draw() {
  126.     /* Reset canvas */
  127.     canvas.width = canvas.width;
  128.    
  129.     /* Background */
  130.     context.fillStyle = "#000000";  
  131.     context.fillRect(0,0,500,500);
  132.    
  133.     /* Draw maze */
  134.     for (var i=0; i<50; i++) {
  135.         for (var j=0; j<50; j++) {
  136.             if (checkBlock(i+x-25,j+y-25))
  137.                 context.drawImage(brick, i*10, j*10, 10, 10); // Draw wall square
  138.         }
  139.     }
  140.    
  141.     /* Draw player */
  142.     context.fillStyle = "#"+colour;
  143.     context.beginPath(); // Start the path
  144.     context.arc(255, 255, 5, 0, Math.PI*2, false); // Draw a circle
  145.     context.closePath(); // Close the path
  146.     context.fill(); // Fill the path
  147.    
  148.     /* Draw opponents */
  149.     for (var i=0; i<players.length; i++) {
  150.         if (players[i].nick!=nick && players[i].x!=null) { // don't draw self or any colliding players or not in play characters
  151.             context.fillStyle = "#"+players[i].colour; // Set player colour
  152.             console.log("Style",context.fillStyle); // ###
  153.             context.beginPath(); // Start the path
  154.             context.arc(players[i].x*10+5-x*10+250, players[i].y*10+5-y*10+250, 5, 0, Math.PI*2, false); // Draw a circle
  155.             context.closePath(); // Close the path
  156.             context.fill(); // Fill the path
  157.         }
  158.     }
  159. }
  160.  
  161. function isFree(x, y) {
  162.     if (x<0||y<0||x>=MAZE_WIDTH||y>=MAZE_HEIGHT)
  163.         return true;
  164.     return (mazedata[y][x]!=1);
  165. }
  166.  
  167. /* Movement */
  168. function moveForward() {
  169.     switch (player_direction) {
  170.         case 0:
  171.             if (isFree(x+1,y))
  172.                 x++;
  173.             break;
  174.         case 1:
  175.             if (isFree(x,y-1))
  176.                 y--;
  177.             break;
  178.         case 2:
  179.             if (isFree(x-1,y))
  180.                 x--;
  181.             break;
  182.         case 3:
  183.             if (isFree(x,y+1))
  184.                 y++;
  185.             break;
  186.     }
  187.    
  188.     /* Folding */
  189.     if (x<0)
  190.         x = MAZE_WIDTH;
  191.     else if (x>=MAZE_WIDTH)
  192.         x = 0;
  193.     else if (y<0)
  194.         y = MAZE_HEIGHT;
  195.     else if (y>=MAZE_HEIGHT)
  196.         y = 0;
  197.    
  198.     updatePosition(x,y);
  199.     draw();
  200. }
  201.  
  202. function moveBackward() {
  203.     switch (player_direction) {
  204.         case 0:
  205.             if (isFree(x-1,y))
  206.                 x--;
  207.             break;
  208.         case 1:
  209.             if (isFree(x,y+1))
  210.                 y++;
  211.             break;
  212.         case 2:
  213.             if (isFree(x+1,y))
  214.                 x++;
  215.             break;
  216.         case 3:
  217.             if (isFree(x,y-1))
  218.                 y--;
  219.             break;
  220.     }
  221.    
  222.     /* Folding */
  223.     if (x<0)
  224.         x = MAZE_WIDTH;
  225.     else if (x>=MAZE_WIDTH)
  226.         x = 0;
  227.     else if (y<0)
  228.         y = MAZE_HEIGHT;
  229.     else if (y>=MAZE_HEIGHT)
  230.         y = 0;
  231.    
  232.     updatePosition(x,y);
  233.     draw();
  234. }
  235.  
  236. function rotateRight() {
  237.     player_direction--;
  238.     if (player_direction==-1)
  239.         player_direction = 3;
  240.     draw();
  241. }
  242.  
  243. function rotateLeft() {
  244.     player_direction++;
  245.     if (player_direction==4)
  246.         player_direction = 0;
  247.     draw();
  248. }
Add Comment
Please, Sign In to add comment