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;.
Related
I'm quite new to Processing.
I'm trying to make Processing randomly play a video after I clear the screen by mouseclick, so I create an array that contain 3 videos and play one at a time.
Holding 'Spacebar' will play a video and release it will stop the video. Mouseclick will clear the screen to an image. The question is how can it randomize to another video if I press spacebar again after clear the screen.
I've been searching all over the internet but couldn't find any solution for my coding or if my logic is wrong, please help me.
Here's my code.
int value = 0;
PImage photo;
import processing.video.*;
int n = 3; //number of videos
float vidN = random(0, n+1);
int x = int (vidN);
Movie[] video = new Movie[3];
//int rand = 0;
int index = 0;
void setup() {
size(800, 500);
frameRate(30);
video = new Movie[3];
video[0] = new Movie (this, "01.mp4");
video[1] = new Movie (this, "02.mp4");
video[2] = new Movie (this, "03.mp4");
photo = loadImage("1.jpg");
}
void draw() {
}
void movieEvent(Movie video) {
video.read();
}
void keyPressed() {
if (key == ' ') {
image(video[x], 0, 0);
video[x].play();
}
}
void mouseClicked() {
if (value == 0) {
video[x].jump(0);
video[x].stop();
background(0);
image(photo, 0, 0);
}
}
You have this bit of logic in your code which picks a random integer:
float vidN = random(0, n+1);
int x = int (vidN);
In theory, if you want to randomise to another video when the spacebar is pressed again you can re-use this bit of logic:
void keyPressed() {
if (key == ' ') {
x = int(random(n+1));
image(video[x], 0, 0);
video[x].play();
}
}
(Above I've used shorthand of the two lines declaring vidN and x, but the logic is the same. If the logic is harder to follow since two operations on the same line (picking a random float between 0,n+1 and rounding down to an integer value), feel free to expand back to two lines: readability is more important).
As side notes, these bit of logic look a bit off:
the if (value == 0) condition will always be true since value never changes, making both value and the condition redundant. (Perhaps you plan to use for something else later ? If so, you could save separate sketches, but start with the simplest version and exclude anything you don't need, otherwise, in general, remove any bit of code you don't need. It will be easier to read, follow and change.)
Currently your logic says that whenever you click current video resets to the start and stops playing and when you hit the spacebar. Once you add the logic to randomise the video that the most recent frame of the current video (just randomised) will display (image(video[x], 0, 0);), then that video will play. Unless you click to stop the current video, previously started videos (via play()) will play in the background (e.g. if they have audio you'll hear them in the background even if you only see one static frame from the last time space was pressed).
Maybe this is the behaviour you want ? You've explained a localised section of what you want to achieve, but not overall what the whole of the program you posted should do. That would help others provide suggestions regarding logic.
In general, try to break the problem down to simple steps that you can test in isolation. Once you've found a solid solution for each part, you can add each part into a main sketch one at a time, testing each time you add something. (This way if something goes wrong it's easy to isolate/fix).
Kevin Workman's How To Program is a great article on this.
As a mental excercise it will help to read through the code line by line and imagine what it might do. Then run it and see if the code behaves as you predicted/intended. Slowly and surely this will get better and better. Have fun learning!
When the song (the age of consent) ends how do I start another one after it ends? This is what I have so far.
import processing.sound.*;
SoundFile file;
void setup() {
file = new SoundFile(this, "neworder.mp3");
file.play();
file.amp(0.25);
}
The setup() method is only called once in processing. When processing starts running it calls an inbuilt draw() method continuously in a loop.
its in here you would need to check if the song has finished and load a new one.
void draw() {
background(200, 100, 50,255);
// YOUR CODE HERE TO CHECK IF SONG HAS FINISHED AND PLAY A BETTER SONG LIKE PO BOYDs - BLUE THREE ;)
}
Unfortunately the processing.org SoundFile reference is ommiting a few helpful functionalities of the class like percent() or position()
You could so something like:
import processing.sound.*;
SoundFile file;
void setup() {
file = new SoundFile(this, "neworder.mp3");
file.play();
file.amp(0.25);
}
void draw(){
if(file.percent() >= 99.8){
println("track pretty close to done: cue out / cross fade here");
file.stop();
}
background(0);
text(file.percent(),5,15);
}
You can then either load/play another track or better yet:
load two tracks in setup() but play only one
when the first track is close to being done, start the second track at 0 volume
use lerp() to interpolate it's volume of the first track down and the inverted volume value to bump up the second track with a nice cross fade
Have fun!
I'm still relatively new to Processing. We are thinking of showing our Processing work in a loop for a small exhibition. Is there a way to play multiple PDEs in a loop? I know I can export as frames and then assemble them as a longer loop-able Quicktime file, but I'm wondering if there's any way to play and loop the files themselves?
Also, for interactive PDEs, what's the best way to present them? We are thinking of having a couple of computers with PDEs running in Processing but it would be nice to have a file run for 20 minutes and then open another file for 20 minutes.
Thanks in advance!
You should be able to put together a shell/batch script that uses the processing-java command line executable.
You should be able to set that up via the Tools > Install "processing-java"
If you're not confortable with bash/batch scripting you could even write a Processing sketch that launches Processing sketches
Here's a very rough take on it using selectFolder() and exec():
final int SKETCH_RUN_TIME = 10000;//10 seconds for each sketch, feel free to change
ArrayList<File> sketches = new ArrayList<File>();
int sketchIndex = 0;
void setup(){
selectFolder("Select a folder containing Processing sketches:", "folderSelected");
}
void draw(){
}
void nextSketch(){
//run sketch
String sketchPath = sketches.get(sketchIndex).getAbsolutePath();
println("launching",sketchPath);
Process sketch = exec("/usr/local/bin/processing-java",
"--sketch="+sketchPath,
"--present");
//increment sketch index for next time around (checking the index is still valid, otherwise go back to 0)
sketchIndex++;
if(sketchIndex >= sketches.size()){
sketchIndex = 0;
}
//delay is deprecated so you shouldn't use this a lot, but as a proof concept this will do
delay(SKETCH_RUN_TIME);
nextSketch();
}
void folderSelected(File selection) {
if (selection == null) {
println("No folder ? Ok, maybe another time. Bye! :)");
exit();
} else {
File[] files = selection.listFiles();
//filter just Processing sketches
for(int i = 0; i < files.length; i++){
if(files[i].isDirectory()){
String folderName = files[i].getName();
File[] sketchFiles = files[i].listFiles();
boolean isValidSketch = false;
//search for a .pde file with the same folder name to check if it's a valid sketch
for(File f : sketchFiles){
if(f.getName().equals(folderName+".pde")){
isValidSketch = true;
break;
}
}
if(isValidSketch) {
sketches.add(files[i]);
}else{
System.out.println(files[i]+" is not a valid Processing sketch");
}
}
}
println("sketches found:",sketches);
nextSketch();
}
}
The code is commented so hopefully it should be easy to read and follow.
You will be prompted to select a folder containing sketches to run.
Note 1: I've used the processing-java path on my machine (/usr/local/bin/processing-java). If you're on Windows this may be different and you need to change this.
Note 2: The processing-java command launches another Process which makes it tricky to close the previous sketch before running the next one. As a workaround for you you could call exit() on each sketch after the amount of time you need.
Note 3: The code will not recursively traverse the selected folder containing sketches, so only the first level sketches will be executed, anything deeper should be ignored.
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.
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
}