Skip to content

Commit 37f4ec4

Browse files
committed
Under reconstruction......
1 parent 12fdd14 commit 37f4ec4

7 files changed

Lines changed: 204 additions & 3 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#ifndef _KEYMONITOR_H
2+
#define _KEYMONITOR_H
3+
#include "globalvar.h"
4+
#include <stdio.h>
5+
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
6+
#include <conio.h>
7+
#include <windows.h>
8+
#include <handleapi.h>
9+
#include <processthreadsapi.h>
10+
11+
#define KeyMonitor_Starter() \
12+
HANDLE hThread1 = CreateThread(NULL, 0, KeyMonitor, NULL, 0, NULL)
13+
14+
#elif defined(__linux__) || defined(__gnu_linux__)
15+
#include <pthread.h>
16+
#include <unistd.h>
17+
#define KeyMonitor_Starter() \
18+
{ \
19+
pthread_attr_t attr; \
20+
pthread_t tid; \
21+
pthread_attr_init(&attr); \
22+
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); \
23+
pthread_create(&tid, &attr, KeyMonitor, NULL); \
24+
}
25+
#elif defined(__APPLE__)
26+
#endif
27+
28+
// KeyMonitor Function
29+
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
30+
DWORD WINAPI
31+
#elif defined(__linux__) || defined(__gnu_linux__)
32+
void *
33+
#elif defined(__APPLE__)
34+
#endif
35+
KeyMonitor(void *arg) // Direction Control:w,s,a,d-->Up Down Left Right
36+
{
37+
char k;
38+
while (1)
39+
{
40+
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
41+
k = _getch();
42+
#elif defined(__linux__) || defined(__gnu_linux__)
43+
k = getchar();
44+
#elif defined(__APPLE__)
45+
#endif
46+
switch (k)
47+
{
48+
case 'w': // Up
49+
{
50+
if (direction != 4)
51+
direction = 2;
52+
break;
53+
}
54+
case 's': // Down
55+
{
56+
if (direction != 2)
57+
direction = 4;
58+
break;
59+
}
60+
case 'a': // Left
61+
{
62+
if (direction != 1)
63+
direction = 3;
64+
break;
65+
}
66+
case 'd': // Right
67+
{
68+
if (direction != 3)
69+
direction = 1;
70+
break;
71+
}
72+
case 'j': // SpeedUp
73+
{
74+
delay = delay * 4 / 5;
75+
break;
76+
}
77+
case 'k': // SpeedDown
78+
{
79+
delay = delay * 5 / 4;
80+
break;
81+
}
82+
case 27: // ESC
83+
{
84+
printf("Exit!\n");
85+
isPause = 0;
86+
direction = -1;
87+
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
88+
return 0;
89+
#elif defined(__linux__) || defined(__gnu_linux__)
90+
return NULL;
91+
#elif defined(__APPLE__)
92+
#endif
93+
break;
94+
}
95+
case ' ': // Space
96+
{
97+
if (isPause)
98+
{
99+
printf("Continue!\n");
100+
}
101+
else
102+
{
103+
printf("Pause!\n");
104+
}
105+
isPause = !isPause;
106+
break;
107+
}
108+
}
109+
}
110+
}
111+
112+
#endif

c/Snake/reconstruction/globalvar.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef _GLOBALVAR_H
2+
#define _GLOBALVAR_H
3+
4+
#include <stdbool.h>
5+
#define WIDTH 40
6+
#define HEIGHT 20
7+
8+
// char HEAD = '@'; // The shape of snake head
9+
// char BODY = 'O'; // The shape of snake body
10+
#define HEAD '@' // The shape of snake head
11+
#define BODY 'O' // The shape of snake body
12+
char a[HEIGHT][WIDTH] = {{BODY, BODY, BODY, HEAD}}; // The initial char is 0
13+
char *p[HEIGHT * WIDTH] = {&a[0][3], &a[0][2], &a[0][1],
14+
&a[0][0]}; // p[0] stand for snake head
15+
16+
int n = 3; // The length of snake body (without head)
17+
int i, j;
18+
int direction = 1; // 1.right;2.up;3.left;4.down;-1.exit
19+
int delay = 200 * 1000; // delay 0.2s(200ms)
20+
bool isPause = 0;
21+
#endif

c/Snake/reconstruction/gotoxy.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef _GOTOXY_H
2+
#define _GOTOXY_H
3+
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
4+
#define gotoxy(y, x) \
5+
{ \
6+
COORD coord = {(x), (y)}; /* coord */ \
7+
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), \
8+
coord); /* Move Cursor to coord */ \
9+
}
10+
#elif defined(__linux__) || defined(__gnu_linux__)
11+
#define gotoxy(y, x) printf("%c[%d;%df", 0x1B, ((y) + 1), ((x) + 1))
12+
#elif defined(__APPLE__)
13+
#endif
14+
#endif

c/Snake/reconstruction/mapInit.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef _MAPINIT_H
2+
#define _MAPINIT_H
3+
#include <stdio.h>
4+
#define mapInit() \
5+
{ \
6+
system("clear"); \
7+
printf("Your Score is:\n"); /* (0,13) */ \
8+
for (i = 0; i < (WIDTH)*2; i++) \
9+
printf("_"); \
10+
printf("\n"); \
11+
for (i = 0; i < (HEIGHT); i++) \
12+
{ \
13+
for (j = 0; j < (WIDTH); j++) \
14+
{ \
15+
if (a[i][j] == 0) \
16+
printf("_|"); \
17+
else \
18+
printf("%c|", a[i][j]); \
19+
} \
20+
printf("\n"); \
21+
} \
22+
printf("\nw,s,a,d->Up Down Left Right;\nj,k->Speed " \
23+
"Up/Down;\nESC: Exit\n"); \
24+
}
25+
26+
#endif

c/Snake/reconstruction/snake.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "snake.h"
2+
#include "globalvar.h"
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
int main()
6+
{
7+
KeyMonitor_Starter();
8+
mapInit();
9+
}

c/Snake/reconstruction/snake.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef _SNAKE_H
2+
#define _SNAKE_H
3+
#include "KeyMonitor.h"
4+
#include "globalvar.h"
5+
#include "gotoxy.h"
6+
#include "mapInit.h"
7+
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
8+
#include <conio.h>
9+
#include <windows.h>
10+
#include <handleapi.h>
11+
#include <processthreadsapi.h>
12+
#elif defined(__linux__) || defined(__gnu_linux__)
13+
#include <pthread.h>
14+
#elif defined(__APPLE__)
15+
#endif
16+
#endif

c/Snake/snake_windows_opt.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ _Bool isPause = 0;
3333
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), \
3434
coord); /* Move Cursor to coord */ \
3535
}
36-
36+
3737
#define moveBody() \
3838
{ \
3939
gotoxy(((p[0] - a[0]) / WIDTH) + 2, 2 * ((p[0] - a[0]) % WIDTH)); \
@@ -51,8 +51,10 @@ _Bool isPause = 0;
5151
void moveRight()
5252
{
5353
moveBody();
54-
p[0] = p[0] + 1; // Move snake head
55-
*p[0] = HEAD; // change the char of new head(new address)'s shape to HEAD
54+
p[0] = p[0] + 1;
55+
/* Move snake head */
56+
*p[0] = HEAD;
57+
/* change the char of new head(new address)'s shape to HEAD */
5658
gotoxy(((p[0] - a[0]) / WIDTH) + 2, 2 * ((p[0] - a[0]) % WIDTH));
5759
printf("%c", HEAD);
5860
gotoxy(0, 62);
@@ -417,6 +419,7 @@ int main()
417419
}
418420
case -1: // Exit
419421
{
422+
gotoxy(27, 0);
420423
printf("Your Final Score is:%d\n", n - 3);
421424
CloseHandle(hThread1);
422425
return -1;

0 commit comments

Comments
 (0)