Trying to make something an array of objects disappear when loop ends - processing

I made a game where you use the mouse to make a rocket move to try to avoid hitting the asteroids falling down the screen. I have everything working right, but when the loop ends after an asteroid and rocket intersect, asteroids still stay on the screen. I am trying to get rid of the asteroids when the loop happens, but I'm not sure how.
Here is the link to my game if you want to see what is going on but it only works on processing: https://www.openprocessing.org/sketch/810719/embed/?plusEmbedHash=ZjhmZWQ4YTFlZGViNWUzOTczYmU3MDBmMzY3ZTE5OWRiNzFiYWE3MDNlZjA2YzMxMmVmNTgyNDNlNjdmYWFjMWUxMjhlNTJmODc5ZGVkZjY0ZTYwZDg3YmM4NzJhNDk2ZDhmYjY0OGQ3Y2MwYWQzNDViMjlhNDI4Y2M3MDlhYmRtSEFHMGxJejA5MTV2dFd1c3pOQmE3Y0RDSGRiYm5WakFkYUlxVzBsQmc2Z1QvWnhUQjh6amExektLU256OHpWSHpFcThvSTZZbHMxOC9PRjJQTSttdz09&plusEmbedTitle=true
This is what I have so far:
void draw() {
background(0, 0, 230);
m = millis();
//when game restarts the game will restart at 0 seconds rather than the time from the
previous game played
gameTime = millis()- startTime;
//displays time is in seconds
textSize(60);
fill(232, 98, 82);
text(gameTime / 1000, 790, 60);
// displays instructions about how to play the game
fill(232, 98, 82);
textSize(60);
textSize(15);
text("*Press a key for a little suprise while playing", 15, 20);
text("*Move mouse to avoid the asteroids", 15, 38);
rocket1.display();
rocket1.face();
//loop through Asteroid objects and move them.
//even though array is [100], only loop through the ones you want to see (numAsteroid)
for (int i=0; i<numAsteroid; i++) {
myAsteroidArray[i].display();
myAsteroidArray[i].move();
myAsteroidArray[i].topPop();
if (rocket1.intersect(myAsteroidArray[i])) {
//when the rocket hits an asteroid loop will stop and display the text and a circle
noLoop();
gameOver = true;
////xPosition, yPosition, xSpeed, color
background(57, 169, 219);
//display text for instructions about the game
fill(232, 98, 82);
textSize(60);
text(0, 255, 0);
text("Click Circle to Play", 191, 370); //if can't figure out timer do double click
textSize(15);
text("*Press a key for a little suprise while playing", 15, 20);
text("*Move mouse to avoid the asteroids", 15, 38);
fill(232, 98, 82);
stroke(232, 98, 82);
ellipse(width/2, height/2 + 40, 50, 50);
//use circle so the rocket doesn't get stuck in "no loop". it just so the user can press the screen to move the rocket away from the collision
textSize(60);
fill(255);
text("Time:" + gameTime/1000, 330, 500); //displays the time from the game the user had just played and lost
}
}
}
Thank you!

I know that noLoop() is sexy and everything, but in your case I would suggest to avoid using it. For several reasons, but the main one is that you won't be able to call mousePressed() or keyPressed(), and on your game over screen it seems like that's something that you would like to do.
Instead, you can use your gameOver boolean to split the draw loop into drawing the game or drawing the game over screen and get a similar result. Here I've hastily thrown an example of what it could look like:
void draw() {
if (gameOver) {
DrawGameOverScreen();
} else {
DrawGame();
}
}
void DrawGame() {
background(0, 0, 230);
m = millis();
//when game restarts the game will restart at 0 seconds rather than the time from the
//previous game played
gameTime = millis()- startTime;
//displays time is in seconds
textSize(60);
fill(232, 98, 82);
text(gameTime / 1000, 790, 60);
// displays instructions about how to play the game
fill(232, 98, 82);
textSize(60);
textSize(15);
text("*Press a key for a little suprise while playing", 15, 20);
text("*Move mouse to avoid the asteroids", 15, 38);
rocket1.display();
rocket1.face();
//loop through Asteroid objects and move them.
//even though array is [100], only loop through the ones you want to see (numAsteroid)
for (int i=0; i<numAsteroid; i++) {
myAsteroidArray[i].display();
myAsteroidArray[i].move();
myAsteroidArray[i].topPop();
if (rocket1.intersect(myAsteroidArray[i])) {
//when the rocket hits an asteroid loop will stop and display the text and a circle
//noLoop();
gameOver = true;
}
}
}
void DrawGameOverScreen() {
////xPosition, yPosition, xSpeed, color
background(57, 169, 219);
//display text for instructions about the game
fill(232, 98, 82);
textSize(60);
text(0, 255, 0);
text("Click Circle to Play", 191, 370); //if can't figure out timer do double click
textSize(15);
text("*Press a key for a little suprise while playing", 15, 20);
text("*Move mouse to avoid the asteroids", 15, 38);
fill(232, 98, 82);
stroke(232, 98, 82);
ellipse(width/2, height/2 + 40, 50, 50);
//use circle so the rocket doesn't get stuck in "no loop". it just so the user can press the screen to move the rocket away from the collision
textSize(60);
fill(255);
}
It solves your graphical problem quite nicely, and then let you handle what you want to do next (if that's something you planned to do). Have fun!

Related

How non-integer coordinates work in Processing when drawing shapes?

I'm trying to draw a series of lines and shapes that connect to each-other. Because of some calculations that I'm doing the coordinates for the lines/shapes end up being non-integer numbers and in this case there's a gap between lines that end and start at the same non-integer coordinate.
For example for this code:
void setup() {
noLoop();
size(500, 500);
}
void draw() {
stroke(1);
strokeWeight(100);
strokeCap(SQUARE);
line(0, 0, 0, 200.5);
line(0, 200.5, 0, 401);
}
I get this result (note the gap between the two lines):
Tried this also in p5.js with the same result. How is this working? Should I just always round to integers after doing my math?
After adding a background() call to the draw() the problem now appears to be with the SQUARE strokeCap as I initially thought and not the noLoop(). REM out strokeCap(SQUARE) and it runs ok. UN-REM it and the lines come back with or without noLoop(); PROJECT caps and no caps are still without lines, with or without noLoop(). I don't think the float values make a difference. Note that I removed the decimal values for strokeCap(SQUARE) to rule this out as a possibility. I'm unable to tell you why the lines occur with strokeCap(SQUARE), but the only way that I see to get rid of the lines is to use the PROJECT option, no caps at all, or keep them, but overlap the ends by 1 point. For whatever it's worth, Processing Reference states that all parameters for line() are floats.
void setup() {
size(500, 500);
noLoop();
}
void draw() {
background(209); // Added and it makes a difference
stroke(0);
strokeWeight(50);
// NO cap
line(60, 80, 180.3, 80);
line(180.3, 80, 280.8, 80);
line(280.8, 80, 380.5, 80);
// (SQUARE) cap
strokeCap(SQUARE); // REM this out to get rid of lines
line(60, 160, 160, 160);
line(160, 160, 260, 160);
line(260, 160, 360, 160);
// (PROJECT) cap
strokeCap(PROJECT);
line(60, 260, 160.5, 260);
line(160.5, 260, 260.5, 260);
line(260.5, 260, 360.5, 260);
// (SQUARE) cap with 1 point of overlap
strokeCap(SQUARE); // No lines with overlap
line(60, 360, 160.8, 360);
line(159.8, 360, 260.3, 360);
line(259.3, 360, 360.5, 360);
}

Processing - Ellipse is covering text

In the below image, I would like to have the score as text within the green ellipse. However, the ellipse is being drawn over the text, regardless of the order of the ellipse() and text() functions in the loop. Can anyone suggest why? My draw loop is shown below.
import processing.core.PApplet;
import processing.core.PFont;
public void drawHUD(PApplet marker, Clock time, int score)
{
PFont font = marker.createFont("Impact", 25, true);
marker.textFont(font);
marker.ellipseMode(CENTER);
fill(25, 100, 25);
marker.ellipse(50, marker.height - 50, 75, 50);
marker.noFill();
marker.text("Score: ", 25, marker.height - 100);
marker.text(score, 50, marker.height - 50);
marker.text("Seconds left: ", marker.width - 175, marker.height - 100);
marker.text(time.toString(), marker.width - 125, marker.height - 50);
}
Try replacing marker.noFill(); with marker.fill(255);. I don't believe noFill() works for text, and I have a feeling the text is being drawn above the ellipse, but is just the same colour as the ellipse so can't be seen.
I deleted the ellipsemode. It got rid of the center ellipse, but it got the text to the front.

(Newbie Processing Q) How do you slow down the the fading of colors? And how do you stop a moving object?

I'm a newbie who working on processing code. I'm using a variable to fade light blue into a darker blue and that's working fine, but I was wondering how I could slow the process of it fading.
Another question(hope people don't mind me asking two on one post), is how do I make a shape stop moving at a certain point? I have a ellipse there labelled the sun. I'd like it to stop when x=700.
Here's my coding:
float x = 0;
float y = 0;
float r = 0;
int gb = 0;
void setup() {
size(800, 600);
background(gb, gb, 255);
imageMode(CENTER);
noStroke();
}
void draw() {
background(0, gb, 255);
gb++;
if (gb>50) {
//the sun
fill(243, 230, 0);
ellipse(x, 60, 75, 75);
fill(243, 230, 0, 80);
ellipse(x, 60, 90, 90);
x++;
}
fill(0, 255, 0);
rect(0, 380, 800, 450);
}
I was wondering how I could slow the process of it fading.
Check out this line:
gb++;
Here you're incrementing (adding 1 to) your gb variable, which you're using to determine the color. To slow down the color changing, just add a smaller value to it. Something like this:
gb = gb + .1;
Which can be shortened to:
gb += .1;
For this to work, you'll have to change your gb variable to be a float so it can hold decimals.
You might also want to check out the lerp() and map() functions in the reference.
Another question(hope people don't mind me asking two on one post), is how do I make a shape stop moving at a certain point? I have a ellipse there labelled the sun. I'd like it to stop when x=700.
In the future, please only ask one question per post. And try to put together a MCVE for each question instead of posting your whole sketch.
But you can do this with an if statement that only increments x when it is less than 700. Like this:
if(x < 700){
x++;
}
Shameless self-promotion: I wrote a tutorial on using if statements to create animations in Processing available here.

What do i need to change to make the code work?

I started programming this pong game for CS class. I want to have the ball starting off in the center of the field so I used:
ellipse (width/2, height/2, 15, 15);
I want to make the game start once I press the space key. In order to do that I used:
if (keyPressed == true) {ellipse (ballX, ballY, 15, 15); fill (0, 255, 0);}
However it does not work. Can someone please help me figure out what is wrong with my code? Please consider that this is not a JavaScript but a Processing question.
Here is my entire code so far:
float ballX = 15, ballY = 15, dX = 15, dY = 15; // variables for the ball
float paddleX; // variables for the paddles
int mouseY; // variable to make the pong move with the mouse movement
boolean key, keyPressed;
void setup() {
size (1500,1100); // the field is going to be 1500x110px big
paddleX = width - 40;
ballX = 15; ballY = 15;
}
void draw() {
background(0); // black background
ellipse (width/2, height/2, 15, 15); // this is the starting point of the ball
if (keyPressed == true) { ellipse (ballX, ballY, 15, 15); fill (0, 255, 0); } // the game will only start when a key is pressed
if (ballX > width || ballX < 0) { dX = -dX; } // if the ball reaches the right or left wall it will switch directions
if (ballY > height || ballY < 0) { dY = -dY; }// if the ball reaches the upper or lower wall it will switch directions
ballX = ballX + dX; ballY = ballY + dY; // the ball with move with the speed set as dX and dY
rect(paddleX/58, mouseY, 20, 100); fill (255,10,20); // green pong
rect(paddleX, mouseY, 20, 100); fill (60,255,0); // red pong
}
The answer to this question is the same as the answer to your other question: you need to store the state of your sketch in variables, then you need to draw each frame based on that state, and finally you need to change those variables to change the state of your game.
Here's a simple example that only draws an ellipse after you press a key:
boolean playing = false;
void keyPressed() {
playing = true;
}
void draw() {
background(0);
if (playing) {
ellipse(width/2, height/2, 50, 50);
}
}
In this example, the playing variable is my state. I then update that state in the keyPressed() function, and I use that state to determine what I draw in my draw() function. You'll have to extrapolate a bit, but this process of breaking your problem down into a state, changing that state, and drawing that state is the answer to all of your questions.

Easiest way to animate a collection of shapes in processing?

I have been working on learning processing and now feel fairly comfortable drawing shapes and editing color etc. I have tried to take the step towards animating one of my prior works, but I just seem to keep running into dead ends or I can't get the unit to move cohesively. Essentially I am trying to move this entire guitar around the screen in any manner whatsoever as long as it stays intact. If you have a recommendation I would greatly appreciate it!
Current code is as follows:
void setup() {
size(600,600);
smooth();
noLoop();
}
void draw() {
//pegs
strokeJoin(SQUARE);
fill(255,255,255);
rect(530,105,20,20);
rect(565,70,20,20);
rect(473,50,20,20);
rect(505,18,20,20);
//neck and head
strokeWeight(60);
strokeCap(SQUARE);
strokeJoin(ROUND);
stroke(165,42,42);
line(490,110,560,40);
fill(255,255,255);
strokeWeight(45);
strokeCap(SQUARE);
strokeJoin(ROUND);
line(300,295,500,105);
//guitar body
noStroke();
fill(222,184,135);
ellipse(200,400,200,200);
ellipse(280,310,150,150);
//Sound Hole
fill(255,140,0);
ellipse(235,360,110,110);
fill(0,0,0);
ellipse(235,360,100,100);
//strings
stroke(255,255,255);
strokeWeight(3);
line(170,390,540,40);
line(180,400,550,45);
line(195,410,560,50);
line(207,420,570,55);
//string attatchment bottom
strokeWeight(6);
strokeCap(SQUARE);
stroke(165,42,42);
line(210,425,170,385);
}
The following code will animate your guitar so that it will move along the positive x-axis. This is just an example to give you an idea how "animation" really works:
// this will be used to move the guitar along x
int alongX = 0;
void setup() {
background(128);
size(600, 600);
smooth();
//noLoop();
}
void draw() {
background(128);
//pegs
strokeJoin(SQUARE);
fill(255, 255, 255);
rect(530+alongX, 105, 20, 20);
rect(565+alongX, 70, 20, 20);
rect(473+alongX, 50, 20, 20);
rect(505+alongX, 18, 20, 20);
//neck and head
strokeWeight(60);
strokeCap(SQUARE);
strokeJoin(ROUND);
stroke(165, 42, 42);
line(490+alongX, 110, 560+alongX, 40);
fill(255, 255, 255);
strokeWeight(45);
strokeCap(SQUARE);
strokeJoin(ROUND);
line(300+alongX, 295, 500+alongX, 105);
//guitar body
noStroke();
fill(222, 184, 135);
ellipse(200+alongX, 400, 200, 200);
ellipse(280+alongX, 310, 150, 150);
//Sound Hole
fill(255, 140, 0);
ellipse(235+alongX, 360, 110, 110);
fill(0, 0, 0);
ellipse(235+alongX, 360, 100, 100);
//strings
stroke(255, 255, 255);
strokeWeight(3);
line(170+alongX, 390, 540+alongX, 40);
line(180+alongX, 400, 550+alongX, 45);
line(195+alongX, 410, 560+alongX, 50);
line(207+alongX, 420, 570+alongX, 55);
//string attatchment bottom
strokeWeight(6);
strokeCap(SQUARE);
stroke(165, 42, 42);
line(210+alongX, 425, 170+alongX, 385);
// incrementing alongX to move it along +x, decrement it to move it along -x
alongX++;
}
The idea is very simple. Animation is basically an illusion of movement. Objects appear such that they're moving but the effect is achieved very simply by drawing and redrawing (at a certain speed: frameRate) the objects on the screen in different locations in such a way that they appear to the viewer as though they're moving when in reality they're not.
For this effect to be effective, you have to have certain things in your sketch (or any other animation package):
You need to have variables that control the position of the object so
when you want to move the object, you can easily change the values
of the variables controlling the position in such a way that it would
give the illusion of movement.
You also, in this case, need to keep redrawing the background. That
is why I added the background(128); line in your draw() method.
If you comment that line out, you will see that the effect is
different and the guitar leaves a trail (maybe that's what you
want...a trail that later fades away? Its up to you!).
Another thing you need to consider in your sketch is that while
rects and ellipses only need to have one variable changed in
order for them to move from left to right (in the case of this
sketch), lines need to have both x values manipulated by your
variable.
noLoop() in setup() is specifically designed so you can stop
draw() from its infinite loop or game loop but stopping
draw() means no animation effects. You can have noLoop(), for
example, to stop the animation upon pressing a key but then you'd
also want to provide loop() to get the animation going upon
pressing a key. These are some things and decisions for you to think
about and play with.
Finally, I just changed your sketch a little to show you how
animation may work. You can add a ton of things to it to make it
awesome.
PS: Cool guitar!
UPDATE
After getting comfortable with the idea of moving objects around, I would look at vectors and how they can be used to set the direction, and velocity of objects. Take a look at PVector in Processing. This tutorial would be sufficient to get you going with PVectors. But I would do this on objects. For example, a full Guitar object. Not on individual lines and rects.

Resources