Assignment 9 : Jumping Ball Game

I really wanted to try making a game for this assignment, rather than some sort of an art piece, so the first thing that came to my mind was Tetris game. I thought it should be fairly simple to make, however, my expectations of Tetris being easy to make were destroyed when I started looking up different examples of how to make Tetris online. It involved making a grid, clearing rows when they were full and just a lot of math, so at that point I thought that I need to think of a game which will make me focus on programming rather than calculating.

 

I thought of some games that have very basic concept, and one that came to mind was Doodle Jump I used to play at school. I remembered how much time I used to spend playing that simple game and it still managed to keep me entertained after I’ve been playing it for a while. So I decided to do something similar, using a ball instead of a doodle.

 

I went looking for examples online to find inspiration, but most of what I found was really complicated, until I came across this very simple (as compared to other) example  https://www.openprocessing.org/sketch/7824
However, reading through this example got me really confused at points, for example sometimes I would try to understand the code he uses and think “why does this guy use ‘float yVel’ for platforms, if the platforms are not moving on the y-axis?”

Then I started playing with the code, commenting things out, like this ‘float yVel’ (among the other things I was commenting out), to see how it works.

Then, there were things that have not made much sense to me, so I asked Nahil to help me out and explain why and how things are used. Eventually, after a couple of hours, realising what is useful and what seemed dumb and redundant (at least to me), I went on and with Nahil’s help started creating my own ‘doodle jump’.

And as things started working out, after getting the circle jump off the platforms and be controlled the I wanted it to be, I started thinking of some things to add to the basic jumping. For example, difficulty levels, with invisible platforms appearing as you go, as well as creating “dangerous” or “monster” squares under Platforms that will make you lose the game once you collide with them.

 

It took around 5-8 hours (can’t really remember at this point), but eventually I am happy with how the project turned out to be. However, there are still things to work on, such as textual visualisations of game start, game over, and just interface in general.

Here is how it looks on the highest difficulty level!
With no platforms you can see to jump next on, and with red ‘dangers’ floating on the screen.

Here is the code, divided into 3 parts: game sketch-the main one, and two classes : platform and player

 

Game sketch

Player p;
ArrayList platforms, platformsDanger;
boolean upPressed, leftPressed, downPressed, rightPressed;
int score, fallCount;
boolean gameOver;



void setup()
{
 size(480, 640);
 frameRate(60);
 initialize();
 //initializeDanger();
 ellipseMode(CORNER);
}

void initialize()
{
 score = 0;
 fallCount = 0;
 gameOver = false;
 p = new Player(width/2, height/2);
 platforms = new ArrayList();
 platforms.add(new MovingPlatform(20,80,70,8, false));
 platforms.add(new Platform(width/2,height/2,100,8, false));
 platforms.add(new Platform((int)random(40,500),320,(int)random(50,120),8, false));
 platforms.add(new Platform((int)random(40,500),220,(int)random(50,120),8, false));
 platforms.add(new Platform((int)random(40,500),120,(int)random(50,120),8, false));
 platforms.add(new Platform((int)random(40,500),20,(int)random(50,120),8, false));
 //platforms.add(new MovingPlatform((int)random(20,400),(int)random(10,150),20,20, true));
 //platforms.add(new MovingPlatform((int)random(20,400),(int)random(10,150),30,30, true));
 //platforms.add(new MovingPlatform((int)random(20,400),(int)random(10,150),45,45, true));
}

//void initializeDanger(){
// platformsDanger = new ArrayList();
// platforms.add(new MovingPlatform(20,80,20,20, true));
// platforms.add(new MovingPlatform(20,80,30,30, true));
// platforms.add(new MovingPlatform(20,80,25,25, true));


 
//}

void draw()
{
 println(score);
 
 background(255);
 fill(0, 10, 153, 204);
 text("score", 15, 15);
 text(score, 60, 15);
 //println(platforms.size());
 for(int i=0; i<platforms.size(); i++)
 {

 p.collide((Platform)platforms.get(i));
 ((Platform)platforms.get(i)).display();
 ((Platform)platforms.get(i)).move();
 //if(i<3){
 // p.collide((Platform)platformsDanger.get(i));
 // ((Platform)platformsDanger.get(i)).display();
 // ((Platform)platformsDanger.get(i)).move();
 //}
 }
 p.display(); 
 p.move();

 adjustViewport();
 cleanUp();
 seedNewPlatforms();
 if (platformsBelow() == 0) gameOver = true;
 if (gameOver) fallCount++;
 if (fallCount > 3 ) initialize();
}

int platformsBelow()
{
 int count = 0;
 for(int i=0; i<platforms.size(); i++)
 {
 if (((Platform)platforms.get(i)).y >= p.y) count++;
 }
 return count;
}

void adjustViewport()
{
 // above midpoint
 float overHeight = height * 0.5 - p.y;
 if(overHeight > 0){
 p.y += overHeight;
 for(int i=0; i<platforms.size(); i++)
 {
 ((Platform)platforms.get(i)).y += overHeight;
 }
 score += overHeight;
 }
 // falling
 float underHeight = p.y - (height-p.h-4);
 if(underHeight > 0)
 {
 p.y -= underHeight;
 for(int i=0; i<platforms.size(); i++)
 {
 ((Platform)platforms.get(i)).y -= underHeight;
 }
 } 
 //above midpoint danger
 //for(int i=0; i<platformsDanger.size(); i++)
 // {
 // ((Platform)platformsDanger.get(i)).y += overHeight;
 // }
 // score += overHeight;
 
 // falling
 //underHeight = p.y - (height-p.h-4);
 //if(underHeight > 0){
 // p.y -= underHeight;
 // for(int i=0; i<platformsDanger.size(); i++)
 // {
 // ((Platform)platformsDanger.get(i)).y -= underHeight;
 // }
 //}
 
}

void cleanUp()
{
 for(int i=platforms.size()-1; i>=0; i--){
 // scrolled off the bottom
 if(((Platform)platforms.get(i)).y > height){
 platforms.remove(i);
 }
 }
 //for(int i=platformsDanger.size()-1; i>=0; i--){
 // // scrolled off the bottom
 // if(((Platform)platformsDanger.get(i)).y > height){
 // platformsDanger.remove(i);
 // }
 //}
}

void seedNewPlatforms()
{
 if(platforms.size() < 9)
 {
 float randomizer = random(0,10);
 
 if(score < 1250){
 if(randomizer < 3) {
 
 platforms.add(new MovingPlatform((int)random(10,width-80),-10,70,8,false));
 } 
 //else if (randomizer < 4) {
 // platforms.add(new MovingPlatform((int)random(20,400),-10,30,30, true));
 // platforms.add(new MovingPlatform((int)random(20,400),-10,30,30, true));
 //}
 // else if (randomizer < 1) {
 // platforms.add(new MovingPlatform((int)random(20,400),-10,45,45, true));
 //}
 else {
 platforms.add(new Platform((int)random(20,400),-10,(int)random(50,120),8, false));
 
 }
 }
 
 
 else if (score < 2000){
 if(randomizer < 3) {
 platforms.add(new MovingPlatform((int)random(10,width-80),300,70,8,false));
 }
 else {
 platforms.add(new Platform((int)random(20,400),300,(int)random(50,120),8, false));
 }
 }
 
 else{ 
 if(randomizer < 3){
 platforms.add(new MovingPlatform((int)random(20,400),-10,30,30, true));
 }
 
 else {
 platforms.add(new MovingPlatform((int)random(10,width-80),300,70,8,false));
 }
 
 
 
 
 }
 }
 
 //if(platformsDanger.size() < 3)
 //{
 // platforms.add(new MovingPlatform((int)random(10,width-80),-10,20,20,true));
 //}
 
}



void keyPressed()
{
 if (keyCode == UP) upPressed = true;
 if (keyCode == LEFT) leftPressed = true;
 if (keyCode == DOWN) downPressed = true;
 if (keyCode == RIGHT) rightPressed = true;
}

void keyReleased()
{
 if (keyCode == UP) upPressed = false;
 if (keyCode == DOWN) downPressed = false;
 if (keyCode == LEFT) leftPressed = false;
 if (keyCode == RIGHT) rightPressed = false;
}

 

Platform

class Platform{
 float x,y,w,h;
 float xvel; 
 boolean danger;
 Platform(int x_, int y_, int w_, int h_, boolean d)
 {
 x = x_;
 y = y_;
 w = w_;
 h = h_;
 danger = d;
 
 println("NEW PLATFORM:");
 println("Y: " + str(y));
 
 }

 void display()
 {
 if (danger == false){
 fill(0);}
 else if (danger == true){
 fill(255,0,0);}
 rect(x,y,w,h);
 }

 void move()
 {
 

 x += xvel;
 y += 0;
 }

}

class MovingPlatform extends Platform
{
 static final float speed = 0.9;
 
 MovingPlatform(int x, int y, int w, int h, boolean d)
 {
 super(x, y, w, h, d);
 xvel = speed;
 }
 
 void move()
 {
 super.move();
 if( (x+w > width - 10) || (x < 10) )
 {
 xvel *= -1;
 }
 }
}

 

Player

class Player
{
 float gravity = 0.14;
 float bounceVel = 9;
 float maxYVel = 13;
 float maxXVel = 3;

 float x, y, xVel, yVel;
 int w, h;
 Player(int x, int y)
 {
 w = h = 20;
 this.x = x;
 this.y = y;

 }

 void display()
 {
 fill(0,0,240);
 ellipse(x,y,w,h);
 }

 void move()
 {
 x += xVel;
 y += yVel;

 if (x > width-w) x = 0;
 if (x < 0) x = width-w;

 // horizontal
 if (!gameOver){
 if (leftPressed) xVel -= 0.05;
 else if (rightPressed) xVel += 0.05;
 else
 {
 if (xVel > 0) xVel -= 0.03;
 else xVel += 0.03;
 }
 }
 if (abs(xVel) < 0.01) xVel = 0;
 xVel = min(maxXVel, xVel);
 xVel = max(-maxXVel, xVel);

 // vertical
 yVel += gravity;
 yVel = min(maxYVel, yVel);
 yVel = max(-maxYVel, yVel);
 }

 void collide(Platform plat){
 
 
 if(x < plat.x + plat.w &&
 x + w > plat.x &&
 y < plat.y + plat.h &&
 y + h > plat.y)
 {
 if (plat.danger == false) {
 if (yVel > 0) {
 yVel = -bounceVel;
 }
 } else {
 if (plat.danger == true){
 gameOver = true;
 }
 // game over
 }
 }
 }

}

 

smth