【C语言】Linux 飞翔的小鸟

news/2024/9/16 7:11:15/文章来源:https://www.cnblogs.com/minuhy/p/18316809

【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 

叁、运行效果

运行效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/758907.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

高斯消元

高斯消元 高斯消元法通常用于求解如下的 \(n\) 元线性方程组 \[\begin{cases}a_{1, 1} x_1 + a_{2, 2} x_2 + \cdots + a_{1, n} x_n = b_1\\a_{2, 1} x_1 + a_{2, 2} x_2 + \cdots + a_{2, n} x_n = b_2\\\cdots\\a_{n, 1} x_1 + a_{n, 2} x_2 + \cdots + a_{n, n} x_n = b_n\…

2024/7/22 模拟赛记录

这次的模拟赛比较简单。 150 T1:100 T2:30 T3:0 T4:20 T1: 【题目描述】 给定两个字符串a,b,从a中选一段前缀,b中选一段后缀(前后缀都可以为 空),并将选出的后缀拼在选出的前缀后面。 你需要求出有多少种本质不同的串(可以为空) 场上思路: 上来直接敲了个扩展kmp,仔细…

[Redis]为什么这么快

redis为什么这么快基于内存实现 redis将数据存储在内存中,内存的读写速度很快,所以redis速度很快 高效的数据结构哈希表 跳表 压缩表 快表 双向链表 简单动态字符串 整数数组单线程模型 使用IO多路复用 合理的数据编码

数据库中字符串连接符的使用

在数据库操作中,字符串处理是日常工作中不可或缺的一部分。无论是构建动态查询,还是处理数据输出,字符串连接符的使用都是至关重要的。 那么,如何正确地使用字符串连接符,才能高效地进行字符串操作呢? 在数据库中,字符串连接符的具体使用方法是什么?我们应该如何利用这…

h5开发心得

运营推广移动端开发 1.适配原理 宽的适配:flexible.js适配,是根据屏幕的宽度计算根标签的大小进行适配, 高的适配:需要用vh,vh在静态页面好用,vh是根据屏幕的可视高度计算的,当页面中有输入框时vh的页面会受到挤压,如下图 用百分比2.移动端布局如果有按钮,最好让ui设计…

BootStrap框架的使用

一、下载:BootStrap是一套用于 HTML、CSS 和 JS 开发的开源工具集。BootStrap下载官网学习的初级阶段,这里下载生产文件即可将下载的压缩包解压得到包含以下的文件: 记住bootstrap.min.css文件的路径创建html骨架,link引入bootstrap.min.css文件可根据文档中的代码复制到ht…

关于ssh的X11Forwarding功能和vnc端口冲突的问题

前言 偶然间发现有时vnc端口启动不了,显示端口已经启动,但是查看发现默认的59xx端口未启动,但是60xx端口缺被一个sshd进程占用,vnc服务除了默认的59xx端口,还会用到60xx端口。 正是因为这个60xx端口被sshd进程占用,所以vnc才启动不了。 那么这个sshd端口是干嘛的呢,默认…

powerdesigner导出图片PDF

1、导出图片 1.1 CTR+A 选中当前页的设计的表(物理模型或概念模型设计的表都可以)1.2、edit-->exportImage1.3选择自己要保存的图片类型:png/JEPG/SVG等等关于导出来的图片的清晰度,这里要说一下,自己保存的png和jepg都比较模糊,SVG虽然清晰度很高,但是手机上却无法查看…

踩坑记录:windows11下使用 VS2022 和 PCL1.14.1 配置点云开发环境

闲话不多说,具体在windows下下载PCL与解压pcl可以看https://www.yuque.com/huangzhongqing/pcl/这位大佬的文章,那我就具体说一下踩过点坑: 踩坑点1: 按照大佬的文章的步骤进行解压与下载,我的PCL环境下在了K盘中,但是最后不知怎么的我的openni2文件夹下在了C盘里,也就是…

记录--进度条真的是匀速的,不信你看

🧑‍💻 写在开头 点赞 + 收藏 === 学会🤣🤣🤣 引言 众所周知,进度条是程序员大大模拟的程序运行进度,一般会在某些数值卡住不动,引起99%悬案。但是背后的原理你真的清楚吗,其实进度条真的是匀速运动的! 先来看看效果接下来开始实现 创建一个矩形,然后折叠起来,…

excel 二级菜单联动筛选,减少筛选项,缓解打平之后筛选项过多不利于查看的问题

问题 二级菜单联动筛选,让标注更省力 步骤准备一级、二级菜单选项的映射表。公式 -> 根据所选内容创建准备一级下拉菜单选项。数据->数据验证->序列->选中区域准备二级下拉菜单选项。数据->数据验证->序列->公式(=INDIRECT(INDIRECT("C" &…