linux curses,Linux下利用curses库实现弹球游戏
控制球水平运动的变量*/
int vdir; /*控制球垂直运动的变量*/
int pos_X; /*球的横坐标*/
int pos_Y; /*球的纵坐标*/
int delay=100;
void moveBall();
void init();
void control();
int main()
{
//初始化curses
initscr();
crmode(); /*中断模式*/
noecho(); /*关闭回显*/
move(6,28);
attron(A_BOLD);
addstr("Welcome to the BallGame!");
move(8,20);
attroff(A_BOLD);
addstr("Help:");
move(9,23);
addstr("'N':Start a new game.");
move(10,23);
addstr("'Q':Quit game.");
move(11,23);
addstr("'KEY_LEFT' :Control baffle left shift.");
move(12,23);
addstr("'KEY_RIGHT':Control baffle right shift.");
move(13,23);
addstr("'KEY_UP' :Control of the ball speed.");
move(14,23);
addstr("'KEY_DOWN' :Control of the ball reducer.");
int flag=1;
char choice;
move(16,24);
addstr("Please choose your choice!(n/q):");
refresh();
choice=getch();
while(flag){
if(choice=='q'||choice=='Q'||choice=='n'||choice=='N')
flag=0;
else choice=getch();
}
if(choice=='n'||choice=='N'){ /*开始游戏*/
clear();
move(10,25);
addstr("BallGame will start! Are you read?");
refresh();
sleep(3);
control();
}
else if(choice=='q'||choice=='Q'){ /*退出游戏*/
clear();
move(10,25);
addstr("You quit the game successfully!");
refresh();
sleep(3);
endwin();
}
endwin(); /*结束curses*/
return 0;
}
void init(){
int i,j;
clear();
if(start_color()==OK){ /*改变球和挡板的颜色*/
attron(A_BOLD); /*打开粗体*/
init_pair(1,COLOR_YELLOW,COLOR_BLACK);
attron(COLOR_PAIR(1));
}
//初始球pos_X =22; /*球初始的横坐标*/
pos_Y = BOTTOM-1; /*球初始的纵坐标*/
//初始化球的运动方向,朝右上方运动hdir=1;
vdir=-1;
//初始挡板left_board=20;
right_board=left_board+BOARD_LENGTH;
for(i=left_board;i<=right_board;i++){ /*显示挡板*/
move(BOTTOM,i);
addch('-');
}
//初始刷新时间signal(SIGALRM,moveBall);
set_ticker(delay);
keypad(stdscr,TRUE); /*打开keypad键盘响应*/
attroff(A_BLINK); /*关闭A_BLINK属性*/
is_lose=0;
move(pos_Y,pos_X);
addch(BALL);
move(LINES-1, COLS-1);
refresh();
usleep(100000); /*睡眠*/
move(LINES-1,COLS-1);
refresh();
}
void moveBall(){
if(is_lose) return;
signal(SIGALRM,moveBall);
move(pos_Y,pos_X);
addch(BLANK);
pos_X += hdir;
pos_Y += vdir;
//改变球的方向时if(pos_X >= RIGHT) { /*当球横坐标大于右边边缘时,球反弹朝左运动*/
hdir = -1;
beep(); /*球撞墙时,发出声音*/
}
if(pos_X <= LEFT) { /*当球横坐标大于左边边缘时,球反弹朝右运动*/
hdir = 1;
beep(); /*球撞墙时,发出声音*/
}
if(pos_Y <= TOP) { /*当球纵坐标大于顶部边缘时,球反弹朝下运动*/
vdir = 1;
beep(); /*球撞墙时,发出声音*/
}
//当球在底部的时候进行额外的处理if(pos_Y >= BOTTOM-1){
if(pos_X>=left_board&&pos_X<=right_board) /*球在挡板处*/
vdir=-1;
else{ /*球不在挡板处*/
is_lose=1;
move(pos_Y,pos_X);
addch(BALL);
move(LINES-1, COLS-1);
refresh();
usleep(delay*1000); /*睡眠*/
move(pos_Y,pos_X);
addch(BLANK);
pos_X += hdir;
pos_Y += vdir;
move(pos_Y,pos_X);
addch(BALL);
move(LINES-1, COLS-1);
refresh();
}
}
//不改变球的方向时move(pos_Y,pos_X);
addch(BALL);
move(LINES-1, COLS-1);
refresh();
}
void control(){
init();
int cmd;
while (1)
{
if(!is_lose){
cmd=getch();
if(cmd=='q'||cmd=='Q'||cmd==27) break; //强制退出游戏//挡板左移if(cmd==KEY_LEFT){
if(left_board>0){
move(BOTTOM,right_board);
addch(' ');
right_board--;
left_board--;
move(BOTTOM,left_board);
addch('-');
move(BOTTOM,RIGHT);
refresh();
}
}
//挡板右移else if(cmd==KEY_RIGHT){
if(right_board
move(BOTTOM,left_board);
addch(' ');
right_board++;
left_board++;
move(BOTTOM,right_board);
addch('-');
move(BOTTOM,RIGHT);
refresh();
}
}
//给球加速else if(cmd==KEY_UP){
delay/=2;
set_ticker(delay);
}
//给球减速else if(cmd==KEY_DOWN){
delay*=2;
set_ticker(delay);
}
}
else{
//输掉球后的处理int flag=1;
char choice;
move(8,15);
addstr("Game Over!try again?(y/n):");
refresh();
choice=getch();
while(flag){
if(choice=='y'||choice=='Y'||choice=='n'||choice=='N')
flag=0;
else choice=getch();
}
if(choice=='y'||choice=='Y'){ /*游戏重新开始*/
delay=100; /*恢复球的初始速度*/
init();
continue;
}
else if(choice=='n'||choice=='N'){ /*结束游戏*/
break;
}
}
}
}
//设置定时器int set_ticker(int n_msecs){
struct itimerval new_timeset;
long n_sec,n_usecs;
n_sec=n_msecs/1000;
n_usecs=(n_msecs%1000)*1000L;
new_timeset.it_interval.tv_sec=n_sec;
new_timeset.it_interval.tv_usec=n_usecs;
new_timeset.it_value.tv_sec=n_sec;
new_timeset.it_value.tv_usec=n_usecs;
return setitimer(ITIMER_REAL,&new_timeset,NULL);
}
linux curses,Linux下利用curses库实现弹球游戏相关推荐
- linux怎么编写弹球游戏,Linux下利用curses库实现弹球游戏.docx
#define TOP #define TOP 0/*当前屏幕的最上边 */ Linux 下利用 curses 库实现弹球游戏 #include #include #include #define R ...
- Linux下基于SDL库贪吃蛇游戏
Linux下基于SDL库贪吃蛇游戏 SDL(Simple DirectMediaLayer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成.SDL提供了数种控制图像.声音.输出入的函数,让开 ...
- c语言curses小游戏,基于curses库实现弹球游戏
在网上找到,某人在基于Linux终端,用curses库实现的弹球游戏.本人曾经也做过五子棋游戏,分在其它文章中分享. /* * 基于curses库弹球游戏,编译gcc xxx.c -lcurses * ...
- Linux下和MySQL下利用python插入亿万级数据
##下载mysqldb 首先必须下载mysqldb,下载语句是 yum install MySQL-python 安装之后,在命令行输入 #>>>python #接下来是python ...
- Linux valgrind java_linux下利用valgrind工具进行内存泄露检测和性能分析
valgrind通常用来成分析程序性能及程序中的内存泄露错误 一 Valgrind工具集简绍 Valgrind包含下列工具: 1.memcheck:检查程序中的内存问题,如泄漏.越界.非法指针等. 2 ...
- Linux下使用readline库实现2048游戏
一.背景 上一篇文章介绍了readline的基本用法,这一篇通过readline读取按键对2048游戏的c语言实现: 二.思路 2.1 游戏介绍 关于2048游戏,网上找了一下介绍,你可以点击这里在线 ...
- 最最完整的二维码生成教程:MFC下利用libqrencode库生成二维码,显示在屏幕上并保存
最近做项目的时候,需要用MFC写一个二维码生成器,要求根据指定内容生成二维码,显示在屏幕上,并能够保存在电脑中. 一. libqrencode库编译 libqrencode是一个日本人写的生成二维码的 ...
- linux系统下替换图片,Linux(ubuntu系统)下使用FreeImage库
Linux(ubuntu系统)下使用FreeImage库 Linux(ubuntu系统)下使用FreeImage库 最近在搞一个图像处理的项目,需要用到FreeImage,之前在Windows下用过, ...
- 详解Linux命令行下常用svn命令
2019独角兽企业重金招聘Python工程师标准>>> 本文讲述了Linux命令行下常用svn命令的使用方法,希望对您有所帮助. 1.Linux命令行下将文件checkout到本地目 ...
最新文章
- Petshop3.0学习笔记(二)Global.asax文档分析
- python wxpy_wxpy
- VsCode 配置 C/C++ 开发环境,真的很简单
- ArcGIS几种数据格式2
- 微信小程序-京东购物系列一
- ibm系列文章 -- Windows 到 Linux 之旅
- python字符串标签转化_如何在TensorFlow中将字符串标签转换为一个热向量?
- run()方法和start()方法测试解析
- 用c语言写代码_教你如何用android mvp分层架构优雅写代码
- 永辉发布元宵数据:汤圆销售明显提升,多个民生产品增长超150%
- 360 nginx+php,windows Nginx+PHP7+Mysql独立环境部署
- 大数据相加_清华大学黄高——图像数据的语义层扩增方法
- centos配置epel和remi源
- python信用卡管理源码_Python随机生成信用卡卡号的实现方法
- Nuvoton I91200 SDS- AudioPlayback
- html设置发光字体制作,用CSS3制作炫酷的自定义发光文字
- 小程序发送短信验证码完整工具
- [笔]蔚来汽车网络笔/面试题汇总解答
- IT 行业中Dev、RD、CPO、TeamLeader、QA、PM、PO ,HC,TL 分别是什么的简称,他们分别对应的职责是什么?
- 测试用例设计方法(一)
热门文章
- python随手记自动记账_对着微信说一声,它就能帮你自动记账并归类,小编已上瘾...
- 浅谈蓝湖使用ps插件快速构建代码
- 我的Blog搭建之旅1——Django基础
- 【锐普PPT教程】用powerpoint2010实现PPT转视频方法介绍
- C++ji将txt文件数据zzhua转为数组;
- 建筑高级工程师职称评审条件有哪些?
- 【历史上的今天】2 月 14 日:第一台通用计算机面世;IBM 诞生;Julia 公开发布
- 连连抽-自助式个人抽签系统 LuckySelect
- 开源项目-ERP企业资源管理系统
- c++ 字符串匹配算法sunday算法