Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Canvas board game
  2. // Global variables
  3. var ctx = null;
  4. var tileW = 100, tileH = 100;
  5. var mapW = 10, mapH = 10;
  6.  
  7. var tileMap = [
  8.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  9.     0, 0, 2, 1, 3, 1, 2, 1, 0, 0,
  10.     0, 0, 1, 2, 1, 2, 3, 2, 0, 0,
  11.     0, 0, 2, 1, 3, 1, 2, 1, 0, 0,
  12.     0, 0, 1, 2, 1, 2, 1, 2, 0, 0,
  13.     0, 0, 3, 1, 2, 1, 3, 1, 0, 0,
  14.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  15.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  16.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17. ];
  18.  
  19. // left and right positions of beads, for player2 I need to add 30 to left so they are not on top of each other
  20. var tiles = [
  21.     {left: 220, bottom: 715},
  22.     {left: 320, bottom: 715},
  23.     {left: 420, bottom: 715, data: {type: "TRAP", move: "SKIP", instruction: "Skip 1 turn.", text: "A camp of evil thieves lays ahead of you, they are too many to confront and you decide to wait until they are gone."}},
  24.     {left: 520, bottom: 715},
  25.     {left: 620, bottom: 715},
  26.     {left: 720, bottom: 715},
  27.     {left: 220, bottom: 615},
  28.     {left: 320, bottom: 615},
  29.     {left: 420, bottom: 615},
  30.     {left: 520, bottom: 615},
  31.     {left: 620, bottom: 615, data: {type: "TRAP", move: "FORWARDS", steps: 3, instruction: "Move forwards 3 steps.", text: "A friendly looking dragon is ahead of you and you decide to hitch a ride."}},
  32.     {left: 720, bottom: 615},
  33.     {left: 220, bottom: 515},
  34.     {left: 320, bottom: 515},
  35.     {left: 420, bottom: 515, data: {type: 'TRAP', move: "GAMBLE", instruction: "Roll 2, 4 or 6 to move forward or roll 1, 3 and 5 to move backwards.", text: "You are robbed of your travel supplies and continuing the journey may be dangerous. You decide to gamble on continuing the journey(roll: 2, 4, 6) or go back to get new supplies(roll: 1, 3, 5)."}},
  36.     {left: 520, bottom: 515},
  37.     {left: 620, bottom: 515},
  38.     {left: 720, bottom: 515},
  39.     {left: 220, bottom: 415},
  40.     {left: 320, bottom: 415},
  41.     {left: 420, bottom: 415},
  42.     {left: 520, bottom: 415},
  43.     {left: 620, bottom: 415},
  44.     {left: 720, bottom: 415},
  45.     {left: 220, bottom: 315, data: {type: 'TRAP', move: "BACKWARDS", steps: -3, instruction: "Move backwards 3 steps.", text: "A group of nightwalkers see you an you have to run!"}},
  46.     {left: 320, bottom: 315},
  47.     {left: 420, bottom: 315},
  48.     {left: 520, bottom: 315},
  49.     {left: 620, bottom: 315, data: {type: 'TRAP', move: "ROLLHIGH" , steps: -1, instruction: "Roll a 3 or above to win or move backwards 1 step.", text: "A war is being faught and you have to assist."}},
  50.     {left: 720, bottom: 315} // When this tile is reached, send user to victory screen
  51. ];
  52.  
  53. // Draw canvas when window has loaded
  54. window.onload = function() {
  55.     ctx = document.querySelector(".canvas").getContext("2d");
  56.     this.requestAnimationFrame(drawGame);
  57.     ctx.font = 'bold 16px "Open Sans", sans-serif';
  58. };
  59.  
  60. function drawGame() {
  61.     if(ctx==null) {
  62.         alert("Your browser does not support HTML canvas. Upgrade it to play the game.");
  63.         return;
  64.     }
  65.  
  66.     for(var y = 0; y < mapH; y++) {
  67.         for(var x = 0; x < mapW; x++) {
  68.  
  69.             switch(tileMap[((y*mapW)+x)]) {
  70.                 case 1:
  71.                     ctx.fillStyle = "#FFFFFF";
  72.                     break;
  73.                 case 2:
  74.                     ctx.fillStyle = "#5A6C7A";
  75.                     break;  
  76.                 case 3:
  77.                     ctx.fillStyle = "#D3A52F";
  78.                     break;
  79.                 default:
  80.                     ctx.fillStyle = "#FFFFFF";
  81.             }
  82.             ctx.fillRect(x*tileW, y*tileH, tileW, tileH);
  83.         }
  84.     }
  85.  
  86.     // Draw board border
  87.     ctx.beginPath();
  88.     ctx.rect(200, 100, 600, 500);
  89.     ctx.lineWidth = "2";
  90.     ctx.strokeStyle = "#5A6C7A";
  91.     ctx.stroke();
  92.  
  93.     // Draw board seperation lines
  94.     ctx.beginPath();
  95.     ctx.moveTo(200, 204);
  96.     ctx.lineTo(700, 204);
  97.  
  98.     ctx.moveTo(300, 304);
  99.     ctx.lineTo(800, 304);
  100.  
  101.     ctx.moveTo(200, 404);
  102.     ctx.lineTo(700, 404);
  103.  
  104.     ctx.moveTo(300, 504);
  105.     ctx.lineTo(800, 504);
  106.  
  107.     ctx.lineWidth = "8";
  108.     ctx.strokeStyle = "#000000";
  109.     ctx.stroke();
  110.  
  111.     // Draw images
  112.     var throneImg = new Image();
  113.     var warningImg = new Image();
  114.  
  115.     throneImg.onload = function (){
  116.         ctx.drawImage(throneImg, 640, 620);
  117.  
  118.         ctx.drawImage(warningImg, 427, 125);
  119.         ctx.drawImage(warningImg, 627, 225);
  120.         ctx.drawImage(warningImg, 427, 325);
  121.         ctx.drawImage(warningImg, 627, 525);
  122.         ctx.drawImage(warningImg, 227, 525);
  123.     }
  124.     throneImg.src = "images/iron_throne_small.png";
  125.     warningImg.src = "images/warning.png";
  126.  
  127.     // Draw text
  128.     ctx.fillStyle = "#000000";
  129.     ctx.fillText("GOAL", 727, 525);
  130.  
  131.     ctx.fillStyle = "#FFFFFF";
  132.     ctx.fillText("START", 223, 120);
  133.  
  134.     ctx.fillStyle = "#000000";
  135.     ctx.fillText("Who can reach the Iron Throne first?", 300, 700);
  136.  
  137.     ctx.fillText("Watch out for the yellow tiles, danger may be lurking!", 200, 70);
  138. };
  139.  
  140. // Function to insert name of players turn
  141. function playerTurn() {
  142.     var playerTurn = document.querySelector(".player-turn");
  143.     var player1 = localStorage.getItem("player1");
  144.     var player2 = localStorage.getItem("player2");
  145.  
  146.     player1Img = document.querySelector(".bead1");
  147.     player2Img = document.querySelector(".bead2");
  148.     var player1SRC = localStorage.getItem("player1").split(' ', 1)[0];
  149.     var player2SRC = localStorage.getItem("player2").split(' ', 1)[0];
  150.     player1Img.src = "images/" + player1SRC + "_small.png";
  151.     player2Img.src = "images/" + player2SRC + "_small.png";
  152.  
  153.  
  154.     playerTurn.innerHTML = player1;
  155.     /*
  156.     if(playerRoll == 6) {
  157.         playerTurn.innerHTML = playerTurn;
  158.     } else {
  159.         if(playerTurn.innerHTML == player2) {
  160.             playerTurn.innerHTML = player1;
  161.         } else if (playerTurn.innerHTML == player1) {
  162.             playerTurn.innerHTML = player2;
  163.         } else {
  164.             playerTurn.innerHTML = player1;
  165.         }
  166.     }*/
  167. };
  168. playerTurn();
  169.  
  170. // Function to spin dice on click
  171. (function(){
  172.     var div = document.querySelector('.dice-container');
  173.     var dice = document.querySelector('#dice');
  174.     var open = false;
  175.    
  176.     div.addEventListener('click', function(){
  177.       if(open){
  178.         dice.className = 'icon-dice';  
  179.       } else{
  180.         dice.className = 'icon-dice rotate';
  181.       }
  182.       open = !open;
  183.     });
  184. })();
  185.  
  186. // Roll result
  187. function rollDice() {
  188.     var rollNumber = roll();
  189.     var playerRoll = document.querySelector(".player-roll");
  190.  
  191.     playerRoll.innerHTML = "You rolled a " + rollNumber + ".";
  192.     // animate players token to move to correct tile
  193. };
  194.  
  195. // Function to generate a number from 1-6
  196. function roll() {
  197.     return Math.floor(Math.random() * 6) + 1;
  198. };
  199.  
  200. // Move bead based on roll
  201. // ...code
  202. // everything here is attempts at how to make roll move dices and include everything else This uses px isntead of the x, y I need
  203. var rollHistory = [];
  204. var currentPosition = 0; // starting positon
  205. var button = document.querySelector("roll-button");
  206.  
  207. button.addEventListener("click", (e) => {
  208.     const roll = rollDice();
  209.  
  210.     // this is not for my array of tiles
  211.     if (currentPosition + roll >= tiles.length) {
  212.         // not enought tiles left, go to last
  213.         currentPosition = tiles.length - 1;
  214.     } else {
  215.         currentPosition += roll;
  216.     }
  217.    
  218.     // have to draw image at new position in canvas
  219.     bead.style.left = tiles[currentPosition].left + 'px';
  220.     bead.style.top = tiles[currentPosition].bottom + 'px';
  221.    
  222.     // if on last, Win!
  223.     if (currentPosition === tiles.length - 1) {
  224.         setTimeout(() => alert('You win!'), 1);
  225.         // When a bead lands on the last tile go to window.location = "file:///F:/School_Noroff/2019_Year2_Semester1/2020-01-27_Semester-Project2_Lisa-Ingemann/WF/Web/finale.html";
  226.     }
  227.  
  228.     // If bead lands on yellow tile, activate trap
  229.     for(var i = 0; i < tiles.length; i++) {
  230.         var currentPositionX = tiles[i].x;
  231.         var currentPositionY = tiles[i].y;
  232.  
  233.         if(tiles[i].data.type == "TRAP") {
  234.             // Display trap message and instructions if bead has landed on a trap
  235.             var trapContainer = document.querySelector(".message-trap");
  236.             var trapInfo = document.querySelector(".text-trap");
  237.             var trapInstructions = document.querySelector(".instruction");
  238.  
  239.             trapContainer.style.display = "block";
  240.             trapInfo.innerHTML = tiles[i].data.text;
  241.             trapInstructions.innerHTML = tiles[i].data.instruction;
  242.  
  243.             if(tiles[i].data.move == "BACKSWARDS") {
  244.                 var backwards = tiles[i].data.steps;
  245.                 // Move playerTurn(1 or 2 whoever rolled) backwards amount of steps
  246.  
  247.             } else if(tiles[i].data.move == "FORWARDS") {
  248.                 var forwards = tiles[i].data.steps;
  249.                 // Move playerTurn(1 or 2 whoever rolled) forwards amount of steps
  250.  
  251.             } else if(tiles[i].data.move == "GAMBLE") {
  252.                 // Current player needs to roll again
  253.                 var roll = "";
  254.                 if((roll == 2) || (roll == 4) || (roll == 6)) {
  255.                     // move players bead forwards steps equal to roll
  256.                 } else [
  257.                     // move players bead backwards steps equal to roll
  258.                 ]
  259.  
  260.             } else if(tiles[i].data.move == "SKIP") {
  261.                 var roll = tiles[i].data.roll;
  262.                 // Current players next turn will be skipped????? How?
  263.                 var skipNext = [
  264.                     {player: playerTurn, }
  265.                 ]; // store this in gloabal varialbe and check for true if playerTurn == this playerTurn and remove it after said player has skipped turn? Check if its true everytime and if it is skip then remove?
  266.  
  267.             } else if(tiles[i].data.move == "ROLLHIGH") {
  268.                 var roll = tiles[i].data.roll;
  269.                 //
  270.  
  271.             }
  272.         } else {
  273.             // Switch Player
  274.             trapContainer.style.display = "none";
  275.         }
  276.     }
  277. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement