int ballPositionX = 0;
int ballPositionY = 330;
int speed = 2;
int playerX = 0;
int playerY = 0;
float gravity = 0.4;
float jumpSpeed = 8;
int pointGain = 0;
boolean buttonCheck = true;
float r = random(0,255);
void setup() {
size (400,400);
smooth();
frameRate(30);
}
//draw background
void draw(){
background(0);//makes the background black
noStroke();
fill(255);
rect(0,355,400,200); //draws the floor
score();
drawPillars();
drawBall();
drawPlayer();
playerMove();
println(pointGain);
}
void drawPillars(){
for(int i = 80; i<=260; i+=80){ //repeats the pillar 3 times
fill(255);
rect(i + 20,170,8,160); //drawing the pillar
rect(i + 35,170,8,160);
rect(i + 50,170,8,160);
rect(i + 20,335,40,5); //drawing the base of the pillar
rect(i + 15,345,50,5);
rect(i + 20,160,40,5); //drawing the top of the pillar
rect(i + 15,150,50,5);
}
}
void drawBall(){
//The following code is taken from the textbook: Learning Processing by Daniel Shiffman, page 75 Example 5-6
ballPositionX = ballPositionX + speed;
if ((ballPositionX > width) || (ballPositionX < 0)) {
speed = speed * -1;
}
// Display circle at x location
stroke(0);
fill(255,r,r);
ellipse(ballPositionX,ballPositionY,32,32);
}
void drawPlayer(){
//body
fill(255);
rect(playerX+200,playerY+290,40,55);
//head
ellipse(playerX+220,playerY+280,60,60);
//eyes
fill(0);
ellipse(playerX+205,playerY+280,10,25);
ellipse(playerX+225,playerY+280,10,25);
}
void playerMove(){
if (keyPressed || ( (keyCode ==UP) && (playerY < 0))){ //prevents players from endlessly moving after pressing a button
if (keyCode == LEFT) { //if the player presses left, the character will move left
playerX--;
}
else if (keyCode == RIGHT) { //if the player presses right, the character will move right
playerX++;
}
else if (keyCode == UP) { //if the player presses up, They character will jump
playerY -= jumpSpeed;
jumpSpeed -= gravity;
playerY = constrain (playerY, -55, 325);
if (jumpSpeed < -8) {
//Jump speed resets
jumpSpeed = 8;
}
}
}
}
void score(){
if (playerX == ballPositionX && playerY == ballPositionY){
pointGain += 1;
}
if (pointGain == 5){
}
}