Keyboard inputs voor games

Hi arduino-ers

I was working on pong in my serial monitor but i wanted an input without external inputs (exept my arduino of course).
Is there a way to just press w on my keyboard and my character goes up.
I have al code but now i am using Serial.readString it doesn't work for some reason but I think it will be a lot cooler to just press w and you go up insted of pressing w and then send.

If there is a way to just press w and my chacater goes up or you know how my Serial.readString works then please let me know.
This is my code by the way:

int mx=32;
int my=24;
int yp=11;
int xb=15;
int yb=11;
bool r=0;
bool u=1;
bool d=0;
int dt=1000;
int milli;
unsigned long oldmilli;
String key;

bool game [][32]={
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};

void setup() {
  Serial.begin(500000);
}


void loop() {
  while(Serial.available()==0){
    key=Serial.readString();
    milli=millis()-oldmilli;
    if(milli>dt){
      oldmilli=millis();
      ball();
      schermr();
    }
  }
  if(key=="z"){
    Serial.println("hello");
    delay(100000);
    yp++;
    if(yp==0){
      yp--;
    }
    game[yp+2][30]=0;
    game[yp-1][30]=1;
  }
  if(key=="s"){
    yp--;
    if(yp==23){
      yp++;
    }
    game[yp-2][30]=0;
    game[yp+1][30]=1;
  }
}

void schermr(){
  Serial.println();
  for(int y=0; y<my; y++){
    Serial.println();
    for(int x=0; x<32; x++){
      if(game[y][x]==1){
        Serial.print("xx");
      }
      if(game[y][x]==0){
        Serial.print("  ");
      }
    }
    Serial.print(y);
  }
}

void ball(){
  game[yb][xb]=0;
  if(r==0){
    xb--;
    if(xb==1){
      xb++;
      r=1;
    }
  }
  if(r==1){
    xb++;
    if(xb==30){
      if(yb==yp){
        u=0;
        d=0;
        xb--;
        xb--;
        r=0;
      }
      if(yb==yp+1){
        u=1;
        d=0;
        xb--;
        xb--;
        r=0;
      }
      if(yb==yp-1){
        u=0;
        d=1;
        xb--;
        xb--;
        r=0;
      }
    }
    if(xb==32){
      lose();
    }
  }
  if(u==1){
    yb--;
    if(yb==-1){
      yb++;
      u=0;
      d=1;
    }
  }
  if(d==1){
    yb++;
    if(yb==24){
      yb--;
      yb--;
      u=1;
      d=0;
    }
  }
  game[yb][xb]=1;
}

void lose(){
  for(int i=0; i<300; i++){
    for(int e=0; e<30; e++){
      Serial.print("YOU LOSE ");
    }
    Serial.println();
  }
  delay(99999999999999999999);
}

I have tried using Serial.prints to look how far it gets but it gets after the while but my String key doesn't become "w" or "s".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.