//Plans
// (x) Create the Environment
// (x) Make the player move
// (x) Restrict moving into the walls of the stage
// (x) Add smooth physics
// (x) Warp the players back on stage when they fall
// (x) Make the game 2 Player
// ( ) Prevent the game from overriding inputs (because of the usage of key)
// ( ) Create hit detection for attacking and when the blocks make contact
// ( ) Implement the Attacking
////////////////////////////////VARIABLES AND DECLARATION
//Player 1's physics variables
float p1gravity; //The base gravity of Player 1
float p1gravIncrease; // The value that gravity is multiplied by when falling for long times
float p1X;//player 1's X
float p1Y;//player 1's Y
float p1VelocityX;//The horizontal value that player 1 moves with
float p1VelocityY;//The vertical value that player 1 moves with
float p1TempY;//The vertex of Player 1's jump
boolean p1Grounded;//The boolean that handles whether or not player 1 is on the ground
boolean p1MoveLeft;//if the A key is held down, this is true
boolean p1MoveRight;//if the D key is held down, this is true
boolean p1HoldJump;//if W is held down, this is true and the player will continue to jump
//Player 2's physics variables (refer to player 1's values for more info)
float p2gravity;
float p2gravIncrease;
float p2X;
float p2Y;
float p2VelocityX;
float p2VelocityY;
float p2TempY;
boolean p2Grounded;
boolean p2MoveLeft;
boolean p2MoveRight;
boolean p2HoldJump;
//Variables for the drawing of the stage
float stageX; //stage x
float stageY; //stage y
float stageW; //stage width
float stageH; //stage height
//Setup just establishes window size and assigns values to variables
void setup() {
size(400, 400);
p1gravity = 1.6;
p1gravIncrease = 1;
p2gravity = 1.6;
p2gravIncrease = 1;
p1VelocityX = 0;
p1VelocityY = 0;
p1X = 140;
p1Y = 225;
p2VelocityX = 0;
p2VelocityY = 0;
p2X = 250;
p2Y = 225;
stageX = 200;
stageY = 317;
stageW = 185;
stageH = 166;
p1Grounded = true;
p1MoveLeft = false;
p1MoveRight = false;
p1HoldJump = false;
p2Grounded = true;
p2MoveLeft = false;
p2MoveRight = false;
p2HoldJump = false;
}
//////////////////////////////////////////////THE FOLLOWING FUNCTIONS ARE FOR CREATING GAME ASSETS
//createBackground creates the gradient that the background has
void createBackground() {
background(100, 100, 200);
for (int i = 1; i <= 9; i++) {
fill(100-(2*i*5), 100-(2*i*5), 200-(2*i*5));
rect(200, 45*i, 400, 50);
}
}
//createPlayer establishes the player models for both player one and two, it's also responsible for reseting them if they fall
void createPlayer(float x, float y, float w, float h, int r, int g, int b) {//asks for rgb and positioning variables
//this if statement places player 1 back into frame if they fall off the bottom of the screen in addition to reseting their physics
if (p1Y > 419) {
p1Y = 150;
p1gravity = 1.6;
p1X = 200;
p1Grounded = false;
}
//this if statement places player 2 back into frame if they fall off the bottom of the screen in addition to reseting their physics
if (p2Y > 419) {
p2Y = 150;
p2gravity = 1.6;
p2X = 200;
p2Grounded = false;
}
//the rest of the function draws the player and their squares
rectMode(CENTER);
noStroke();
fill(r, g, b);//fill with the given rgb values
rect(x, y, w, h);//create the rect with the given position and size values
}
//createStage draws the stage for the players to move on
void createStage() {
fill(100, 90, 70);//create a brown fill
rect(stageX, stageY, stageW, stageH); //use the stage variables to place it
}
/////////////////////////////////////////////////////////THE FOLLOWING FUNCTIONS HANDLE PLAYER 1'S MOVEMENT AND PHYSICS
//p1Movement handles all of player 1's jumping and moving
void p1Movement() {
//if the jump key is presssed or held
if (key == 'w' && p1Grounded == true || p1HoldJump == true) {
p1TempY = p1Y;//set the vertex of the jump
p1VelocityY = -4.5;//start moving
p1HoldJump = true;//turn this boolean to true in case the player holds the key
p1Grounded = false;//they are moving up so they aren't grounded
}
//if the fastfall key is pressed or held and the player is moving downwards
if (key == 's' && p1Grounded != true && p1VelocityY == 0) {
p1Y = p1Y + p1VelocityY + p1gravity*1.03;//start applying the increase
}
//if the left key is pressed or held
if ((key == 'a' && p1MoveLeft == false) || p1MoveLeft == true) {
p1MoveLeft = true;//turn this boolean to true in case it's held
p1VelocityX = -2;//apply the X velocity
//IF THE PLAYER IS FALLING AND MOVING AGAINST THE STAGE WALL ON THE RIGHT SIDE
if (p1Y > 225 && p1X <= 302 && p1X > 290) {
p1VelocityX = 0;//revent them from moving through the wall
}
p1X = p1X + p1VelocityX;//add the X velocity to the player X
}
//if the right key is pressed or held
if ((key == 'd' && p1MoveRight == false) || p1MoveRight == true) {
p1MoveRight = true;// set the boolean to true if the key is held
p1VelocityX = 2;//apply the X velocity
//IF THE PLAYER IS FALLING AND MOVING INTO THE STAGE WALL ON THE LEFT SIDE
if (p1Y > 225 && p1X >= 100 && p1X < 110) {
p1VelocityX = 0;//prevent them from phasing through the wall
}
p1X = p1X + p1VelocityX;//add X velocity to player X
}
}
//p1Airborne caps the jump height and the pull of the gravity
void p1Airborne() {
//if the player is not grounded
if (p1Grounded != true) {
p1gravIncrease = 1.035;//change the incremental increase from 1 to 1.035
//if the player reaches the apex of their jump, reset the variables to give that smooth arc pattern to the jump
if (p1TempY - 70 >= p1Y) {
p1gravity = 1.6;
p1VelocityY = 0;
}
//if the gravity is less than 10, multiple gravity by grav increase
if (p1gravity < 10) {
p1gravity = p1gravity * p1gravIncrease;//this is done to give the gravity a bit more pull
}
p1Y = p1Y + p1VelocityY + p1gravity;//calculate Y changes
}
}
//p1OnStage determines when p1Grounded should be true or false
void p1OnStage() {
//if the player is standing on the stage
if (p1X >= 102 && p1X <= 299 && p1Y >= 223) {
p1Grounded = true;//grounded should be true
p1VelocityY = 0;//stop vertical movement
p1Y = (int)p1Y;//round the Y value to an int
//this assures that the Y value doesn't round the player halfway into the floor
if (p1Y >= 223 && p1Y <= 235) {
p1Y = 225;
}
//reset gravity and change the increase to 1
p1gravity = 1.6;
p1gravIncrease = 1;
}
//if the player is too far off to one of the sides of the stage, they are not grounded
if (p1X < 102 || p1X > 299) {
p1Grounded = false;
}
}
//////////////////////////////////////////////////THE FOLLOWING FUNCTIONS ARE FOR PLAYER 2'S MOVEMENT AND PHYSICS
//p2Movement handles player 2's movement just like player 1 but it used keyCode instead
void p2Movement() {
if (keyCode == UP && p2Grounded == true || p2HoldJump == true) {
p2TempY = p2Y;
p2VelocityY = -4.5;
p2Grounded = false;
p2HoldJump = true;
}
if (keyCode == DOWN && p2Grounded != true && p2VelocityY == 0) {
p2Y = p2Y + p2VelocityY + p2gravity*1.03;
}
if ((keyCode == LEFT && p2MoveLeft == false) || p2MoveLeft == true) {
p2MoveLeft = true;
p2VelocityX = -2;
if (p2Y > 225 && p2X <= 302 && p2X > 290) {
p2VelocityX = 0;
}
p2X = p2X + p2VelocityX;
}
if ((keyCode == RIGHT && p2MoveRight == false) || p2MoveRight == true) {
p2MoveRight = true;
p2VelocityX = 2;
if (p2Y > 225 && p2X >= 100 && p2X < 110) {
p2VelocityX = 0;
}
p2X = p2X + p2VelocityX;
}
}
//p2Airborne caps the jump height and gravity pull
void p2Airborne() {
//if the player is not grounded
if (p2Grounded != true) {
p2gravIncrease = 1.035;//change the incremental increase from 1 to 1.035
//if the player reaches the apex of their jump, reset the variables to give that smooth arc pattern to the jump
if (p2TempY - 70 >= p2Y) {
p2gravity = 1.6;
p2VelocityY = 0;
}
//if the gravity is less than 10, multiple gravity by grav increase
if (p2gravity < 10) {
p2gravity = p2gravity * p2gravIncrease;//this is done to give the gravity a bit more pull
}
p2Y = p2Y + p2VelocityY + p2gravity;//calculate the Y using gravity and velocity
}
}
//p2OnStage determines when grounded is true or false
void p2OnStage() {
//if the player is standing on the stage
if (p2X >= 102 && p2X <= 299 && p2Y >= 223) {
p2Grounded = true;//set the grounded
p2VelocityY = 0;//remove Y velocity
p2Y = (int)p2Y;//round the Y value to an int
//this assures that the Y value doesn't round the player halfway into the floor
if (p2Y >= 223 && p2Y <= 235) {
p2Y = 225;
}
//reset gravity and change the increase to 1
p2gravity = 1.6;
p2gravIncrease = 1;
}
//if the player is too far off to one of the sides of the stage, they are not grounded
if (p2X < 102 || p2X > 299) {
p2Grounded = false;
}
}
/////////////////////////////////////////////////////////////////KEYRELEASED IS FOR TURNING OFF CERTAIN MOVEMENT CALLS
void keyReleased() {
/////////If player 1's WASD keys are released
if (key == 'a') {//if Left is released
p1MoveLeft = false;//turn off the left held boolean
p1VelocityX = 0;
key = '=';//set the key to something else so this call isn't repeated
} else if (key == 'd') {//if Right is released
p1MoveRight = false;//turn off the right held boolean
p1VelocityX = 0;
key = '=';//set the key to something else so this call isn't repeated
}
if (key == 'w') {//if Jump is released
p1HoldJump = false;//turn off the jump held boolean
key = '=';//set the key to something else so this call isn't repeated
}
//////////If player 2's arrow keys are released
if (keyCode == LEFT) {
p2MoveLeft = false;//turn off the left held boolean
p2VelocityX = 0;
keyCode = 18;//set the keyCode to something else so this call isn't repeated
} else if (keyCode == RIGHT) {
p2MoveRight = false;//turn off the right held boolean
p2VelocityX = 0;
keyCode = 18;//set the keyCode to something else so this call isn't repeated
}
if (keyCode == UP) {
p2HoldJump = false;//turn off the jump held boolean
keyCode = 18;//set the keyCode to something else so this call isn't repeated
}
}
////////////////////////////////////////////////////////////////////////////DRAW LOOP FOR CALLING EVERYTHING
void draw() {
//create the assets of the game
createBackground();
createStage();
createPlayer(p1X, p1Y, 18, 18, (int)random(175), 200, 200);
createPlayer(p2X, p2Y, 18, 18, (int)random(100), (int)random(100), 200);
//Call player 1's functions
p1Movement();
p1Airborne();
p1OnStage();
//Call player 2's functions
p2Movement();
p2Airborne();
p2OnStage();
}