为一个游戏添加边框
#include<stdio.h>#include<stdlib.h>
#include<windows.h>
int main()
{
int i,j;
int x=0;
int y=5;
int velocity_y=1;
int velocity_x=1;
int left=0;
int right =20;
int top=0;
int bottom=10;
while(1)
{
x=x+velocity_x;
y=y+velocity_y;
system("cls"); //清屏函数
for(i=0;i<x;i++) //输出小球上面的空行
{
printf("\n");
}
for(j=0;j<y;j++) //输出小球左边的空格
{
printf(" ");
}
printf("o"); //输出小球o
printf("\n");
Sleep(50); //在输出小球后等待50毫秒
if((x==top)||(x==bottom)) //碰到上下边界要变化速度
{
velocity_x = -velocity_x;
printf("\a");
}
if((y==left)||(y==right))
{
velocity_y = -velocity_y;
printf("\a");
}
}
return 0;
}
代码如上,大家可在自己的编译器上跑一下,我的目的的为这个小球弹跳的边界加一个眼睛可以看到的边框,就打印*以表示边框吧。我自己写的边框就出现看一瞬间就消失了,不知道怎么搞,求助大家 在return前加一句
system("pause"); #include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int main()
{
int i, j;
int x = 0;
int y = 5;
int velocity_y = 1;
int velocity_x = 1;
int left = 0;
int right = 20;
int top = 0;
int bottom = 10;
while (1)
{
x = x + velocity_x;
y = y + velocity_y;
system("cls");//清屏函数
printf("***********************\n");//输出顶端边框
for (i = 0; i < x; i++)//输出小球上面的空行
{
printf("* *\n");//输出小球出现前的左右边框
}
printf("*");//输出小球出现行左边边框
for (j = 0; j < y; j++)//输出小球左边的空格
{
printf(" ");
}
printf("o");//输出小球o
for (j = y + 1; j <= 20; j++)//输出小球右边空格
{
printf(" ");
}
printf("*\n");//输出小球右边边框
for (i = x + 1; i <=10; i++)
{
printf("* *\n");//输出小球出现后的左右边框
}
printf("***********************\n");//输出底边框
Sleep(50);//在输出小球后等待50毫秒
if ((x == top) || (x == bottom))//碰到上下边界要变化速度
{
velocity_x = -velocity_x;
printf("\a");
}
if ((y == left) || (y == right))
{
velocity_y = -velocity_y;
printf("\a");
}
}
return 0;
}
随便试了下,大概这个样子,没有仔细考虑位置关系{:5_103:} 重新捡c过程中,忘的差不多了 BngThea 发表于 2017-11-19 21:01
在return前加一句
system("pause");
你好!你的方法我试了,不行耶。您可以参考您楼下那位的解答。很抱歉,鱼C论坛我以前不够重视,来的比较少 freefox213 发表于 2017-11-22 00:14
随便试了下,大概这个样子,没有仔细考虑位置关系 重新捡c过程中,忘的差不多了
首先抱歉额,以前鱼C论坛我不够重视,来的比较少。刚刚试了下您的改进,确实如我想要,谢谢!好人一声平安哈哈哈{:5_91:}
页:
[1]