We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Ball bl;
void setup () {
size (600, 300);
}
void draw () {
background(0);
bl = new Ball(300, 150);
bl.display();
}
class Ball {
float x;
float y;
float diameter = 100;
float xspeed = 1;
float yspeed = 1;
Ball (float tempx, float tempy) {
x = tempx;
y = tempy;
}
void display() {
x=x+xspeed;
y=y+yspeed;
if ( y > height-diameter/2 || y < diameter/2) {
yspeed = -yspeed;
} else if ( x > width-diameter/2 || x < diameter/2 ) {
xspeed = -xspeed;
}
ellipse (x, y, diameter, diameter);
}
}
Thanks for help from now. ^^
Answers
You are creating a new ball every frame. Move line 9 to setup.
Oh, you are right thank you. And do you maybe have any idea how can i create a second ball without adding a new class?
I did this btw, what do you think? Is it good way to do this?
yeah, that's the purpose of a class: to have many objects derived from the same class:without you need to rewrite the class. In the class you can write as if there's only one ball. Outside the class you handle many balls.
The class is the cookie maker and the objects are the cookies. ONE cookie maker, many objects.
There is also a tutorial on that named objects
https://www.processing.org/tutorials/objects/
Now you have
and then
later use arrays / ArrayList to hold the balls together with a for loop please
ah our posts overlapped.
we were writing at the same time
looks good
as said: later use arrays / ArrayList to hold the balls together with a for loop please
line 19 and 20
could be
you could give the ball class a color to fill (also random in the constructor of the class or before it)
http://studio.ProcessingTogether.com/sp/pad/export/ro.9oyKfI9kOIa77/latest
http://studio.ProcessingTogether.com/sp/pad/export/ro.9s0026dE7B8v-/latest
line 33 it's not necessary to use
else if
here, it's even wrong, because we could have an x and y bounce at the same time, in a cornerjust use
if
technically
technically the method
display
does also move the ball. Thus is should be namedmoveAndDisplay
or so. Since it also does the bouncing, we could also userun
orscript
as a name for it. But all names should reflect what they are doing and the methods should not hold surprises (do other things beside their purposes suggested by their name)It looks now much better. :\">
I will learn the ArrayList and study the codes GoToLoop has sent. Then let's see what could i do. :\">
;-)
Chrisir, i think i could make sense of this ArrayList.
What do you think about this one? My goal is this: If enemy balls hit our ball then loop clears the array. How can i do this? Or have can i add ball every 30 frame for example?
Classname and listname should be Ball and balls
Note that class names are always beginning with a capital and are singular and the List small first letter and plural form.
Display our ball does not belong inside the ball , it does not use any of the variables in that class. More over you are drawing the same ball again and again how many times as you have enemy balls. Complete waste of processor time.
Move it outside the class
Line 18 must be outside the for loop for this
Look at dist() and use
if(dist(.... to check whether hit takes place. if so say list.clear();
To spawn a new one look at millis() and say in draw
if (millis() - startTime > interval){
list.add.....
startTime= millis();
}
startTime and interval must be variables before setup()
set interval to 1200 or so
I got the creating new ball every x sec, thanks.
I learned dist(), logic must something like this i guess: if (dist(mouseX, Balls'X) < diameterOfBall && dist(mouseY, Balls'Y) < diameterOfBall ){ }
But how can i find the position of these random balls?
Here are the last version of codes:
If diameter is used exclusively inside class Ball, you should move it to there instead. *-:)
And if its value never changes, you should consider declare it
static final
and use an ALL_CAPS name for it. :>And while we're at it, how about defining an accompany RADIUS too, so you don't need to type in DIAMETER/2 all the time. ;)
It looks now more intresting but can you explain two thing:
Why did you use DIAMETER >> 1 instead of DIAMETER/2? Why do we use ALL_CAPS names for the values never change?
>> 1
is equivalent to/ 2
. :PALL_CAPS convention for constants is even adhered by Processing itself.
Check out interface PConstants at the link below: ~O)
https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PConstants.java#L40
But you've also used bitwise or operator instead of my logical || operator. It seems you have a good reason for that and i feel you don't tell me this. [-X
In Java, the operator
|
is overloaded to acceptboolean
type in addition to integer values. :ar!Therefore the only diff. between
||
&|
is that the former set ups a short-circuit for lazy evaluation:https://en.Wikipedia.org/wiki/Short-circuit_evaluation
While for the latter, its evaluation strategy is eager. :-B
https://en.Wikipedia.org/wiki/Eager_evaluation
Thanks, i've learned a lot again. Do you know how can i get the x and y positions of the random balls that i can calculate the dist() to my mouseX and Y and give ''game over'' message?
your
dist
is wrongIt must be in your ball's
for loop
indraw
(because you want to check every enemy ball against our own mouse ball)What you're asking is about point/circle collision check:http://www.JeffreyThompson.org/collision-detection/point-circle.php
Let's add a new method to your class Ball to check whether mouse's cursor is inside it:Now within draw(), in your loop there, invoke this new method along w/ the others:Not bad but it's not taking into account the mouse ball radius
Oops! I didn't realize those mouse coords. were bound to an ellipse()! b-(
So it's not point/circle anymore but circle/circle intersect check: 3:-O
http://www.JeffreyThompson.org/collision-detection/circle-circle.php
Then let's declare constants for that solely ellipse()'s diameter & radius at the top of the sketch:
Inside draw(), we'll display that single ball w/:
For Ball::checkMouseBallInside(), we're gonna take that extra radius in consideration too: L-)
Expression
RADIUS*RADIUS + MOUSE_RADIUS*MOUSE_RADIUS
seems too big for my taste. /:)How about we cache its result as a constant too: :P
WOW! It works, very smart. :x
I just changed your codes little bit that i can picture it to myself better :
Is it o.k. so?
And can you please explain what does this do:
(random(1) < .5? -1 : 1);
(random(1) < .5? -1 : 1);
is a terniary operator, a compressed if-else statement:although this example is redundant as it can be shorten to:
boolean is_bigger_than_50 = input >50
Kf
Gottfried, be careful copying these syntactic quirks, they are often not best practice.
using >> 1 instead of the shorter and clearer / 2 for instance, or anything that's unexpected and needs further clarification, wouldn't pass any decent code review.
GottfriedBoehm
I don't think your last entry is correct- just compare to gotoloops version
As already pointed out, it should be the sum of radii: #-o
return sqrt(sq(mouseX - x) + sq(mouseY - y)) < RADIUS + MOUSE_DIAMETER;
Alternatively you can also use dist(): https://Processing.org/reference/dist_.html :)
return dist(mouseX, x, mouseY, y) < RADIUS + MOUSE_DIAMETER;
To simplify even more, cache the sum as 1 constant: B-)
? :
is called the conditional operator. A.K.A ternary b/c it demands 3 operands:https://Processing.org/reference/conditional.html
That particular expression w/
? :
got 50% of evaluating as-1
and 50% of1
.Multiplying that to the 1st expression
random(MIN_SPEED, MAX_SPEED)
, make field vx 50% chance to have an initial negative value. :PHere is the last version, i am almost happy. :D <:-P
My next goal is if collisions happen, it display ''GAME OVER'' text with background colour and if player clicks space game starts again. Can you guys help me also for this one?
Show your attempt for this one.
Where are you stuck?
Make a boolean gameOver which is initially false; set it to true then.
In draw() make a BIG if(gameOver)....
My attempt is like this:
above the setup: //Classes for stupid enemy balls and intelligent follower enemy balls EnemyBall e; FollowerEnemyBall f;
in setup: //Load the classes e = new EnemyBall(); f = new FollowerEnemyBall();
And here is the whole codes (i get somehow all the time game over):
First
Well, some remarks.
don't we want < RADIUS + MOUSE_DIAMETER
(gotoloop said that)
Second
not needed:
we handle the enemies via the ArrayList not with simple objects
Third
this must be in a for-loop
because we want to check every enemy against the player
Fourth
Make a
boolean gameOver
which is initially false; set it to true on collision.In draw() make a BIG
if(gameOver)....
You use something like this twice in your code. Look up dist () in the reference.
For first remark:
I have just tought < FRIEND_DIAMETER (meant the mouse diameter) works but if i check, i doesn't work pinpoint. Then i had this one:
return sqrt(sq(mouseX - febX) + sq(mouseY - febY)) <= FRIEND_DIAMETER/2 + ENEMY_DIAMETER/2;
But after this i've noticed, it doesn't check the collision exactly. then i added
FRIEND_DIAMETER/2 + ENEMY_DIAMETER/2 + 5;
and this works somehow, i don't know why.Remark fourth:
I've now this one in classes (should it be actually there?) but i get error message (found one too many { characters without a } to match it.
And here is the last version:
This:
??
you can't just throw lines into the class; they must be inside a function
you failed to use radius, you can always use radius where you say diameter / 2
WOW! I dig it! Very smart, thanks!
My next goal is showing the last level on game over screen and if player clicks the space button then level starts from 1 again. I tried too much thing but could not do this, my brain has stopped. :DDo you have any idea how can i do this?I did this. <:-P
Here is the last version:
You know that this:
will appear in both game and gameOver?
If we use such a BIG if in draw() nothing should be outside of it.
So it belongs in a function showLevel which we then call from showGameOver(); and / or showGame();
I want to display level in game (i could do this btw, our posts overlapped) and show the last level on gameOver screen, do you have any idea for this? :ar!
our posts overlapped
You know that this:
will appear in both game and gameOver?
That's why I removed it from draw() and moved it into the function showGame().
Lesson learned: When we have a BIG if in draw, nothing in draw() is allowed outside the if.
Yeah, got it. :)>-
But this millis()/INTERVAL shows only the time beginning from the game starts. It doesn't display the last level really. Am i right? :-/
True.
But did you test my version?
I put
you set lastTimeCheck when level starts again
Yeah, i tested. On game over screen it shows the added up levels. For example first game over on level 2 and the second 3 then i see something like 5 or even more.
That's another question than your previous
Adjust line 176