#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How to repeat draw() void multiple times after pressing a button?

edited December 2017

I got a code for a game that has PC turn and Player turn. The question is, how can I return to PC turn, after the Player presses "Enter"?

``````void setup()
{
size (1250, 500);
Background();
noStroke();
rectMode (CENTER);
ellipseMode (CENTER);
frameRate (60);
}

void draw()
{
if (F == false)                              //F is a Flag for player/PC turn (false - PC, true - user)
{
for (++i; i<p1; i += 1)
{
k = round (random (1, 6));
Dice (x, y, k);                          //Making a random Dice from 1 to 6
if (k == 1)                              //If dice getting 1 on it then stop and go to the player turn
{
x = 100;
break;
}
x += 115;
}
F = true;                                    //Giving a turn to player
x = 100;
}
}

void keyPressed()
{
if (F == true)                               //If it's player turn
{
if (key == ' ')                             //And if spacebar is pressed
{
k = round (random (1, 6));
y = 350;
Dice (x, y, k);                           //Make a random Dice from 1 to 6
x += 115;
}
if (keyCode == ENTER)              //If Enter is pressed the turn must go to the PC and code must started again, but nothing happens
{
F = false;
Background();
x = 100;
y = 100;
draw();
}
if (keyCode == ESC)                   //Closing the game, if Esc is pressed
{
exit();
}
}
}
``````

Keep in mind that some void's ("void Dice (x, y, k);" and "void Background ();") are hidden. If you need, I can show the full code. Thank you in advance

Tagged:

• Imagine it's the player's turn. What is stopping the game from suddenly being the computer's turn? Well, we are waiting for the player to press a button, right? Okay, so the user presses a button, keyPressed() happens, we see that it is the players turn and one of the valid buttons is pressed, so the player has made their move and so we update some of the game's variables based on their move and then... it is the computer's turn.

What stops it from being the player's turn again? Well, the computer has to go. Do we need to wait for the computer to press a key? No. Do we have to wait for... anything? Probably not. So the computer can just go RIGHT AWAY. In fact, they can go so right away that it actually makes sense to have the computer's turn happen immediately after EVERY player turn.

Your sketch will thus look something like this:

``````// Global variables go here

void setup(){
size(...);
}

void draw(){
// Draw the game board here based on the values stored in the global variables.
}

void keyPressed(){
if( player key is valid ){
update_game_variables_based_on_player_input();
// Thus ends the player's turn. Now the computer goes.
update_game_variables_based_on_computers_move();
}
}
``````

Notice that this means that it is basically ALWAYS the player's turn. Whenever the player has made their move, the computer replies with a move of its own, which then means it is the player's turn again.

• You mean moving the PC turn code into the `keyPressed()` void when the key is Enter? Or just reset the variables after pressing the Enter?

The PC must go first here, I just want to know, how could I return to the PC code

• edited December 2017 Answer ✓

Hello,

this is strange:

``````  for (++i; i<p1; i += 1)
``````

most people would say `for (i; i<p1; i += 1)` or even `for (int i=0; i<p1; i += 1)` when i is meant to be known only within the for-loop

In fact, you don't reset `i` to 0 so the for-loop is not run at all after the first.

This is better: `for (int i=0; i<p1; i += 1)` I guess.

Now, I think your PC code is ran but only briefly because it's so fast. And draw() doesn't wait.

here is a version that shows a small logfile while you play; you can see that PC plays.

idea: While PC shows its (his) dice result you could display a message, "Hit any key to roll your dice" or so.

And please don't call draw() as you did in line 48 above; draw() is called automatically.

Chrisir

``````boolean F=false ;   //F is a Flag for player/PC turn (false - PC, true - user)
//int i;
int p1=6;
int k;
int x, y;

String logFile="";

void setup()
{
size (1250, 500);
//  Background();
noStroke();
rectMode (CENTER);
ellipseMode (CENTER);
frameRate (60);

logFile+="WELCOME \n\nPC turn\n";
}

void draw()
{
background(0);
if (F == false)                              //F is a Flag for player/PC turn (false - PC, true - user)
{
//PC turn

text(logFile, 22, 22);

for (int i=0; i<p1; i += 1)
{
k = round (random (1, 6));
//  Dice (x, y, k);                          //Making a random Dice from 1 to 6
k=int(random(6));
logFile+="PC: "+k+".\n";
if (k == 1)                              //If dice getting 1 on it then stop and go to the player turn
{
x = 100;
break;
}
x += 115;
}
F = true;  // Giving a turn to player
logFile+="Player turn"+".\n";
x = 100;
} else
{
text(logFile, 22, 22);
}
}

void keyPressed()
{
if (F == true)                               //If it's player turn
{
if (key == ' ')                             //And if spacebar is pressed
{
k = round (random (1, 6));
logFile+="Player: "+k+".\n";
y = 350;
//Dice (x, y, k);                           //Make a random Dice from 1 to 6
x += 115;
}
if (keyCode == ENTER)              // If Enter is pressed the turn must go to the PC and code must started again, but nothing happens
{
F = false;
logFile+="PC turn\n";
// Background();
x = 100;
y = 100;
// draw();
}
if (keyCode == ESC)                   //Closing the game, if Esc is pressed
{
exit();
}
}
}
``````