Need help with a game
in
Programming Questions
•
1 year ago
Making a game for class were balls fall from the top of the screen at random x variables and have slightly different speeds. I have everything figured out but 1 thing:
I need to figure out how to make it so when a ball is popped (when a ball hits the pad) the points value is displayed and floats to the top.I am rather new to processing and any help would be greatly appreciated.
ArrayList balls;
int ballRadius = 25;
float level = 1;
int score = 0;
float ballspop = 0;
void setup(){
size (650, 700);
fill(0);
smooth();
strokeWeight(4);
PFont font = loadFont("ComicSansMS-Bold-24.vlw");
textFont(font);
balls = new ArrayList();
for (int i = 0; i < level*2; i++){
balls.add(new Ball(random(15,width-15), random(-300,0), ballRadius));
}
}
void draw(){
background(255);
frameRate(45);
fill(0);
rect(mouseX, height-10, 50,10);
triangle((mouseX)+18,height, (mouseX)+25, height-40, (mouseX)+32, height);
text("Score: " + score, 10,30);
text("Level: " + level, width-150,30);
if (balls.size() == 0) {
text ("popped "+(ballspop/(level*2)*100 +"%") , 0 , height/2 + 50);
if ((ballspop/(level*2)*100)>= 50) text ("Click to start next level", 0, height/2);
else if ((ballspop/(level*2)*100)< 50) {
text ("Casfadsfad", 0, height/2);
}
}
for (int i = balls.size()-1; i >= 0; i--) {
Ball ball = (Ball) balls.get(i);
ball.move();
ball.display();
if(mouseX > ball.x-50 && mouseX+32 < ball.x+50 && ball.y > height-40){
score= score +50 - ball.bounceCount*10;
balls.remove(i);
ballspop= ballspop+1;
}
else if ( ball.bounceCount> 5) balls.remove(i);
}
}
void mousePressed() {
if (balls.size() == 0)
{
if ((ballspop/(level*2)*100)>= 50) {
level = level + 1;
ballspop = 0;
for (int i = 0; i < level*2; i++){
balls.add(new Ball(random(15,width-15), random(-300,0), ballRadius)); }
}
else if ((ballspop/(level*2)*100)< 50){ level = 1; ballspop = 0; score = 0;
for (int i = 0; i < level*2; i++){balls.add(new Ball(random(15,width-15), random(-300,0), ballRadius)); }}
}
}
class Ball {
float x;
float y;
float speed;
float gravity;
float w;
int bounceCount = 0;
Ball(float xPos, float yPos, float bWidth) {
x = xPos;
y = yPos;
w = bWidth;
speed = 0;
gravity = 0.98;
bounceCount = 0;
}
void move() {
speed = speed + gravity;
y = y + speed;
if (y > height) {
speed = speed * -0.8;
y = height;
bounceCount++;
}
if (x == (mouseX)+25 && y == height){
noLoop();
}
}
void display()
{
// This is what i am using at the moment but it doesn't really work.
if(mouseX > x-50 && mouseX+32 < x+50 && y > height-40) {
for (int r = 1; r < 7; r ++) {
text (50- (bounceCount*10), x,(y - 10*r));
}}
if (bounceCount == 0){
fill (0,255,0);
}
else if (bounceCount == 1){
fill (255,255,0);
}
else if (bounceCount == 2){
fill (255,150,0);
}
else if (bounceCount == 3||bounceCount == 4){
fill (255,0,0);
}
else if (bounceCount == -1){
fill(255);
stroke(255);
}
else if ( bounceCount == 5) {
fill(200,200,200); }
else{
fill(255);
}
ellipse(x,y,w,w);
}
}
I need to figure out how to make it so when a ball is popped (when a ball hits the pad) the points value is displayed and floats to the top.I am rather new to processing and any help would be greatly appreciated.
ArrayList balls;
int ballRadius = 25;
float level = 1;
int score = 0;
float ballspop = 0;
void setup(){
size (650, 700);
fill(0);
smooth();
strokeWeight(4);
PFont font = loadFont("ComicSansMS-Bold-24.vlw");
textFont(font);
balls = new ArrayList();
for (int i = 0; i < level*2; i++){
balls.add(new Ball(random(15,width-15), random(-300,0), ballRadius));
}
}
void draw(){
background(255);
frameRate(45);
fill(0);
rect(mouseX, height-10, 50,10);
triangle((mouseX)+18,height, (mouseX)+25, height-40, (mouseX)+32, height);
text("Score: " + score, 10,30);
text("Level: " + level, width-150,30);
if (balls.size() == 0) {
text ("popped "+(ballspop/(level*2)*100 +"%") , 0 , height/2 + 50);
if ((ballspop/(level*2)*100)>= 50) text ("Click to start next level", 0, height/2);
else if ((ballspop/(level*2)*100)< 50) {
text ("Casfadsfad", 0, height/2);
}
}
for (int i = balls.size()-1; i >= 0; i--) {
Ball ball = (Ball) balls.get(i);
ball.move();
ball.display();
if(mouseX > ball.x-50 && mouseX+32 < ball.x+50 && ball.y > height-40){
score= score +50 - ball.bounceCount*10;
balls.remove(i);
ballspop= ballspop+1;
}
else if ( ball.bounceCount> 5) balls.remove(i);
}
}
void mousePressed() {
if (balls.size() == 0)
{
if ((ballspop/(level*2)*100)>= 50) {
level = level + 1;
ballspop = 0;
for (int i = 0; i < level*2; i++){
balls.add(new Ball(random(15,width-15), random(-300,0), ballRadius)); }
}
else if ((ballspop/(level*2)*100)< 50){ level = 1; ballspop = 0; score = 0;
for (int i = 0; i < level*2; i++){balls.add(new Ball(random(15,width-15), random(-300,0), ballRadius)); }}
}
}
class Ball {
float x;
float y;
float speed;
float gravity;
float w;
int bounceCount = 0;
Ball(float xPos, float yPos, float bWidth) {
x = xPos;
y = yPos;
w = bWidth;
speed = 0;
gravity = 0.98;
bounceCount = 0;
}
void move() {
speed = speed + gravity;
y = y + speed;
if (y > height) {
speed = speed * -0.8;
y = height;
bounceCount++;
}
if (x == (mouseX)+25 && y == height){
noLoop();
}
}
void display()
{
// This is what i am using at the moment but it doesn't really work.
if(mouseX > x-50 && mouseX+32 < x+50 && y > height-40) {
for (int r = 1; r < 7; r ++) {
text (50- (bounceCount*10), x,(y - 10*r));
}}
if (bounceCount == 0){
fill (0,255,0);
}
else if (bounceCount == 1){
fill (255,255,0);
}
else if (bounceCount == 2){
fill (255,150,0);
}
else if (bounceCount == 3||bounceCount == 4){
fill (255,0,0);
}
else if (bounceCount == -1){
fill(255);
stroke(255);
}
else if ( bounceCount == 5) {
fill(200,200,200); }
else{
fill(255);
}
ellipse(x,y,w,w);
}
}
1