【C语言】Linux 飞翔的小鸟
【C语言】Linux 飞翔的小鸟
零、环境部署
安装Ncurses库
sudo apt-get install libncurses5-dev
壹、编写代码
代码如下:
bird.c
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<signal.h>
#include<curses.h>
#include<sys/time.h>
#include<unistd.h>int window_w = 100;
int window_h = 25;typedef struct pipe{int x;int r;struct pipe *next;
}*pipe_list;char bird_char = '@';
char pipe_char = '+';
char edge_char = '#';
char bird_die_char = '*';
char empty_char = ' ';
int bird_x, bird_y;
pipe_list piping = NULL;
int pipeline_count = 5;
int pipeline_width = 10;
int pipeline_min_height = 5;
int pipeline_gap = 6;int running = 0;int score = 0;void init_curses();
int set_timer(int ms);void show_bird();
void clear_bird();
void move_bird(int x, int y);
void move_bird_up();
void move_bird_down();
void bird_ctrl();
void show_bird_die();void referee(int is_user);
void show_score();
void show_game_over(int empty);pipe_list create_pipe(int n);
void draw_pipe(char ch);
void show_pipe();
void clear_pipe();
void move_pipe();
void oper_pipe();void show_map_edge();void init_map();void check_yx(int *y,int *x);
void handler();int main(void) {int gap_time;while (1) {printf("==========欢迎来到飞翔的小鸟==========\n");printf("1.普通 2.简单 3.困难\n");printf("请选择游戏难度:");char choose = getchar();getchar();if (choose == '2') {printf("游戏难度:简单\n");gap_time = 800;} else if (choose == '3') {printf("游戏难度:困难\n");gap_time = 300;} else {printf("游戏难度:普通\n");gap_time = 500;}int time_down = 2;while (time_down-- > 0) {printf("%d秒后开始游戏,请做好准备!\n", time_down);sleep(1);}// 初始化随机种子srand(time(NULL));// 初始化软中断signal(SIGALRM, handler);set_timer(gap_time);score = 0;running = 1;// 初始化组件init_curses();// 显示小鸟bird_x = 10; // 列bird_y = window_h / 2; // 行// 显示地图init_map();show_map_edge();show_game_over(1);// 显示管道piping = create_pipe(pipeline_count);show_pipe();// 显示小鸟show_bird();// 小鸟控制bird_ctrl();// 结束游戏endwin();printf("游戏结束!\n");printf("你本次游戏得分:%d\n", score);printf("按任意键继续游戏,按Q键退出游戏\n");choose = getchar();getchar();if (choose == 'q' || choose == 'Q') {break;}}return 0;
}void game_run() {if (!running) {return;}// 小鸟move_bird_down();// 管道clear_pipe();move_pipe();oper_pipe();show_pipe();// 地图show_map_edge();// 游戏得分referee(0);show_score();
}void handler() {game_run();
}void referee(const int is_user) {if (!is_user) {score += 1;}// 判断小鸟是否撞到了管子move(bird_y, bird_x);const chtype ch = inch();if ((char) ch == pipe_char) {// 撞到管子,游戏结束running = 0;show_game_over(0);show_bird_die();}
}void show_game_over(const int empty) {const char tips[] = "Game over, please press any key to continue ~ ";const char *p = tips;move(window_h + 2, 2);while (*p != '\0') {if (empty) {addch(empty_char);} else {attron(COLOR_PAIR(4));addch(*p);attroff(COLOR_PAIR(4));}p++;}refresh();
}void show_score() {char score_str[50];const char *p = score_str;sprintf(score_str, "score : %04d", score);score_str[49] = 0;move(window_h + 1, 2);while (*p != '\0') {addch(*p);p++;}refresh();
}void show_bird_die() {for (int i = bird_y - 1; i <= bird_y + 1; i++) {for (int j = bird_x - 1; j <= bird_x + 1; j++) {move(i, j);attron(COLOR_PAIR(5));addch(bird_die_char);attroff(COLOR_PAIR(5));}}show_bird();refresh();
}void show_bird() {move(bird_y, bird_x);attron(COLOR_PAIR(1));addch(bird_char);attroff(COLOR_PAIR(1));refresh();
}void clear_bird() {move(bird_y, bird_x);addch(empty_char);refresh();
}void check_yx(int *y, int *x) {if (*x <= 0) {*x = 1;}if (*y <= 0) {*y = 1;}if (*x >= window_w) {*x = window_w - 1;}if (*y >= window_h) {*y = window_h - 1;}
}void move_bird(const int x, const int y) {clear_bird();bird_x = x;bird_y = y;check_yx(&bird_y, &bird_x);show_bird();
}void move_bird_up() {move_bird(bird_x, bird_y - 1);
}void move_bird_down() {move_bird(bird_x, bird_y + 1);
}void bird_ctrl() {while (1) {const char ch = getchar();if (ch == ' ') {move_bird_up();referee(1);}if (!running) {break;}}
}int set_timer(const int ms) {int s = ms / 1000;int us = (ms - s * 1000) * 1000;struct itimerval timer;timer.it_value.tv_sec = s;timer.it_value.tv_usec = us;timer.it_interval.tv_sec = s;timer.it_interval.tv_usec = us;return setitimer(ITIMER_REAL, &timer, NULL);
}void init_curses() {// 进入curses模式initscr();// 不显示光标curs_set(0);// 不显示输入的字符noecho();// 启用键盘keypad(stdscr, 1);// 启动颜色机制start_color();init_pair(1, COLOR_WHITE, COLOR_BLUE); // 鸟init_pair(2, COLOR_WHITE, COLOR_GREEN); // 柱子init_pair(3, COLOR_WHITE, COLOR_RED); // 墙init_pair(4, COLOR_RED, COLOR_WHITE); // 游戏结束提示init_pair(5, COLOR_YELLOW, COLOR_RED); // 小鸟爆炸特效
}void init_map() {for (int i = 0; i < window_h + 2; i++) {for (int j = 0; j < window_w; j++) {move(i, j);addch(empty_char);}}refresh();
}void show_map_edge() {attron(COLOR_PAIR(3));for (int i = 0; i <= window_h; i++) {move(i, 0);addch(edge_char);move(i, window_w);addch(edge_char);}for (int i = 0; i <= window_w; i++) {move(0, i);addch(edge_char);move(window_h, i);addch(edge_char);}attroff(COLOR_PAIR(3));
}void oper_pipe() {if (piping == NULL) {return;}pipe_list head = piping, tail = piping;while (tail->next != NULL) {tail = tail->next;}// 检查是否已经进入左边,+1 是墙壁if (head->x <= (-1 * pipeline_width) + 1) {const pipe_list del_pipe = head;head = head->next;free(del_pipe);}// 检查右边是否太空了if (tail->x <= window_w - window_w / pipeline_count) {// 右边没得了,添加一个tail->next = create_pipe(1);}piping = head;
}pipe_list create_pipe(const int n) {pipe_list main = NULL;for (int i = 0; i < n; i++) {const pipe_list p = malloc(sizeof(struct pipe));if (p == NULL) {perror("分配内存失败");return main;}p->x = (int) (window_w - window_w / (float) n * (float) i);p->r = random() % (window_h - pipeline_gap - pipeline_min_height) + pipeline_min_height;p->next = NULL;if (main != NULL) {p->next = main;}main = p;}return main;
}void show_pipe() {draw_pipe(pipe_char);
}void draw_pipe(const char ch) {if (ch == pipe_char) {attron(COLOR_PAIR(2));}pipe_list p = piping;while (p != NULL) {for (int i = 0; i < pipeline_width; i++) {// 列// 绘制上半部分for (int j = 0; j < p->r; j++) {// 行int y = j;int x = p->x + i;check_yx(&y, &x);move(y, x); // 行 列addch(ch);}// 绘制下半部分for (int j = p->r + pipeline_gap; j < window_h; j++) {int y = j;int x = p->x + i;check_yx(&y, &x);move(y, x); // 行 列addch(ch);}}p = p->next;}if (ch == pipe_char) {attroff(COLOR_PAIR(2));}refresh();
}void clear_pipe() {draw_pipe(empty_char);
}void move_pipe() {pipe_list p = piping;while (p != NULL) {p->x -= 1;p = p->next;}
}
贰、编译运行
编译
gcc bird.c -o bird -Wall -lncurses
运行
./bird
叁、运行效果

相关文章:
【C语言】Linux 飞翔的小鸟
【C语言】Linux 飞翔的小鸟 零、环境部署 安装Ncurses库 sudo apt-get install libncurses5-dev壹、编写代码 代码如下: bird.c #include<stdio.h> #include<time.h> #include<stdlib.h> #include<signal.h> #include<curses.h>…...
mcasttest-tool组播检测工具
作者:广大 检测组播 mcasttest-tool是oracle组播检测工具,组播是oracle 11.2.0.2开始的新功能。 1、上传mcasttest工具解压并授权 [rootrac1 soft]# cd /u01/soft/ [rootrac1 soft]# tar -xvf mcasttest.tgz[rootrac1 soft]# chown -R grid:oinstall…...
ncnn 库编译的一些问题,使用交叉编译
一开始的问题是编译完程序,但是部分工具没有编译出来。 主要的问题是: 1. ncnn2in8 程序没有编译出来:主要原因应该是cmakelists.txt文件中对于的模块没打开on,或者这个模块没加进去编译: 添加以下 -DNCNN_BUILD_EXAMPLESON -…...
Python基础教程(一)
1.编程基础 1.1标识符 标识符是变量、函数、模块和其他对象的名称。Python中标识符的命名不是随意的,而是要遵守一定的命名规则,比如说: 1、标识符是由字母 (A~Z 和 a~z) 、下划线和数字组成,但第一个字符不 能是数字。 2、标识符不…...
基于C51和OLED12864实现贪吃蛇小游戏
引言 在微电子技术飞速发展的今天,单片机作为智能控制的核心,广泛应用于各种电子设备中。C51系列单片机以其高效、稳定的特性,成为众多电子爱好者和工程师的首选平台。而OLED显示屏以其轻薄、低功耗、响应速度快等优点,在显示设备…...
JVM性能调优全指南:高流量电商系统的最佳实践
1.G1(Garbage-First) 官网: G1 Garbage Collection G1收集器是Java 7中引入的垃圾收集器,用于替代CMS(Concurrent Mark-Sweep)收集器。它主要针对大内存、多核CPU环境下的应用场景,具有以下特点: 分代收集:G1仍然保留了分代的概念,但新生代和老年代不再是物理隔离的,…...
前端常见场景、JS计算精度丢失问题(Decimal.js 介绍)
目录 一. Decimal.js 介绍 二. 常用方法 1. 创建 Decimal 实例 2.加法 add 或 plus 3.减法 sub 或 minus 4.乘法 times 或 mul 5.除法 div 或 dividedBy 6.取模 7.幂运算 8.平方根 9.保留小数位 toFixed方法(四舍五入) 三.项目应用 前端精度丢失问题通常由以下原因…...
Python写UI自动化--playwright(点击操作)
本篇介绍playwright点击操作,click()方法的常用参数 目录 0. selector (必需) 1. modifiers(可选) 2. position(可选) 3. button(可选) 4. click_count(可选) 5. delay 6. timeout(可选) 7. forceTrue(可选) 8. trialTrue(可选) 9. no_wait_after(可选) …...
[C#面对对象] 之抽象方法 虚方法 接口
1.虚方法 我的理解 "法国的“巴黎公社”,俄国的“十月革命”,都是把主要战略方向首先夺取中心城市 " 设计为 一个父类中的虚方法(virtual),这个虚方法已经有实现了(就是通过暴力革命夺取的方法 最终返回 城市)然而秋收暴动(子类)失败…...
docker 发布geoserver服务添加字体
1. 创建容器时可直接挂载到系统字体库 2. 已发布的容器挂载字体目录 关闭docker服务 : systemctl stop docker.socket 修改config.v2.json :位置在 cd /var/lib/docker/containers/容器id 重新启动docker服务:systemctl start docker...
数据赋能(162)——开发:数据整理——技术方法、主要工具
技术方法 从商业角度来看,从前未知的数据分析模式或趋势的发现为企业提供了非常有价值的洞察力。数据整理技术能够为企业对未来的发展具有一定的预见性。数据整理技术可以分成3类:群集、分类和预测。 群集技术: 这是一种将相似的数据项进行…...
安全服务面试
对安全服务是怎么理解的 安全服务对象是人, 渗透测试对象是网站。(我的理解) 安全概念和资讯 安全工具使用 渗透测试 安全基线检查 应急响应 代码审计 安全边界建设 安全规范 1.拿到一个待检测的站,你觉得应该先做什么&…...
昇思25天学习打卡营第23天|LSTM+CRF序列标注
Mindspore框架CRF条件随机场概率图模型实现文本序列命名实体标注|(一)序列标注与条件随机场的关系 Mindspore框架CRF条件随机场概率图模型实现文本序列命名实体标注|(二)CRF模型构建 Mindspore框架CRF条件随机场概率图模型实现文本…...
抖音直播弹幕数据逆向:websocket和JS注入
🔍 思路与步骤详解 🕵️♂️ 思路介绍 首先,我们通过抓包工具进入的直播间,捕获其网络通信数据,重点关注WebSocket连接。发现直播弹幕数据通过WebSocket传输,这种方式比传统的HTTP更适合实时数据的传输。…...
AIGC diffusers文生图模型optimum量化使用案例
参考: https://github.com/huggingface/blog/blob/main/quanto-diffusers.md 安装 pip install optimum-quanto %pip install optimum使用 from optimum.quanto import freeze, qfloat8, quantize from diffusers import PixArtSigmaPipeline import torchpipeline = PixArt…...
PDF怎么转换成Word?这些工具一键搞定!
在日常生活中,我们经常遇到需要将PDF文件转换成Word文档的情况。PDF怎么转换成Word?一些工具的使用十分重要!下文中就为大家推荐几个亲测好用的PDF转换工具。 一、Foxit PDF转换大师(365客户端) 链接:www…...
【TS】TypeScript函数类型:提升函数的类型安全性和可读性
🌈个人主页: 鑫宝Code 🔥热门专栏: 闲话杂谈| 炫酷HTML | JavaScript基础 💫个人格言: "如无必要,勿增实体" 文章目录 TypeScript函数类型:提升函数的类型安全性和可读性1. 引言2. 基本函…...
“八股文”在实际工作中是助力、阻力还是空谈?
前言:在当今快速发展的技术时代,程序员的角色变得日益重要。随着技术的不断进步,招聘流程也在不断演变以适应新的需求。在程序员的招聘过程中,“八股文”作为一种面试现象,已成为不可忽视的一部分。所谓“八股文”&…...
代码随想录算法训练营第22天-leetcode-回溯算法part01:
#回溯算法理论基础 能解决的问题: 组合问题:N个数里面按一定规则找出k个数的集合切割问题:一个字符串按一定规则有几种切割方式子集问题:一个N个数的集合里有多少符合条件的子集排列问题:N个数按一定规则全排列&…...
MySql 触发器、存储器练习
一: 触发器 1、建立两个表:goods(商品表)、orders(订单表) 查看数据库:mysql> show databases; 使用数据库:mysql> use mydb16_trigger; 创建goods表: mysql> create table goods(gid char(8) not null primary key, …...
谷歌浏览器插件
项目中有时候会用到插件 sync-cookie-extension1.0.0:开发环境同步测试 cookie 至 localhost,便于本地请求服务携带 cookie 参考地址:https://juejin.cn/post/7139354571712757767 里面有源码下载下来,加在到扩展即可使用FeHelp…...
synchronized 学习
学习源: https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖,也要考虑性能问题(场景) 2.常见面试问题: sync出…...
java_网络服务相关_gateway_nacos_feign区别联系
1. spring-cloud-starter-gateway 作用:作为微服务架构的网关,统一入口,处理所有外部请求。 核心能力: 路由转发(基于路径、服务名等)过滤器(鉴权、限流、日志、Header 处理)支持负…...
树莓派超全系列教程文档--(62)使用rpicam-app通过网络流式传输视频
使用rpicam-app通过网络流式传输视频 使用 rpicam-app 通过网络流式传输视频UDPTCPRTSPlibavGStreamerRTPlibcamerasrc GStreamer 元素 文章来源: http://raspberry.dns8844.cn/documentation 原文网址 使用 rpicam-app 通过网络流式传输视频 本节介绍来自 rpica…...
k8s从入门到放弃之Ingress七层负载
k8s从入门到放弃之Ingress七层负载 在Kubernetes(简称K8s)中,Ingress是一个API对象,它允许你定义如何从集群外部访问集群内部的服务。Ingress可以提供负载均衡、SSL终结和基于名称的虚拟主机等功能。通过Ingress,你可…...
HTML 列表、表格、表单
1 列表标签 作用:布局内容排列整齐的区域 列表分类:无序列表、有序列表、定义列表。 例如: 1.1 无序列表 标签:ul 嵌套 li,ul是无序列表,li是列表条目。 注意事项: ul 标签里面只能包裹 li…...
vue3 字体颜色设置的多种方式
在Vue 3中设置字体颜色可以通过多种方式实现,这取决于你是想在组件内部直接设置,还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法: 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...
C++.OpenGL (10/64)基础光照(Basic Lighting)
基础光照(Basic Lighting) 冯氏光照模型(Phong Lighting Model) #mermaid-svg-GLdskXwWINxNGHso {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GLdskXwWINxNGHso .error-icon{fill:#552222;}#mermaid-svg-GLd…...
汇编常见指令
汇编常见指令 一、数据传送指令 指令功能示例说明MOV数据传送MOV EAX, 10将立即数 10 送入 EAXMOV [EBX], EAX将 EAX 值存入 EBX 指向的内存LEA加载有效地址LEA EAX, [EBX4]将 EBX4 的地址存入 EAX(不访问内存)XCHG交换数据XCHG EAX, EBX交换 EAX 和 EB…...
微软PowerBI考试 PL300-在 Power BI 中清理、转换和加载数据
微软PowerBI考试 PL300-在 Power BI 中清理、转换和加载数据 Power Query 具有大量专门帮助您清理和准备数据以供分析的功能。 您将了解如何简化复杂模型、更改数据类型、重命名对象和透视数据。 您还将了解如何分析列,以便知晓哪些列包含有价值的数据,…...
