代码重构

这个游戏是这篇博客的进阶版弹力球小程序C语言实现,如果有哪些地方不是很理解可以翻回我的那篇博客看看

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>int high, width;
int x, y;
int vx, vy;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()
{high = 15;width = 20;x = 0;y = width / 2;vx = 1;vy = 1;
}void show()
{gotoxy(0, 0);HideCursor();int i, j;for(i = 0; i < high; i++){for(j = 0; j < width; j++){if((i == x) && (j == y))printf("o");elseprintf(" ");}printf("\n");}
}void updateWithoutInput()
{x = x + vx;y = y + vy;if((x == 0) || (x == high - 1))vx = -vx;if((y == 0) || (y == width - 1))vy = -vy;// sleep(1);
}void updateWithInput()
{
}int main()
{startup();while(1){show();updateWithoutInput();updateWithInput();}return 0;
}

显示边框

跳动的小球游戏

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>int high, width;
int x, y;
int vx, vy;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()
{high = 15;width = 20;x = 0;y = width / 2;vx = 1;vy = 1;
}void show()
{gotoxy(0, 0);HideCursor();int i, j;for(i = 0; i <= high; i++){for(j = 0; j <= width; j++){if((i == x) && (j == y))printf("o");else if(j == width)printf("|");else if(i == high)printf("_");elseprintf(" ");}printf("\n");}
}void updateWithoutInput()
{x = x + vx;y = y + vy;if((x == 0) || (x == high - 1))vx = -vx;if((y == 0) || (y == width - 1))vy = -vy;// sleep(1);
}void updateWithInput()
{
}int main()
{startup();while(1){show();updateWithoutInput();updateWithInput();}return 0;
}

显示移动挡板



#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>int high, width;
int x, y;
int vx, vy;
int px, py;
int ridus;
int left, right;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()
{high = 15;width = 20;x = 0;y = width / 2;vx = 1;vy = 1;ridus = 5;px = high;py = width / 2;left = py - ridus;right = py + ridus;
}void show()
{gotoxy(0, 0);HideCursor();int i, j;for(i = 0; i <= high + 1; i++){for(j = 0; j <= width; j++){if((i == x) && (j == y))printf("o");else if(j == width)printf("|");else if(i == high + 1)printf("_");else if((i == high) && (j >= left) && (j <= right))printf("*");elseprintf(" ");}printf("\n");}
}void updateWithoutInput()
{x = x + vx;y = y + vy;if((x == 0) || (x == high - 1))vx = -vx;if((y == 0) || (y == width - 1))vy = -vy;// sleep(1);
}void updateWithInput()
{char input;if(kbhit()){input = getch();if(input == 'a'){py--;left = py - ridus;right = py + ridus;}if(input == 'd'){py++;left = py - ridus;right = py + ridus;}}
}int main()
{startup();while(1){show();updateWithoutInput();updateWithInput();}return 0;
}

消砖块

弹力球消砖块

大体粗略Demo:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>int high, width;
int x, y;
int vx, vy;
int px, py;
int ridus;
int left, right;
int num;      //反弹小球个数
int bx, by;
int score;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()
{high = 20;width = 20;x = 0;y = width / 2;vx = 1;vy = 1;ridus = 5;px = high;py = width / 2;left = py - ridus;right = py + ridus;num = 0; bx = 0;by = width / 2 - 1;score = 0;
}void show()
{gotoxy(0, 0);HideCursor();int i, j;for(i = 0; i <= high + 1; i++){for(j = 0; j <= width; j++){if((i == x) && (j == 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 == bx) && (j == by)){printf("###");j += 2;}elseprintf(" ");}printf("\n");}printf("反弹小球数:%d\n", num);printf("消除的砖块数:%d\n", score);
}void updateWithoutInput()
{if(x == high - 1){if((y >= left) && (y <= right)){num++;printf("\a");if(num % 2)y = y + rand() * 10 % 6 - 5;else{if(y + rand() * 10 % 6 + 3 < width - 1)y = y + rand() * 10 % 6 + 3;}}else{printf("Lose!!!\n");system("pause");exit(0);}}if((x == bx) && (y > by && y <= by + 3)){score++;by = rand() * 10 % width;}x = x + vx;y = y + vy;if((x == 0) || (x == high - 1))vx = -vx;if((y == 0) || (y == width - 1))vy = -vy;
}void updateWithInput()
{char input;if(kbhit()){input = getch();if(input == 'a'){py--;left = py - ridus;right = py + ridus;}if(input == 'd'){py++;left = py - ridus;right = py + ridus;}}
}int main()
{startup();while(1){show();updateWithoutInput();updateWithInput();}return 0;
}

因为速度太快了,有点小难,这是我的辣鸡战绩:

终极版弹力球消砖块

添加了挡板上下移动功能

C语言自制竞速消砖块游戏

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>int life = 3;
int high, width;   //游戏画面大小
int x, y;          //小球坐标
int vx, vy;            //小球速度
int px, py;            //挡板中心坐标
int ridus;            //挡板半径大小
int left, right;
int h;
int num;          //反弹小球个数
int bx, by;            //砖块位置
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 HideCursor()          //治光标闪烁问题
{CONSOLE_CURSOR_INFO cursor_info = {1, 0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}void startup()     //数据初始化
{high = 20;width = 20;h = high;x = 0;y = width / 2;vx = 1;vy = 1;ridus = 5;px = high;py = width / 2;left = py - ridus;right = py + ridus;num = 0; bx = 0;                  by = width / 2 - 1;score = 0;
}void show()                //显示画面
{gotoxy(0, 0);HideCursor();int i, j;for(i = 0; i <= high + 1; i++){for(j = 0; j <= width; j++){if((i == x) && (j == y))printf("o");else if(j == width)printf("|");else if(i == high + 1)printf("_");else if((i == h) && (j >= left) && (j <= right))printf("*");else if((i == bx) && (j == by)){printf("###");j += 2;}elseprintf(" ");}printf("\n");}printf("反弹小球数Num:%d\n", num);printf("消除的砖块数Score:%d\n", score);printf("你的生命值: %d", life);if(y == width - 1 && x == h - 1)y -= 1;
}void updateWithoutInput()
{int flag = 0;if(x == h - 1){if(((y >= left) || (left < 0 && y >= 0)) && ((y <= right) || (right > width && y < width))){if(vx > 0){flag = 1;num++;printf("\a");
//              int t = 0;if(num % 2){do{y = y + rand() * 7 % 3 - 1;if(y >= width)y = width - 1;if(y > right)y -= 1;if(y < left)y += 1;
//                      if(t >= 1)
//                          y += 1;
//                      y -= 1;
//                      t += 1;}while(y < left || y > right);}else{do{y = y + rand() * 7 % 3 + 1;if(y >= width)y = width - 1;if(y > right)y -= 1;if(y < left)y += 1;
//                      y += 1;}while((y < left || y > right));}   }}else if(life == 0){printf("Lose!!!\n");system("pause");exit(0);}else {life--;}}if(x > h + 1){printf("Lose!!!\n");system("pause");exit(0);}if((x == bx) && (y >= by && y <= by + 3)){score++;by = rand() * 10 % width;}x = x + vx;y = y + vy;if((x == 0) || (x == h) || (x == high - 1))vx = -vx;if(flag)x--;if((y == 0) || (y == width)){vy = -vy;}if(y == 0)y += 1;if(y == width){if(x == h - 1)x -= 1;y -= 1;}}void updateWithInput()
{char input;if(kbhit()){input = getch();if(input == 'a'){py--;left = py - ridus;right = py + ridus;}if(input == 'd'){py++;left = py - ridus;right = py + ridus;}if(input == 'w'){h--;}if(input == 's'){h++;if(h == high + 1)h--;}}
}int main()
{startup();while(1){show();updateWithoutInput();updateWithInput();
//      sleep(1);}return 0;
}

如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持,明天我们不见不散!!!

反弹球消砖块C语言重构函数封装相关推荐

  1. C语言实现【小游戏——反弹球消砖块】

    目录: 1.目标要求: 2.C语言代码: 3.运行结果: 1.目标要求: 1.击中上方方块'H',分数加1 2.下方控制线没有接到小球时,游戏结束 2.C语言代码: #include<stdio ...

  2. 【C语言】数组反弹球消砖块

    数组反弹球消砖块 一.模板 二.反弹球 三.增加挡板 四.消砖块和计数功能 五.最终成果 一.模板 简单的模板,目前由头文件,全局变量,清屏函数,初始化数据,显示画面,与用户有关的更新,与用户无关的更 ...

  3. 项目: 用数组实现反弹球消砖块

    项目:用数组实现反弹球消砖块 一.效果展示: 二.代码如下: 一.效果展示: 二.代码如下: #include<stdio.h> #include<string.h> #inc ...

  4. C语言——反弹球消砖块

    目录 前言 一.小球代码的重构 函数 二. 实现游戏的边框 三.显示移动挡板 四.反弹小球 五.消除砖块 六.玩家输入 getchar() kbhit() 七.结尾--最后实现的代码 #define ...

  5. 初识C语言之函数封装篇——带你嗅探万花从中的清香(上)

    函 数 概 述 构成C程序的基本单元是函数.函数中包含程序的可执行代码. 每个C程序的入口和出口都位于main函数之中.编写程序时,并不是将所有的内容都放在主函数main中.为了方便规划.组织.编写和 ...

  6. 项目: 用函数实现反弹球消砖块

    目录 一.项目描述和最终的成果展示 二.封装后的弹跳小球 三.显示移动挡板 四.反弹小球 五.添加砖块并实现打砖块操作 一.项目描述和最终的成果展示 这是在上一次弹跳小项目上进行了一系列的优化和封装. ...

  7. 【C语言】反弹球消砖块游戏

    目录 0. 前期准备 1. 打印小球 2. 显示边界 3. 显示挡板 Tip one Tip two 4. 控制挡板

  8. C语言嵌套函数封装替换,函数可以嵌套定义但不能嵌套调用吗,嵌套函数

    函数可以嵌套定义但不能嵌套调用吗一个函数可以被定义为嵌套的,但不能被称为嵌套的吗,不对,函数可以嵌套调用,但不能嵌套定义.在C语言中,所有函数都是并行的,即定义函数时相互独立,一个函数不从属于另一个函 ...

  9. C语言基础入门48篇_20_函数入门:为什么使用函数?(函数使得程序模块化 使用函数封装细节,使得程序员只要面向函数的接口编程(参数与返回值),而不用关心函数内部细节)

    菜鸟和高手都会基本,那菜鸟为什么是菜鸟,高手为什么是高手呢?很大的区别在于,高手写的程序[结构性]更好,更容易维护.而函数就是C语言结构化的一种手段. 函数可以将大的计算任务划分为多个较小的任务(解耦 ...

  10. C语言使用 ASN.1对报文进行编解码(将c函数封装成类简化使用)

    文章目录 1.为什么要报文编解码 2.ASN.1是什么 3.使用函数介绍 4.对数据进行编解码 5.C语言使用 6.将上述函数封装成c++类 1.为什么要报文编解码 两台机器通信: 1.两台机器的操作 ...

最新文章

  1. 感知哈希算法——找出相似的图片
  2. 软件发布版本的业界规则?
  3. 中国新冠统计20200128-20200227 统计于网络发布数据 便于数据同比分析规律 公开透明 加强防范 减少恐慌 数学来加持
  4. git 硬回滚和软回滚
  5. 新生赛(2) problem 2 丁磊养猪
  6. JS获取字符串实际长度!
  7. kubeadm加载k8s镜像实现集群搭建
  8. python应用POP3、IMAP、SMTP 协议,获取邮箱验证码
  9. php显示动态通告信息方式,Joomla PHP通知,警告和错误指南
  10. 函数计算助力语雀构建稳定且安全的业务架构
  11. hive获取mysql里的文件_apache – 如何在hive中获取数据库用户名和密码
  12. 基于javaweb的物资配送管理系统_基于JAVA的物流配送管理系统毕业设计书
  13. 二进制、十进制和16进制对照表以及对应的字符
  14. PackageManager的基本使用
  15. csapp 大作业 hello的自白
  16. 离散选择模型(DCM)和深度神经网络(DNN)结合
  17. 电磁炉各主要元件名词,符号及功能解析
  18. WILDFLY + idea配置
  19. SQL语句实现关系代数中的“除法”
  20. 指尖轻舞桌面:Slide On Desk

热门文章

  1. 【python教程入门学习】拒绝反爬虫!教你搞定爬虫验证码
  2. GAN原理,优缺点、应用总结
  3. The puzzle
  4. Amlogic Linux系列(四) 视频解码分析2
  5. 3d建模做一单多少钱?做外包赚钱吗?
  6. Adversarial Attack的粗略总结
  7. 算法课设——逃狱的汉尼拔博士
  8. java中display中的属性_全面解析display属性
  9. 推荐两款很好用的听书APP
  10. 草莓网购物软件测试,网上商城系统的测试用例集