Title page issue

edited July 2015 in Questions about Code

I've been trying to make a decent title page for the past couple of days and i've tried many ways to do it and I can't seem to get it to work with my code, or it will work and stop other functions. i'm using global variable int for stage..and I'm using 2 if conditions the first stage for the title page and i copied and pasted the rest in the 2nd! Now the game itself isn't working. Please help me out, i can't seem to understand why!

float gx = 300;
float gy = 100;
float gw = 50;
float gh = 5;
float gSpeed = 1;
int score = 0;
int miss = 3;
int stage;
int maxscore = 3;
PFont font;

/** Variables for Soccer Ball | b in the variable stands for ball
 ================================================================ */
float bx, by, bySpeed, bSpeed;

/** Increment the speed of the ball with each kick
 ================================================= */
float i = -6;
PImage soccerField, soccerBall, startGame;
// End Global Variables

/** Begin Setup
 ============== */
void setup() {
  size(600, 600);
  startGame = loadImage("startgame.png");
  image(startGame, width/2, height/2);
  //background(startGame);
  theBall = new Ball(bx, by);
  theGoalKeeper = new GoalKeeper(gx, gy);
  soccerField = loadImage("field.png");
  //image(soccerField, width/2, height/2);
  minim = new Minim(this);
  kick = minim.loadFile("kick.wav");
  block = minim.loadFile("block.wav");
  crowd = minim.loadFile("crowd.mp3");
  goal = minim.loadFile("goal.mp3");
  font = loadFont("Champagne&Limousines-Bold-48.vlw");
}
/** End Setup
 ============ */

/** Draw
 ======= */
void draw() {
  // Title Screen
  background(startGame);
  textAlign(CENTER);
  textFont(font);
  text("PENALTY BOX", 300, 300);
  textSize(30);
  text("Press 's' to start game.", 300, 400);
  textSize(15);
  text("Click mouse to kick ball.", 120, 500);
  text("Use the spacebar to reset game.", 130, 530);

  if (key == 's') {
    background(soccerField);
    theBall.run();
    theGoalKeeper.run();
    fill(255);
    textAlign(CENTER);
    textSize(10);
    textSize(40);
    text("SCORE: " +score, 105, 520);
    text("LIVES: " + miss, 90, 580);
  }

  // Set the speed of Soccer Ball
  by = by + bySpeed;

  // Start the Goalie
  if (gx > 221) {
    gx += gSpeed;
  }

  // Stop the Goalie on right side
  if (gx == 379) {
    gSpeed = 0;
    gx = 378;
  }

  // Reverse the Goalie
  if (gx > 221 && gSpeed == 0) {
    gx -= gSpeed--;
  }

  // Stop the Goalie on left side
  if (gx == 220 && gSpeed == -1) {
    gSpeed = 0;
    gx = 221;
  }

  // Start the Goalie again
  if (gx == 221) {
    gSpeed = 1;
    gx += gSpeed;
  }

  // Reset the ball after kicked in net
  if (by <= 53) {
    bySpeed = 0;
    by = 170 + bSpeed++ * 29;
  }

  // Blocking System
  if (gx >= 264 && gx <= 334 && by == 115 || gx >= 264 && gx <= 334 && by == 113 || gx == 300 && by == 115 || gx == 300 && by == 113) {
    block.play(0); // Blocking Sound
    bySpeed = 1;
  }

  if (bySpeed == 1 && by == 160) {
    bySpeed = 0;
    miss--;
    by = 170 + bSpeed++ * 29;
  }

  // Scoring system
  if (gx >= 335 && by == 90 || gx >= 335 && by == 89) {
    score++;
  }

  if (gx <= 263 && by == 90 || gx <= 263 && by == 89) {
    score++;
  }

  // Maximum amount of Goals 
  if (score==3) { 
    fill(35, 193, 34);
    rect(0, 0, width, height);
    fill(0);
    textAlign(CENTER);
    textSize(80);
    text("WINNER !! ", width/2, height/2, width, height);
    noLoop();
    goal.play(0);
  }

  // Game Over
  if (miss == 0) {
    fill(35, 193, 34);
    rect(width/2, height/2, width, height);
    fill(255, 255, 255);
    text("End Game", 300, 200);
    text ("Reset: Spacebar", 300, 250);
    text ("Score: " + score, 300, 300);
    noLoop();
  }
}
/** End Draw
 =========== */

/** MouseClicked Function
 ======================== */
void mouseClicked() {
  kick.play(0);
  bySpeed = i;
}

/** KeyPressed Function
======================= */
void keyPressed() {
  if (key == ' ') {
    loop();
    by = 170;
    bySpeed = 0;
    bSpeed = 0;
    i = -3;
    score = 0;
    miss = 3;
  }

  if (key == 's') {
    crowd.play(0);
  }
}
Tagged:

Answers

  • Answer ✓

    in draw() say

    if (stage==0) {

    // draw start screen

    if(keyPressed) stage = 1;

    } else if (stage==1) {

    // what you have in draw now

    }

  • THANK YOU!!!

  • Stage is often referred to as state

    You can also name 0 and 1 to

    final int stateStartScreen = 0;

    final int stateGame = 1;

Sign In or Register to comment.