//------------------------------------------------------------| // This example tries to show the use of threads in C, | // but it only runs on Windows, because of the windows.h | // header file. | // There's also one problem, I wanted to use the arrows, | // But in order to use them, the user would have to let | // pressed the arrow, 'cause the getch() function sometimes | // returns different values, so, if you wanna play whit it a | // little bit, just uncomment the part where says 'Using the | // arrows' and comment the par where says 'Without arrows'. | // | // You will also need to have 'myconio.h' in the same | // directory of this program. | // | // Author: Juan Francisco Benavides Nanni. | // mail: elnanni@gmail.com | // URL: http://mmabj.tk | // date: 29/12/2005 | //------------------------------------------------------------| #include #include "myconio.h" #define MAXWIDX 20 #define MAXWIDY 20 #define MIN 0 #define CENTER 10 // Without arrows. #define UP 115 #define DOWN 120 #define LEFT 122 #define RIG 99 // Using the arrows. /* #define UP 72 #define DOWN 80 #define LEFT 75 #define RIG 77 */ //Please don't modify this value. #define SALE 27 void thread_move(int *param); int main(){ int opc, last, x, y; char car = ':'; HANDLE handle; opc = UP; x = y = CENTER; while(1){ if(opc == SALE) break; clrscr(); printf("Welcome to the 'try of nibbles' game.\n\n"); printf("UP = %c, DOWN = %c, LEFT = %c, RIGHT = %c, EXIT = .\n\n" , UP, DOWN, LEFT, RIG, SALE); gotoxy(x, y); printf("%c", car); switch(opc){ case UP: if(y > MIN) y -= 1; else y = y; car = ':'; break; case DOWN: if(y < MAXWIDY) y += 1; else y = y; car = ':'; break; case LEFT: if(x > MIN) x -= 1; else x = x; car = '-'; break; case RIG: if(x < MAXWIDX) x += 1; else x = x; car = '-'; break; case SALE: break; default: opc = last; } last = opc; handle = (HANDLE) _beginthread(thread_move, 0, &opc); WaitForSingleObject(handle, 1); } } void thread_move(int *param){ (int) * param = (int) getch(); _endthread(); }