Help I need to edit a game code (clouds)

Hello,
I’m really puzzled on what to do with this quiz. I was given a gaming code and need to personalize it.

I tried adding clouds into the game code but I want to find a way to simplify the code and make it look cleaner. I also tried to randomize the clouds positioning so that each time the game re-start, the clouds would be in a new position but I haven’t manage to figure that out.

please I’m in desperate need of help any help would be greatly appreciated.

 /********* VARIABLES *********/

// We control which screen is active by settings / updating
// gameScreen variable. We display the correct screen according
// to the value of this variable.
// 
// 0: Initial Screen
// 1: Game Screen
// 2: Game-over Screen 

int gameScreen = 0;

// gameplay settings
float gravity = .3;
float airfriction = 0.00001;
float friction = 0.1;

// scoring
int score = 0;
int maxHealth = 100;
float health = 100;
float healthDecrease = 1;
int healthBarWidth = 60;

// ball settings
float ballX, ballY;
float ballSpeedVert = 0;
float ballSpeedHorizon = 0;
float ballSize = 20;
color ballColor = color(0);

// racket settings
color racketColor = color(0);
float racketWidth = 100;
float racketHeight = 10;

// wall settings
int wallSpeed = 5;
int wallInterval = 1000;
float lastAddTime = 0;
int minGapHeight = 200;
int maxGapHeight = 300;
int wallWidth = 80;
color wallColors = color(44, 62, 80);
// This arraylist stores data of the gaps between the walls. Actuals walls are drawn accordingly.
// [gapWallX, gapWallY, gapWallWidth, gapWallHeight, scored]
ArrayList<int[]> walls = new ArrayList<int[]>();


/********* SETUP BLOCK *********/

void setup() {
  size(500, 500);
  // set the initial coordinates of the ball
  ballX=width/4;
  ballY=height/5;
  smooth();
}


/********* DRAW BLOCK *********/

void draw() {
  // Display the contents of the current screen
  if (gameScreen == 0) { 
    initScreen();
  } else if (gameScreen == 1) { 
    gameScreen();
  } else if (gameScreen == 2) { 
    gameOverScreen();
  }
}


/********* SCREEN CONTENTS *********/

void initScreen() {
  background(236, 240, 241);
  textAlign(CENTER);
  fill(52, 73, 94);
  textSize(70);
  text("Flappy Pong", width/2, height/2);
  textSize(15); 
  text("Click to start", width/2, height-30);
}

void gameScreen() {
  background(236, 240, 241);
  cloud(300,300,300);
  cloud(400,400,200);
  cloud(200,500,400);
  cloud(200,500,400);
  cloud(320,570,300);
  cloud(550,370,300);
  cloud(600,700,400);
  cloud(100,540,200);
  drawRacket();
  watchRacketBounce();
  drawBall();
  applyGravity();
  applyHorizontalSpeed();
  keepInScreen();
  drawHealthBar();
  printScore();
  wallAdder();
  wallHandler();  
}

void gameOverScreen() {
  background(44, 62, 80);
  textAlign(CENTER);
  fill(236, 240, 241);
  textSize(12);
  text("Your Score", width/2, height/2 - 120);
  textSize(130);
  text(score, width/2, height/2);
  textSize(15);
  text("Click to Restart", width/2, height-30);
}


/********* INPUTS *********/

public void mousePressed() {
  // if we are on the initial screen when clicked, start the game 
  if (gameScreen==0) { 
    startGame();
  }
  if (gameScreen==2) {
    restart();
  }
}



/********* OTHER FUNCTIONS *********/

// This method sets the necessery variables to start the game  
void startGame() {
  gameScreen=1;
}
void gameOver() {
  gameScreen=2;
}

void restart() {
  score = 0;
  health = maxHealth;
  ballX=width/4;
  ballY=height/5;
  lastAddTime = 0;
  walls.clear();
  gameScreen = 1;
}

void cloud(int x,int y,int theSize){
  int offset=theSize/2;
  fill(225);
  ellipse(x-offset/1.05,y-offset-offset/1.9,offset/2,offset/2);
  ellipse(x-offset/1.25,y-offset-offset/1.4,offset/2,offset/2);
  ellipse(x-offset/1.8,y-offset-offset/1.7,offset/2,offset/2);
  ellipse(x-offset/2.5,y-offset-offset/1.9,offset/2,offset/2);
  ellipse(x-offset/1.6,y-offset-offset/2,offset/2,offset/2); 
} 

void drawBall() {
  fill(ballColor);
  ellipse(ballX, ballY, ballSize, ballSize);
}
void drawRacket() {
  fill(racketColor);
  rectMode(CENTER);
  rect(mouseX, mouseY, racketWidth, racketHeight, 5);
}

void wallAdder() {
  if (millis()-lastAddTime > wallInterval) {
    int randHeight = round(random(minGapHeight, maxGapHeight));
    int randY = round(random(0, height-randHeight));
    // {gapWallX, gapWallY, gapWallWidth, gapWallHeight, scored}
    int[] randWall = {width, randY, wallWidth, randHeight, 0}; 
    walls.add(randWall);
    lastAddTime = millis();
  }
}
void wallHandler() {
  for (int i = 0; i < walls.size(); i++) {
    wallRemover(i);
    wallMover(i);
    wallDrawer(i);
    watchWallCollision(i);
  }
}
void wallDrawer(int index) {
  int[] wall = walls.get(index);
  // get gap wall settings 
  int gapWallX = wall[0];
  int gapWallY = wall[1];
  int gapWallWidth = wall[2];
  int gapWallHeight = wall[3];
  // draw actual walls
  rectMode(CORNER);
  noStroke();
  strokeCap(ROUND);
  fill(wallColors);
  rect(gapWallX, 0, gapWallWidth, gapWallY, 0, 0, 15, 15);
  rect(gapWallX, gapWallY+gapWallHeight, gapWallWidth, height-(gapWallY+gapWallHeight), 15, 15, 0, 0);
}
void wallMover(int index) {
  int[] wall = walls.get(index);
  wall[0] -= wallSpeed;
}
void wallRemover(int index) {
  int[] wall = walls.get(index);
  if (wall[0]+wall[2] <= 0) {
    walls.remove(index);
  }
}


void watchWallCollision(int index) {
  int[] wall = walls.get(index);
  // get gap wall settings 
  int gapWallX = wall[0];
  int gapWallY = wall[1];
  int gapWallWidth = wall[2];
  int gapWallHeight = wall[3];
  int wallScored = wall[4];
  int wallTopX = gapWallX;
  int wallTopY = 0;
  int wallTopWidth = gapWallWidth;
  int wallTopHeight = gapWallY;
  int wallBottomX = gapWallX;
  int wallBottomY = gapWallY+gapWallHeight;
  int wallBottomWidth = gapWallWidth;
  int wallBottomHeight = height-(gapWallY+gapWallHeight);

  if (
    (ballX+(ballSize/2)>wallTopX) &&
    (ballX-(ballSize/2)<wallTopX+wallTopWidth) &&
    (ballY+(ballSize/2)>wallTopY) &&
    (ballY-(ballSize/2)<wallTopY+wallTopHeight)
    ) {
    decreaseHealth();
  }
  if (
    (ballX+(ballSize/2)>wallBottomX) &&
    (ballX-(ballSize/2)<wallBottomX+wallBottomWidth) &&
    (ballY+(ballSize/2)>wallBottomY) &&
    (ballY-(ballSize/2)<wallBottomY+wallBottomHeight)
    ) {
    decreaseHealth();
  }

  if (ballX > gapWallX+(gapWallWidth/2) && wallScored==0) {
    wallScored=1;
    wall[4]=1;
    score();
  }
}

void drawHealthBar() {
  noStroke();
  fill(189, 195, 199);
  rectMode(CORNER);
  rect(ballX-(healthBarWidth/2), ballY - 30, healthBarWidth, 5);
  if (health > 60) {
    fill(46, 204, 113);
  } else if (health > 30) {
    fill(230, 126, 34);
  } else {
    fill(231, 76, 60);
  }
  rectMode(CORNER);
  rect(ballX-(healthBarWidth/2), ballY - 30, healthBarWidth*(health/maxHealth), 5);
}
void decreaseHealth() {
  health -= healthDecrease;
  if (health <= 0) {
    gameOver();
  }
}
void score() {
  score++;
}
void printScore() {
  textAlign(CENTER);
  fill(0);
  textSize(30); 
  text(score, height/2, 50);
}

  
void watchRacketBounce() {
  float overhead = mouseY - pmouseY;
  if ((ballX+(ballSize/2) > mouseX-(racketWidth/2)) && (ballX-(ballSize/2) < mouseX+(racketWidth/2))) {
    if (dist(ballX, ballY, ballX, mouseY)<=(ballSize/2)+abs(overhead)) {
      makeBounceBottom(mouseY);
      ballSpeedHorizon = (ballX - mouseX)/10;
      // racket moving up
      if (overhead<0) {
        ballY+=(overhead/2);
        ballSpeedVert+=(overhead/2);
      }
    }
  }
}
void applyGravity() {
  ballSpeedVert += gravity;
  ballY += ballSpeedVert;
  ballSpeedVert -= (ballSpeedVert * airfriction);
}
void applyHorizontalSpeed() {
  ballX += ballSpeedHorizon;
  ballSpeedHorizon -= (ballSpeedHorizon * airfriction);
}
// ball falls and hits the floor (or other surface) 
void makeBounceBottom(float surface) {
  ballY = surface-(ballSize/2);
  ballSpeedVert*=-1;
  ballSpeedVert -= (ballSpeedVert * friction);
}
// ball rises and hits the ceiling (or other surface)
void makeBounceTop(float surface) {
  ballY = surface+(ballSize/2);
  ballSpeedVert*=-1;
  ballSpeedVert -= (ballSpeedVert * friction);
}
// ball hits object from left side
void makeBounceLeft(float surface) {
  ballX = surface+(ballSize/2);
  ballSpeedHorizon*=-1;
  ballSpeedHorizon -= (ballSpeedHorizon * friction);
}
// ball hits object from right side
void makeBounceRight(float surface) {
  ballX = surface-(ballSize/2);
  ballSpeedHorizon*=-1;
  ballSpeedHorizon -= (ballSpeedHorizon * friction);
}
// keep ball in the screen
void keepInScreen() {
  // ball hits floor
  if (ballY+(ballSize/2) > height) { 
    makeBounceBottom(height);
  }
  // ball hits ceiling
  if (ballY-(ballSize/2) < 0) {
    makeBounceTop(0);
  }
  // ball hits left of the screen
  if (ballX-(ballSize/2) < 0) {
    makeBounceLeft(0);
  }
  // ball hits right of the screen
  if (ballX+(ballSize/2) > width) {
    makeBounceRight(width);
  }
}

What do you mean by Quiz?

Do you want to program a Quiz?

Or do you mean homework / assignment?

2 Likes

this depends from your skill level. One way to do this is using classes (Wall, Cloud…). This capsules the variables and functions for a Cloud in a special code section.

You can also distribute your Sketch over different tabs, so that you have one main tab and then Inputs, SCREEN_CONTENTS, Ball_Functions, Wall_Functions …

You can also make different cloud images in another Sketch (100 of them) and load one of them randomly on restart.
Then you would have the cloud stuff in another Sketch.

Name Your Screens

one neat trick is to give names to your screens.

So instead of


// 0: Initial Screen
// 1: Game Screen
// 2: Game-over Screen

and

  if (gameScreen==2) {
    restart();

You can say:

final int  Initial_Screen    = 0; 
final int  Game_Screen   = 1; 
final int  Game_over_Screen = 2;

and then:

  if (gameScreen==Game_over_Screen ) {
    restart();

Nice.

1 Like

here is the idea: You make a new image of clouds on the fly and use it as a background:

call cloudInit() in setup and in restart().

As you can see, we don’t draw the clouds on the screen (they would flicker and jump when using random), but on a PGraphics which is a virtual image. All clouds commands need to have clouds1. in front of them now.


PGraphics clouds1; 

void cloudInit() {

  clouds1=createGraphics(width, height); 

  clouds1.beginDraw();
  clouds1.background(236, 240, 241);

  int randomUpperBound = int(random(4, 12)); 
  for (int i = 0; i < randomUpperBound; i++) {
    cloud(random(55, 650), random(55, 700), random ( 200, 400 ));
  }
  clouds1.endDraw();
}

void cloud(float x, float y, float theSize) {
  float offset=theSize/2.0;

  // clouds1.fill(225);
  clouds1.fill(random(111, 240));
  clouds1.noStroke();  

  clouds1.ellipse(x-offset/1.05, y-offset-offset/1.9, offset/2, offset/2);
  clouds1.ellipse(x-offset/1.25, y-offset-offset/1.4, offset/2, offset/2);
  clouds1.ellipse(x-offset/1.8, y-offset-offset/1.7, offset/2, offset/2);
  clouds1.ellipse(x-offset/2.5, y-offset-offset/1.9, offset/2, offset/2);
  clouds1.ellipse(x-offset/1.6, y-offset-offset/2, offset/2, offset/2);
}

in game screen, delete the calls to cloud() and just display the image clouds1:

void gameScreen() {
  background(236, 240, 241);

  image(clouds1, 0, 0); 

  //cloud(300, 300, 300);
  //cloud(400, 400, 200);
  //cloud(200, 500, 400);
  //cloud(200, 500, 400);
  //cloud(320, 570, 300);
  //cloud(550, 370, 300);
  //cloud(600, 700, 400);
  //cloud(100, 540, 200);

Additionally you can also change the numbers inside cloud() using random

1 Like

ah I mean for homework and thank you for recommending on how to make it neater. Im really new to this so my lvl of knowledge is just beginner lvl

1 Like

tysm for the explanation I think I can understand it well enough to apply it this is very helpful tyy ;4;

1 Like