Skip to content

Instantly share code, notes, and snippets.

@disjukr
Created November 15, 2013 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save disjukr/7483482 to your computer and use it in GitHub Desktop.
Save disjukr/7483482 to your computer and use it in GitHub Desktop.
move the star by pressing w, a, s ,d
#include <stdio.h>
#include <stdlib.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define clearConsole system("cls")
#define get_char _getch()
#include <conio.h>
#include <time.h>
#include <windows.h>
typedef char bool;
#define false 0
#define true 1
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
int gettimeofday(struct timeval *tv, struct timezone *tz) {
FILETIME ft;
unsigned __int64 tmpres = 0;
static int tzflag = 0;
if (NULL != tv) {
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
tmpres /= 10;
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tv->tv_sec = (long) (tmpres / 1000000UL);
tv->tv_usec = (long) (tmpres % 1000000UL);
}
if (NULL != tz) {
if (!tzflag) {
_tzset();
tzflag++;
}
tz->tz_minuteswest = _timezone / 60;
tz->tz_dsttime = _daylight;
}
return 0;
}
#else
#define clearConsole system("clear")
#define get_char getchar()
#include <stdbool.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
int _kbhit() {
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF) {
ungetc(ch, stdin);
return 1;
}
return 0;
}
#endif
int x;
int y;
int fps;
int cnt;
double last_frame;
bool exit_flag;
double double_time() {
struct timeval time;
gettimeofday(&time, NULL);
return time.tv_sec + (time.tv_usec / 1000000.0);
}
void frame() {
int i, j;
last_frame = double_time();
clearConsole;
for (j = 0; j < 10; ++j) {
for (i = 0; i < 10; ++i) {
if (i == x && j == y)
printf("*");
else
printf(".");
printf(" ");
}
printf("\n");
}
}
void input() {
int ch;
if (_kbhit()) {
switch (get_char) {
case 27:
if (!_kbhit()) { //esc
exit_flag = true;
break;
}
else if (get_char == 91) { //arrow key in mac terminal
switch (ch = get_char) {
case 65: //up
--y;
break;
case 66: //down
++y;
break;
case 68: //left
--x;
break;
case 67: //right
++x;
break;
default:
printf("%i\n", ch);
exit_flag = true;
break;
}
}
break;
case 'w':
--y;
break;
case 's':
++y;
break;
case 'a':
--x;
break;
case 'd':
++x;
break;
case 'q':
exit_flag = true;
break;
}
}
}
void loop() {
input();
if ((double_time() - last_frame) > (1.0 / (double) fps))
frame();
}
int main() {
cnt = 0;
x = y = 0;
fps = 24;
while (!exit_flag)
loop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment