//Jump_Man
//Paolo Di Filippo
//Student ID: 991442028
//Using the 'Player' class, declaring an object called 'player1'
Player player1;
//Using the 'Platform' class, declaring an array of 'Platform' objects
Platform [] platformArray;
void setup() {
size(800, 600);
//Initializing the 'player1' object as a new object of the 'Player' class
player1 = new Player();
//Initializing an array of objects from the 'Platform' class
platformArray = new Platform[6];
//Using y for the value passed through the for loop to indicate this loop is used for the y location of a platform. Platforms will only be
for (int y = 475; y >= 100; y -= 75) {
//Print the output of the formula to cycle through my array (for debugging)
//println((y-100)/75);
//Draws platforms in the array using random x coordinates, a y that is changed through each cycle of the above for loop,
//a conditional statement (generate a random number between 1 and -1, is that number greater than 0?
//If so, evaluate the statement as true and pass that boolean as the value for a platform direction, else its false and that value is passed instead),
//and a random value being cast as an int to make the length of the platform
platformArray[(y-100)/75] = new Platform(random(100, 700), y, random(-1, 1) > 0 ? true : false, (int) random(50, 150));
}
}
void draw() {
background(255);
player1.drawPlayer();
player1.playerMovement();
//A for loop that increments the location in the array and runs the methods inside 'Platform' for each instance of the array
for (int i = 0; i < platformArray.length; i++) {
platformArray[i].drawPlatform();
//Print the location in the array (for debugging)
//println(platformArray[i]);
platformArray[i].movePlatform();
}
}
/*CHECK KEY CODES AND SET MOVEMENT BOOLEANS*/
void keyPressed() {
if (keyCode == LEFT) {
player1.playerLeft = true;
} else if (keyCode == RIGHT) {
player1.playerRight = true;
} else if (keyCode == ' ' || keyCode == UP) {
player1.playerJump = true;
}
}
/*CHECK KEY CODES AND SET MOVEMENT BOOLEANS*/
/*RECHECK KEY CODES AND RESET MOVEMENT BOOLEANS WHEN KEYS ARE RELEASED*/
void keyReleased() {
if (keyCode == LEFT) {
player1.playerLeft = false;
} else if (keyCode == RIGHT) {
player1.playerRight = false;
} else if (keyCode == ' ' || keyCode == UP) {
player1.playerJump = false;
}
}
/*RECHECK KEY CODES AND RESET MOVEMENT BOOLEANS WHEN KEYS ARE RELEASED*/class Platform {
PVector platformPosition;
float platformX;
float platformY;
float platformSpeed;
//True = move to the right, False = move to the left
boolean platformDirection;
int platformLength;
Platform(float tempX, float tempY, boolean tempDirection, int tempLength) {
platformX = tempX;
platformY = tempY;
platformSpeed = 1;
platformDirection = tempDirection;
platformLength = tempLength;
}
Platform() {
platformX = 0;
platformY = 0;
platformSpeed = 1;
platformDirection = false;
platformLength = 0;
}
/*DRAW PLATFORM*/
void drawPlatform() {
fill(255, 0, 0);
rect(platformX, platformY, platformLength, 25);
}
/*DRAW PLATFORM*/
/*MOVE PLATFORM*/
void movePlatform() {
//Move the platform from left to right
if (platformDirection) {
platformX += platformSpeed;
//If the platform goes offscreen to the right, reset it offscreen to the left
if (platformX > 900) {
platformX = -100;
}
//Move the platform
} else if (!platformDirection) {
platformX -= platformSpeed;
if (platformX < -100) {
platformX = 900;
}
}
}
/*MOVE PLATFORM*/
}class Player {
float playerX;
float playerY;
float playerHorizontalSpeed;
float playerVerticalSpeed;
PVector playerPosition;
float jumpSpeed;
float gravity;
boolean playerLeft;
boolean playerRight;
boolean playerJump;
//Constructor
Player() {
playerX = 400;
playerY = 500;
playerHorizontalSpeed = 1;
playerVerticalSpeed = 0;
playerPosition = new PVector(playerX, playerY);
jumpSpeed = -5;
gravity = 0.5;
playerLeft = false;
playerRight = false;
playerJump = false;
}
/*DRAW PLAYER*/
void drawPlayer() {
rectMode(CENTER);
strokeWeight(3);
stroke(0);
fill(0, 0, 255);
//Player character rectangle
rect(playerPosition.x, playerPosition.y, 25, 50);
}
/*DRAW PLAYER*/
/*MOVE PLAYER USING BOOLEANS FROM KEYPRESSES*/
void playerMovement() {
if (playerLeft) {
//Use a speed value to change the player's position
playerPosition.x -= playerHorizontalSpeed;
//Increase the value that affects the player's position to create acceleration
playerHorizontalSpeed++;
//Limit the max speed the player can move at
if (playerHorizontalSpeed >= 10) {
playerHorizontalSpeed = 10;
}
} else if (playerRight) {
playerPosition.x += playerHorizontalSpeed;
playerHorizontalSpeed++;
if (playerHorizontalSpeed >= 10) {
playerHorizontalSpeed = 10;
}
}
//If no keys are being pressed, reset the player's speed so that they accelerate again next time they move
else {
playerHorizontalSpeed = 1;
}
//The 'playerJump' is given a seperate if statement to allow for simaltaneous input with a direction. This way the player can move and jump at the same time
playerPosition.y += playerVerticalSpeed;
//This 'println' was used to help me know my player's y position when they were off screen, and how drastically the 'playerVerticalSpeed' affected the y position (for debugging)
//println(playerVerticalSpeed);
//When jumping, add the 'jumpSpeed' to the 'playerVerticalSpeed'
if (playerJump) {
playerVerticalSpeed += jumpSpeed;
//This is to prevent the player from infinitely jumping off screen. This could be elaborated on by having a function that evaluates if the player has jumped, and if they have stop applying the 'jumpSpeed' to the 'playerVerticalSpeed'
if (playerPosition.y <= 0) {
jumpSpeed = 0;
} else {
jumpSpeed = -3;
}
} else {
//Otherwise check to see if the player is on the ground, and if they are reset the 'playerVerticalSpeed' to zero
if (playerPosition.y >= 500) {
playerPosition.y = 500;
playerVerticalSpeed = 0;
//If the player is not on the ground, add 'gravity' to 'playerVerticalSpeed' to bring them back to the ground
} else {
playerVerticalSpeed += gravity;
}
}
}
/*MOVE PLAYER USING BOOLEANS FROM KEYPRESSES*/
}