-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.cpp
More file actions
185 lines (171 loc) · 4.03 KB
/
view.cpp
File metadata and controls
185 lines (171 loc) · 4.03 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "common.h"
#include "view.h"
#include "tetris.h"
// ブロックを表示するときの縦横
#define TETRIS_VIEW_WIDTH 10
#define TETRIS_VIEW_HEIGHT 20
// 次のブロックを表示する場所
#define NEXT_BLOCK_WIDTH 5
#define NEXT_BLOCK_HEIGHT 5
typedef struct
{
int flg;
int color;
}view_block_struct;
view_block_struct view_block[TETRIS_VIEW_HEIGHT][TETRIS_VIEW_WIDTH];
view_block_struct next_block[NEXT_BLOCK_HEIGHT][NEXT_BLOCK_WIDTH];
// 四角形を描画する 移植を容易にするための処置.
void render_box(int x1, int y1, int x2, int y2, int color)
{
DrawBox(x1, y1, x2, y2, color, TRUE);
}
// 文字を描画する これも上に同じ
void render_char(int x, int y, const char *string)
{
DrawString(x, y, string, COLOR_WHITE);
}
// 基本的な外観を描画する.
void draw_appearance()
{
int x,y;
// ブロックが入るスペース
x=45;
y=20;
render_box( x, y, x+4, y+205, COLOR_DARKGREY);
render_box(x+106, y, x+110, y+205, COLOR_DARKGREY);
render_box( x+5, y+201, x+105, y+205, COLOR_DARKGREY);
// 次のブロックを表示する場所
x=165;
y=20;
render_box( x, y, x+60, y+5, COLOR_DARKGREY);
render_box( x, y, x+5, y+60, COLOR_DARKGREY);
render_box(x+55, y, x+60, y+60, COLOR_DARKGREY);
render_box( x, y+55, x+60, y+60, COLOR_DARKGREY);
render_char(x, y, "NEXT");
//スコア用のスペース
x=165;
y=100;
char buf[20];
sprintf(buf, "SCORE: %06d", score);
render_char(x, y, buf);
}
// ダイアログの表示
void draw_dialogue(const char *string)
{
render_box(0, SCREEN_HEIGHT/2-10, SCREEN_WIDTH, SCREEN_HEIGHT/2+10, COLOR_NAVY);
int len = strlen(string);
render_char((SCREEN_WIDTH/2)-(len*CHARA_WIDTH/2), SCREEN_HEIGHT/2, string);
}
// ブロックを全部リセットする.
void init_block()
{
int x,y;
for(y=0; y<TETRIS_VIEW_HEIGHT; y++)
{
for(x=0; x<TETRIS_VIEW_WIDTH; x++)
{
view_block[y][x].flg = 0;
view_block[y][x].color = COLOR_BLACK;
}
}
for(y=0; y<NEXT_BLOCK_HEIGHT; y++)
{
for(x=0; x<NEXT_BLOCK_WIDTH; x++)
{
next_block[y][x].flg = 0;
next_block[y][x].color = COLOR_BLACK;
}
}
}
// 適当な色のブロックを表示しておく.
void demo()
{
int x,y;
int colors[] = {COLOR_BLUE, COLOR_GREEN, COLOR_YELLOW, COLOR_RED, COLOR_PURPLE, COLOR_PINK, COLOR_ORANGE, COLOR_WHITE, COLOR_GREENYELLOW};
for(y=0; y<TETRIS_VIEW_HEIGHT; y++)
{
for(x=0; x<TETRIS_VIEW_WIDTH; x++)
{
view_block[y][x].flg = 1;
view_block[y][x].color = colors[rand()%9];
}
}
}
// ブロックを描画する.
void draw_block()
{
int x,y;
for(y=0; y<TETRIS_VIEW_HEIGHT; y++)
{
for(x=0; x<TETRIS_VIEW_WIDTH; x++)
{
if(view_block[y][x].flg)
{
// 50, 20はブロックのスペースの右上の座標
int ulx = 50+x*10;
int uly = 20+y*10;
render_box(ulx+1, uly+1, ulx+9, uly+9, view_block[y][x].color);
}
}
}
}
// 次のブロックの部分を描画する.
void draw_next_block()
{
int x, y;
for(y=0; y<NEXT_BLOCK_HEIGHT; y++)
{
for(x=0; x<NEXT_BLOCK_WIDTH; x++)
{
if(next_block[y][x].flg)
{
// 170, 25はブロックのスペースの右上の座標
int ulx = 170+x*10;
int uly = 25+y*10;
render_box(ulx+1, uly+1, ulx+9, uly+9, next_block[y][x].color);
}
}
}
}
// ブロックの状態を代入する.
void assign_block(board_element board[BOARD_HEIGHT][BOARD_WIDTH])
{
int i,n;
for(i=4; i<BOARD_HEIGHT-1; i++)
{
for(n=1; n<BOARD_WIDTH-1; n++)
{
if(board[i][n].flg)
{
view_block[i-4][n-1].flg = 1;
view_block[i-4][n-1].color = board[i][n].color;
}
else
{
view_block[i-4][n-1].flg = 0;
}
}
}
}
// 次のブロックを代入する.
void assign_next_block(block b)
{
int x,y,i;
// 初期化する.
for(y=0; y<NEXT_BLOCK_HEIGHT; y++)
{
for(x=0; x<NEXT_BLOCK_WIDTH; x++)
{
next_block[y][x].flg = 0;
next_block[y][x].color = COLOR_BLACK;
}
}
// 原点の位置
x = 2;
y = 2;
for(i=0; i<BLOCK_NUM; i++)
{
next_block[b.child[i].y + y][b.child[i].x + x].flg = 1;
next_block[b.child[i].y + y][b.child[i].x + x].color = b.color;
}
}