利用c语言图形库,通过链表实现七巧板,并且可以自动判定游戏是否结束

时间: 2023-07-15 17:10:26 浏览: 55
实现七巧板的游戏可以分为以下几个步骤: 1. 使用图形库创建游戏窗口,并绘制七巧板的背景图案。 2. 使用链表来存储七巧板中各个形状的坐标信息。每个形状可以看作是一个链表节点,包含该形状的起始坐标、宽度、高度和节点指针等信息。 3. 实现七巧板的拖拽功能。通过监听鼠标事件,找到当前鼠标所在的形状,记录下鼠标与该形状的偏移量,并将该形状从链表中移除。 4. 实现七巧板的旋转功能。通过监听键盘事件,实现对当前选中的形状进行旋转操作。 5. 实现七巧板的碰撞检测功能。每当有形状被拖拽后,需要检测该形状是否与其他形状发生重叠,以及是否超出了七巧板的边界,如果是,则不能放置该形状。 6. 实现游戏结束的判定。当所有形状都被正确地放置在七巧板中,游戏结束。 下面是一个简单的示例代码,可以帮助你更好地理解如何实现七巧板的游戏。 ```c #include <stdio.h> #include <stdlib.h> #include <graphics.h> #define WIDTH 800 #define HEIGHT 600 // 链表节点 typedef struct node { int x, y; // 坐标 int w, h; // 宽度和高度 struct node *next; // 指向下一个节点的指针 } Node; // 创建链表节点 Node *create_node(int x, int y, int w, int h) { Node *node = (Node *)malloc(sizeof(Node)); node->x = x; node->y = y; node->w = w; node->h = h; node->next = NULL; return node; } // 添加节点到链表 void add_node(Node **head, Node *node) { if (*head == NULL) { *head = node; } else { Node *p = *head; while (p->next != NULL) { p = p->next; } p->next = node; } } // 删除节点 void delete_node(Node **head, Node *node) { if (*head == NULL || node == NULL) { return; } if (*head == node) { *head = node->next; } else { Node *p = *head; while (p != NULL && p->next != node) { p = p->next; } if (p != NULL) { p->next = node->next; } } free(node); } // 清空链表 void clear_list(Node **head) { Node *p = *head; while (p != NULL) { Node *temp = p; p = p->next; free(temp); } *head = NULL; } // 判断两个矩形是否重叠 int rect_overlap(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) { if (x1 + w1 <= x2 || x2 + w2 <= x1 || y1 + h1 <= y2 || y2 + h2 <= y1) { return 0; } else { return 1; } } // 判断是否可以放置该形状 int can_place(Node *shape, Node *head) { while (head != NULL) { if (rect_overlap(shape->x, shape->y, shape->w, shape->h, head->x, head->y, head->w, head->h)) { return 0; } head = head->next; } if (shape->x < 0 || shape->y < 0 || shape->x + shape->w > WIDTH || shape->y + shape->h > HEIGHT) { return 0; } return 1; } // 判断游戏是否结束 int is_game_over(Node *head) { while (head != NULL) { if (head->x < 0 || head->y < 0 || head->x + head->w > WIDTH || head->y + head->h > HEIGHT) { return 0; } head = head->next; } return 1; } int main() { // 初始化图形库 initgraph(WIDTH, HEIGHT); // 绘制七巧板的背景图案 setfillcolor(BLACK); bar(0, 0, WIDTH, HEIGHT); setfillcolor(DARKGRAY); fillrectangle(100, 100, 500, 500); setfillcolor(WHITE); fillrectangle(120, 120, 480, 480); setfillcolor(DARKGRAY); fillrectangle(140, 140, 460, 460); setfillcolor(WHITE); fillrectangle(160, 160, 440, 440); setfillcolor(DARKGRAY); fillrectangle(180, 180, 420, 420); setfillcolor(WHITE); fillrectangle(200, 200, 400, 400); // 创建链表并添加形状节点 Node *head = NULL; add_node(&head, create_node(300, 200, 40, 40)); // 方块 add_node(&head, create_node(200, 300, 40, 80)); // 长条 add_node(&head, create_node(100, 200, 40, 40)); // 方块 add_node(&head, create_node(200, 200, 40, 40)); // 方块 add_node(&head, create_node(400, 200, 40, 40)); // 方块 add_node(&head, create_node(300, 300, 40, 40)); // 方块 add_node(&head, create_node(400, 300, 40, 40)); // 方块 // 绘制形状 Node *p = head; while (p != NULL) { setfillcolor(YELLOW); fillrectangle(p->x, p->y, p->x + p->w, p->y + p->h); p = p->next; } // 循环处理鼠标和键盘事件 int is_dragging = 0; // 是否正在拖拽 Node *selected = NULL; // 当前选中的形状 int offset_x = 0, offset_y = 0; // 鼠标与选中形状的偏移量 while (!is_game_over(head)) { if (kbhit()) { // 处理键盘事件 char ch = getch(); if (ch == 'r' && selected != NULL) { // 旋转选中的形状 int temp = selected->w; selected->w = selected->h; selected->h = temp; cleardevice(); setfillcolor(BLACK); bar(0, 0, WIDTH, HEIGHT); setfillcolor(DARKGRAY); fillrectangle(100, 100, 500, 500); setfillcolor(WHITE); fillrectangle(120, 120, 480, 480); setfillcolor(DARKGRAY); fillrectangle(140, 140, 460, 460); setfillcolor(WHITE); fillrectangle(160, 160, 440, 440); setfillcolor(DARKGRAY); fillrectangle(180, 180, 420, 420); setfillcolor(WHITE); fillrectangle(200, 200, 400, 400); p = head; while (p != NULL) { setfillcolor(YELLOW); fillrectangle(p->x, p->y, p->x + p->w, p->y + p->h); p = p->next; } } } if (ismouseclick(WM_LBUTTONDOWN)) { // 处理鼠标按下事件 int x = mousex(); int y = mousey(); p = head; while (p != NULL) { if (x >= p->x && x <= p->x + p->w && y >= p->y && y <= p->y + p->h) { // 找到当前鼠标所在的形状 is_dragging = 1; selected = p; offset_x = x - p->x; offset_y = y - p->y; delete_node(&head, p); break; } p = p->next; } } if (ismouseclick(WM_LBUTTONUP)) { // 处理鼠标松开事件 if (is_dragging) { // 放置选中的形状 int x = mousex() - offset_x; int y = mousey() - offset_y; selected->x = x; selected->y = y; if (can_place(selected, head)) { add_node(&head, selected); } else { free(selected); } is_dragging = 0; selected = NULL; } } if (is_dragging) { // 处理鼠标拖拽事件 int x = mousex() - offset_x; int y = mousey() - offset_y; selected->x = x; selected->y = y; cleardevice(); setfillcolor(BLACK); bar(0, 0, WIDTH, HEIGHT); setfillcolor(DARKGRAY); fillrectangle(100, 100, 500, 500); setfillcolor(WHITE); fillrectangle(120, 120, 480, 480); setfillcolor(DARKGRAY); fillrectangle(140, 140, 460, 460); setfillcolor(WHITE); fillrectangle(160, 160, 440, 440); setfillcolor(DARKGRAY); fillrectangle(180, 180, 420, 420); setfillcolor(WHITE); fillrectangle(200, 200, 400, 400); p = head; while (p != NULL) { setfillcolor(YELLOW); fillrectangle(p->x, p->y, p->x + p->w, p->y + p->h); p = p->next; } setfillcolor(YELLOW); fillrectangle(x, y, x + selected->w, y + selected->h); } } // 清空链表 clear_list(&head); // 关闭图形库 closegraph(); return 0; } ```

相关推荐

最新推荐

recommend-type

C语言数据结构实现链表逆序并输出

主要介绍了C语言数据结构实现链表逆序并输出的相关资料,需要的朋友可以参考下
recommend-type

C语言实现带头结点的链表的创建、查找、插入、删除操作

主要介绍了C语言实现带头结点的链表的创建、查找、插入、删除操作方法,对于了解数据结构中链表的各项操作有很好的借鉴价值,需要的朋友可以参考下
recommend-type

C语言链表题目(附答案).docx

大一初学C语言时的期末作业,涉及到链表的建立和功能的实现,涉及指针、函数、动态结构建立等方面的知识,初学者可以参考参考尝试尝试哟!!!
recommend-type

课设报告-“贪吃蛇_C语言_链表实现”-CSDN~.docx

课设报告-“贪吃蛇_C语言_链表实现~_课设”中 兑现将课设报告另发一篇博客~
recommend-type

C语言实现食堂就餐管理系统(带链表)

主要为大家详细介绍了C语言实现食堂就餐管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。