Small snake game in C
Commit cd184c572942
tarball1 file changed+3-12
@@ -118,8 +118,7 @@ int main(int argc, char *args[]) {
118118
current_size = init_size;119119
bool too_small = false;120120
121
- struct timeval start;122
- gettimeofday(&start, NULL);121
+ struct timespec mili100 = {0, 1000000 * SPEED};123122
124123
/* Menu */125124
@@ -144,10 +143,7 @@ int main(int argc, char *args[]) {
144143
struct timeval current;145144
gettimeofday(¤t, NULL);146145
147
- uint64_t time_diff = (current.tv_sec - start.tv_sec) * 1000000 +148
- (current.tv_usec - start.tv_usec);149
-150
- if (time_diff >= SPEED * 1000) {146
+ if (true) {151147
152148
ioctl(STDOUT_FILENO, TIOCGWINSZ, ¤t_size);153149
@@ -196,6 +192,7 @@ int main(int argc, char *args[]) {
196192
break;197193
}198194
}195
+ nanosleep(&mili100, NULL);199196
}200197
201198
/* initialisation of the game */@@ -234,8 +231,6 @@ int main(int argc, char *args[]) {
234231
235232
/* start of the actual game loop */236233
237
- struct timespec mili100 = {0, 100000000};238
-239234
while (run) {240235
241236
/* input handling */@@ -266,9 +261,6 @@ int main(int argc, char *args[]) {
266261
struct timeval current;267262
gettimeofday(¤t, NULL);268263
269
- uint64_t time_diff = (current.tv_sec - start.tv_sec) * 1000000 +270
- (current.tv_usec - start.tv_usec);271
-272264
if (/*time_diff >= SPEED * 1000*/ true) {273265
274266
ioctl(STDOUT_FILENO, TIOCGWINSZ, ¤t_size);@@ -283,7 +275,6 @@ int main(int argc, char *args[]) {
283275
284276
if (!paused) {285277
286
- start = current;287278
if (tmp_direction != NO_DIRECTION &&288279
((shift + (DIRECTIONS >> 1)) % DIRECTIONS) != tmp_direction) {289280
shift = tmp_direction;