C语言进阶版的反弹球消方块

2023-10-11 18:30
文章标签 语言 进阶 方块 反弹球

本文主要是介绍C语言进阶版的反弹球消方块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一 . 首先搭建好小球

要求小球半径为20 ,并且初速度为1 , 撞墙后能够进行反弹.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <graphics.h>#define high 480  // 游戏画面尺寸
#define width 640// 全局变量
int ball_x, ball_y; // 小球的坐标
int ball_vx, ball_vy; // 小球的速度
int ball_radius; //小球半径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 };  // 第二个值为0表示隐藏光标SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}void startup()  // 数据初始化
{HideCursor(); // 隐藏光标// 小球属性 ball_x = width / 2;ball_y = high / 2;ball_vx = 1;ball_vy = 1;ball_radius = 20; //初始化画笔initgraph(width , high);BeginBatchDraw(); // 开始批量绘画
}void clear()
{//清除小球setcolor(BLACK);setfillcolor(BLACK);fillcircle(ball_x, ball_y, ball_radius);
}void show()  // 显示画面
{gotoxy(0, 0);    // 光标移动到原点位置,以下重画清屏//画球setcolor(GREEN); //绿线setfillcolor(YELLOW);//黄边fillcircle(ball_x, ball_y, ball_radius);// 执行未完成的绘制任务FlushBatchDraw();Sleep(5);}void updateWithoutInput()  // 更新小球和减少方块
{//更新小球的位置ball_x += ball_vx;ball_y += ball_vy;//撞墙调整球的方向if ((ball_x - ball_radius) < 0 || (ball_x + ball_radius) > width)ball_vx = -ball_vx;if ((ball_y - ball_radius) < 0 || (ball_y + ball_radius) > high)ball_vy = -ball_vy;
}void updateWithInput()  // 与用户输入有关的更新
{}void gameOver() // 游戏结束处理
{// 关闭群刷EndBatchDraw();// 关闭画笔closegraph();
}int main()
{startup();  // 数据初始化	while (1)  //  游戏循环执行{clear(); // 清屏updateWithoutInput();  // 与用户输入无关的更新updateWithInput();     // 与用户输入有关的更新show();  // 显示画面}gameOver(); //游戏结束处理return 0;
}

运行以上代码可以实现该功能.

 

二 . 绘制出挡板的效果 

1. 先绘制出挡板

2.给挡板添加移动功能

3.给挡板添加限制功能

4.给挡板添加反弹球功能

5.要求挡板接到球可以反弹 ,没接到则游戏停止.

以上功能可以边调试边实现功能

 

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <graphics.h>#define high 480  // 游戏画面尺寸
#define width 640// 全局变量
int ball_x, ball_y; // 小球的坐标
int ball_vx, ball_vy; // 小球的速度
int ball_radius; //小球半径
int bar_x, bar_y; // 挡板的中心坐标
int bar_width, bar_high; // 挡板的高宽
int bar_top, bar_bottom, bar_left, bar_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 HideCursor() // 用于隐藏光标
{CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };  // 第二个值为0表示隐藏光标SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}void startup()  // 数据初始化
{HideCursor(); // 隐藏光标// 小球属性 ball_x = width / 2;ball_y = high / 2;ball_vx = 1;ball_vy = 1;ball_radius = 20; //挡板的属性bar_width = width / 6; bar_high = high / 20;bar_x = width / 2;bar_y = high - bar_high;bar_top = bar_y - bar_high / 2;bar_bottom = bar_y + bar_high / 2;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;//初始化画笔initgraph(width , high);BeginBatchDraw(); // 开始批量绘画
}void clear()
{//清除小球setcolor(BLACK);setfillcolor(BLACK);fillcircle(ball_x, ball_y, ball_radius);//擦除挡板移动的痕迹setcolor(BLACK);setfillcolor(BLACK);fillrectangle(bar_left, bar_top, bar_right, bar_bottom);
}void show()  // 显示画面
{gotoxy(0, 0);    // 光标移动到原点位置,以下重画清屏//画球setcolor(GREEN); //绿线setfillcolor(YELLOW);//黄边fillcircle(ball_x, ball_y, ball_radius);// 画 挡板setcolor(BLUE); setfillcolor(RED);fillrectangle(bar_left, bar_top, bar_right, bar_bottom);// 执行未完成的绘制任务FlushBatchDraw();Sleep(5);}void updateWithoutInput()  // 更新小球和减少方块
{//更新小球的位置ball_x += ball_vx;ball_y += ball_vy;//挡板接球if ( ((ball_y + ball_radius) >= bar_top && ball_x >= bar_left && ball_x <= bar_right )  )ball_vy = -ball_vy;//撞墙调整球的方向if ((ball_x - ball_radius) < 0 || (ball_x + ball_radius) > width)ball_vx = -ball_vx;if ((ball_y - ball_radius) < 0 || (ball_y + ball_radius) > high)ball_vy = -ball_vy;
}void updateWithInput()  // 与用户输入有关的更新
{//获取键位输入char input;if (_kbhit()){input = _getch();if (input == 'a' && bar_left >= 0){bar_x -= 15;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;}if (input == 'w' && bar_top >= 0){bar_y -= 15;bar_top = bar_y - bar_high / 2;bar_bottom = bar_y + bar_high / 2;}if (input == 'd' && bar_right <= width){bar_x += 15;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;}if (input == 's' && bar_bottom <= high){bar_y += 15;bar_top = bar_y - bar_high / 2;bar_bottom = bar_y + bar_high / 2;}}
}void gameOver() // 游戏结束处理
{// 关闭群刷EndBatchDraw();// 关闭画笔closegraph();
}int main()
{startup();  // 数据初始化	while (1)  //  游戏循环执行{clear(); // 清屏updateWithoutInput();  // 与用户输入无关的更新updateWithInput();     // 与用户输入有关的更新show();  // 显示画面}gameOver(); //游戏结束处理return 0;
}

第三步:实现小方块功能

1. 现实小方块

2.撞击则消失

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <graphics.h>#define high 480  // 游戏画面尺寸
#define width 640
#define block_num 16 //砖块数量16
// 全局变量
int ball_x, ball_y; // 小球的坐标
int ball_vx, ball_vy; // 小球的速度
int ball_radius; //小球半径
int bar_x, bar_y; // 挡板的中心坐标
int bar_width, bar_high; // 挡板的高宽
int bar_top, bar_bottom, bar_left, bar_right; // 挡板的上下左右
int block_width, block_high;  // 宽高
int isBlockExist[block_num]; // 判断砖块是否存在 , 1 为存在 , 0 为被撞击消失了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 };  // 第二个值为0表示隐藏光标SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}void startup()  // 数据初始化
{HideCursor(); // 隐藏光标// 小球属性 ball_x = width / 2;ball_y = high / 2;ball_vx = 1;ball_vy = 1;ball_radius = 20; //挡板的属性bar_width = width / 6; bar_high = high / 20;bar_x = width / 2;bar_y = high - bar_high;bar_top = bar_y - bar_high / 2;bar_bottom = bar_y + bar_high / 2;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;//方块属性block_high = high / block_num;block_width = width / block_num;for (int i = 0; i <= block_num; i++){isBlockExist[i] = 1;}//初始化画笔initgraph(width , high);BeginBatchDraw(); // 开始批量绘画
}void show()  // 显示画面
{gotoxy(0, 0);    // 光标移动到原点位置,以下重画清屏//画球setcolor(GREEN); //绿线setfillcolor(YELLOW);//黄边fillcircle(ball_x, ball_y, ball_radius);// 画 挡板setcolor(BLUE); setfillcolor(RED);fillrectangle(bar_left, bar_top, bar_right, bar_bottom);// 方块int block_left, block_top, block_right, block_bottom;for (int i = 0; i < block_num; i++){block_left = i * block_width;block_right = block_left + block_width;block_top = 0;block_bottom = block_high;if (isBlockExist[i] ){setcolor(DARKGRAY);setfillcolor(LIGHTGREEN);fillrectangle(block_left, block_top, block_right, block_bottom);}}// 执行未完成的绘制任务FlushBatchDraw();Sleep(5);}void clear()
{//清除小球setcolor(BLACK);setfillcolor(BLACK);fillcircle(ball_x, ball_y, ball_radius);//擦除挡板移动的痕迹fillrectangle(bar_left, bar_top, bar_right, bar_bottom);// 擦除方块int block_left, block_top, block_right, block_bottom;for (int i = 0; i < block_num; i++){block_left = i * block_width;block_right = block_left + block_width;block_top = 0;block_bottom = block_high;if ( !isBlockExist[i])fillrectangle(block_left, block_top, block_right, block_bottom);}
}void updateWithoutInput()  // 更新小球和减少方块
{//更新小球的位置ball_x += ball_vx;ball_y += ball_vy;//撞墙调整球的方向if ((ball_x - ball_radius) < 0 || (ball_x + ball_radius) > width)ball_vx = -ball_vx;if ((ball_y - ball_radius) < 0 || (ball_y + ball_radius) > high)ball_vy = -ball_vy;//挡板接球if ( ((ball_y + ball_radius) >= bar_top && ball_x >= bar_left && ball_x <= bar_right )  )ball_vy = -ball_vy;// 方块int block_left, block_top, block_right, block_bottom;for (int i = 0; i < block_num; i++){if (isBlockExist[i] ){block_left = i * block_width;block_right = block_left + block_width;block_top = 0;block_bottom = block_high;if (((ball_y - ball_radius) == block_bottom && (ball_x >= block_left) && (ball_x <= block_right))){isBlockExist[i] = 0;ball_vy = -ball_vy;}}}
}void updateWithInput()  // 与用户输入有关的更新
{//获取键位输入char input;if (_kbhit()){input = _getch();if (input == 'a' && bar_left >= 0){bar_x -= 15;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;}if (input == 'w' && bar_top >= 0){bar_y -= 15;bar_top = bar_y - bar_high / 2;bar_bottom = bar_y + bar_high / 2;}if (input == 'd' && bar_right <= width){bar_x += 15;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;}if (input == 's' && bar_bottom <= high){bar_y += 15;bar_top = bar_y - bar_high / 2;bar_bottom = bar_y + bar_high / 2;}}
}void gameOver() // 游戏结束处理
{// 关闭群刷EndBatchDraw();// 关闭画笔closegraph();
}int main()
{startup();  // 数据初始化	while (1)  //  游戏循环执行{clear(); // 清屏updateWithoutInput();  // 与用户输入无关的更新updateWithInput();     // 与用户输入有关的更新show();  // 显示画面}gameOver(); //游戏结束处理return 0;
}

本效果到此!!!!代码全贴出 , 需要的可以学习

这篇关于C语言进阶版的反弹球消方块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/190000

相关文章

C语言中位操作的实际应用举例

《C语言中位操作的实际应用举例》:本文主要介绍C语言中位操作的实际应用,总结了位操作的使用场景,并指出了需要注意的问题,如可读性、平台依赖性和溢出风险,文中通过代码介绍的非常详细,需要的朋友可以参... 目录1. 嵌入式系统与硬件寄存器操作2. 网络协议解析3. 图像处理与颜色编码4. 高效处理布尔标志集合

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

C语言中的数据类型强制转换

《C语言中的数据类型强制转换》:本文主要介绍C语言中的数据类型强制转换方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C语言数据类型强制转换自动转换强制转换类型总结C语言数据类型强制转换强制类型转换:是通过类型转换运算来实现的,主要的数据类型转换分为自动转换

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

C语言实现两个变量值交换的三种方式

《C语言实现两个变量值交换的三种方式》两个变量值的交换是编程中最常见的问题之一,以下将介绍三种变量的交换方式,其中第一种方式是最常用也是最实用的,后两种方式一般只在特殊限制下使用,需要的朋友可以参考下... 目录1.使用临时变量(推荐)2.相加和相减的方式(值较大时可能丢失数据)3.按位异或运算1.使用临时

使用C语言实现交换整数的奇数位和偶数位

《使用C语言实现交换整数的奇数位和偶数位》在C语言中,要交换一个整数的二进制位中的奇数位和偶数位,重点需要理解位操作,当我们谈论二进制位的奇数位和偶数位时,我们是指从右到左数的位置,本文给大家介绍了使... 目录一、问题描述二、解决思路三、函数实现四、宏实现五、总结一、问题描述使用C语言代码实现:将一个整