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

相关文章

GO语言短变量声明的实现示例

《GO语言短变量声明的实现示例》在Go语言中,短变量声明是一种简洁的变量声明方式,使用:=运算符,可以自动推断变量类型,下面就来具体介绍一下如何使用,感兴趣的可以了解一下... 目录基本语法功能特点与var的区别适用场景注意事项基本语法variableName := value功能特点1、自动类型推

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

Python进阶之列表推导式的10个核心技巧

《Python进阶之列表推导式的10个核心技巧》在Python编程中,列表推导式(ListComprehension)是提升代码效率的瑞士军刀,本文将通过真实场景案例,揭示列表推导式的进阶用法,希望对... 目录一、基础语法重构:理解推导式的底层逻辑二、嵌套循环:破解多维数据处理难题三、条件表达式:实现分支

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

Go语言使用Gin处理路由参数和查询参数

《Go语言使用Gin处理路由参数和查询参数》在WebAPI开发中,处理路由参数(PathParameter)和查询参数(QueryParameter)是非常常见的需求,下面我们就来看看Go语言... 目录一、路由参数 vs 查询参数二、Gin 获取路由参数和查询参数三、示例代码四、运行与测试1. 测试编程路

基于Python编写自动化邮件发送程序(进阶版)

《基于Python编写自动化邮件发送程序(进阶版)》在数字化时代,自动化邮件发送功能已成为企业和个人提升工作效率的重要工具,本文将使用Python编写一个简单的自动化邮件发送程序,希望对大家有所帮助... 目录理解SMTP协议基础配置开发环境构建邮件发送函数核心逻辑实现完整发送流程添加附件支持功能实现htm

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

Go语言网络故障诊断与调试技巧

《Go语言网络故障诊断与调试技巧》在分布式系统和微服务架构的浪潮中,网络编程成为系统性能和可靠性的核心支柱,从高并发的API服务到实时通信应用,网络的稳定性直接影响用户体验,本文面向熟悉Go基本语法和... 目录1. 引言2. Go 语言网络编程的优势与特色2.1 简洁高效的标准库2.2 强大的并发模型2.

Go语言使用sync.Mutex实现资源加锁

《Go语言使用sync.Mutex实现资源加锁》数据共享是一把双刃剑,Go语言为我们提供了sync.Mutex,一种最基础也是最常用的加锁方式,用于保证在任意时刻只有一个goroutine能访问共享... 目录一、什么是 Mutex二、为什么需要加锁三、实战案例:并发安全的计数器1. 未加锁示例(存在竞态)