Cyrus Blog

FLAG{S0_H4PPY_C_U_H3R3} (>.<)

贪吃蛇 - C语言非刷屏版

本文共 584 字,预计阅读时间 3 分钟。

没有gotoxy,怎么种庄稼。不过为什么不试试用API写一个gotoxy呢?
Console刷屏真的会闪瞎眼。这里的代码只更新头尾两个字符。
真是懒得找方向键的键盘码啊,用WASD算了。
据说吃豆豆和+1s更配哦!
(仅限Windows)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include <conio.h>

int nx, ny, ni;
int dx, dy;
int ax[2000], ay[2000], scr[62][31] = { 0 };
char nd;
int t0, tn, dt, t1;
int score = 0;

void gotoxy(int x, int y)
{
HANDLE hOutput;
COORD loc;
loc.X = x;
loc.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, loc);
}
void rangotoxy(int xx, int yy, char c)
{
int x = rand() % 62 + 1;
int y = rand() % 31 + 1;
gotoxy(x, y);
putchar(c);
gotoxy(xx, yy);
}
void ctrl()
{
if (_kbhit())
{

char nd2 = _getch();
if (((((nd=='w')&&(nd2=='s'))
||((nd == 'a') && (nd2 == 'd'))
||((nd == 's') && (nd2 == 'w'))
|| ((nd == 'd') && (nd2 == 'a'))) == 0)
&& ((nd2 == 'w') || (nd2 == 'a')
||(nd2 == 's') || (nd2 == 'd')))
{
nd = nd2;
}
}
}
void exciting()
{
gotoxy(1, 1);
printf("This is the colorful egg.\n");
gotoxy(26, 31);
printf("EXCITED!");
}
int main()
{
system("chcp 437");
system("mode con cols=63 lines=32");
gotoxy(18, 10);
printf("PRESS ANY KEY TO START");
//exciting();
nd = _getch();
gotoxy(18, 10);
printf(" ");
t0 = GetTickCount();
t1 = t0 + 99000;
score = 0;
srand((unsigned int)time(NULL));
int tx = nx, ty = ny;
gotoxy(3, 31);
printf("Your score : %d", score);
gotoxy(tx, ty);
for (int i = 0; i <= 5; i++)
{
gotoxy(i+4, 16);
putchar(35);
scr[i + 4][16];
ax[i] = i + 4;
ay[i] = 16;
}
ni = 5;
nx = 9;
ny = 16;//*/
dx = rand() % 61+1;
dy = rand() % 29+1;
scr[dx][dy] = 2;
gotoxy(dx, dy);
printf("+1s");
int flag = 1;
nd = 'd';
while (flag == 1)
{
tn = GetTickCount();
int tx = nx, ty = ny;
gotoxy(45, 31);
printf("Left time : %d ", (t1 - tn) / 1000);
gotoxy(tx, ty);
ctrl();
switch (nd)
{
case 'w':
ny--; break;
case 'd':
nx++; break;
case 's':
ny++; break;
case 'a':
nx--; break;
}
if ((scr[nx][ny] == 1)||(nx<0)||(ny<0)||
(nx>=63)||(ny>=31)||(tn>=t1))
{
gotoxy(26, 10);
printf("GAME OVER");
nd = _getch();
flag = 0;
}
else
{
gotoxy(nx, ny);
putchar(35);
if (scr[nx][ny] != 2)
{
gotoxy(ax[0], ay[0]);
putchar(32);
scr[ax[0]][ay[0]] = 0;
for (int i = 0; i < ni; i++)
{
ctrl();
ax[i] = ax[i + 1];
ay[i] = ay[i + 1];
}
ax[ni] = nx;
ay[ni] = ny;
}
else
{
t1 += 10000;
score++;
if ("where is"=="colorful egg")
{
exciting();
nd = _getch();
flag = 0;
}
int tx = nx, ty = ny;
gotoxy(3, 31);
printf("Your score : %d", score);
gotoxy(tx, ty);
gotoxy(dx, dy);
printf(" ");
gotoxy(nx, ny);
putchar(35);
ni++;
ax[ni] = nx;
ay[ni] = ny;
dx = rand() % 61 + 1;
dy = rand() % 29 + 1;
scr[dx][dy] = 2;
gotoxy(dx, dy);
printf("+1s");
}
scr[nx][ny] = 1;
gotoxy(dx, dy);
printf("+1s");
gotoxy(dx, dy);
}
Sleep(512 / (score / 16 + 1));
}
}

P.S. 被dalao吐槽有浓浓的pascal味→。→