We are about to switch to a new forum software. Until then we have removed the registration on this forum.
final int stateSampleGame = 0;
final int stateBoss = 1;
final int stateVictory = 2;
int state = stateSampleGame;
void setup () {
size (960, 640);
background(255);
}
void draw () {
background(255);
switch (state) {
case stateSampleGame:
gameScreen();
break;
case stateBoss:
bossScreen();
break;
case stateVictory:
victoryScreen();
break;
default: // error
println("Error number 939; unknown state : "
+ state
+ ".");
exit();
break;
} // switch
}
void keyPressed () {
switch (state) {
case stateSampleGame:
keyPressedForGameScreen();
break;
case stateBoss:
keyPressedForBossScreen();
break;
case stateVictory:
keyPressedForVictoryScreen();
break;
default: // error
println("Error number 939; unknown state : "
+ state
+ ".");
exit();
break;
} // switch
}
void mousePressed () {
switch (state) {
case stateSampleGame:
mousePressedForGameScreen();
break;
case stateBoss:
mousePressedForBossScreen();
break;
case stateVictory:
mousePressedForVictoryScreen();
break;
default: // error
println("Error number 939; unknown state : "
+ state
+ ".");
exit();
break;
} // switch
}
int score;
void gameScreen() {
fill(0);
text("Click the screen and score 100 to win", 200, 280);
text(score, 200, 300);
if (score >= 100) {
state = stateBoss;
}
}
void keyPressedForGameScreen() {
}
void mousePressedForGameScreen() {
if (mousePressed) {
score = score + 5;
}
}
int bossHp = 150;
int startTime;
int duration = 5000;
float xspeed = 5;
float yspeed= 3;
float x = 480;
float y = -100;
void bossScreen() {
text(bossHp, 600, 100);
if (millis() > duration) {
boss();
}
if (millis() < startTime+5*1000) //"timer"
///banner
if (blink(20)) {
fill(80, 80, 80, 100);
rect(-1, height/3, width, height/4);
fill(0);
text("BOSS BATTLE", width/2, height/2);
}
///banner
}
boolean blink(int span) {
return frameCount%(span*2) < span;
}
void keyPressedForBossScreen() {
}
void mousePressedForBossScreen() {
if (mousePressed) {
bossHp = bossHp - 10;
if (bossHp <=0) {
state = stateVictory;
}
}
}
void boss() {
ellipse(x, y, 100, 300);
y=y+yspeed;
if (y >= height/2-10) {
yspeed =0;
x = x + xspeed;
if ((x > width - 105) || (x < 105)) {
xspeed = xspeed * -1;
}
}
}
void victoryScreen() {
text("You Win", width/2, height/2);
}
void keyPressedForVictoryScreen() {
}
void mousePressedForVictoryScreen() {
}
Answers
The way I understand it, once you run your program, the timer automatically starts, however in this code, there should be some kind of
though that doesn't work. Can someone help me figure it out?
you just need to stet
startTime
in the moment the Boss mode starts (that's where it saysstate = stateBoss;
):I also corrected the if / else clause with the timer and made a new boolean bossGameStarted so that mouse clicks are ignored during the banner time. (This variable could be replaced with a new state bannerBeforeBoss or so.)
here is the full sketch
@Chrisir Hi Chrisir, thanks for giving an answer to the code but it caused another problem, I actually have a
in both the KeyPressedForGme and KeyPressedForBoss screen. I thought the timer problem would not affect it. The problem right now is when you pause in the boss screen, the game will be paused. However after resuming the game, the banner "animation" will play first. Thanks again.
Line 114 use bossGameStarted
here in an appropriate way for example
like here:
lots of other changes like variable
previousState