Browse Source

Funktion hinzugefügt, um Schlangen und Leitern auf dem Spielbrett zu zeichnen

main
Richard Halsall 2 years ago
parent
commit
72b15be403
  1. 7
      schlangen_und_leitern/js/Bridge.js
  2. 119
      schlangen_und_leitern/js/makeBoard.js

7
schlangen_und_leitern/js/Bridge.js

@ -28,8 +28,13 @@ function init(playerCount){
////////experiemnt ////////experiemnt
var canvasPointArr= []; var canvasPointArr= [];
canvasPointGen(canvasPointArr);
drawCanavas(); drawCanavas();
canvasPointGen(canvasPointArr);
createBoardObjects(canvasPointArr, objectArr)
} }
function gameloop (boardArr, playerArray, playerCount){ function gameloop (boardArr, playerArray, playerCount){

119
schlangen_und_leitern/js/makeBoard.js

@ -116,7 +116,7 @@ function drawCanavas(){
//canvas point objects //canvas point objects
function canvasPoint () { function canvasPoint () {
this.cellNumber
this.cell
this.x this.x
this.y this.y
} }
@ -126,64 +126,141 @@ function canvasPointGen(canvasPointArr){
//get canvas size //get canvas size
var canvas = document.getElementById("canvas"); var canvas = document.getElementById("canvas");
//get induvidual cell sizes //get induvidual cell sizes
cellSize = (canvas.width/10)
halfCell = (cellSize/2)
canvasWidth = canvas.width
cellSize = canvas.width/10
halfCell = cellSize/2
//starting position for both axises //starting position for both axises
xTracker = halfCell
yTracker = halfCell yTracker = halfCell
//tracker for cell count //tracker for cell count
let count = 100 let count = 100
//for each cell create a point at its center
//flip to account for direction change
let direction = true // 0 is left to right, 1 is right to left
//for each cell create a point at its center
for(let y = 0; y < 10; y++){ for(let y = 0; y < 10; y++){
for(let x = 0; x < 10; x++ ){
//create new cell point obj
let newPoint = new canvasPoint()
//set new objects variables
newPoint.cellNumber = count
newPoint.x = xTracker
newPoint.y = yTracker
canvasPointArr.push(newPoint)
//advance x and count
xTracker += cellSize
count--
if (direction == true){
var xTracker = halfCell
for(let x = 0; x < 10; x++ ){
//create new cell point obj
let newPoint = new canvasPoint()
//set new objects variables
newPoint.cell = count
newPoint.x = xTracker
newPoint.y = yTracker
console.log("new point"+ newPoint.cell, newPoint.x, newPoint.y)
canvasPointArr.push(newPoint)
//advance x and count
xTracker += cellSize
//toggle direction
count--
}
}
else if (direction == false){
var xTracker = (canvasWidth - halfCell)
for(let x = 10; x > 0; x--){
//create new cell point obj
let newPoint = new canvasPoint()
//set new objects variables
newPoint.cell = count
newPoint.x = xTracker
newPoint.y = yTracker
console.log("new point"+ newPoint.cell, newPoint.x, newPoint.y)
canvasPointArr.push(newPoint)
//advance x and count
xTracker -= cellSize
//toggle direction
count--
}
} }
yTracker += cellSize yTracker += cellSize
if (direction == true){
direction = false
}
else if (direction == false){
direction = true
} }
} }
}
//function that draws a line using x,y cooridinates of two points //function that draws a line using x,y cooridinates of two points
function drawLine(startPosX, startPosY, endPosX, endPoseY){
function drawLine(type, startPosX, startPosY, endPosX, endPosY){
var c = document.getElementById("canvas"); var c = document.getElementById("canvas");
var ctx = c.getContext("2d"); var ctx = c.getContext("2d");
ctx.beginPath(); ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(537, 537);
ctx.moveTo(startPosX, startPosY);
ctx.lineTo(endPosX, endPosY);
ctx.stroke(); ctx.stroke();
ctx.lineWidth = 10;
switch (type) {
case "l":
ctx.strokeStyle = '#0FD71D';
break;
case "s":
ctx.strokeStyle = '#E21317';
break;
default:
}
ctx.strokeStyle; ctx.strokeStyle;
} }
function boardObjects(){
function createBoardObjects(canvasPointArr, objectArray){
//loop through all object in the object array
for(let i = 0; i < objectArray.length; i++){
let startCell = objectArray[i].start
let endCell = objectArray[i].end
let startPointX;
let startPointY;
let endPointX;
let endPointY;
//find cell points on canvas
for(let j = 0; j < canvasPointArr.length; j++){
if(canvasPointArr[j].cell == startCell){
startPointX = canvasPointArr[j].x;
startPointY =canvasPointArr[j].y
console.log("start found")
}
else if(canvasPointArr[j].cell == endCell){
endPointX = canvasPointArr[j].x;
endPointY =canvasPointArr[j].y
console.log(endPointX, endPointX)
}
}
drawLine(objectArray[i].type,startPointX, startPointY, endPointX, endPointY)
}
}
}

Loading…
Cancel
Save