W,A,S,D (Player 1) + Arrow Keys (Player 2)
A fork of [OOP] 1 Player Movement + Stop Obstacle (p5.js) **WIP** by Juan Olaya
xxxxxxxxxx
/*
***** 2 PLAYERS AND BLOCKS *****
by Juan Olaya
https://juanolaya.github.io/
*/
var player1;
var player2;
var obstacleList=[];
function setup() {
createCanvas(320,480);
player1 = new Player(width/2 , height*2/3, '#7180AC');
player2 = new Player(width/2 , height*1/3, '#97A2C2');
var obstacleTemp = new Obstacle(width/2-50,height/2-15,100,30);
obstacleList.push(obstacleTemp);
obstacleTemp = new Obstacle(width/2-80,height/2-15,30,100);
obstacleList.push(obstacleTemp);
obstacleTemp = new Obstacle(width/2+50,height/2-85,30,100);
obstacleList.push(obstacleTemp);
}
function draw() {
background('#E49273');
player1.display();
player2.display();
collisionPlayersObstacles();
movementPlayers();
texto();
bordes();
}
function collisionPlayersObstacles(){
player1.collisionSides=[0,0];
player2.collisionSides=[0,0];
for(let i=0; i<obstacleList.length ; i++){
// Player 1 vs Obstacles
let isCollidingPlayer1 = player1.collisionPlayerObstacle(obstacleList[i].xpos, obstacleList[i].ypos, obstacleList[i].widthObst,obstacleList[i].heightObst );
// Player 2 vs Obstacles
let isCollidingPlayer2 = player2.collisionPlayerObstacle(obstacleList[i].xpos, obstacleList[i].ypos, obstacleList[i].widthObst,obstacleList[i].heightObst );
obstacleList[i].display(isCollidingPlayer1 || isCollidingPlayer2);
}
}
function movementPlayers(){
//MOVIMIENTO PLAYER 1
//Invoca a los métodos de mover de la clase Player para el objecto player 1
if (keyIsDown(LEFT_ARROW)) {
player1.moveLeft();
}
if (keyIsDown(RIGHT_ARROW)) {
player1.moveRight();
}
if (keyIsDown(UP_ARROW)) {
player1.moveUp();
}
if (keyIsDown(DOWN_ARROW)) {
player1.moveDown();
}
//MOVIMIENTO PLAYER 2
//Invoca a los métodos de mover de la clase Player para el objecto player 2
if (keyIsDown(65)){ // 65 -> a || A
player2.moveLeft();
}
if (keyIsDown(68)){ // 68 -> d || D
player2.moveRight();
}
if (keyIsDown(87)){ // 87 -> w || W
player2.moveUp();
}
if (keyIsDown(83)){ // 83 -> s || S
player2.moveDown();
}
}
function texto(){
textSize(20);
text("Collision Sides of the Player 1: "+player1.collisionSides[0],10,height-40);
text("Collision Sides of the Player 1: "+player1.collisionSides[1],10,height-20);
text("Collision Sides of the Player 2: "+player2.collisionSides[0],10,20);
text("Collision Sides of the Player 2: "+player2.collisionSides[1],10,40);
}
function bordes() {
noFill();
stroke('#7180AC'); // #A75265
strokeWeight(2);
rect(0, 0, width - 1, height - 1, 25);
noStroke();
}