本文主要是介绍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语言进阶版的反弹球消方块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!