Skip to content

Commit 46b798e

Browse files
committed
There is a lot work to do~~~~~~~~~~~~~~~~~~~~~
1 parent f8dc965 commit 46b798e

6 files changed

Lines changed: 53 additions & 47 deletions

File tree

c/Snake/reconstruction/KeyMonitor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef _KEYMONITOR_H
22
#define _KEYMONITOR_H
3-
#include "globalvar.h"
3+
#include "GlobalVar.h"
44
#include <stdio.h>
55
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
66
#include <conio.h>
@@ -15,6 +15,7 @@
1515
#include <pthread.h>
1616
#include <unistd.h>
1717
#define KeyMonitor_Starter() \
18+
system("stty -icanon"); \
1819
pthread_attr_t attr; \
1920
pthread_t tid; \
2021
pthread_attr_init(&attr); \

c/Snake/reconstruction/Move.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,55 @@
11
#ifndef _MOVE_H
22
#define _MOVE_H
33
#include "GetXYFromArrays.h"
4-
#include "globalvar.h"
5-
#include "gotoxy.h"
4+
#include "GlobalVar.h"
5+
#include "GotoXY.h"
66
#define moveBody() \
77
{ \
88
*p[n] = 0; \
9+
PRINTXY((GETX_CHAR(a[0], p[n], WIDTH) + 2), \
10+
(GETY_CHAR(a[0], p[n], WIDTH) * 2), "_") \
911
for (i = n; i > 0; i--) \
1012
{ \
1113
p[i] = p[i - 1]; \
1214
/* per part goes to the address of the next part of body*/ \
1315
} \
1416
*p[0] = BODY; \
1517
/* The First part of snake body come to snake head*/ \
18+
PRINTXY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
19+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), BODY_STRING) \
1620
}
1721

1822
#define moveRight() \
1923
{ \
2024
moveBody(); \
2125
p[0] = p[0] + 1; /* Move snake head */ \
22-
*p[0] = HEAD; /* change the char of new head(new address)'s shape to \
23-
HEAD */ \
26+
*p[0] = HEAD; \
27+
/* change the char of new head(new address)'s shape to HEAD */ \
28+
PRINTXY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
29+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
2430
}
2531
#define moveLeft() \
2632
{ \
2733
moveBody(); \
2834
p[0] = p[0] - 1; \
2935
*p[0] = HEAD; \
36+
PRINTXY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
37+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
3038
}
3139
#define moveDown() \
3240
{ \
3341
moveBody(); \
3442
p[0] = p[0] + WIDTH; \
3543
*p[0] = HEAD; \
44+
PRINTXY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
45+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
3646
}
3747
#define moveUp() \
3848
{ \
3949
moveBody(); \
4050
p[0] = p[0] - WIDTH; \
4151
*p[0] = HEAD; \
52+
PRINTXY((GETX_CHAR(a[0], p[0], WIDTH) + 2), \
53+
(GETY_CHAR(a[0], p[0], WIDTH) * 2), HEAD_STRING) \
4254
}
4355
#endif

c/Snake/reconstruction/globalvar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
// char HEAD = '@'; // The shape of snake head
99
// char BODY = 'O'; // The shape of snake body
1010
#define HEAD '@' // The shape of snake head
11+
#define HEAD_STRING "@"
1112
#define BODY 'O' // The shape of snake body
13+
#define BODY_STRING "O"
1214
char a[HEIGHT][WIDTH] = {{BODY, BODY, BODY, HEAD}}; // The initial char is 0
1315
char *p[HEIGHT * WIDTH] = {&a[0][3], &a[0][2], &a[0][1],
1416
&a[0][0]}; // p[0] stand for snake head

c/Snake/reconstruction/snake.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "snake.h"
1+
#include "Snake.h"
2+
#include "GlobalVar.h"
23
#include "Sleep.h"
3-
#include "globalvar.h"
44
#include <stdio.h>
55
#include <stdlib.h>
66
int main()
@@ -19,13 +19,25 @@ int main()
1919
case 0:
2020
break;
2121
case 1:
22-
PRINTXY(HEIGHT + 7, 0, "Fail!\nDon't hit the wall!\n");
22+
gotoxy(HEIGHT + 7, 0);
23+
printf("Fail!Don't eat your body!\nYour Final Score is:%d\n",
24+
n - 3);
25+
return -1;
2326
break;
2427
case 2:
25-
PRINTXY(HEIGHT + 7, 0, "Fail!\nDon't eat your body!\n");
28+
gotoxy(HEIGHT + 7, 0);
29+
printf("Fail!Don't eat your body!\nYour Final Score is:%d\n",
30+
n - 3);
31+
return -1;
2632
break;
2733
}
28-
canEat();
34+
35+
if (canEat())
36+
{
37+
n++; // length++
38+
p[n] = p[n - 1];
39+
}
40+
2941
switch (direction) // choose which direction to move
3042
{
3143
case 1: // Right
@@ -48,12 +60,6 @@ int main()
4860
moveDown();
4961
break;
5062
}
51-
case -1: // Exit
52-
{
53-
printf("Your Final Score is:%d\n", n - 3);
54-
return -1;
55-
break;
56-
}
5763
}
5864
}
5965
KeyMonitor_Stoper();

c/Snake/reconstruction/snake.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#ifndef _SNAKE_H
22
#define _SNAKE_H
33
#include "GetXYFromArrays.h"
4+
#include "GlobalVar.h"
5+
#include "GotoXY.h"
46
#include "KeyMonitor.h"
5-
#include "globalvar.h"
6-
#include "gotoxy.h"
7-
#include "mapInit.h"
8-
#include "move.h"
9-
#include "random.h"
7+
#include "MapInit.h"
8+
#include "Move.h"
9+
#include "Random.h"
1010
#include "Sleep.h"
1111
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
1212
#include <conio.h>
@@ -24,7 +24,7 @@
2424
{ \
2525
gotoxy((x), (y)); \
2626
printf("%s", (content)); \
27-
gotoxy(0, 1000); \
27+
gotoxy(HEIGHT + 7, 40); \
2828
}
2929

3030
/* Random Food */
@@ -51,8 +51,6 @@ _Bool canEat()
5151
case 1: {
5252
if (*(p[0] + 1) == '*')
5353
{
54-
n++; // length++
55-
p[n] = p[n - 1];
5654
return 1;
5755
}
5856
break;
@@ -61,8 +59,6 @@ _Bool canEat()
6159
case 2: {
6260
if (*(p[0] - WIDTH) == '*')
6361
{
64-
n++; // length++
65-
p[n] = p[n - 1];
6662
return 1;
6763
}
6864
break;
@@ -71,8 +67,6 @@ _Bool canEat()
7167
case 3: {
7268
if (*(p[0] - 1) == '*')
7369
{
74-
n++; // length++
75-
p[n] = p[n - 1];
7670
return 1;
7771
}
7872
break;
@@ -81,8 +75,6 @@ _Bool canEat()
8175
case 4: {
8276
if (*(p[0] + WIDTH) == '*')
8377
{
84-
n++; // length++
85-
p[n] = p[n - 1];
8678
return 1;
8779
}
8880
break;
@@ -97,7 +89,7 @@ int isFail()
9789
if (p[0] < &a[0][0] ||
9890
p[0] > &a[HEIGHT - 1][WIDTH - 1]) // snake is not in the matrix
9991
{
100-
gotoxy(27, 0);
92+
// gotoxy(27, 0);
10193
// printf("Fail!\nDon't hit the wall!\n");
10294
direction = -1;
10395
return 1;
@@ -113,7 +105,7 @@ int isFail()
113105
{
114106
if ((p[0] + 1) == p[i]) // Right of the head is body
115107
{
116-
gotoxy(27, 0);
108+
// gotoxy(27, 0);
117109
// printf("Fail!\nDon't eat your body!\n");
118110
direction = -1;
119111
return 2;
@@ -129,7 +121,7 @@ int isFail()
129121
{
130122
if ((p[0] - WIDTH) == p[i]) // Up of the head is body
131123
{
132-
gotoxy(27, 0);
124+
// gotoxy(27, 0);
133125
// printf("Fail!\nDon't eat your body!\n");
134126
direction = -1;
135127
return 2;
@@ -145,7 +137,7 @@ int isFail()
145137
{
146138
if ((p[0] - 1) == p[i]) // Left of the head is body
147139
{
148-
gotoxy(27, 0);
140+
// gotoxy(27, 0);
149141
// printf("Fail!\nDon't eat your body!\n");
150142
direction = -1;
151143
return 2;

c/Snake/test.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
#include <conio.h>
2-
#include <stdio.h>
3-
#include <windows.h>
4-
5-
61
#include <stdio.h>
72
#include <stdlib.h>
83
#include <time.h>
9-
#define gotoxy(y, x) \
10-
{ \
11-
COORD coord = {(x), (y)}; /* coord */ \
12-
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), \
13-
coord); /* Move Cursor to coord */ \
14-
}
4+
#define gotoxy(y, x) printf("%c[%d;%df", 0x1B, ((y) + 1), ((x) + 1))
155
int main()
166
{
17-
/* 我的第一个 C 程序 */
7+
system("stty -icanon");
188
system("clear");
19-
printf("Hello, World! \n0123456789\n1234567890\n2345678901\n3456789012\n4567890123\n5678901234");
9+
printf("Hello, World! "
10+
"\n0123456789\n1234567890\n2345678901\n3456789012\n4567890123\n56789"
11+
"01234");
12+
getchar();
2013
gotoxy(3, 2);
2114
printf("asdfdsasdf");
2215
gotoxy(8, 9);

0 commit comments

Comments
 (0)