xxxxxxxxxx
350
let character;
let enemy = [];
let pawn;
let rook;
let knight;
let bishop;
let queen;
let king;
let anywhere;
//make a hole where you lose if you land there
//lowkey the same as the hole but an obstacle you can't surpass
//and thats pretty much what enemy is now so make enemy capturable
//checks
//en passant
//castling
//make other pieces not able to go into enemy aswell
function setup() {
createCanvas(800, 800);
character = new Character();
enemy[0] = new Enemy(300, 100);
enemy[1] = new Enemy(400, 100);
//decides which piece you are
pawn = !false;
rook = false;
knight = false;
bishop = false;
queen = false;
king = false;
anywhere = false;
}
function draw() {
background(200, 200, 255);
character.show();
//the board
for (let x = 0; x < width; x += 100) {
for (let y = 0; y < height; y += 100) {
if ((x/100 + y/100)/2 == round((x/100 + y/100)/2)) {
fill(255);
} else {
fill(100, 200, 100);
}
rect(x, y, 100, 100);
}
}
for (let i = 0; i < enemy.length; i++) {
enemy[i].show();
}
//lets you go anywhere when you're in anywhere mode
if (anywhere && mouseIsPressed) {
character.x = round(mouseX - 50, -2);
character.y = round(mouseY - 50, -2);
}
//promotes automatically to queen
if (character.y == 0 && pawn) {
pawn = false;
queen = true;
}
//captures... an issue
for (let i = 0; i < enemy.length; i++) {
if (character.x == enemy[i].x && character.y == enemy[i].y) {
console.log(enemy[i].x);
//splice(enemy, i);
}
}
}
class Character {
constructor(){
this.x = 400;
this.y = 600;
}
show() {
//character
fill(50, 255, 50);
stroke(0);
rect(this.x + 25, this.y + 25, 50, 50);
//shows where you can go
noFill();
stroke(0);
strokeWeight(2);
if (pawn) {
//shows if a piece isnt in front
for (let i = 0; i < enemy.length; i++) {
if (character.x != enemy[i].x && character.y - 100 != enemy[i].y) {
rect(this.x + 25, this.y - 75, 50, 50);
//double movement from starting position if no piece in front
if (this.y == height - 200) {
for (let i = 0; i < enemy.length; i++) {
if (character.x != enemy[i].x && character.y - 200 != enemy[i].y) {
rect(this.x + 25, this.y - 175, 50, 50);
}
}
}
}
}
}
if (rook) {
for (let y = 0; y < this.y; y += 100) {
rect(this.x + 25, y + 25, 50, 50);
}
for (let y = height; y > this.y; y -= 100) {
rect(this.x + 25, y + 25, 50, 50);
}
for (let x = 0; x < this.x; x += 100) {
rect(x + 25, this.y + 25, 50, 50);
}
for (let x = width; x > this.x; x -= 100) {
rect(x + 25, this.y + 25, 50, 50);
}
}
if (knight) {
rect(this.x + 225, this.y - 75, 50, 50);
rect(this.x - 175, this.y - 75, 50, 50);
rect(this.x + 125, this.y - 175, 50, 50);
rect(this.x - 75, this.y - 175, 50, 50);
rect(this.x + 225, this.y + 125, 50, 50);
rect(this.x - 175, this.y + 125, 50, 50);
rect(this.x + 125, this.y + 225, 50, 50);
rect(this.x - 75, this.y + 225, 50, 50);
}
if (bishop) {
for (let i = width; i > 0; i -= 100) {
rect(this.x - i + 25, this.y - i + 25, 50, 50);
rect(this.x - i + 25, this.y + i + 25, 50, 50);
rect(this.x + i + 25, this.y - i + 25, 50, 50);
rect(this.x + i + 25, this.y + i + 25, 50, 50);
}
}
if (queen) {
bishop = true;
rook = true;
}
if (king) {
rect(this.x - 75, this.y - 75, 50, 50);
rect(this.x + 25, this.y - 75, 50, 50);
rect(this.x + 125, this.y - 75, 50, 50);
rect(this.x - 75, this.y + 25, 50, 50);
rect(this.x + 125, this.y + 25, 50, 50);
rect(this.x - 75, this.y + 125, 50, 50);
rect(this.x + 25, this.y + 125, 50, 50);
rect(this.x + 125, this.y + 125, 50, 50);
}
}
}
class Enemy {
constructor(x, y) {
this.x = x;
this.y = y;
}
show() {
//enemy
fill(255, 100, 100, 200);
noStroke();
rect(this.x, this.y, 100, 100);
}
}
function mousePressed() {
//so you can't leave the board
if (mouseX < width && mouseY < height) {
//moves you where you click for each piece
if (pawn) {
//doesnt allow you to advance into an enemy
for (let i = 0; i < enemy.length; i++) {
if (character.x != enemy[i].x && character.y - 100 != enemy[i].y) {
//normal movement
if (mouseX > character.x + 25 && mouseX < character.x + 75 && mouseY > character.y - 75 && mouseY < character.y - 25) {
character.y -= 100;
}
//allows for double movement at the start given no piece in front
for (let i = 0; i < enemy.length; i++) {
if (character.x != enemy[i].x && character.y - 200 != enemy[i].y) {
if (character.y == height - 200) {
if (mouseX > character.x + 25 && mouseX < character.x + 75 && mouseY > character.y - 175 && mouseY < character.y - 125) {
character.y -= 200;
}
}
}
}
}
}
}
if (rook) {
for (let i = 0; i < character.y; i += 100) {
if (mouseX > character.x + 25 && mouseX < character.x + 75 && mouseY > i + 25 && mouseY < i + 75) {
//x stays the same
character.y = i;
}
}
for (let i = height; i > character.y; i -= 100) {
if (mouseX > character.x + 25 && mouseX < character.x + 75 && mouseY > i + 25 && mouseY < i + 75) {
//x stays the same
character.y = i;
}
}
for (let i = 0; i < character.x; i += 100) {
if (mouseX > i + 25 && mouseX < i + 75 && mouseY > character.y + 25 && mouseY < character.y + 75) {
//y stays the same
character.x = i;
}
}
for (let i = width; i > character.x; i -= 100) {
if (mouseX > i + 25 && mouseX < i + 75 && mouseY > character.y + 25 && mouseY < character.y + 75) {
//y stays the same
character.x = i;
}
}
}
if (knight) {
for (let y = -75; y == -75; y++) {
for (let x = 225; x >= -175; x -= 400) {
if (mouseX > character.x + x && mouseX < character.x + x + 50 && mouseY > character.y + y && mouseY < character.y + y + 50) {
character.x += x - 25;
character.y += y - 25;
}
}
}
for (let y = -175; y == -175; y++) {
for (let x = 125; x >= -75; x -= 200) {
if (mouseX > character.x + x && mouseX < character.x + x + 50 && mouseY > character.y + y && mouseY < character.y + y + 50) {
character.x += x - 25;
character.y += y - 25;
}
}
}
for (let y = 125; y == 125; y++) {
for (let x = 225; x >= -175; x -= 400) {
if (mouseX > character.x + x && mouseX < character.x + x + 50 && mouseY > character.y + y && mouseY < character.y + y + 50) {
character.x += x - 25;
character.y += y - 25;
}
}
}
for (let y = 225; y == 225; y++) {
for (let x = 125; x >= -75; x -= 200) {
if (mouseX > character.x + x && mouseX < character.x + x + 50 && mouseY > character.y + y && mouseY < character.y + y + 50) {
character.x += x - 25;
character.y += y - 25;
}
}
}
}
if (bishop) {
for (let i = width; i > 0; i -= 100) {
if (mouseX > character.x - i + 25 && mouseX < character.x - i + 75 && mouseY > character.y - i + 25 && mouseY < character.y - i + 75) {
character.x -= i;
character.y -= i;
}
if (mouseX > character.x - i + 25 && mouseX < character.x - i + 75 && mouseY > character.y + i + 25 && mouseY < character.y + i + 75) {
character.x -= i;
character.y += i;
}
if (mouseX > character.x + i + 25 && mouseX < character.x + i + 75 && mouseY > character.y - i + 25 && mouseY < character.y - i + 75) {
character.x += i;
character.y -= i;
}
if (mouseX > character.x + i + 25 && mouseX < character.x + i + 75 && mouseY > character.y + i + 25 && mouseY < character.y + i + 75) {
character.x += i;
character.y += i;
}
}
}
if (king) {
if (mouseY > character.y - 75 && mouseY < character.y - 25) {
if (mouseX > character.x - 75 && mouseX < character.x - 25) {
character.x -= 100;
character.y -= 100;
} else if (mouseX > character.x + 25 && mouseX < character.x + 75) {
character.y -= 100;
} if (mouseX > character.x + 125 && mouseX < character.x + 175) {
character.x += 100;
character.y -= 100;
}
}
if (mouseY > character.y + 25 && mouseY < character.y + 75) {
if (mouseX > character.x - 75 && mouseX < character.x - 25) {
character.x -= 100;
} else if (mouseX > character.x + 125 && mouseX < character.x + 175) {
character.x += 100;
}
}
if (mouseY > character.y + 125 && mouseY < character.y + 175) {
if (mouseX > character.x - 75 && mouseX < character.x - 25) {
character.x -= 100;
character.y += 100;
} else if (mouseX > character.x + 25 && mouseX < character.x + 75) {
character.y += 100;
} if (mouseX > character.x + 125 && mouseX < character.x + 175) {
character.x += 100;
character.y += 100;
}
}
}
}
}
function keyPressed() {
//pick which piece you are using keyboard
pawn = false;
rook = false;
knight = false;
bishop = false;
queen = false;
king = false;
anywhere = false;
if (keyCode === 80) {
pawn = true;
}
if (keyCode === 82) {
rook = true;
}
if (keyCode === 78) {
knight = true;
}
if (keyCode === 66) {
bishop = true;
}
if (keyCode === 81) {
queen = true;
}
if (keyCode === 75) {
king = true;
}
if (keyCode === 17) {
anywhere = true;
}
}