用C语言写一个七巧板程序,要求使用到libgraphics和simpleGUI
时间: 2023-11-27 11:53:57 浏览: 110
用C语言编写的图形界面简单程序
好的,下面是一个简单的七巧板程序示例,使用了libgraphics和simpleGUI库:
```c
#include <graphics.h>
#include <simplegui.h>
void draw_board(int x, int y, int w, int h) {
int i, j;
int x0 = x - w / 2;
int y0 = y - h / 2;
int n = 7;
int colors[7] = { RED, YELLOW, GREEN, CYAN, BLUE, MAGENTA, BROWN };
int shapes[7][5][2] = {
{{0, 0}, {1, 0}, {1, 1}, {1, 2}, {2, 2}},
{{0, 0}, {1, 0}, {1, 1}, {2, 1}, {2, 2}},
{{0, 0}, {1, 0}, {2, 0}, {2, 1}, {3, 1}},
{{0, 0}, {0, 1}, {1, 1}, {1, 2}, {2, 2}},
{{0, 0}, {0, 1}, {1, 1}, {2, 1}, {2, 2}},
{{0, 0}, {0, 1}, {0, 2}, {1, 1}, {2, 1}},
{{0, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}},
};
setfillstyle(SOLID_FILL, BLACK);
bar(x0, y0, x0 + w, y0 + h);
for (i = 0; i < n; i++) {
setfillstyle(SOLID_FILL, colors[i]);
for (j = 0; j < 5; j++) {
int px = x0 + w * shapes[i][j][0] / 4;
int py = y0 + h * shapes[i][j][1] / 4;
bar(px + 1, py + 1, px + w / 4 - 1, py + h / 4 - 1);
}
}
}
int main() {
int w = 400, h = 400;
int x = w / 2, y = h / 2;
initwindow(w, h, "七巧板", 100, 100);
while (true) {
draw_board(x, y, w, h);
if (wait_event(50) == MOUSE_EVENT) {
int mx = get_mouse_x();
int my = get_mouse_y();
x = mx < w ? mx : w;
y = my < h ? my : h;
}
}
closegraph();
return 0;
}
```
这个程序可以在窗口中绘制一个七巧板,并且可以通过鼠标交互来移动七巧板的位置。
阅读全文