xxxxxxxxxx
157
// Define the board size and spacing
const rows = 10;
const cols = 10;
const cellSize = 60;
let img;
function preload() {
img = loadImage('Snake and Ladders.png');
snake = loadImage('snake1.png');
}
// Define the board layout
const board = [
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, 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, 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, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
// Define the ladders and snakes
const ladders = [
{ start: 2, end: 22 },
{ start: 12, end: 58 },
{ start: 25, end: 57 },
{ start: 38, end: 70 },
{ start: 45, end: 98 }
];
const snakes = [
{ start: 99, end: 1 },
{ start: 87, end: 34 },
{ start: 74, end: 19 },
{ start: 60, end: 5 },
{ start: 51, end: 6 }
];
// Define the current player and position
let currentPlayer = 1;
let playerPositions = [0, 0];
function setup() {
createCanvas(cellSize * cols, cellSize * rows);
textAlign(CENTER, CENTER);
textSize(32);
}
function draw() {
image(img, 0, 0);
background(220);
drawBoard();
drawLadders();
drawSnakes();
drawPlayers();
drawDice();
checkForWin();
}
function drawBoard() {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
let x = j * cellSize;
let y = i * cellSize;
stroke(0);
noFill();
rect(x, y, cellSize, cellSize);
fill(0);
textSize(16);
text(i * cols + j + 1, x + cellSize / 2, y + cellSize / 2);
}
}
}
function drawLadders() {
for (let ladder of ladders) {
let start = ladder.start;
let end = ladder.end;
let startX = (start % cols) * cellSize + cellSize / 2;
let startY = floor(start / cols) * cellSize + cellSize / 2;
let endX = (end % cols) * cellSize + cellSize / 2;
let endY = floor(end / cols) * cellSize + cellSize / 2;
stroke(0, 255, 0);
line(startX, startY, endX, endY);
}
}
function drawSnakes() {
for (let snake of snakes) {
let start = snake.start;
let end = snake.end;
let startX = (start % cols) * cellSize + cellSize / 2;
let startY = floor(start / cols) * cellSize + cellSize / 2;
let endX = (end % cols) * cellSize + cellSize / 2;
let endY = floor(end / cols) * cellSize + cellSize / 2;
stroke(255, 0, 0);
line(startX, startY, endX, endY);
}
}
function drawPlayers() {
for (let i = 0; i < playerPositions.length; i++) {
let pos = playerPositions[i];
let x = (pos % cols) * cellSize + cellSize / 2;
let y = floor(pos / cols) * cellSize + cellSize / 2;
fill(i === 0 ? color(255, 255, 0) : color(0, 0, 255));
ellipse(x, y, cellSize / 2);
}
}
function drawDice() {
fill(255);
rect(cellSize * cols + 20, 20, 100, 100);
fill(0);
textSize(64);
textAlign(CENTER, CENTER);
text(getDiceRoll(), cellSize * cols + 70, 70);
}
function getDiceRoll() {
return floor(random(1, 7));
}
function checkForWin() {
if (playerPositions[currentPlayer] === rows * cols - 1) {
fill(currentPlayer === 0 ? color(255, 255, 0) : color(0, 0, 255));
textSize(64);
textAlign(CENTER, CENTER);
text("Player " + (currentPlayer + 1) + " wins!", width / 2, height / 2);
noLoop();
}
}
function keyPressed() {
let roll = getDiceRoll();
playerPositions[currentPlayer] += roll;
playerPositions[currentPlayer] = checkSnakeOrLadder(playerPositions[currentPlayer]);
currentPlayer = (currentPlayer + 1) % 2;
}
function checkSnakeOrLadder(pos) {
for (let ladder of ladders) {
if (ladder.start === pos) {
return ladder.end;
}
}
for (let snake of snakes) {
if (snake.start === pos) {
return snake.end;
}
}
return pos;
}