本文为大家分享了C语言反弹球游戏的具体代码,供大家参考,具体内容如下
这是利用函数写的C语言小游戏,用来检验自己的学习成果
反弹球的实现主要有几个子函数组成
问题也在于如何实现小球的下落,以及碰撞得分等情况
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
#include<stdio.h>
#include<windows.h>
#include<conio.h>
//定义全局变量
int high,width; //游戏边界
int ball_x,ball_y; //小球位置
int ball_vx,ball_vy; //小球速度
int position_x,position_y; //挡板中心坐标
int radius; //挡板半径
int left,right; //键盘左右边界
int ball_number; //反弹小球次数
int block_x,block_y; //方块的位置
int score; //消掉方块的个数
void HideCursor() //隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
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 startup() //数据初始化
{
high=18; //定义边界
width=26;
ball_x=0; //小球坐标
ball_y=width/2;
ball_vx=1; //小球速度方向
ball_vy=1;
position_x=high-1; //挡板中心坐标
position_y=width/2;
radius=5; //挡板半径
left=position_y-radius; //键盘边界
right=position_y+radius;
block_x=0; //方块位置
block_y=width/2-4;
ball_number=0; //反弹小球个数
score=0; //消掉小球个数
HideCursor();
}
void show() //显示界面
{
gotoxy(0,0);
int i,j;
for(i=0;i<=high;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("@");
else if(i==high) //输出下边界
printf("-");
else if(j==width) //输出右边界
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 updateWithoutInpute() //与用户输入无关的更新
{
if(ball_x==position_x-1) //小球撞到挡板
{
if((ball_y>=left)&&(ball_y<=right))
{
ball_number++;
//printf("\a");
}
else
{
printf("游戏失败\n");
system("pause");
exit(0);
}
}
ball_x = ball_x + ball_vx; //小球向速度方向移动
ball_y = ball_y + ball_vy;
if((ball_x==0) || (ball_x==high-2)) //小球撞到上下边界
ball_vx=-ball_vx;
if((ball_y==0) || (ball_y==width-1)) //小球撞到左右边界
ball_vy=-ball_vy;
if((block_x==ball_x) && (block_y==ball_y)) //小球撞到滑块
{
block_y=rand()%width-1;
score++;
}
Sleep(120);
}
void updateWithInpute() //与用户输入有关的更新
{
char input;
if(kbhit())
{
input=getch();
if((input=='a')&&(left>=0))
{
position_y--;
left=position_y-radius; //键盘边界
right=position_y+radius;
}
if((input=='d')&&(right<width))
{
position_y++;
left=position_y-radius; //键盘边界
right=position_y+radius;
}
}
}
int main()
{
system("color 2f"); //改变控制台颜色
startup();
while(1)
{
show(); //显示界面
updateWithoutInpute(); //与用户输入无关的更新
updateWithInpute(); //与用户输入有关的更新
}
}
|
小编之前也收藏了一段代码:C语言实现小球反弹,分享给大家
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void ball()//1.画出小球
{
printf("\t\t\t◎");
}
int main()
{
int h=20;//球的高度初始化为20
int i,j;//i是用来确定球的起点与终点,j是确定球的位置
int der=1;//判断等于1时球下落,为0时球上升
while(h>0)//高度大于0时,球都在动(当高度为0时停止)
{
if(der==1)
{
for(i=20-h;i<20;i++)//确定起点和终点 下落过程
{
system("cls");
for(j=0;j<=i;j++)//确定球的位置
{
printf("\n");
}
ball();
Sleep(50);
}
der=0;
}
else
{
h=h*8/9;//强起来高度是原来的9分之8
for(i=20;i>=20-h;i--)//确定起点和终点 上升过程
{
system("cls");
for(j=0;j<=i;j++)//确定球的位置
{
printf("\n");
}
ball();
Sleep(50);
}
der=1;
}
}
return 0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_40685101/article/details/80238642








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。