1 Star 0 Fork 0

LeeMayZ / Leemayproject

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
rebound ball.cpp 2.43 KB
一键复制 编辑 原始数据 按行查看 历史
LeeMayZ 提交于 2019-05-25 17:18 . 增加了一些些注释
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<windows.h>
int high,width; //框的长和宽
int ball_x,ball_y; // 小球的位置
int ball_vx,ball_vy;
int position_x,position_y; //挡板的中心坐标
int ridus; // 挡板的半径
int left,right; //挡板到左右边框的距离
int ball_number;
int block_x,block_y; //砖块的坐标
int score;
void gotoxy(int x,int y) //光标定义函数
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
void startup()
{
high=20;
width=25;
ball_x=0;
ball_y=width/2;
ball_vx=1;
ball_vy=1;
ridus=6;
position_x = high;
position_y = width/2;
left=position_y - ridus;
right=position_y + ridus;
ball_number = 0;
block_x=0;
block_y=width/2+1;
score=0; // 计算消掉砖块数
}
void show()
{
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(j==width)
printf("|");
else if(i==high+1)
printf("-");
else if((i==high) && (j>left) && (j<right))
printf("#");
else if((i==block_x)&&(j==block_y))
printf("B");
else
printf(" ");
}
printf("\n");
}
printf("反弹小球数:%d\n",ball_number);
printf("消掉的砖块数:%d\n",score);
}
void updataWithoutInput()
{
if(ball_x == high-1)
{
if((ball_y>=left)&&(ball_y<=right))
{
ball_number++;
printf("\a"); //响铃符,系统自带扬声器发出声音
}
else
{
printf("游戏失败\n");
system("pause");
exit(0);
}
}
if((ball_x==block_x) && (ball_y==block_y))
{
score++;
block_y=rand() % width; //rand()产生随机数的随机函数 (伪随机数)
}
ball_x = ball_x + ball_vx;
ball_y = ball_y + ball_vy;
if((ball_x==0)||(ball_x==high-1))
ball_vx = -ball_vx;
if((ball_y==0)||(ball_y==width-1))
ball_vy = -ball_vy;
Sleep(80); //执行挂起一段时间,即让函数滞留0.08秒(小球移动的速度),其单位是毫秒
}
void updataWithInput()
{
char input;
if(kbhit()) //检查当前是否有键盘输入,有则返回非0数,无则返回0
{
input = getch(); //从键盘获取一个字符,然后继续;
if(input == 'a')
{
position_y--;
left = position_y-ridus;
right = position_y+ridus;
}
if(input == 'd')
{
position_y++;
left = position_y-ridus;
right = position_y+ridus;
}
}
}
int main()
{
startup();
while(1)
{
show();
updataWithoutInput();
updataWithInput();
}
return 0;
}
C
1
https://gitee.com/LeeMayZ/Leemayproject.git
git@gitee.com:LeeMayZ/Leemayproject.git
LeeMayZ
Leemayproject
Leemayproject
master

搜索帮助