Tetris 俄罗斯方块

简介

2014年,大一上学期只教程序设计基础(简单的教了伪代码,没有涉及具体的程序设计语言),便买了本 C Primer Plus 自学 C 语言,课余时间做了两个小东西,一个是贪吃蛇,另一个就是俄罗斯方块。

C 语言写的控制台应用程序,没有用第三方图形库,使用了一些特殊字符(如’■’)充当图形。调用了一些 Windows API,主要用来控制光标位置,在特定坐标打印特殊字符。

部分代码

部分代码展示,代码比较稚嫩。完整代码&注释可前往 https://github.com/HugHoo/GluttonousSnake 下载

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
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#define FORM_SIZE 3
#define ROW 23
#define COLUMN 14
#define RANDOM_TRANSLATION 7
#define ORDINATE 2
#define DISPLACEMENT 3
#define TEMP 1
#define WIN 15
#define DIE_LINE 1
#define UPDATE_TIME 40
#define SPACE " "
#define SQUARE "■"
using namespace std;

COORD far_away = { 0,24 };
int time_down = 0;
int scoure = 0;

enum color {
blue = 121,
green = 114,
lake_blue = 115,
red = 124,
gray = 120,
pink = 117,
yellow = 126
};

struct square {
int state;
COORD pos[FORM_SIZE];
COORD center;
int form;
int color;
};

static struct fixed {
short fix[ROW][COLUMN];
int color[ROW][COLUMN];
};

struct fixed initial_fixed(struct fixed * fixed);
inline void gotoxy(COORD pos);
void print_square(struct square * form, const char ch[]);
void print_fixed(struct fixed * fixed);
void move_square(struct square * form, short xdir, short ydir);
square create_square();
static square form1();
static square form2();
static square form3();
static square form4();
static square form5();
static square form6();
static square form7();
square turn(square * pform);
bool turn_success(struct square * form, struct fixed * fixed);
static inline COORD tempXY();
bool is_buttom(struct square * form, struct fixed * fixed);
bool is_wall(struct square * form, struct fixed * fixed, short dir);
struct fixed fix_square(struct square * form, struct fixed * fixed);
static bool check_full(const struct fixed * fixed, const int line);
static bool is_blank(const struct fixed * fixed, const short line);
static bool is_in_line(const short y, const short line[], const short t);
static void update_fixed(struct fixed * fixed, short line[], short t);
void eliminate(struct fixed * fixed, struct square * form);
bool is_die(struct fixed * fixed);
void add_one(short line, const char ch[]);
void scoure_board();
void print_scoure();
void print_next(const struct square * form);
void time_board();

HANDLE hSqureConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO bInfo;

int main()
{
GetConsoleScreenBufferInfo(hSqureConsole, &bInfo);
COORD size = { 70, 25 };
SetConsoleScreenBufferSize(hSqureConsole, size);
SMALL_RECT rc = { 0, 0, 60, 23 };
SetConsoleWindowInfo(hSqureConsole, true, &rc);
system("color 73");
struct fixed fixed;
initial_fixed(&fixed);
print_fixed(&fixed);

scoure_board();

struct square form_temp = create_square();
time_board();

int is_out = 0;
while (!is_out)
{

fflush(stdin);
struct square form = form_temp;
form_temp = create_square();
print_square(&form, SQUARE);

print_next(&form_temp);

bool isout = 0;
while (!isout)
{
char ch;
if (_kbhit())
{
ch = _getch();
switch (ch)
{
case 72:
if (!turn_success(&form, &fixed))
{
print_square(&form, SPACE);
turn(&form);
print_square(&form, SQUARE);
}
break;
case 75:
if (!is_wall(&form, &fixed, -1))
{
move_square(&form, -1, 0);
}
break;
case 77:
if (!is_wall(&form, &fixed, +1))
{
move_square(&form, +1, 0);
}
break;
case 80:
if (!is_buttom(&form, &fixed))
{
move_square(&form, 0, +1);
}
break;
default:
break;
}
}
Sleep(15);
if (0 == ++time_down % 15)
{
if (is_buttom(&form, &fixed))
{
fix_square(&form, &fixed);
eliminate(&fixed, &form);
isout = 1;
if (scoure >= WIN || is_die(&fixed))
is_out = 1;
}
else
move_square(&form, 0, +1);
}
}
}
}