//global variables that will be used throughout the "game"
float x = 120; float y = -40;
boolean stopped = true; //the game is still not activated
float speed = 0; //velocity
float gravity = 0.1; //value of gravity
Player onlyPlayer;
void setup()
{
size(500,500);
onlyPlayer = new Player();
smooth();
//creating squares
keyPressed();
for(x=120;x<360;x+=60)
{
fill(125);
rectMode(CENTER);
stroke(0);
rect(x,y,30,30);
}
if (!stopped){ //if the game has been activated, run whatever is below
//Adding velocity to the squares
y= y+speed;
//Adding gravity to the speed
speed = speed + gravity;
//Damping effect on the squares
if (y>height)
{
if(y==height-15)
{speed=0; gravity=0;}
//Multiplying speed by -0.9 triggers the damping effect.
//HOWEVER, when the squares reach the very bottom and apparently lose any acceleration or speed
//there's a horrible bug, you will notice... just paste it onto your processing
speed = speed * -0.9;
background(125); //Makes the screen flash when the squares hit the bottom
if (speed == 0) //Attempt to create a restarting system to the game
{restart();}
}
else if (y>height+1)
{speed = speed+0.5*-1;} //Attempt to prevent the bug from taking place
}
}
void keyPressed()
{
if (key=='c')
{
stopped = false;//verifies if the game's already started
}
}
void restart(){//I STILL NEED TO MAKE IT WORK SOMEHOW
stopped = true;
onlyPlayer = new Player();
}
*------------------------------------------------*
//Paste whatever is below on a different tab.. which will be the class for the player (blue rectangle)
//I'm also having difficulties to make edge collisions... I am using PVectors and it's currently providing me
//headaches only... HELP PLEASE
*-----------------------------------------------*
//player's class
class Player
{
PVector charPos = new PVector();
PVector velocity = new PVector();
Player()
{
charPos = new PVector(5,490);
velocity = new PVector(2,0);
keyPressed();
}
void keyPressed()
{
if (key == 'd') {velocity.x += 0.02;}
if (key == 'a') {velocity.x -=0.02;}
if (key == 'w') {velocity.y -= 0.02;}
if (key == 's') {velocity.y += 0.02;}
}
void update()
{
charPos.add(velocity); //position + velocity = movement of the character
if (charPos.y > 490) //Trying to deal with edge collisions
{
charPos.y = 490;
}
else if(charPos.y <=10)
{
charPos.y =10;
}
else if(charPos.x==5){velocity.x =0; velocity.y=0;} //Desperate attempt to make the edge collisions work!HELP!
else if(charPos.x>=495){charPos.x =495;}
else if(charPos.x<=5 && charPos.y<=10){charPos.x =5; charPos.y=10;}
}