Pushing a key for a game (player walking)

Hello everyone I am trying to make a code that will act like an auto clicker but works for keys on the keyboard. E.X. Im in a game and for some reason, I want to keep walking around every now and then how can I just push say the letter ā€œZā€ and the payer will move in intervals?

Register key Z


void keyPressed() {
   if (key=='Z') {
       startWalking=true; 
   }
}

Declare this marker:

before setup boolean startWalking=false;

and in draw()

if(startWalking){
   if(millis() - timer > randomTime)  {
      walk(); 
      timer=millis(); 
      randomTime=random(700, 2200); // next walking occurs between 0.7 sec and 2.2 sec
      i++; 
      // stop 
      if(i>10) {
         i=0; 
         startWalking=false;
      }
   }
}

Remark 1

Declare timer and randomTime as int before setup()

Remark 2

You can have a 2nd timer to pause between intervals so he walks, stops, walks, several times.

Chrisir

1 Like