import static javax.swing.JOptionPane.showMessageDialog;
import java.util.Random;
Random rand = new Random();
float x = 450;
float Ball = 0;
//int RandomSpawnWidth;
float speedBall = 3;
int score = 0;
float speedPlayer = 10;
PFont Font;
float BallX = 25;
//float BallY = 25;
void setup()
{
size(800, 600);
//
BallX = rand.nextInt(600);
Font = createFont("arial.ttf", 24);
// textFont(Font);
}
//Background background = new Background(); //first class
//Grass grass = new Grass(); // second class
void draw()
{
float ellipseRadius = 25;
float circleRadius = 25;
background(0);
if (keyPressed) {
if (key == 'a' || key == 'A') {
x = (x-speedPlayer);
}
}
if (keyPressed) {
if (key == 'd' || key == 'D') {
x = (x+speedPlayer);
}
}
if (x >= 800) {
x = 800;
}
if (x <= 80) {
x = 80;
}
fill (#00F727);
ellipse (400, 700, 900, 500); // landscape
fill (#674700);
rect(400, 400, 20, 50); // middle tree stump
fill (#018E02);
ellipse (410, 400, 50, 50); // middle tree leafs
fill (#674700);
rect(500, 450, 21, 51); // right tree stump
fill (#018E02);
ellipse (510, 445, 50, 50); // right tree leafs
fill (#674700);
rect(50, 500, 150, 400); // left tree stump
fill (#018E02);
ellipse (110, 350, 350, 350); // left tree leafs
fill (#E52525);
rect(x-60, 545, 30, 50); //body
fill (#DEACAC);
ellipse(x-45, 540, 50, 50); // head
fill (#000000);
rect(x-60, 532, 13, 10); //left eye sunglasses
rect(x-50, 532, 11, 3); //middle piece sunglasses
rect(x-42, 532, 13, 10); //right eye sunglasses
ellipse(x-45, 551, 10, 5); //mouth
ellipse(x-55, 595, 13, 5); //left foot
ellipse(x-35, 595, 13, 5); //right foot
fill (#FEFF0D);
ellipse(10, -5, 250, 250); //sun
if (mousePressed) {
fill (#E0E0E0);
ellipse(10, -5, 250, 250); //moon
}
//grass.display();
fill(#FFFFFF);
text("Score: " + score, 693, 33);
Ball = Ball + speedBall;
if (Ball > 600) {
showMessageDialog(null, "Close One!");
System.exit(0);
}
if (Ball <= 600);
{
fill (#FF780F);
ellipse(BallX, 0+Ball, 50, 50); // RandomSpawnWidth
if (dist(BallX, Ball, x, 590) < ellipseRadius + circleRadius+44) {
//if (BallX > x - 30 && BallX < x+30);
BallX = random(20, width-20);
Ball = random(-30, -10);
}
}
fill(255);
noStroke();
ellipse( BallX, Ball, 6, 6);
}
Did you notice, you had this RandomSpawnWidth
instead of BallX
AND you had Ball instead of BallY.
- So please, instead of coming up with new variables, have a plan and stick with it. Use the variable names you got.
Hi, I made a whole new account just to thank you!
I noticed, I feel so dumb. Thnx for the advice!
Kind regards,
Mitch
You are most welcome!
I know itâs so confusing in the beginningâŚ
If you have time over, do you know how to generate a random color every time it collides, that would be very cool (it needs to be in an array).
seriously?
before setup()
color[] cols = new color[5];
in setup
cols [0] = color(random(256), random(256), random(256) );
cols [1] = ..;
cols [2] = ..;
cols [3] = ..;
cols [4] = ..;
make a random number that you use as an index for cols:
this is it:
int index=int(random(5));
fill(cols[index]);
Okay so now I have
Before setup:
color[] cols = new color[5];
In setup:
cols [0] = color(random(256), random(256), random(256) );
cols [1] = color(random(256), random(256), random(256) );
cols [2] = color(random(256), random(256), random(256) );
cols [3] = color(random(256), random(256), random(256) );
cols [4] = color(random(256), random(256), random(256) );
But where do I need to fill in this?
int index=int(random(5));
fill(cols[index]);
And how exactly do you mean âmake a random number that you use as an index for colsâ.
I am pretty new to programming so my processing knowledge is not great.
this int index=int(random(5)); comes every time it collides
this fill(cols[index]); right before ellipse() command
You know what, heres my code! Now you can look what I did wrong
float x = 450;
float Ball = 0;
//int RandomSpawnWidth;
float speedBall = 3;
int score = 0;
float speedPlayer = 10;
PFont Font;
float BallX = 25;
color[] cols = new color[5];
//float BallY = 25;
void setup()
{
size(800, 600);
BallX = rand.nextInt(600);
Font = createFont(âarial.ttfâ, 24);
textFont(Font);
cols [0] = color(random(256), random(256), random(256) );
cols [1] = color(random(256), random(256), random(256) );
cols [2] = color(random(256), random(256), random(256) );
cols [3] = color(random(256), random(256), random(256) );
cols [4] = color(random(256), random(256), random(256) );
}
//Background background = new Background(); //first class
//Grass grass = new Grass(); // second class
void draw()
{
float ellipseRadius = 25;
float circleRadius = 25;
//background.display();
if (keyPressed) {
if (key == âaâ || key == âAâ) {
x = (x-speedPlayer);
}
}
if (keyPressed) {
if (key == âdâ || key == âDâ) {
x = (x+speedPlayer);
}
}
if (x >= 800) {
x = 800;
}
if (x <= 80) {
x = 80;
}
fill (#00F727);
ellipse (400, 700, 900, 500); // landscape
fill (#674700);
rect(400, 400, 20, 50); // middle tree stump
fill (#018E02);
ellipse (410, 400, 50, 50); // middle tree leafs
fill (#674700);
rect(500, 450, 21, 51); // right tree stump
fill (#018E02);
ellipse (510, 445, 50, 50); // right tree leafs
fill (#674700);
rect(50, 500, 150, 400); // left tree stump
fill (#018E02);
ellipse (110, 350, 350, 350); // left tree leafs
fill (#E52525);
rect(x-60, 545, 30, 50); //body
fill (#DEACAC);
ellipse(x-45, 540, 50, 50); // head
fill (#000000);
rect(x-60, 532, 13, 10); //left eye sunglasses
rect(x-50, 532, 11, 3); //middle piece sunglasses
rect(x-42, 532, 13, 10); //right eye sunglasses
ellipse(x-45, 551, 10, 5); //mouth
ellipse(x-55, 595, 13, 5); //left foot
ellipse(x-35, 595, 13, 5); //right foot
fill (#FEFF0D);
ellipse(10, -5, 250, 250); //sun
if (mousePressed) {
fill (#E0E0E0);
ellipse(10, -5, 250, 250); //moon
}
//grass.display();
fill(#FFFFFF);
text("Score: " + score, 693, 33);
Ball = Ball + speedBall;
if (Ball > 600) {
showMessageDialog(null, âClose One!â);
System.exit(0);
}
if (Ball < 600);
{
fill (#FF780F);
ellipse(BallX, 0+Ball, 50, 50);
int index=int(random(5));
fill(cols[index]);
if (dist(BallX, Ball, x, 590) < ellipseRadius + circleRadius+25) {
BallX = random(20, width-20);
Ball = random(-30, -10);
speedBall = speedBall + 0.3;
Ball = Ball + speedBall;
score++;
speedPlayer = speedPlayer + 0.25;
}
}
}
I am spoon feeding youâŚ
Please, you solve this
Maybe here is where you can use your new color fill?
Okay, sorry I meant the background. I put int index=int(random(5));
background(cols[index]); at the the top under the draw so all the other things are drawn on top of it, it is flashing 5 colors in lightspeed, my eyes hurt Is there a way that I prevent it so it only changes when it collides. Sorry that I need your help this much, but otherwise it will never work.
Obviously call this only when our player has catched the ball!
Yeah that works but now the ball and the characters are drawn many times so the ball looks like a snake
Do you still have background working?
Sounds like it doesnât work anymore
But without seeing your code, guess work
it does work but: it draws the ball and the characters multiple times.
import static javax.swing.JOptionPane.showMessageDialog;
import java.util.Random;
Random rand = new Random();
float x = 450;
float Ball = 0;
//int RandomSpawnWidth;
float speedBall = 3;
int score = 0;
float speedPlayer = 10;
PFont Font;
float BallX = 25;
color[] cols = new color[10];
//float BallY = 25;
void setup()
{
size(800, 600);
BallX = rand.nextInt(600);
Font = createFont(âarial.ttfâ, 24);
textFont(Font);
cols [0] = color(random(256), random(256), random(256) );
cols [1] = color(random(256), random(256), random(256) );
cols [2] = color(random(256), random(256), random(256) );
cols [3] = color(random(256), random(256), random(256) );
cols [4] = color(random(256), random(256), random(256) );
cols [5] = color(random(256), random(256), random(256) );
cols [6] = color(random(256), random(256), random(256) );
cols [7] = color(random(256), random(256), random(256) );
cols [8] = color(random(256), random(256), random(256) );
cols [9] = color(random(256), random(256), random(256) );
}
//Background background = new Background(); //first class
Grass grass = new Grass(); // second class
void draw()
{
float ellipseRadius = 25;
float circleRadius = 25;
//background.display();
if (keyPressed) {
if (key == âaâ || key == âAâ) {
x = (x-speedPlayer);
}
}
if (keyPressed) {
if (key == âdâ || key == âDâ) {
x = (x+speedPlayer);
}
}
if (x >= 800) {
x = 800;
}
if (x <= 80) {
x = 80;
}
fill (#00F727);
ellipse (400, 700, 900, 500); // landscape
fill (#674700);
rect(400, 400, 20, 50); // middle tree stump
fill (#018E02);
ellipse (410, 400, 50, 50); // middle tree leafs
fill (#674700);
rect(500, 450, 21, 51); // right tree stump
fill (#018E02);
ellipse (510, 445, 50, 50); // right tree leafs
fill (#674700);
rect(50, 500, 150, 400); // left tree stump
fill (#018E02);
ellipse (110, 350, 350, 350); // left tree leafs
fill (#E52525);
rect(x-60, 545, 30, 50); //body
fill (#DEACAC);
ellipse(x-45, 540, 50, 50); // head
fill (#000000);
rect(x-60, 532, 13, 10); //left eye sunglasses
rect(x-50, 532, 11, 3); //middle piece sunglasses
rect(x-42, 532, 13, 10); //right eye sunglasses
ellipse(x-45, 551, 10, 5); //mouth
ellipse(x-55, 595, 13, 5); //left foot
ellipse(x-35, 595, 13, 5); //right foot
fill (#FEFF0D);
ellipse(10, -5, 250, 250); //sun
if (mousePressed) {
fill (#E0E0E0);
ellipse(10, -5, 250, 250); //moon
}
grass.display();
fill(#FFFFFF);
text("Score: " + score, 693, 33);
Ball = Ball + speedBall;
if (Ball > 600) {
showMessageDialog(null, âClose One!â);
System.exit(0);
}
if (Ball < 600);
{
fill (#FF780F);
ellipse(BallX, 0+Ball, 50, 50);
if (dist(BallX, Ball, x, 590) < ellipseRadius + circleRadius+35) {
BallX = random(20, width-20);
Ball = random(-30, -10);
speedBall = speedBall + 0.3;
Ball = Ball + speedBall;
score++;
speedPlayer = speedPlayer + 0.25;
int index=int(random(10));
background(cols[index]);
}
}
}
But I appreciate the fact that you are already helping me for 3 hours long of your time. Couldnât make it witouth you man! I really mean that. Didnât expect to get a reply at first!
You need this line at start of draw () function
But that is a class, which is nothing at the moment so I did //. But I did put there a background (#FFFFFF); for testing, it doesnât draw it multiple times but the background stays white then.
??
Didnât we agree on
background (cols[index]);
YOU NEED some kind of background() at start of draw() because otherwise, you see falling ball like a snake