We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I found a brilliant game on processing online. However, I want to learn how to restart a game. can anyone help me to do so?
---------------here is the code I found online that is the closest code that I have learned recently------------
ArrayList Targets; // ArrayList to put all targets in.
ArrayList scoreFire; // ArrayList to put all the pretty +1 and -1 notices above the score
int timeStamp;
int score;
int bullets;
int ammo = 2; // Do we have ammo? 0 means no, 1 means reloading, 2 means yes.
int reloadTime; // Reload time
int totalTargets; // Total targets were spawned.
int maxTargets = 50; // game ends umtil targets reach to 50.
void setup() {
size(400, 400);
smooth();
//bg = loadImage("blue.jpg");
frameRate(30);
cursor(HAND); // make the cursor to hand
Targets = new ArrayList();
scoreFire = new ArrayList();
timeStamp = second(); //either millis or seconds
score = 0;
bullets = 15;
// minim = new Minim(this);
// gun_shot = minim.loadSnippet("gun_shot.wav");
}
//Draw Targets
class Target {
// define properties
float x, y, w, h; // position X, Y, width, height, color of each target.
color c;
// constructor
Target(float init_x, float init_y, float init_width, float init_height, color init_c) {
x = init_x; // Give the object's properties the values passed as arguments.
y = init_y;
w = init_width;
h = init_height;
c = init_c;
}
void display() {
stroke(0);
fill(c); // Fill the target with random color.
ellipse(x, y, w, h); // Draw the random targets.
noStroke();
fill(255);
ellipse(x, y, w-(w*30/100), h-(h*30/100));
noStroke();
fill(255, 0, 0);
ellipse(x, y, w-(w*60/100), h-(h*60/100));
fill(0);
textSize(15);
text("Shoot the Targets to get the highest score: ", 10, 50 );
}
}
///Make ScoreFire
class scoreFire {
//define properties
float x = 370;
float y = height-20;
float a = 255;
// constructor
scoreFire() {
}
void display() {
fill(255, a);
textSize(12);
text("+1", x, y);
}
}
void draw() {
background(255);
//Display target
for (int i = 0; i < Targets.size(); i++) {
Target t = (Target) Targets.get(i);
t.display(); //target to display
}
// Display scoreFire
for (int i = 0; i < scoreFire.size(); i++) {
scoreFire s = (scoreFire) scoreFire.get(i);
s.display();
if (s.y > height-40) {
s.y--;
} else {
scoreFire.remove(i);
}
}
// Counter(count)
int passedTime = millis() - timeStamp;
if (passedTime >= random(300, 4000) && totalTargets < maxTargets) { // If 0.5 to 2 seconds have passed since the last target was placed..
int diameter = int(random(35, 65));
//add some targets
Targets.add(new Target(random(0, width), random(0, height), diameter, diameter, color(random(0, 255), random(0, 255), random(0, 255))));
totalTargets++; //totalTargets increase when more targets are added
timeStamp = millis(); // Reset the timer.
}
//removing targets until they have 6
if (Targets.size() == 6) { // If there's 6 targets on screen
Targets.remove(0); // Remove the firsr target
}
//Printing Score:
if (totalTargets < maxTargets) {
noStroke();
fill(30, 40, 90, 200);
rect(0, height-20, width, 20); //making a small box at the bottom
fill(255);
textSize(12);
text("SCORE: " + score, 10, height-5);
//Printing in Box:
fill(255);
textSize(12);
text("BULLETS: " + bullets, 100, height-5);
}
//Reloading the bullets
if (ammo == 0 && totalTargets < maxTargets) {
textSize(30);
fill(255, 0, 0);
text("Press[R] to RELOAD!", width*0.1, height*0.5);
}
//when reloading the bullets
int timeSinceReload = millis() - reloadTime;
if (timeSinceReload > 3000 && ammo == 1) {
ammo = 2;
bullets = 10;
}
//game over
if (totalTargets >= maxTargets) {
// Remove remaining targets
for (int i = 0; i < Targets.size(); i++) {
Target s = (Target) Targets.get(i);
Targets.remove(i);
}
// End game text
textSize(32);
fill(255);
text("Game over!", width*0.25, height*0.4);
text("You scored: " + score, width*0.20, height*0.5);
}
} // end of draw()
void mousePressed() {
if (bullets > 0 && ammo != 1 && ammo != 0) {
bullets--;
for (int i = 0; i < Targets.size(); i++) {
Target t = (Target) Targets.get(i);
float distance = dist(mouseX, mouseY, t.x, t.y);
if (distance < t.w*0.5) {
Targets.remove(i);
score++;
scoreFire.add(new scoreFire());
}
}
} else if (bullets <= 0 && ammo != 1) {
ammo = 0;
}
//gun_shot.play(0);
}
void keyPressed() {
if (key == 'r' || key =='R' & ammo != 1) {
ammo = 1;
reloadTime = second();
}
}
Thanks guys!
Answers
Please highlight your code then press CTRL + K to fix it.
Have a function (eg. named reset()) that put the global variables to their default values. Call it in setup(), and when you want to reset your game.
^ thx, but can you give me an example please D: i have a hard time to understand these stuff >< hope you can help me >~< thx
Don't (re)declare the global variables in the reset() function! Keep them global.
I meant just do something like:
like this, also i changed color of game over message and '+1' scoring so you can see them.
Thank You. You really taught me a lot! I am appreciated!