We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello! completely new to processing here, having started it for a university module. I have created a really simple code for a an image of a balloon that changes to a picture of a "pop" when it is pressed, however I'm having trouble with replacing the image, it simply ends up overlaying it? similarly, I have tried to code my sketch so that the dimensions of the image has to be clicked on for the image to change, but my code annoyingly just lets you click anywhere in the sketch and it changes. Any sort of help would be much appreciated! thank you!
PImage balloon;
PImage balloonPop;
float r1; //
float r2; //
void setup() { //
background(255, 204, 0);
size(1200, 700);
balloon = loadImage("processingballoon.png");
balloonPop = loadImage("processingballoon2.png");
r1 = random(width);
r2 = random(height);
}
void draw() {
image(balloon, r1, r2);} //
void mousePressed(){
println(mouseX,mouseY);
if (mouseX > r1 && mouseX < (r1 + balloon.width) && mouseY > r2 && mouseY < (r2 + balloon.height ));
{
background(255, 204, 0);
image(balloonPop,r1,r2);
}
}
Answers
Hi and welcome to the forum!
In processing you can use
ctrl-t
to get better indents automaticallyNow, your code is not bad.
But
draw()
runs on and on soballoon
is always displayed.The function
mousePressed()
is only called once per click so the popping image only appears briefly.What you could do
To avoid that create a new variable, before
setup()
sayboolean hasPopped=false;
to indicate that the balloon is intact.Now in
draw()
all the drawing takes place to make it permanent and not only briefly.So, in
draw()
sayThat’s doing the drawing but it knows what to draw based on
hasPopped
, our famous marker/ flag variable.In
mousePressed()
just inside the if-clause writehasPopped=true;
to store permanently that the situation has changed.Your if-clause
Regarding the utter disaster of your
if
-clause: it’s caused by the;
at the end ofif
.;
separates theif
from the entire{...}
section.So delete the bad
;
here.Okay?
Best, Chrisir ;-)
thank you so much you are a star! works perfectly now!
Great! Well done!
If you like hit
ctrl-t
and post your entire new version here for others.Chrisir , I also was wondering how i could develop the code further by having another balloon image generate in a random location in the sketch after the initial balloon is popped and disappears? kind of like a balloon popping game? thank you again!
Thank you very much!
Ctrl-t has not happened
You can delete line 25
To make a moving balloon, line 24 would go to the beginning of draw with a color stored in a color variable which is then changed in line 24
The inner brackets () in line 22 are not necessary
if(hasPopped==true)
can be shortened to
if(hasPopped)
do you mean like this? I feel I have definitely got something wrong, sorry!
Excellent, well done!
first regarding your background I have this version, using two fixed colors yellow and red and one currentColor that gets used by background. When I want to change the background color I say currentColor = red; or currentColor = yellow;. So again it's storing data or situation and using / applying it. Both storing and applying are separate.
I also made line breaks in the if-clauses in line 18 and 25 and some empty lines for readability. It's a matter of taste but in the long run readability is what you should aim for in programming the most.
I realize that I overlooked your question:
That's a good question.
Try to rephrase the question into a concept
Please think it through more thoroughly. What exactly do you want to happen after the balloon exploded? Have a small message that appears "Play again"? Or do you want it so that the next balloon appears immediately (popped balloon would flash only briefly that would look lame).
Programming is also about having a clear step by step concept and to fill out the gaps in a vague expression from the customer like "by having another balloon image".
Try to rephrase the question into a concept.
But let's see. Best would be
hasPopped
(to what?) and alsocurrentColor
(to what?)Also we want to reset / set again
r1
andr2
- to what?(By convention they should better be called x1 and y1 by the way.)
Please try to go that path and we'll help if you need more.
Timer
OK, a word to the timer. A timer is like a stop watch. When the balloon pops, you start the stop watch (set a variable timerStart) and when 1.2 seconds have passed the time is up and you want to reset the balloon to intact.
We use
millis()
which is initially0
and endlessly goes up during the run of the sketch (read: milliseconds since I started the sketch). We don't really care how big millis is since we just use the difference between now (millis()
) and the time we started the timer (where we copied the former current millis into the variable timerStart). Don't worry, you'll get used to it. Remark: 1.2 seconds are 1200 milliseconds.Here is one piece of the code:
before setup() say
int timerStart;
and in mousePressed() say
As you can see, I left some things out for you to solve e.g.
// do something....................
. ;-)Best, Chrisir
if (hasPopped) image(balloonPop, r1, r2);
is my favorite style, as long as the whole line isn't too long. :P;-)
next step is to have a balloon that slowly moves up and bit sideways. It would be harder to hit with the mouse.
in the mean time I made a version that shows one balloon after the other and measures your reaction time. My record is around 550 milliseconds.
I got it working,thank you for the tips! The balloon 'pops' and then re-appears soon after it is pressed, perfect! :) Now to figure out how to make it re-appear in a different location in the sketch every time... :-? your version also sounds interesting!
Look at lines 20 and 21
Also line 44 could be after line 52
also the section line 31 to 36 applies only when the ballon is popped, right?
hello again! I have figured out how to make the balloon move to a random position when clicked/popped, however, about 3 or 4 clicks in, it completely disappears off of the screen! Is this to do with the balloon leaving the boundaries of the sketch, or something else?
Line 22/23 is not perfect
You need to take into account the width and height of the image so it cannot disappear right or bottom of the screen
solved it :) also added a video of moving clouds as the background and a click counter, so its really coming together. How would I go about making a reaction timer? i.e. when a certain number of balloons are clicked (20, for example) a message comes up showing how long in seconds it took for you to click them all and then restarts the game.
You need a new timer variable like
timerStartPlayer=millis();
That starts at when the new Ballon appears intact
It stops when user popps it
You can then e.g. measure the shortest time, the longest / slowest time and the average.
You need also a balloon counter to count to 20
Show your attempt- there’s already a timer and you can use a similar approach for the new timer timerStartPlayer
this is what i have so far, I set the counter to count down from 20 and to reset when it gets to 0. Im not too sure how to get the sketch to record and add up each time between every 'pop'.
before setup() :
when the new intact Ballon appears :
when user popps ballon:
Thank you so much for your help so far,its almost complete! I adapted it a little bit so that the reaction time is shown on the left corner of the sketch. The final step of my game is when the balloon count reaches 0, I want the words "Game Over" and the end reaction time to appear, and then the game restarts. However for some reason, it isn't working:
Are you kidding?
As I said
The function mousePressed() is only called once per click so the popping image only appears briefly. (or your game over screen)
Now in draw() all the drawing takes place to make it permanent and not only briefly.
So as we did with
hasPopped
you need to set a marker in mousePressed() and act accordingly indraw()
(your game over screen)sorry if my questions seem really dumb, I'm quite a slow learner but I'm slowly getting there, I appreciate your patience! I have got the gameover screen to work,and added a start screen using the same technique,but now I'm having difficulty resetting the timer (timerStartPlayer) at the start screen and back to 0 each time the game is reset after game over. I know that milis can't be 'reset' once the sketch has started, so should i make a class for the timer or is there an easier solution?
this is my version, with last time, best time (left upper corner) and current time (in the right upper corner) and a fancy countdown 3-2-1 Go
you don't have to reset millis
just start timerStartPlayer at the right point in your code (you got that horrible wrong) with timerStartPlayer=millis();
and measure the time (you got that wrong too) when the balloon popps:
timeItTookTheUserToReact = millis() - timerStartPlayer;
this
must be
if (start==true
with double == (or justif (start)
)Same
if (GameOver=true
don't have that twice:
Your draw is a mess.
You got several variables start gameOver haspopped that indicate different situations or screens.
Those are called states of a program.
Since only ONE variable is allowed to be true at one point in time, the are connected.
Hence, make ONE variable int state; that can have several values of type integer indicating the current situation.
the values could be start (let's say 0 ), gameover (ok, 1), gameRunningWithBallonIntact (2), gameRunningWithBallonPopped(3)
now your draw would be
believe me, much better.
Instead of setting gameover or start now, delete all of them, just set state correctly throughout
Chrisir
have tried puttingnever mind i got it!! (i think) thank you :)timerStartPlayer=millis();
andtimeItTookTheUserToReact = millis() - timerStartPlayer;
almost everywhere and can't get it to work :( maybe i should get some sleephit ctrt-t,
can you post it again?
I managed to get the timer to work, but i'm not familiar with utilising switch statements at all so i have definitely messed something up lol:
switch is really simple, it's just like a long
if(...) ... else if (...) ... else if (...) ...
clausedefinitely you shouldn't have anything in draw that is outside of switch. Because to which state should it belong to?
Also you should have
in mousePressed as well : every mouse inputs happens in the context of a state and should be treated differently depending on state
I kind of understand what you mean, but when i replace the if statements with the switch statements, I get all sorts of errors. do you have a small example for me to try and understand how it could work?
are you sticking with start (let's say 0 ), gameover (ok, 1), gameRunningWithBallonIntact (2), gameRunningWithBallonPopped(3)
then get rid of gameover, haspopped and start
here is an example:
using the example, I've made a bit of progress, but now the balloons now spin around the screen really fast instead of doing what they're meant to :/ just can't get my head around it
this:
is wrong because of the semicolon in
if (mousePressed);
. The if clause ends with ; andis executed always
since mousePressedForStart() is called by mousePressed this :
is not necessary
in draw you have:
but then you swapped start and end in mousePressed
handlestateGameRunningWithBalloonIntact
in handlestateGameRunningWithBalloonIntact
you want
and so on.....
please proof read and debug.
you still need the timers, but not the booleans !
still a lot to do for you:
yay!!! all working!! :) let me know please if theres any other small mistakes but everything seems good to me. thanks so much!
hit ctrl-t
You are not using the constants:
You can use them in
draw()
andmousePressed()
instead of 0,1,2....So code is better readable
Are you sure timerStartPlayer and timerStart are handled correctly??
The timers are working as I want them to, but would you suggest a better place for them to go in the code?
On another related topic, do you have any suggestions or pointers as to how I would go about extending this app by connecting it to an Arduino?
it's horrible, so many mistakes...
in draw() these are swapped, 2 is intact, 3 is popped (following your constants), not vice versa:
That's the natural order: first intact then popped. Not vice versa.
to avoid this kind of errors, use the constants instead of the numbers:
why do you have this?
why do you have this?
I suggest
Remark
This
can be
(with changes to random from above)
Remark
Do you realize that when you click next to a balloon, mouseClicks goes back to 20? Do you want this? (if so, shouldn't you reset the reaction time counter?)
mousePressedForStart()
This
void mousePressedForStart() { state=3; }
shouldn't we say state=2; here (
stateGameRunningWithBalloonIntact = 2;
)(probably you here compensate the mistake in draw(), making it all the more hard to read)
instead of state = 2 or state = 3 you want
state = stateGameRunningWithBalloonIntact;
TIMER
in mousePressedForStart() :
You forgot to place this here too :
because in my opinion, on first start timerStartPlayer is zero (you never set it to
millis()
), than he loads the movie and the images, which can take a lot of millis, then we show the start screen (more millis pass by!!!!), the ballons are shown and all these milliseconds are calculated as reaction time of the player. You make him slower than he is!!!in the second round of the game (after the first 20 clicks) I think it's NOT better because of these lines in handlestateGameRunningWithBalloonPopped() :
You are going to state end here then later to state start and the counter timerStartPlayer is all the time counting before the first balloon is shown.
Moreover:
During
handlestateGameRunningWithBalloonPopped()
with this lineif(millis()-timerStart > 200) {
you wait 200 millis before the next balloon is shown.In this phase, the player can't react at all!!!! But the timer keeps on running. So the 200 millis are counted against him, making him appear slower than he is again. And 20x200 mills: 4000 - these are 4 seconds! That's unacceptable.
Testing
You really need a proper testing. Test yourself. Get a friend to test it, wait longer times on start and stop screen, measure the time roughly with your phone to check those obvious errors!
I can't believe you said, you're happy with your timers...
Chrisir
best ask in the arduino section here in the processing forum.