这篇文章主要为大家详细介绍了C语言实现简单反弹球消砖块游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
反弹球消砖块,是一款很简单的打砖块游戏,控制你的挡板挡住弹球,打掉上面的砖块,本篇博客中,主要使用printf与scanf函数实现消砖块游戏
整体思路
主函数
int main()
{
startup();//初始化
while (1)
{
show();//显示画面
updateWitoutIput();//与用户输入无关的更新 //更新数据
updateWithInput(); //与用户输入有关的更新 //输入
}
return 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 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
全局变量的定义
#define HIGH 20 //游戏界面高度
#define WIDTH 30 // 游戏界面宽度
#define V 1 //小球的速度
#define RIDUS 5 //挡板的半径
int ball_x, ball_y; //小球的坐标
int ball_vx, ball_vy; //小球的速度
int position_x, position_y; //挡板的中心位置
int left, right; //挡板的左右位置
int ball_number; //反弹小球的次数
int block_x, block_y; //砖块的坐标
int score; //消除砖块的个数
数据的初始化
//初始化小球
ball_x = HIGH / 2; //高度
ball_y = WIDTH / 2; //宽度
ball_vx = V; //小球的纵向速度
ball_vy = V; //小球的横向速度
//初始化挡板
position_x = HIGH / 2;
position_y = WIDTH / 2; //挡板的中心位置
left = position_y - RIDUS;//挡板的左位置
right = position_y + RIDUS; //挡板的右位置
//初始化砖块
block_x = 0; //高度
block_y = WIDTH / 2; //宽度
score = 0;
显示画面
循环不断输出游戏的界面, 0 表示小球, B 表示砖块, * 表示挡板
for (i = 0; i < HIGH + 1; i++) //行
{
for (j = 0; j < WIDTH; j++) //列
{
if (i == ball_x && j == ball_y)//输出小球
{
printf("0");
}
else if (i == block_x && j == block_y)//输出砖块
{
printf("B");
}
else if (i == HIGH) //下边界 -1,否则循环走不到high和width
{
printf("-");
}
else if (j == WIDTH - 1) //右边界
{
printf("|");
}
else if (i == HIGH - 1 && j >= left && j <= right) //输出挡板
{
printf("*");
}
else
{
printf(" ");
}
}
}
printf("反弹次数:%d\n", ball_number);
printf("消除砖块的个数:%d\n", score);
与用户无关的更新
判断游戏胜负
if (ball_x == HIGH - 2) //到达挡板上方
{
if (ball_y >= left && ball_y <= right) //被挡板挡住
{
ball_number++;
}
else //没被挡板挡住
{
printf("游戏失败!\n");
exit(0); //直接结束
}
}
消除砖块得分
if (ball_x == block_x && ball_y == block_y)
{
score++;
block_y = rand() % WIDTH;
}
改变小球的位置
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
//为了使小球在反弹和结束时能够真缺判断,所以小球下反弹边界应该在挡板的前一行,小球右反弹边界应该在右边界的前一列
if (ball_x >= HIGH - 2 || ball_x <= 0) //遇到任意一个边界即发生变化
ball_vx = -ball_vx;
if (ball_y >= WIDTH -2 || ball_y <= 0) //-1保证下球再使小球遇到边界时反弹但不会消失
ball_vy = -ball_vy;
与用户输入有关的更新
input = _getch();
if (input == 'a')
position_y--;
if (input == 'd')
position_y++;
left = position_y - RIDUS;//挡板的左位置
right = position_y + RIDUS; //挡板的右位置
完整代码
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
//全局变量的定义
#define HIGH 20 //游戏界面高度
#define WIDTH 30 // 游戏界面宽度
#define V 1 //小球的速度
#define RIDUS 5 //挡板的半径
int ball_x, ball_y; //小球的坐标
int ball_vx, ball_vy; //小球的速度
int position_x, position_y; //挡板的中心位置
int left, right; //挡板的左右位置
int ball_number; //反弹小球的次数
int block_x, block_y; //砖块的坐标
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()//数据的初始化
{
//初始化小球
ball_x = HIGH / 2; //高度
ball_y = WIDTH / 2; //宽度
ball_vx = V; //小球的纵向速度
ball_vy = V; //小球的横向速度
//初始化挡板
position_x = HIGH / 2;
position_y = WIDTH / 2; //挡板的中心位置
left = position_y - RIDUS;//挡板的左位置
right = position_y + RIDUS; //挡板的右位置
//初始化砖块
block_x = 0; //高度
block_y = WIDTH / 2; //宽度
score = 0;
}
void show()//显示画面
{
//system("cls");
gotoxy(0, 0);
int i, j;
for (i = 0; i < HIGH + 1; i++)
{
for (j = 0; j < WIDTH; j++)
{
if (i == ball_x && j == ball_y)
{
printf("0");
}
else if (i == block_x && j == block_y)
{
printf("B");
}
else if (i == HIGH)
{
printf("-");
}
else if (j == WIDTH - 1)
{
printf("|");
}
else if (i == HIGH - 1 && j >= left && j <= right)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
printf("反弹次数:%d\n", ball_number);
printf("消除砖块的个数:%d\n", score);
}
void updateWitoutIput()
{
if (ball_x == HIGH - 2)
{
if (ball_y >= left && ball_y <= right)
{
ball_number++;
}
else
{
printf("游戏失败!\n");
exit(0);
}
}
if (ball_x == block_x && ball_y == block_y)
{
score++;
block_y = rand() % WIDTH;
}
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if (ball_x >= HIGH - 2 || ball_x <= 0)
ball_vx = -ball_vx;
if (ball_y >= WIDTH -2 || ball_y <= 0)
ball_vy = -ball_vy;
Sleep(150); //睡眠时间
}
void updateWithInput()//与用户输入有关的更新
{
char input;
if (_kbhit())
{
input = _getch();
if (input == 'a')
position_y--;
if (input == 'd')
position_y++;
left = position_y - RIDUS;
right = position_y + RIDUS;
}
}
int main()
{
srand((unsigned)time(NULL));
startup();//初始化
HideCursor();
while (1)
{
show();//显示画面
updateWitoutIput();//与用户输入无关的更新
updateWithInput(); //与用户输入有关的更新
}
return 0;
}
效果演示
总结
虽然完成了一个简单的消砖块游戏,但是很多的功能都未能实现,如砖块的个数、游戏暂停和结束后的开始等等,任需要继续的努力。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:C语言实现简单反弹球消砖块游戏


猜你喜欢
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- C语言详解float类型在内存中的存储方式 2023-03-27
- C++ 数据结构超详细讲解顺序表 2023-03-25
- ubuntu下C/C++获取剩余内存 2023-09-18
- Easyx实现扫雷游戏 2023-02-06
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- C语言qsort()函数的使用方法详解 2023-04-26
- Qt计时器使用方法详解 2023-05-30
- C语言手把手带你掌握带头双向循环链表 2023-04-03