How to create something happen when time = x - processing

I have a question about timers in Processing. I want to make a square spawn after every second that passes. Would I use a for loop, an if statement, etc.?

You could just use the millis() function or the frameCount variable in an if statement.
Something like this:
void draw(){
if(frameCount % 60 == 0){
//do something
}
}
More info can be found in the reference.

Related

Using random in JSP5

Does someone know how I can fix this problem?
var Arguments [2003,1002,3932,10203,2030,1828912,12912]
var Argument = Arguments[Math.floor(Math.random() * Arguments.length)]
Because of these variables, a random argument is always chosen at the beginning. However, if you are in the program and switch from page to page, it will keep the random value it had at the beginning. I want him to be constantly creating new values.
In the draw function I call the argument like this:
text(Argument, 300,180);
If I understand your question and the comments correctly, you need re-assign Argument to be a different random value each time you switch pages, such as this line every time it switchs:
Argument = Arguments[Math.floor(Math.random() * Arguments.length)];
You don't put var there every time. Give it a value in setup(), then re-assign it when you need another value.
Unless, of course, I completely missed your problem.
In p5.js there is a helpful function called random
so you could use it like this
var Arguments [2003,1002,3932,10203,2030,1828912,12912]
var Argument = Arguments[random(0, Arguments.length - 1)]
you can read more about it here
Make argument into a function.
var Arguments [2003,1002,3932,10203,2030,1828912,12912];
var Argument = function(){
return Arguments[ Math.floor( Math.random() * Arguments.length ) ];
}
text(Argument(), 300,180);
If you make a random selection in setup() it will only happen once in your whole script. Also, Don't choose variable names that might already be used by the system (I suspect "arguments" might be.
let args = [2003,1002,3932,10203,2030,1828912,12912];
let a;
function setup() {
createCanvas(400, 400);
a = random(args);
}
function draw() {
background(220);
textSize(30);
text(a,50,70);
}
function mousePressed() {
a = random(args);
}

How to forcefully end an iteration of a for-loop (without stopping the for-loop)?

This may be a stupid question, but here goes. Is there a way to forcefully end an iteration of a for-loop and ignore all other conditional statements within the loop, and move onto the next iteration?
I'm trying to make a tool where whenever a user types a letter, it's printed to the canvas.
I'm using a for loop to do this, with each iteration making the next letter move to the right each time.
Inside the for-loop includes the conditional statements for typing the letter:
function draw(){
}
function keyPressed(){
for(i=0; i<100; i++){
if(keyCode == 65){
text("a", 60 + i*10, 60)
}
...
...
...
}
}
and so on, and so forth. However, this would only work if after each letter it moved onto the next iteration (otherwise the letters would be printed in the same place due to 'i' not increasing, making it unreadable).
Using 'return' at the end of each condition statement doesn't work for me as 'return' ends the entire for-loop, whereas I just want to end that specific iteration.
All help is appreciated, thank you.
It sounds like you're looking for the continue keyword. Here's an example from W3Schools:
for (i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
Here is a linke to try the code yourself.
But taking a step back, this feels like a flawed design. If you want to get the key they user pressed, you don't have to use a bunch of if statements. You can just use the key variable. From the P5.js reference:
function setup() {
fill(245, 123, 158);
textSize(50);
}
function draw() {
background(200);
text(key, 33, 65); // Display last key pressed.
}
Even if that won't work for some reason (if you want to display something for the arrow keys, for example), there are probably better ways to solve this problem than a bunch of if statements in a for loop. For example you could create a mapping from keyCode to the string you want to display, and then call that mapping. Something like:
var m = new Map()
m.set(65, 'a');
m.set(66, 'b');
//..
function keyPressed(){
text(m.get(keyCode), 50, 50);
}

XNA game : calculate time between two shoots

I'm trying to make a game with the XNA library. I want a sprite to throw a fireball to hit falling asteroids. But I have a problem with pressing the concrete key: I want to throw fireballs, for example, with one second between throws.
I want to measure the time difference between creating instances. How can I do that?
UYou can use the ElapsedGameTime property of the gameTime variable passed to the Update method like this:
const float shootTimer = 1.0f;
float elapsedTime = 0f;
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
elapsedTime += gameTime.ElapsedGameTime.TotalSeconds;
if(elapsedTime >= shootTimer && /* Your logic to see if you should shoot a fireball */ )
{
// Shoot the fireball!
elapsedTime = 0f;
}
base.Update(gameTime);
}
Basically, what you are doing in the above code is setting a minimum value (seconds) that need to pass between each shot.
Then you create a variable that will store the amount of time that has passed between each shot.
In the Update method, you add the time between each Update call and then check if it is bigger than the timer, and if it is, then you shoot and reset the elapsed time.
Note: I wrote that piece of code out of the top of my mind so it may have some minor issue.
Each call to Update of your main Game class or any GameComponent receives an instance of GameTime as an argument. Its property ElapsedGameTime can be used to accumulate the passage of time.

reset game in processing

I am making a Tron game in Processing. I have the game all worked out but I do not know how to add a reset option to start a new game after the player loses.
Anyone have some suggestions?
Well usually you should make a method that will reset/recreate/delete what is needed to restart your game. Like(pseudo):
void reset(){
score = 0;
ballsList.removeAll();
playerPositionX = 0;
playerPositionY = 0;
}
And then call it when needed.
Avoid using "init" as name of the method or you will override a built in method.
Wouldn't a simple switch case work fine ?
Switch (levels):
Case one:
Case last level:
If (this == that){
levels = one;
break
}
What I would say is wrap your whole gamecode in a function like void inGame(){gamecodeing} and when something happens like if (player.state == "dead"){inGame();} and the ingame at the starting as well. Like so:
void setup() {
size(500,500);
}
void draw() {
inGame();
if (playerHasLost) {inGame();}
}
void inGame() {gameStuff}
and every time inGame() is called it kind of does it all over again.
I'd reccomend running the setup() again.
And then store your variables in there like x = 0;, score = 0;.

Lap timer in XNA 4.0?

Right, I've got a slight problem here, in which I've attempted to implement a lap timer.
In my protect override void update I've got this;
if ((IntersectPixels(destinationRedRect, car2redTextureData, startingLineRectangle, startingLineTextureData)))
{
{
redHit = true;
_timer1 += gameTime.ElapsedGameTime.TotalMilliseconds;
}
}
What I'm saying here^ is, if car2red is colliding with the starting line, the timer begins, but if it's not, timer does not add seconds (it just stops_ . How can I make it so, if car2red hits the startingLine and moves forward a few pixels (without touching starting line) the timer still continues?
Thank you.
You should have a separate if statement like this:
if (redHit) {
_timer1 += gameTime.ElapsedGameTime.TotalMilliseconds;
}
if ((IntersectPixels(destinationRedRect, car2redTextureData, startingLineRectangle, startingLineTextureData)))
{
redHit = true;
//Only use this line if you want to reset the timer to 0 when the player crosses that line again.
_timer1 = 0;// I'm assuming that _timer1 is a double
}

Resources