C语言怎么实现打砖块游戏
发表于:2025-02-24 作者:千家信息网编辑
千家信息网最后更新 2025年02月24日,这篇文章主要介绍"C语言怎么实现打砖块游戏",在日常操作中,相信很多人在C语言怎么实现打砖块游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"C语言怎么实现打砖块游戏"
千家信息网最后更新 2025年02月24日C语言怎么实现打砖块游戏
这篇文章主要介绍"C语言怎么实现打砖块游戏",在日常操作中,相信很多人在C语言怎么实现打砖块游戏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"C语言怎么实现打砖块游戏"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
效果如下:
代码:
#include#include #include #include int score;int ball_row, ball_col;int ball_vv, ball_vh;int area_height, area_width;int baffle_col, baffle_row, baffle_size;int brick_col, brick_row;bool isLose;void gotoxy(int x, int y) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X = x; pos.Y = y; SetConsoleCursorPosition(handle, pos);}void HideCursor() { CONSOLE_CURSOR_INFO cursor_info = { 1,0 }; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);}void startup(){ area_height = 20; area_width = 40; ball_row = area_height / 2; ball_col = area_width / 2; ball_vv = 1; ball_vh = 1; baffle_col = area_width / 2; baffle_row = area_height - 2; baffle_size = 8; brick_row = 1; brick_col = rand() % area_width; score = 0; isLose = false;}void show(){ gotoxy(0, 0); int i, j; //system("cls"); for (i = 0; i <= area_height; i++) { for (j = 0; j <= area_width; j++) { if (i == ball_row && j == ball_col) { printf("O"); } else if (i == 0 || i == area_height) printf("-"); else if (j == 0 || j == area_width) printf("|"); else if (i == baffle_row && (j >= baffle_col && j <= baffle_col + baffle_size)) printf("="); else if (i == brick_row && j == brick_col) printf("#"); else printf(" "); } printf("\n"); } printf("score:%d\n", score);}void updateWithhoutInput() { static int count = 0; if(count % 5 == 0){ ball_col += ball_vh; ball_row += ball_vv; } if (ball_col == 0 + 1 || ball_col == area_width - 1)//vh changed ball_vh *= -1; if (ball_row == 0 + 1 /*|| ball_row == area_height - 1*/)//vv changed ball_vv *= -1; if (ball_row == baffle_row - 1 && (ball_col >= baffle_col && ball_col < baffle_col + baffle_size))//在baffle上一行就判断碰撞。 ball_vv *= -1; // 砖块的左,右,下左,下,下右可以检测到碰撞 if ((ball_row == brick_row + 1 && (ball_col >= brick_col - 1 && ball_col <= brick_col + 1))||(ball_row == brick_row && (ball_col == brick_col - 1 || ball_col==brick_col+1))) { ball_vv *= -1; brick_row = -1; score += 10; brick_row = 1; brick_col = rand() % area_width; } if (ball_row >= area_height) isLose = true; count++;}void updateWithInput() { char input; if (kbhit()) { input = getch(); switch (input) { case 'a': if (baffle_col > 0 + 1)baffle_col--; break; case 'w': if (baffle_row > 0 + 1)baffle_row--; break; case 'd': if (baffle_col < area_width - baffle_size - 1)baffle_col++; break; case 's': if (baffle_row < area_height - 1)baffle_row++; break; default: break; } }}int Lost(){ if(ball_row > area_height) return 1; return 0;}int IsFinish() {//游戏是否结束 if (score == 100) { system("cls"); printf("congretulations!!!"); score = 0; _sleep(500);//先暂停在现实符合人性化 system("pause"); return 1; } else if (Lost() == 1) { system("cls"); printf("you have lost!!!"); score = 0; _sleep(500); system("pause"); return 1; } return 0;}int main(){ HideCursor(); startup(); while (1) { show(); updateWithInput(); updateWithhoutInput(); if(IsFinish() == 1){ startup(); continue; } } return 0;}
到此,关于"C语言怎么实现打砖块游戏"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
砖块
语言
学习
更多
帮助
实用
接下来
人性
代码
效果
文章
方法
现实
理论
知识
篇文章
网站
资料
跟着
问题
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全技术高峰论坛致辞
泰州电子网络技术哪家好
数据库原理及应用教程总结
开化租房网络安全
网络安全信息金句
服务器管理口不亮
数据库连接10060
服务器被当跳板
数据库表中添加主键id
达梦7数据库重启
海南网络技术有限公司
qq三国群雄服务器
湖南上头条软件开发有限公司
丰台区信息化软件开发一体化
数据库开发工程师 女生
软件开发与管理实训
公共安全信息化与网络安全
道通软件开发工程师职责
安徽软件开发服务价钱
幼儿园网络安全和信息化
网吧服务器可以用于工作室吗
数据库系统工程师课程视频
辽宁专业网络技术五星服务
最便宜云服务器
第五空间网络安全专题
华为网络技术工程师的工作地点
北京2019年软件开发招聘
幼儿园网络安全和信息化
方舟手游服务器怎么加mod
网络安全责任和保密承诺书