Game Loops involving kbhit() and getch()

hey folks,

I am trying to get a key input to repeat itself in my game loop and I cant seem to figure it out. Simple controls, w,s,a,and d for movement. In my loop as you will see below, I have a movement function, a collision check, and a section where the game board is redrawn. In my movement function I have a if statement revolving around kbhit(), then a switch statement to check the char that's entered. This is done via getch(). The goal is to not have to hold down a key to get movement. Any ideas?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
void game(int *px, int *py, int *cx, int *cy, bool gameOver,char (&gameboard)[25][80])
{
  for (int i = 0; i<25; i++)
    {
        for (int j = 0; j<80; j++)
        {
          gameboard[i][j]=' ';
        }
    } 
  cout<<"To begin moving, press the W key to move up, D to move right, A to move left, or S to move down\n";
  system("PAUSE");
  while(gameOver==false)
 
   {
    
    for (int i = 0; i < 25; i++)
    {
        for (int j = 0; j < 80; j++)
        {
          cout<<gameboard[i][j];
          gameboard[*px][*py]='X';
          gameboard[*cx][*cy]='Y';
        }
    } 
    move1(px,py,cx,cy, &gameOver); 
    system("CLS");
    collision(px,py,cx,cy,&gameOver, gameboard);
   }
  } 
     

void move1(int *px, int *py, int *cx, int *cy, bool *gameOver)
{
 
 if (kbhit())
 {
  
  switch (getch())
   {
    case 'w':
    *px=*px-1;
    *py=*py;
    *cx=*cx-1;
    if (*px==*cx && *py==*cy)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     }
    cout<<endl; 
    break;
    case 'W':
    *px=*px-1;
    *py=*py;
    *cx=*cx-1;
    *cy=*cy;
    if (*px==*cx && *py==*cy)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     }
    cout<<endl;  
    break;
    case 'd':
    *py=*py+1;
    *px=*px;
    *cx=*cx;
    *cy=*cy+1;
    if (*px==*cx && *py==*cy)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     }
    cout<<endl;  
    break;
    case 'D':
    *py=*py+1;
    *px=*px;
    *cx=*cx;
    *cy=*cy+1;
    if (*px==*cx && *py==*cy)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     }
    cout<<endl;  
    break;
    case 's':
    *px=*px+1;
    *py=*py;
    *cx=*cx+1;
    *cy=*cy;
    if (*px==*cx && *py==*cy)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     }
    cout<<endl;  
    break;
    case 'S':
    *px=*px+1;
    *py=*py;
    *cx=*cx+1;
    *cy=*cy;
    if (*px==*cx && *py==*cy)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     }
    cout<<endl;  
    break;
    case 'a':
    *py=*py-1;
    *px=*px;
    *cy=*cy+1;
    *cx=*cx;
    if (*px==*cx && *py==*cy)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     }
    cout<<endl;  
    break;
    case 'A':
    *py=*py-1;
    *px=*px;
    *py=*py-1;
    *px=*px;
    *cy=*cy+1;
    *cx=*cx;
    if (*px==*cx && *py==*cy)
     {
      *gameOver=true;
      cout<<"COLLISION YOU BE DEAD YO!\n";
      cout<<"GAME OVER!\n";
     }
    cout<<endl;
    break;
    default:
    cout<<"You entered an invalid menu selection."<<endl;
    break;
   }
  }
}
Topic archived. No new replies allowed.