c语⾔字符怎么打出⼩⽅块,C语⾔写简单的⼩游戏-挡板接⼩⽅
#include //清屏,⽤于代替系统的"cls"清屏功能
void gotoxy(int x , int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
/
/隐藏光标
void HideCursor()
{
CONSOLE_CURSOR_INFO concur_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&concur_info);
}
//作为全局变量进⾏定义
int width,high;//作为画布的⼤⼩
int pos_x,pos_y; //作为滑板的定位
int radius; //挡板的半径
int left,right; //挡板的左右边界
int ball_x,ball_y; //作为球的定位
int ball_vx,ball_vy; //作为球的运动速度
int ball_num; //积分
int target_x,target_y; //作为⽬标的定位
void init() //画⾯数据初始化
{
c语言编程小游戏width = 40;
high = 42;
ball_x = 1;
ball_y = width/2;
ball_vx =1;
ball_vy =1;
ball_num = 0;
pos_y = width/2;
pos_x = high;
radius = 2;
left = pos_y - radius;
right = pos_y + radius;
HideCursor(); //隐藏光标
}
void show() //画⾯展⽰
{
gotoxy(0,0); //⾃定义的清屏函数,⽤于代替系统的“cls”int i,j;
for( i= 0;i <= high ; i++)
{
for (j = 0; j <= width ; j++)
{
if((ball_x ==i) && (ball_y == j))
printf("o");
else if (j == width)
printf("|");
else if(i == high)
printf("-");
else if ((i == high-1) && (j>=left) && (j<=right))
printf("*");
else
printf(" ");
}
printf("\n");
//Sleep(20);
}
printf("反弹总分:%d \n",ball_num);
}
void userInput() //⽤户输⼊
char input;
if(kbhit())
{
input =getch();
if(input =='a')
{
pos_y--;
left = pos_y - radius;
right = pos_y + radius;
}
if(input =='d')
{
pos_y++;
left = pos_y - radius;
right = pos_y + radius;
}
}
}
void userWithOut() //游戏限制和规则{
if(ball_x == high-1)
{
if ((ball_y>= left) && (ball_y<=right)) {
ball_num++;
//ball_vy = -ball_vy;
ball_vx = -ball_vx;
}
else
{
printf("Game Over");
exit(0);
}
//球根据球速的变化,更新球的位置
ball_x = ball_x +ball_vx; //倾斜弹球d ball_y = ball_y +ball_vy;
/
/挡板不能国过界限
if(pos_y < 0)
pos_y = 0;
else if(pos_y>width)
pos_y = width;
//碰到了球的边界,则反向
if((ball_x == 0) || (ball_x == high-1)) ball_vx = -ball_vx;
if((ball_y == 0) || (ball_y == width-1)) ball_vy = -ball_vy;
}
int main()
{
/
/初始化
init();
while(1)
{
//显⽰画⾯,绘图
show();
//⽤户输⼊
userInput();
//与⽤户⽆关
userWithOut();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。