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语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

Go语言中泄漏缓冲区的问题解决

《Go语言中泄漏缓冲区的问题解决》缓冲区是一种常见的数据结构,常被用于在不同的并发单元之间传递数据,然而,若缓冲区使用不当,就可能引发泄漏缓冲区问题,本文就来介绍一下问题的解决,感兴趣的可以了解一下... 目录引言泄漏缓冲区的基本概念代码示例:泄漏缓冲区的产生项目场景:Web 服务器中的请求缓冲场景描述代码

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个

Go语言中Recover机制的使用

《Go语言中Recover机制的使用》Go语言的recover机制通过defer函数捕获panic,实现异常恢复与程序稳定性,具有一定的参考价值,感兴趣的可以了解一下... 目录引言Recover 的基本概念基本代码示例简单的 Recover 示例嵌套函数中的 Recover项目场景中的应用Web 服务器中

Go语言中使用JWT进行身份验证的几种方式

《Go语言中使用JWT进行身份验证的几种方式》本文主要介绍了Go语言中使用JWT进行身份验证的几种方式,包括dgrijalva/jwt-go、golang-jwt/jwt、lestrrat-go/jw... 目录简介1. github.com/dgrijalva/jwt-go安装:使用示例:解释:2. gi

Go 语言中的 Struct Tag 的用法详解

《Go语言中的StructTag的用法详解》在Go语言中,结构体字段标签(StructTag)是一种用于给字段添加元信息(metadata)的机制,常用于序列化(如JSON、XML)、ORM映... 目录一、结构体标签的基本语法二、json:"token"的具体含义三、常见的标签格式变体四、使用示例五、使用