Stop and Restart game in Processing on button press - processing

I'm currently working on a small game to learn Processing.
I want my game to stop when I press "Stop" and want my game to reset/restart when "Stop" changed to Start.
(When I click on the button, it changes Stop to Start and back to Stop etc etc. (so basically, I have 1 'button')
I'm struggling and can't find a solution on internet / stackoverflow so maybe someone can help me?
(# mousePressed's if else I need the 'stop and restart function')
float x = width/2;
float speed = 2;
boolean textHasBeenClicked = false;
int aantalRaak = 0;
int aantalMis = 0;
int positieText = 20;
void setup() {
background(0);
size(600,500);
}
void draw() {
clear();
move();
display();
smooth();
//Scoreboard bovenaan
fill(255);
textSize(20);
textAlign(LEFT);
text("Aantal geraakt: " + aantalRaak,0, positieText); text("Aantal gemist: " + aantalMis, width/2, positieText);
//button onderaan
fill(0,255,0);
rect(width/2-40, height-40, 100, 50);// draw anyway...
}
void mousePressed() {
// toggle
textHasBeenClicked = ! textHasBeenClicked;
fill(0);
if (textHasBeenClicked) {
// display text 2
textSize(30);
textAlign(CENTER);
text("Stop" , width/2,height-10);
}
else {
// display text 1
textSize(30);
textAlign(CENTER);
text("Start" , width/2,height-10);
}
}
void move() {
x = x + speed;
if (x > width) {
x = 0;
}
}
void display(){
//schietschijf
float y = height/2;
noStroke();
fill(255, 0, 0);
ellipse(x, y, 40, 40);
fill(255);
ellipse(x, y, 30, 30);
fill(255, 0, 0);
ellipse(x, y, 20, 20);
fill(255);
ellipse(x, y, 10, 10);
}

You should try to break your problem down into smaller steps and take those steps on one at a time. You're really asking two questions:
How do I show a stop button that turns into a start button?
How do I reset a sketch?
For the first question, you can create a boolean variable. Use that variable in your draw() function, and then modify that variable in the mousePressed() function.
boolean running = false;
void draw() {
fill(0);
if (running) {
background(255, 0, 0);
text("Stop", 25, 25);
} else {
background(0, 255, 0);
text("Start", 25, 25);
}
}
void mousePressed() {
running = !running;
}
Then for resetting the sketch, you can create a function that changes all of your variables back to their default values. Here's a simple example:
float circleY;
void setup() {
size(100, 500);
}
void draw() {
background(0);
circleY++;
ellipse(width/2, circleY, 20, 20);
}
void reset() {
circleY = 0;
}
void mousePressed() {
reset();
}
Try to work from small examples like this instead of your full program, and post a MCVE if you get stuck. Good luck.

You may consider implementing a while loop. I don't know what library you're using for input, so I can't tell you exactly what to do. But something along the lines of:
while(!InputReceived) {
if(CheckForMouseInput()) // Assuming CheckForMouseInput returns true if input was detected
break // Input was detected, now do stuff based on that.
else {
// Must #include <thread> and #include <chrono>
// Wait a bit...
continue; // Jump back to the top of the loop, effectively restarting it.
}
Would probably suit your needs. That is what I would do, at least. After the loop breaks, the game is effectively restarted, and you can do whatever you need to do based on that.

Related

How do I make the object (ball) move after 5 seconds?

I have the code for which the ball moves as soon as I run the code but I want it to move after 5 seconds.
I have written the code in Processing.
This is my code:
void setup(){
size(640,450);
}
void draw() {
background(155);
ellipse(x, 100, 50, 50);
x = x + 1;
}
millis() will return the number of milliseconds since you started the program. You can try this:
int x = 0;
void setup(){
size(640,450);
}
void draw() {
background(155);
ellipse(x, 100, 50, 50);
if (millis() > 5000) {
x = x + 1;
}
}
And the ball will wait 5 seconds. Have fun!

I came across a reflect invocation target exeption in processing and can't find what is causing it

I was remaking a platformer after I closed it without saving my code and came across the error. I don't know how the error occurred and am completely lost.
Here's the code below(sorry for any incomplete code):
PImage saw = loadImage("saw.png");
game run = new game();
void setup(){
size(1000, 1000);
}
void draw(){
run.saw(100, 100);
}
class game{
boolean dead;
int ballX;
int ballY;
int sawRotate;
game(){
dead = false;
ballX = 100;
ballY = 50;
sawRotate = 1;
}
void ball(){
}
void saw(int x, int y){
pushMatrix();
rotate(sawRotate);
translate(x, y);
image(saw, 100, -100, 500, 500);
popMatrix();
}
void platform(){
}
}
You can't use Processing functions until after the setup() function has been called. This line happens before that:
PImage saw = loadImage("saw.png");
You need to move the call to loadImage() to be inside the setup() function:
PImage saw;
void setup(){
size(1000, 1000);
saw = loadImage("saw.png");
}

Moving around inside a larger-than-screen image

Context: Processing v3; Windows 10
I have an 853x2048 pixel image that I want to display fullsize on the screen. A large part of it will disappear off the bottom. I want to use Processing to navigate around inside the image, motion-path like, pause at certain points and also zoom in and out.
The code below is adapted from a demo of the Robot class as I thought that would be the go for moving around inside the image.
This code works but I can't as yet figure out how to move the viewport. And then there's the issue of zooming (which I am yet to address.)
How does one move the mouse so that the image moves or how does one move the image with respect to the viewport?
//
// how to use java.awt.Robot class in processing ...
//
import java.awt.*;
import java.awt.event.*;
Robot robot;
PFont pfont;
Point save_p;
PImage img;
void setup() {
try {
robot = new Robot();
robot.setAutoDelay(0);
}
catch (Exception e) {
e.printStackTrace();
}
surface.setResizable(true);
fullScreen();
img = loadImage("bigpic.jpg");
pfont = createFont("Impact", 32);
}
void draw() {
background(#ffffff);
fill(#000000);
imageMode(CORNERS);
image(img, 0, 0, 640*3, 480*8);
Point p = getGlobalMouseLocation();
textFont(pfont);
text("now x=" + (int)p.getX() + ", y=" + (int)p.getY(), 10, 32);
if (save_p != null) {
text("save x=" + (int)save_p.getX() + ", y=" + (int)save_p.getY(), 10, 64);
}
}
void keyPressed() {
switch(key) {
case 's':
save_p = getGlobalMouseLocation();
break;
case 'm':
if (save_p != null) {
mouseMove((int)save_p.getX(), (int)save_p.getY());
}
break;
case 'c':
break;
case 't':
translate(2000, 0);
break;
case ' ':
if (save_p != null) {
mouseMoveAndClick((int)save_p.getX(), (int)save_p.getY());
}
break;
}
}
Point getGlobalMouseLocation() {
// java.awt.MouseInfo
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
Point p = pointerInfo.getLocation();
return p;
}
void mouseMove(int x, int y) {
robot.mouseMove(x, y);
}
void mouseMoveAndClick(int x, int y) {
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
}
I'm a little confused by what you're doing with the Robot class.
But in any case, basically what you want to do is draw the image so its Y value is lower, which will cause the image to rise so you can see more of it. Here's a small example:
PImage img;
float imageY = 0;
void setup() {
img = loadImage("bigpic.jpg");
}
void draw() {
image(img, 0, imageY);
imageY--;
}
You can do something similar with resizing, just by passing in more parameters to the image() function.
But it sounds like what you're really looking for are the transformation functions. See the transform section of the Processing reference for more info, but basically you would call the translate() function to move the origin. You could also call the scale() function to "zoom" in and out.
Here's a little example that moves around a rectangle instead of an image:
void setup(){
size(500, 500);
}
void draw(){
background(64);
translate(-mouseX, -mouseY);
rect(0, 0, width, height);
}
Don't forget that Processing comes with a ton of examples. In the Processing editor, go to File > Examples.

How to make an image fade to another image when a certain amount of light falls on a LDR?

I have been trying to make an image transistion to another image when a certain amount of light falls on a LDR, and transistions back when that light is not shining on the LDR anymore. However, the piece of code is not working properly. I am new to processing, so there is a good chance that I am doing it completely wrong. It would be lovely if someone could help me achieve the fade in and out! Thanks in advance! Here is a part of my code, and what I have tried to do for the fade:
`
int transparency = 0;
void setup() {
img = loadImage(imgFileName+"."+fileType);
bla = loadImage("mermaid.jpg");
size(img.width, img.height);
image(img, 0, 0);
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(ledPin, Arduino.OUTPUT);
arduino.pinMode(sensorPin, Arduino.INPUT);
}
void draw() {
int analogValue = arduino.analogRead(sensorPin);
println(analogValue);
if (analogValue > 900) {
noTint();
image(bla, 0, 0);
} else {
while (column < width-1) {
img.loadPixels();
sortColumn();
column++;
img.updatePixels();
}
while (row < height-1) {
img.loadPixels();
sortRow();
row++;
img.updatePixels();
}
if (transparency < 255) {
transparency++;
}
tint(255, 255, 255, transparency);
image(img, 0, 0);
//automatically save the image
if (!saved && frameCount >= loops) {
saveFrame(imgFileName+"_"+mode+".png");
saved = true;
}
}
}`
Your current approach seems to be more complicated than necessary, as it a fade transition can be handled solely by the tint function without reloading all of the pixels in an image. Additionally, transparency doesn't decrease at any point in the sketch. A simple alternative might be:
void draw() {
int analogValue = arduino.analogRead(sensorPin);
println(analogValue);
noTint();
image(bla, 0, 0);
if (analogValue > 900) {
if (transparency > 0) {
transparency--;
}
} else {
if (transparency < 255) {
transparency++;
}
}
tint(255, transparency);
//only need 1 color variable, ref. https://processing.org/reference/fill_.html
image(img, 0, 0);
}

Image Cursor in Tuio Code

I'm looking to make an external image cursor to track the fiducial codes in tuio for my graffiti wall. Since I need background to not be run constantly in void draw(), the image cursor leaves a trail of the image around the stage. Anyone know how I can stop it from doing this without placing background in void draw()?
This is my code;
// background(255);
textFont(font, 18*scale_factor);
float obj_size = object_size*scale_factor;
float cur_size = cursor_size*scale_factor;
Vector tuioObjectList = tuioClient.getTuioObjects();
for (int i=0;i<tuioObjectList.size();i++) {
TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
stroke(0);
fill(0);
//pushMatrix();
//translate(tobj.getScreenX(width),tobj.getScreenY(height));
//rotate(tobj.getAngle());
image(spray,10,10);
//rect(-obj_size/2,-obj_size/2,obj_size,obj_size);
// popMatrix();
if (mp == true)
{
if (tobj.getSymbolID()==66) {
fill(#FF00FF);
noStroke();
tint(255,127);
ellipse(tobj.getScreenX(width), tobj.getScreenY(height), 50, 50);
fill(#FF00FF);
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
}
if (tobj.getSymbolID()==23) {
fill(#0000FF);
noStroke();
tint(255,127);
ellipse(tobj.getScreenX(width), tobj.getScreenY(height), 50, 50);
fill(#0000FF);
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
}
if (tobj.getSymbolID()==22) {
fill(#00FF00);
noStroke();
tint(255,127);
ellipse(tobj.getScreenX(width), tobj.getScreenY(height), 50, 50);
fill(#00FF00);
text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
}
}
}
Vector tuioCursorList = tuioClient.getTuioCursors();
for (int i=0;i<tuioCursorList.size();i++) {
TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(i);
Vector pointList = tcur.getPath();
if (pointList.size()>0) {
stroke(0, 0, 255);
TuioPoint start_point = (TuioPoint)pointList.firstElement();
;
for (int j=0;j<pointList.size();j++) {
TuioPoint end_point = (TuioPoint)pointList.elementAt(j);
line(start_point.getScreenX(width), start_point.getScreenY(height), end_point.getScreenX(width), end_point.getScreenY(height));
start_point = end_point;
}
stroke(192, 192, 192);
fill(192, 192, 192);
ellipse( tcur.getScreenX(width), tcur.getScreenY(height), cur_size, cur_size);
fill(0);
text(""+ tcur.getCursorID(), tcur.getScreenX(width)-5, tcur.getScreenY(height)+5);
}
}
}
void mousePressed () {
bDrawFullSize = true;
mp = true;
player.play();
if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
button = !button;
}
}
/*if(mousePressed) {
if(mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h){
image(stencil1,200,100,FULL_SIZE, FULL_SIZE);
}
else
{
if(mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h){
image(stencil2,200,100,FULL_SIZE, FULL_SIZE);
}
}
}
}*/
void mouseReleased() {
mp = false;
player.close();
//since close closes the file, we need to load the sound effect again.
player = minim.loadFile("spray_close.wav");
}
void mouseDragged() {
drag = true;
}
// these callback methods are called whenever a TUIO event occurs
// called when an object is added to the scene
void addTuioObject(TuioObject tobj) {
println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
}
// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
}
// called when an object is moved
void updateTuioObject (TuioObject tobj) {
println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
+" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
}
// called when a cursor is added to the scene
void addTuioCursor(TuioCursor tcur) {
println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY());
}
// called when a cursor is moved
void updateTuioCursor (TuioCursor tcur) {
println("update cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY()
+" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel());
}
// called when a cursor is removed from the scene
void removeTuioCursor(TuioCursor tcur) {
println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");
}
// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime) {
redraw();
}
void keyPressed() {
endRecord();
background(bg1);
bDrawFullSize = false;
button = false;
}
Maybe using layers, draw your cursor and/or the other stuff in different layers. Erase one layer and not other as you need, display all them in draw. This is usually done with PGraphics objects. Search processing + layers to see samples. Here and/or in processing forum
something like this:
EDIT: Actually I think that the way to do it is to move all non refreshing draw to a PGraphics and do refresh draw with background(), where you can draw refreshing stuff without a trail. I changed the code to reflect that.
PGraphics l1;
void setup() {
size(200, 200);
l1 = createGraphics(200, 200, JAVA2D);
background(255);
noStroke();
}
void draw() {
background(255);
l1.beginDraw();
l1.fill(255, 0, 255);
l1.noStroke();
if (frameCount%100 == 0) {
l1.rect(random(width), random(height), 20, 20);
}
l1.endDraw();
image(l1, 0, 0);
fill(230);
ellipse(mouseX, mouseY, 30, 30);
}

Resources