本文为大家分享了使用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
|
#include <graphics.h> // 引用图形库头文件 #include <conio.h> #include<stdio.h> typedef enum { UP, DOWN, LEFT, RIGHT, UNKOWN } DRCT; const int level = 5; DRCT getKey() { char key; DRCT ret; key = _getch(); switch (key) { case 'a' : case 'A' : ret = LEFT; break ; case 's' : case 'S' : ret = DOWN; break ; case 'd' : case 'D' : ret = RIGHT; break ; case 'w' : case 'W' : ret = UP; break ; default : ret = UNKOWN; break ; } return ret; } void drawImage( int layout[level][level], IMAGE* pimg) { int w, h; w = h = pimg->getwidth() / level; int x1, y1, x2, y2; setcolor(RGB(255, 255, 255)); setfillcolor(RGB(0, 0, 0)); for ( int i = 0; i < level; ++i) { for ( int j = 0; j < level; ++j) { x1 = j * w; y1 = i * h; if (layout[i][j] != -1) { x2 = layout[i][j] % level * w; y2 = layout[i][j] / level * h; putimage(x1, y1, w, h, pimg, x2, y2); } else { fillrectangle(x1, y1, x1 + w, y1 + h); } } } for ( int i = 1; i < level; ++i) { line(0, i*h, pimg->getwidth(), i*h); } for ( int i = 1; i < level; ++i) { line(i*w, 0, i*w, pimg->getheight()); } } int isGameOver( int layout[level][level]) { for ( int i = 0; i < level; i++) { for ( int j = 0; j < level; j++) { if (!(i == level - 1 && j == level - 1)) { if (layout[i][j] != i*level + j) { return 0; } } } } return 1; } int updateLayout( int layout[level][level], DRCT d) { int x = 0, t = 0; int row, col, i, j, a, b; for (i = 0; i < level; i++) //先找黑框坐标 i,j { for (j = 0; j < level; j++) { if (layout[i][j] == -1) { a = i; b = j; } } } switch (d) { case UP: { row = a + 1; col = b; break ; } case DOWN: { row = a - 1; col = b; break ; } case LEFT: { row = a; col = b + 1; break ; } case RIGHT: { row = a; col = b - 1; break ; } default : {row = a, col = b; break ; } } //根据d的值算出与(i,j)交换的方块的坐标(row,col) if (row<0 || row>level - 1 || col<0 || col>level - 1) { x = 0; } else { x = 1; t = layout[a][b]; layout[a][b] = layout[row][col]; layout[row][col] = t; } //坐标(row,col)合法:交换 并且ret=1 // 否则: ret=0 return x; } int main() { int layout[level][level] = { 0,1,2,3,4, 5,6,7,8,9, 10,11,12,13,14, 15,16,17,18,19, 20,21,-1,22,23 }; initgraph(850, 500); // 创建绘图窗口,大小为 640x480 像素 IMAGE img,imgTip; loadimage(&img, _T( "./image/0.jpg" )); //图片为自己定义的路径 loadimage(&imgTip, _T( "./image/0.jpg" ),200,200); drawImage(layout, &img); putimage(650, 0, &imgTip); while (!isGameOver(layout)) { DRCT d = getKey(); //根据d的值修改layout数组中的数据 if (updateLayout(layout, d)) drawImage(layout, &img); } putimage(0, 0, &img); outtextxy(100, 50, _T( "牛逼" )); _getch(); closegraph(); // 关闭绘图窗口 } |
效果如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/mhjie1998/article/details/97403043