Hi,
I have this game into which I would like to incorporate a timer. However, I've been having difficulty implementing it with my menu and pause function. The timer should only run once the menu has been cleared (mouse has been pressed) and should not run when there is a pause. Although TfGuy44 gave me an amazing response, as a person very new to programming, I am having difficulty implementing his idea. So I was wondering if anyone would be able to help me out? I can;t post the entire code, because it is very large, but I will just post the setup and draw functions. It is quite self explanatory the way menu and pause works, and the timer should not run when those are activated.
Here is my code:
void setup ()
{
minim = new Minim(this);
cue_stick_hit = minim.loadSnippet("stick_cue_hit.wav");
ball_wall_collision = minim.loadSnippet("ball_wall_collision.wav");
game_over=minim.loadSnippet("game_over.wav");
size(900, 600);//size of processing display window
frameRate(60); //Sets refresh rate of processing window
cue.rad = 31.0;//diameter
cue.col = #e2e9f0;//colour of cue ball
ball = new Ball [number_of_balls];
ball();
coordinate1 = new PVector (ball[0].center.x, ball[0].center.y);
}
void draw ()
{
if(menu_condition)
{
table ();
ball();
menu();
cue();
clock();
if(mousePressed)
{
menu_condition = false;
}
}
else if(pause)
{
table();
cue();
ball();
pause();
clock();
textSize(40);
fill(255);
text("PAUSED",width/2.5,height/2);
}
else
{
table();
cue();
stick();
position.add(velocity_cue);
coordinate1.add(ball1_velocity);
checkBoundaryCollision();
velocity_cue.mult(friction_factor);
ball1_velocity.mult(friction_factor);
velocity_check();
cue_in_pocket = pocket();
ball();
pause();
clock();
collisionBetweenBalls();
if(cue_in_pocket)
{
textSize(45);
fill(255);
text("GAME OVER",width/2.75,height/2);
game_over.play();
game_over.rewind();
noLoop();
}
textSize(14);
fill(text_color_r,text_color_g,text_color_b);
text("POWER:"+power_percentage+"%", 100,100);
}
println(ball1_velocity.x);
println(ball1_velocity.y);
}
Here is TfGuy44's response, which I think suits my application very well:
int time_left = 12000;
color bg;
boolean running;
int last_millis;
void setup() {
size(400, 400);
bg = rcolor();
last_millis = millis();
running = true;
}
void draw() {
background(bg);
fill(255);
text("Click to pause/unpause.", 20, 20);
if (running) {
time_left -= ( millis() - last_millis );
} else {
text("PAUSED!", 20, 100);
}
last_millis = millis();
text(time_left, 20, 60);
if(time_left < 0 ){
bg = rcolor();
time_left = int(random(60000));
}
}
void mousePressed(){
running = !running;
}
color rcolor(){
return( color( random(128),random(128),random(128)) );
}