write a code in Processing wherein every time the pedestrian (the angel) hits the vehicle (the cloud), it loses a life and once the life
write a code in Processing wherein every time the pedestrian (the angel) hits the vehicle (the cloud), it loses a life and once the life hits 0, the game ends. Every time the pedestrian crosses the lane, it should also display one extra point in the point counter. Lives and points should be displayed. My code is below.
Right now, I am also trying to see if I am able to make this code clearer by using functions. Everything should also be scalable so when the display size changes, the game scales.
My biggest problem is the lives and points counter. Please advise me on how I can achieve this.
float cloudx; float cloudy; float widthdia; float heightdia; float speed; float laneY; float w2; float angelx; float angely; void setup () { size (1200, 400); smooth (); cloudx = 0; angelx = 0; angely = 0; w2 = width/2; } void draw () { background(#E0F3FF); float speed = 3; //lane float x = 0; laneY = height/4; stroke(#71A9FF); while (x < width) { if (x%7 != 0) { strokeWeight(3); line(x,laneY, x, laneY); } else { strokeWeight(0); } x=x+3; } //cloud cloudy = laneY - 5; widthdia = 30; heightdia = 30; noStroke(); noFill(); rect(cloudx + 33, cloudy-20, 65, 35); //hitbox noStroke(); fill(255); ellipse(cloudx + 15, cloudy-15, widthdia, heightdia); //bottom left ellipse(cloudx + 35, cloudy-15, widthdia, heightdia); //bottom middle ellipse(cloudx + 50, cloudy-15, widthdia, heightdia); //bottom right ellipse(cloudx + 20, cloudy-30, widthdia-8, heightdia-8); //top left ellipse(cloudx + 40, cloudy-30, widthdia+5, heightdia); //top right cloudx = (cloudx + speed); if (cloudx > width+40) { cloudx = 0-60; } //points section where im stuck at int points = 0; /*score for(int p = points; skirty > laneY; p++) { points = p + 1; }*/ String score = "Score: " + points; textAlign(LEFT); fill(#71A9FF); textSize(20); text(score, width-90, height-10); //lives section where im stuck at int MAX_LIVES = 3; String lives = "Lives: " + MAX_LIVES; textAlign(CENTER); fill(#71A9FF); textSize(20); text(lives, 50, height-10); int WIN_SCORE = 10; //once the pedestrian reaches 10 points, they win the game //angel float angeldia1 = 30; float angeldia2 = 15; float heady = angely + (height-40); float skirty = heady; float AngelX = angelx+w2; float angelbottom = angely + (height-5); fill(#FFE0FE); triangle(AngelX, skirty, angelx + (w2+15), angely + (height-5), angelx + (w2-15), angelbottom); //skirt fill(#FFF6ED); ellipse(AngelX, heady, 25, 25); //head fill(255); ellipse((AngelX) + 17, angely + (height-25), angeldia1, angeldia2); //right wing ellipse((AngelX) - 17, angely + (height-25), angeldia1, angeldia2); //left wing noFill(); strokeWeight(2); stroke(#FFEB0A); rectMode(CENTER); rect(AngelX, angely + (height-60), 20, 5); noStroke(); noFill(); rectMode(CENTER); rect(AngelX, skirty, 40, 70); //hitbox if ((skirty < cloudy-30) && (AngelX < cloudx + 92) && (AngelX > cloudx + 33)) { textAlign(CENTER); textSize(100); background(255, 255, 255); fill(0); text("Game Over!", width/2, height/2); noLoop(); } else if ((skirty < cloudy-30) && (AngelX + 40 < cloudx + 33) && (AngelX > cloudx - 20)) { textAlign(CENTER); textSize(100); background(255, 255, 255); fill(0); text("Game Over!", width/2, height/2); noLoop(); } } void keyPressed() { if (key == CODED) { if (keyCode == UP) { angely = angely - height/4; } else if (keyCode == DOWN) { angely = angely + height/4; } else if (keyCode == LEFT) { angelx = angelx - 50; } else if (keyCode == RIGHT) { angelx = angelx + 50; } } }
Step by Step Solution
3.50 Rating (153 Votes )
There are 3 Steps involved in it
Step: 1
To implement the lives and points system in your game you can modify your code as follows 1 Initialize variables for points and lives outside of the d...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started