How to take user input and reuse it later on

I have a project for college in which we must create a 2 player version of a simple pong style game. I want to take the 2 usernames at the start-up screen and reuse them instead of player 1 and 2 below is the code i have for the game so far any help would be greatly appreciated :)

// width of Bat int base=10;

// setting initial values to 0 int x,y,BatX,Bat2X,Player1=0,Player2=0;

// setting speed – rate of change in X and Y int changeX=-6; int changeY=-6;

// setting initial score to 0 int gameOver=0;

// setting game screen size void setup() { size(760, 640); BatX = width/2; Bat2X = width/2-100;

x=(int)random(width); y=height-base; }

// start of game void draw() { if((Player1<5)&&(Player2<5)) { background(0); text("SCORE:"+Player1,width/2,height/2); text("SCORE:"+Player2,width/2,height/2.1);

// bat is moved by change in mouseX value – a system variable if (keyPressed){ if((key=='r')&&(BatX<width-200)){ BatX=BatX+5; } else if((key=='q')&&(BatX>0)){ BatX=BatX-5; } } // bat2x is moved by change in mouseX value – a system variable if (mousePressed && (mouseButton == RIGHT)&&(Bat2X<width-200)) { Bat2X=Bat2X+5; } else if (mousePressed && (mouseButton == LEFT)&&(Bat2X>0)) { Bat2X=Bat2X-5; }

rect(BatX,height-base,200,base); rect(Bat2X,0,200,base);

ellipse(x,y,10,10);

// moving ball by changing X and Y values x=x+changeX; y=y+changeY;

// checking if ball has hit either side, or top, and changing direction if // it has if(x<0 | x>width) { changeX=-changeX; }

// checking if Ball has hit Bat i.e. is Y value of Ball at top of Bat if(y>height-base) { //check whether it is falling inside the rectangle of Bat or not if(x>BatX && x<BatX+200) { changeY=-changeY; //bounce back

}
else
{
  Player2++;
  y=height/2;
  //delay(2000);

}

} // checking if Ball has hit Bat i.e. is Y value of Ball at top of Bat if(y<base) { //check whether it is falling inside the rectangle of Bat or not if(x>Bat2X && x<Bat2X+200) { changeY=-changeY; //bounce back

}
else
{
  Player1++;
  y=height/2;
  //delay(2000);

}

}

} else { background(100,100,200); text("Game Over!",width/2,height/2); text("Press a to restart",width/2,height/2+20); text( "Top Player = " + Player2 , width/2, (height/2)+60); text( "Bottom Player = " + Player1 , width/2, (height/2)+50);

} }

void keyPressed() {if (key=='a'){ changeY=-changeY; Player1=0; Player2=0; gameOver=0; } }

Tagged:

Answers

  • Edit post. Select code. Press Ctrl + o.

  • edited April 2016

    Work with states

    because first you want a screen where player 1 enters her name. So state is 0

    when player 2 enters her name, state is 1

    then you go to game state is 2

    GameOverScreen state is 3

    (And helpScreen 4 e.g.)

    Now

    Now in draw() Start with

    switch(state) {
    
    
        And act accordingly with case 0: etc.; 
    
        break;
    
    }
    

    Nothing is allowed in draw() outside that switch() {}

    for the input just use

    userName1+= key;
    

    And same for user name 2

    In keyPressed and MousePressed also use switch(state){ .... to decide whether to add the key to user name 1 or 2 or use it in game

    Ok?

Sign In or Register to comment.