本文实例为大家分享了C语言实现图形化打砖块游戏的具体代码,供大家参考,具体内容如下
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
186
187
188
189
190
191
192
193
|
#include<stdio.h> #include<conio.h> #include<easyx.h> #include<Windows.h> #define WINDOW_Width 768 #define WINDOW_Height 1073 #define BrickWidth 81 #define BrickHeight 27 #define ROWS 5 //行 #define COLS 8 //列 enum bk { null, red, orange, yellow, green, blue }; #define Broad_Width 110 #define Broad_Height 20 int brick[ROWS][COLS] = { 0 }; IMAGE img[6], img_ball[2]; IMAGE img_bk, img_board; int Ball_x = 6; //球的x方向向量 int Ball_y = -6; //球的y方向向量 #define Radius 5 int Broad_x = (WINDOW_Width - Broad_Width) / 2 +25; //Broad初始化 int Broad_y = WINDOW_Height - Broad_Height-100; int circle_x = WINDOW_Width / 2-16; //circle初始化 int circle_y = WINDOW_Height - Radius - Broad_Height-122 ; void loadRescource() { loadimage(img + 1, "./res/redblock.png" ,78,25); loadimage(img + 2, "./res/orangeblock.png" ,78,25); loadimage(img + 3, "./res/yellowblock.png" ,78,25); loadimage(img + 4, "./res/greenblock.png" ,78,25); loadimage(img + 5, "./res/blueblock.png" ,78,25); loadimage(&img_bk, "./res/bk.png" ); loadimage(img_ball + 0, "./res/ball0.png" ); loadimage(img_ball + 1, "./res/ball.png" ); loadimage(&img_board, "./res/board.png" ); } void drawBall() { putimage(circle_x, circle_y, &img_ball[0]); putimage(circle_x, circle_y, &img_ball[1]); } void ballMove() { circle_x += Ball_x; circle_y += Ball_y; } int DisappearBrick() { int ZK_COL =( circle_x-60) / BrickWidth; int ZK_ROW = (circle_y-250) / BrickHeight; if (ZK_ROW < 5 && ZK_COL < 8 && brick[ZK_ROW][ZK_COL] != -1) { brick[ZK_ROW][ZK_COL] = -1; return 1; } return 0; } //碰撞检测int circle_x, int circle_y void CollisionDetection() { //球如果往右边移动,检测球是否撞上右边沿 if (Ball_x > 0 && circle_x >= WINDOW_Width - Radius-55) { Ball_x = -6; } //球如果往上边移动,检测球是否撞上上边沿 if (Ball_y < 0 && circle_y <= Radius || DisappearBrick()) { Ball_y = 6; } //球如果往左边移动,检测球是否撞上左边沿 if (Ball_x < 0 && circle_x <= Radius +35) { Ball_x = 6; } //球如果往下边移动,检测球是否撞上下边沿 if (Ball_y > 0 && circle_y >= WINDOW_Height - Radius) { Ball_y = 0; Ball_x = 0; } //检测球是否撞上板子,只能从上往下撞,只有两种极限情况 if ((Ball_y > 0) && (circle_y >= (Broad_y - Radius) - 25) && //球y坐标 (circle_x >= Broad_x) && (circle_x <= (Broad_x + Broad_Width))) { Ball_y = -6; } } //键盘控制 void KeyControl() { CollisionDetection(); //不管有没有按键都要碰撞检测 int ch; if ( true == _kbhit()) { //检测是否有按键 ch = _getch(); //获取按键的值 switch (ch) { case 'a' : case 'A' : case 75: if (Broad_x >= Radius + 35+38) Broad_x -= 50; break ; case 'd' : case 'D' : case 77: if (Broad_x <= WINDOW_Width - Radius - 55-149) Broad_x += 50; break ; } } } void DrawAllBrick() { for ( int i = 0; i < ROWS; i++) { for ( int j = 0; j < COLS; j++) switch (brick[i][j]) { case 1: putimage(j * 81 + 60, i * 27 + 250, &img[1]); break ; case 2: putimage(j * 81 + 60, i * 27 + 250, &img[2]); break ; case 3: putimage(j * 81 + 60, i * 27 + 250, &img[3]); break ; case 4: putimage(j * 81 + 60, i * 27 + 250, &img[4]); break ; case 5: putimage(j * 81 + 60, i * 27 + 250, &img[5]); } } } void init( int brick[][COLS]) { int k = 1; for ( int i = 0; i < ROWS; i++) { for ( int j = 0; j < COLS; j++) { brick[i][j] = k; } k++; } } int main() { initgraph(WINDOW_Width, WINDOW_Height /*,EW_SHOWCONSOLE*/ ); init(brick); loadRescource(); while ( true ) { BeginBatchDraw(); putimage(0, 0, &img_bk); putimage(Broad_x, Broad_y, &img_board); DrawAllBrick(); drawBall(); ballMove(); KeyControl(); DisappearBrick(); EndBatchDraw(); Sleep(20); } while (1); return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_60569662/article/details/124038612