Urgent, please! I want to create a startscreen for my pong game. What am I doing wrong ?

edited January 2017 in Questions about Code

PImage startscreen; int startscreen; int stage; int base=20; int x,y; int hit = 0; int miss = 0; int changeX=-5; int changeY=-5; int gameOver=0;

void setup(){ stage = 1; startscreen = LoadImage("2teniscourt.jpg"); img(startscreen,0,0,600,600); size(600, 600); x=(int)random(width); y=height-base; } void draw() { if(stage==1){ image(startscreen,600,600); textAlign(CENTER); textSize(45); fill(66, 104, 244); text("PONG GAME",100,170); text("Press any key to start",100,170); if (keyPressed == true){ stage = 2; } }
if (stage == 2){ background(100,200,100); } if(gameOver==0) { background(0); fill(244, 66, 66); text("hit: " + hit,50,40); fill(66, 104, 244); rect(mouseX,height-base,150,base); ellipse(x,y,40,40); x=x+changeX; y=y+changeY; if(x<0 | x>width) { changeX=-changeX; } if(y<0) { changeY=-changeY; } if(y>height-base) { //check whether it is falling inside the rectangle or not if(x>mouseX && x<mouseX+200) { changeY=-changeY; //bounce back hit +=1; } else { gameOverSplash(); } } } else { background(244, 78, 66); fill(66, 104, 244); text("Game Over!",width/2,height/2); text("CLICK MOUSE TO RESTART",width/2,height/2+20); } } void gameOverSplash()
{ gameOver=1; } void mouseClicked() { changeY=-changeY; hit +=1; gameOver=0; }

Tagged:

Answers

  • Answer ✓

    You aren't formatting your code properly for the forums. Edit your post, select your code, and press Ctrl + o.

  • Answer ✓

    You have two variables called startscreen. One is an image. The other is a int. You probably only want the image one.

  • edited January 2017 Answer ✓

    Hello,

    I formatted the code a little and made some corrections :

    • e.g. it's loadImage with a small start letter

    • and image, not img

    • I inserted 20 in a few points because that is the radius of the ball (it looks better that way since it is not leaving the screen or overlap / reaching over the border/ paddle).

    • Text align:

    text("hit: " + hit, 7, 40); is now with textAlign(LEFT); which looks better

    • Also you had one closing } here:

      if (stage == 2) { 
      
              background(100, 200, 100);
      
      } // I am wrong!!!
      

    which isolated the whole part after it inside draw() from this.

    Instead we want the } at the end of draw().

    • Remember you can use ctrl-t in processing (not in the forum) to get auto indent. Good.

    Best, Chrisir ;-)

    PImage startscreen; 
    // int startscreen; 
    
    int stage; // stage of the program  
    
    int base=20; 
    int x, y; 
    int hit = 0; 
    int miss = 0; 
    
    int changeX=-5; 
    int changeY=-5;
    
    int gameOver=0;
    
    void setup() { 
      stage = 1; 
      startscreen = loadImage("2teniscourt.jpg"); 
      image(startscreen, 0, 0, 600, 600); 
      size(600, 600); 
      x=(int)random(23, width-23); 
      y=height-base-23;
    } 
    
    void draw() { 
      if (stage==1) { 
        //background (0); 
        image(startscreen, 600, 600);    
        textAlign(CENTER); 
        textSize(45); 
        fill(66, 104, 244); 
        text("PONG GAME", width/2, 170); 
        text("Press any key to start", width/2, 170+77); 
        if (keyPressed == true) { 
          stage = 2;
        }
      } else if (stage == 2) { 
    
        background(100, 200, 100);
    
        if (gameOver==0) { 
          background(0); 
          textAlign(LEFT); 
          fill(244, 66, 66); 
          text("hit: " + hit, 7, 40); 
          fill(66, 104, 244); 
          rect(mouseX, height-base, 150, base); 
          ellipse(x, y, 40, 40); 
          //move
          x=x+changeX; 
          y=y+changeY; 
          // we use 20 here which is the radius of the ball  
          if (x<20 | x>width-20) { 
            changeX=-changeX;
          } 
          if (y<20) { 
            changeY=-changeY;
          } 
          if (y>height-base-20) { 
            //check whether it is falling inside the rectangle or not
            if (x>mouseX && x<mouseX+200) { 
              changeY = -changeY; 
              y-=6; 
              //bounce back 
              hit += 1;
            } else { 
              // game over
              gameOverSplash();
            }
          }
        } else { 
          background(244, 78, 66); 
          fill(66, 104, 244); 
          textAlign(CENTER); 
          text("Game Over!", width/2, height/2); 
          text("CLICK MOUSE TO RESTART", width/2, height/2+60);
        }//else
      }//stage 2
    }// func 
    
    void gameOverSplash()
    { 
      gameOver=1;
    } 
    
    void mouseClicked() {
      if (gameOver==1) {
        changeY=-changeY; 
        hit +=1; 
        gameOver=0;
      }
    }
    
  • Answer ✓

    you have stage 1 and 2

    now you can name your stages by saying

    int stageStartscreen = 1; 
    int stageGame = 2; 
    

    and use those like

    if (stage==stageStartscreen) { 
    

    or

    stage = stageGame;
    

    Also, since gameover is also stage you can alter the structure and think of gameover as a 3rd stage:

    int stageGameOver = 3; 
    

    now you don't need a confusing if (gameOver==0) { inside the if (stage == 2) { anymore...

    now you can say in draw():

    if (stage==stageStartscreen) { 
    //....
    }
    else if (stage==stageGame) { 
    //....
    }
    else if(stage==stageGameOver) { 
    //....
    }
    

    or even use switch (you'll need constants for this, see below):

    switch(stage)  {
    
        case stageStartscreen:
        //...
        break; 
    
        case stageGame:
        //...
        break; 
    
        case stageGameOver:
        // ....
        break;
    
    } // switch 
    

    BTW

    since those names never change (only stage does, not its names) we can mark them as constants (with final):

    final int stageStartscreen = 1; 
    final int stageGame = 2; 
    final int stageGameOver = 3; 
    
  • you're unbelieveble, thank you so much:)

This discussion has been closed.