C语言中怎么用函数实现反弹球消砖块
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,本篇内容介绍了"C语言中怎么用函数实现反弹球消砖块"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、
千家信息网最后更新 2025年02月23日C语言中怎么用函数实现反弹球消砖块
本篇内容介绍了"C语言中怎么用函数实现反弹球消砖块"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
一、项目描述和最终的成果展示
这是在上一次弹跳小项目上进行了一系列的优化和封装。项目: 弹跳的小球
上次没有用函数进行的封装。这次在上次的基础上进行封装和一些功能的优化。
最终效果图如下:
二、封装后的弹跳小球
代码如下:
#include#include #include #include //全局变量int high,width; //游戏画面大小int ball_x,ball_y;//小球的坐标int ball_vx,ball_vy;//小球的速度void gotoxy(int x,int y)//将光标移动到(x,y)位置{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos);}void startup()//数据的初始化{ high = 15; width = 20; ball_x = 0; ball_y = width/2; ball_vx = 1; ball_vy = 1;}void show()//显示画面{ gotoxy(0,0);//光标移动到原点位置,以下重画清屏 int i,j; for(i=0;i<=high;i++) { for(j=0;j<=width;j++) { if( ( i == ball_x) && ( j == ball_y ) ) printf("O");//输出小球 else if( j == width) printf("+");//输出右边框 else if( i == high) printf("-");//输出下边框 else printf(" ");//输出空格 } printf("\n"); }}void updateWithoutInput()//与用户输入无关的更新{ ball_x = ball_x + ball_vx; ball_y = ball_y + ball_vy; if( (ball_x == 0 ) || (ball_x == high-1 )) ball_vx = -ball_vx; if( (ball_y == 0 ) || (ball_y == width-1 )) ball_vy = -ball_vy; Sleep(50);}void updateWithInput()//与用户输入有关的更新{}int main(void){ startup();//数据的初始化 while(1) { show();//显示画面 updateWithoutInput();//与用户输入无关的更新 updateWithInput();//与用户输入有关的更新 } return 0;}
效果图如下:
三、显示移动挡板
代码如下:
#include#include #include #include //全局变量int high,width; //游戏画面大小int ball_x,ball_y;//小球的坐标int ball_vx,ball_vy;//小球的速度int position_x,position_y;//挡板的中心坐标int ridus;//挡板的半径大小int left,right;//挡板的左右位置void gotoxy(int x,int y)//将光标移动到(x,y)位置{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos);}void startup()//数据的初始化{ high = 15; width = 20; ball_x = 0; ball_y = width/2; ball_vx = 1; ball_vy = 1; ridus = 5; position_x = high; position_y = width/2; left = position_y -ridus; right = position_y + ridus;}void show()//显示画面{ gotoxy(0,0);//光标移动到原点位置,以下重画清屏 int i,j; for(i=0;i<=high+1;i++) { for(j=0;j<=width;j++) { if( ( i == ball_x) && ( j == ball_y ) ) printf("O");//输出小球 else if( j == width) printf("+");//输出右边框 else if( i == high+1) printf("-");//输出下边框 else if ( (i==high)&&(j>=left)&&(j<=right)) printf("*"); else printf(" ");//输出空格 } printf("\n"); }}void updateWithoutInput()//与用户输入无关的更新{ ball_x = ball_x + ball_vx; ball_y = ball_y + ball_vy; if( (ball_x == 0 ) || (ball_x == high-1 )) ball_vx = -ball_vx; if( (ball_y == 0 ) || (ball_y == width-1 )) ball_vy = -ball_vy; Sleep(50);}void updateWithInput()//与用户输入有关的更新{ char input; if(kbhit()) { input = getch(); if( input == 'a' || input == 'A' ) { position_y--;//位置左移 left = position_y-ridus; right = position_y+ridus; } if( input == 'd' || input == 'D' ) { position_y++; left = position_y - ridus; right = position_y + ridus; } }}int main(void){ startup();//数据的初始化 while(1) { show();//显示画面 updateWithoutInput();//与用户输入无关的更新 updateWithInput();//与用户输入有关的更新 } return 0;}
效果图如下:
四、反弹小球
代码如下:
#include#include #include #include //全局变量int high,width; //游戏画面大小int ball_x,ball_y;//小球的坐标int ball_vx,ball_vy;//小球的速度int position_x,position_y;//挡板的中心坐标int ridus;//挡板的半径大小int left,right;//挡板的左右位置int ball_number;//反弹小球的次数void gotoxy(int x,int y)//将光标移动到(x,y)位置{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos);}void startup()//数据的初始化{ high = 15; width = 20; ball_x = 0; ball_y = width/2; ball_vx = 1; ball_vy = 1; ridus = 5; position_x = high; position_y = width/2; left = position_y -ridus; right = position_y + ridus; ball_number=0;}void show()//显示画面{ gotoxy(0,0);//光标移动到原点位置,以下重画清屏 int i,j; for(i=0;i<=high+1;i++) { for(j=0;j<=width;j++) { if( ( i == ball_x) && ( j == ball_y ) ) printf("O");//输出小球 else if( j == width) printf("+");//输出右边框 else if( i == high+1) printf("-");//输出下边框 else if ( (i==high)&&(j>=left)&&(j<=right)) printf("*"); else printf(" ");//输出空格 } printf("\n"); } printf("反弹小球数:%d\n",ball_number);}void updateWithoutInput()//与用户输入无关的更新{ if( ball_x == high -1) { if( (ball_y>=left) && (ball_y<=right) ) { ball_number++; printf("\a");//响铃 } else { printf("游戏失败\n"); system("pause"); exit(0); } } ball_x = ball_x + ball_vx; ball_y = ball_y + ball_vy; if( (ball_x == 0 ) || (ball_x == high-1 )) ball_vx = -ball_vx; if( (ball_y == 0 ) || (ball_y == width-1 )) ball_vy = -ball_vy; Sleep(50);}void updateWithInput()//与用户输入有关的更新{ char input; if(kbhit()) { input = getch(); if( input == 'a' || input == 'A' ) { position_y--;//位置左移 left = position_y-ridus; right = position_y+ridus; } if( input == 'd' || input == 'D' ) { position_y++; left = position_y - ridus; right = position_y + ridus; } }}int main(void){ startup();//数据的初始化 while(1) { show();//显示画面 updateWithoutInput();//与用户输入无关的更新 updateWithInput();//与用户输入有关的更新 } return 0;}
效果图如下:
五、添加砖块并实现打砖块操作
代码如下:
#include#include #include #include //全局变量int high,width; //游戏画面大小int ball_x,ball_y;//小球的坐标int ball_vx,ball_vy;//小球的速度int position_x,position_y;//挡板的中心坐标int ridus;//挡板的半径大小int left,right;//挡板的左右位置int ball_number;//反弹小球的次数int block_x1,block_y1;//砖块1的位置int block_x2,block_y2;//砖块2的位置int block_x3,block_y3;//砖块3的位置int score;//消掉砖块的个数void gotoxy(int x,int y)//将光标移动到(x,y)位置{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle,pos);}void startup()//数据的初始化{ high = 15; width = 20; ball_x = 0; ball_y = width/2; ball_vx = 1; ball_vy = 1; ridus = 5; position_x = high; position_y = width/2; left = position_y -ridus; right = position_y + ridus; ball_number=0; block_x1 = 0; block_y1 = 1; block_x2 = 0; block_y2 = 2; block_x3 = 0; block_y3 = 3; score=0;}void show()//显示画面{ gotoxy(0,0);//光标移动到原点位置,以下重画清屏 int i,j; for(i=0;i<=high+1;i++) { for(j=0;j<=width;j++) { if( ( i == ball_x) && ( j == ball_y ) ) printf("O");//输出小球 else if( j == width) printf("+");//输出右边框 else if( i == high+1) printf("-");//输出下边框 else if ( (i==high)&&(j>=left)&&(j<=right)) printf("*"); else if( (i==block_x1) && (j==block_y1) ) printf("A");//输出砖块1 else if( (i==block_x2) && (j==block_y2) ) printf("B");//输出砖块2 else if( (i==block_x3) && (j==block_y3) ) printf("C");//输出砖块3 else printf(" ");//输出空格 } printf("\n"); } printf("反弹小球数:%d\n",ball_number); printf("消掉的砖块数: %d\n",score);}void updateWithoutInput()//与用户输入无关的更新{ if( ball_x == high -1) { if( (ball_y>=left) && (ball_y<=right) )//被挡板挡住了 { ball_number++; printf("\a");//响铃 } else { printf("游戏失败\n"); system("pause"); exit(0); } } if( (ball_x == block_x1) && (ball_y ==block_y1) )//小球击中砖块1 { score++;//分数加1 block_y1=rand()%width;//产生新的砖块 while((block_y1==block_y2) || ( block_y1==block_y3)) //当新产生的砖块和其他砖块重合时 { block_y1=rand()%width;//产生新的砖块 } } if( (ball_x == block_x2) && (ball_y ==block_y2) )//小球击中砖块2 { score++;//分数加1 block_y2=rand()%width;//产生新的砖块 while((block_y2==block_y1) || ( block_y2==block_y3)) //当新产生的砖块和其他砖块重合时 { block_y2=rand()%width;//产生新的砖块 } } if( (ball_x == block_x3) && (ball_y ==block_y3) )//小球击中砖块3 { score++;//分数加1 block_y3=rand()%width;//产生新的砖块 while((block_y3==block_y1) || ( block_y3==block_y2)) //当新产生的砖块和其他砖块重合时 { block_y3=rand()%width;//产生新的砖块 } } ball_x = ball_x + ball_vx; ball_y = ball_y + ball_vy; if( (ball_x == 0 ) || (ball_x == high-1 )) ball_vx = -ball_vx; if( (ball_y == 0 ) || (ball_y == width-1 )) ball_vy = -ball_vy; Sleep(66);}void updateWithInput()//与用户输入有关的更新{ char input; if(kbhit()) { input = getch(); if( input == 'a' || input == 'A' ) { position_y--;//位置左移 left = position_y-ridus; right = position_y+ridus; } if( input == 'd' || input == 'D' ) { position_y++; left = position_y - ridus; right = position_y + ridus; } }}int main(void){ startup();//数据的初始化 while(1) { show();//显示画面 updateWithoutInput();//与用户输入无关的更新 updateWithInput();//与用户输入有关的更新 } return 0;}
效果图如下:
"C语言中怎么用函数实现反弹球消砖块"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
砖块
位置
小球
用户
更新
输入
画面
挡板
有关
移动
光标
数据
坐标
大小
输出
效果
效果图
代码
全局
原点
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
长宁区市场软件开发服务报价表
更改服务器本地安全策略
cobol 数据库
京东慧采服务器网卡销量
湖南塔式服务器经销商
一年一度的网络安全攻防演习
成都英德网络技术有限公司
数据库和数据库之间实时同步
贵州鲲鹏系列服务器企业
网络安全审计系统答题及解析
软件开发优秀员工主要事迹
王者荣耀换服务器头像可以再换吗
农业智慧供排水系统软件开发商
宁波订制app软件开发
网络服务器安全吗
pg备份某张表的数据库
想开发可以搜索和录入的数据库
数据库组件基于什么语言
软件开发技术属于工程类吗
株洲学习软件开发培训
网络安全法是什么的能力
cobol 数据库
服务器存片
万网邮件服务器地址
uuid 数据库性能
战术目标瞄准网络技术
java手机软件开发下载
2016网络安全法通过
淮安软件开发工资
税控服务器管理系统打不开