运用数组--实现简易的打砖块游戏 Part 2.5

这次我们再次进行代码重构,使用二维数组作为画布。(图文无关)


前置知识:二维数组(C语言)

重构

我们用一个二维数组canvas存储需要打印到屏幕上的所有元素。该数组存储几个特定的值,比如0输出空格,1输出小球,在后续添加挡板,砖块时可以用2,3来表示。

ps:我只是说用这个数字来进行表示,并不是要直接打印0,1,2到屏幕上。

(偷个懒,用了以前的代码,该代码只会显示一半的边框,您可以尝试在细节处进行修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void show() {
gotoxy(0, 0);
for (int i = 0; i < High; i++) {
for (int j = 0; j < Width; j++) {
if (canvas[i][j] == 0)
printf(" ");
else if (canvas[i][j] == 1)
printf("0");
else if (canvas[i][j] == 2)
printf("*");
else if (canvas[i][j] == 3)
printf("#");

}
printf("|\n");
}
for (int j = 0; j < Width; j++)
printf("-");
printf("\n");
printf("score: %d\n", score);

}

添加大量砖块

在采用数组后,初始化砖块变得方便了许多。敲掉砖块的操作也很好写,直接把值3变为值0即可。

在startup函数里可以这样写:

1
2
3
4
5
6
for (int k = left; k <= right; k++)
canvas[position_x][k] = 2;

for (int k = 0; k < Width; k++)
for (int i = 0; i < High / 4; i++)
canvas[i][k] = 3;

消砖块

判断小球坐标是否与砖块坐标重合即可。其实没那么麻烦,只需要询问一下小球坐标对应的canvas值是不是3就可以了。在敲掉砖块后小球也是要反弹的,规律与撞墙相同。

在updateWithoutInput这样写:

1
2
3
4
5
6
if (canvas[ball_x - 1][ball_y] == 3) {
ball_vx = -ball_vx;
canvas[ball_x - 1][ball_y] = 0;
score++;
printf("\a");
}

成品

(里面包含了少许我用来debug的东西,无视就好。

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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
#define High 15
#define Width 20
#define debug printf("ok\n");

int ball_x, ball_y;
int ball_vx, ball_vy;
int canvas[High][Width] = { 0 };
int position_x, position_y;
int ridus;
int left, right;
int score;
int SPD;

void gotoxy(int x, int y) {
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}

void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup() {
ball_x = 10;
ball_y = Width / 2;
ball_vx = -1;
ball_vy = 1;
canvas[ball_x][ball_y] = 1;

ridus = 5;
position_x = High - 1;
position_y = Width / 2;
left = position_y - ridus;
right = position_y + ridus;

for (int k = left; k <= right; k++)
canvas[position_x][k] = 2;

for (int k = 0; k < Width; k++)
for (int i = 0; i < High / 4; i++)
canvas[i][k] = 3;

score = 0;
}


void show() {
gotoxy(0, 0);
for (int i = 0; i < High; i++) {
for (int j = 0; j < Width; j++) {
if (canvas[i][j] == 0)
printf(" ");
else if (canvas[i][j] == 1)
printf("0");
else if (canvas[i][j] == 2)
printf("*");
else if (canvas[i][j] == 3)
printf("#");

}
printf("|\n");
}
for (int j = 0; j < Width; j++)
printf("-");
printf("\n");
printf("score: %d\n", score);

}

void updateWithoutInput() {
if (ball_x == High - 2) {
if ((ball_y >= left) && (ball_y <= right)) {
//printf("\a");
}
else {
printf("GG\n");
system("pause");
exit(0);
}
}





canvas[ball_x][ball_y] = 0;
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
canvas[ball_x][ball_y] = 1;

if ((ball_x == 0) || (ball_x == High - 2)) {
ball_vx = -ball_vx;
}
if ((ball_y == 0) || (ball_y == Width - 1)) {
ball_vy = -ball_vy;
}

if (canvas[ball_x - 1][ball_y] == 3) {
ball_vx = -ball_vx;
canvas[ball_x - 1][ball_y] = 0;
score++;
printf("\a");
}

Sleep(100);
}

void updateWithInput() {
char input;
if (_kbhit()) {
input = _getch();
if ((input == 'a') && (left > 0)) {
canvas[position_x][right] = 0;
position_y--;
left = position_y - ridus;
right = position_y + ridus;
canvas[position_x][left] = 2;
}
if ((input == 'd') && (right < Width - 1)) {
canvas[position_x][left] = 0;
position_y++;
left = position_y - ridus;
right = position_y + ridus;
canvas[position_x][right] = 2;
}
}
}


int main() {
startup();
HideCursor();
while (1) {
show();
updateWithInput();
updateWithoutInput();
}
return 0;
}


-------------本文结束,感谢您的阅读转载请注明原作者及出处-------------


本文标题:运用数组--实现简易的打砖块游戏 Part 2.5

文章作者:Shawn Zhou

发布时间:2018年08月04日 - 22:08

最后更新:2018年12月09日 - 19:12

原始链接:http://yoursite.com/2018/08/04/运用数组-实现简易的打砖块游戏-Part-2-5/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

知识无价,码字不易。对您有用,敬请打赏。金额随意,感谢关心。