使用请点赞!
按一下空格动一下、无操控的
(代码来源:《C语言课程设计与游戏开发实践教程》童晶,丁海军,金永霞,周小芹 著)
运行截图
C语言代码:(后缀名请保存为.c)
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int main(){
int i,j,k;//计数器
int x=5;//初始距离最上的高度
int y=10;//初始距离最左的距离
int velocity_x = 1;//垂直速度矢量
int velocity_y = 1;//水平速度矢量
//高和宽
int high = 20;
int wide = 30;
//边界
int left = 1;
int right = left + wide;
int top = 1;
int bottom = top + high;
int test[5]={0,0,0,0,0};
test[0]=right+1;
while(1){
system("cls");//清屏
//输出上边界 right+1次
for(i=0;i<right+1;i++)
printf("%d",(1+i)%10);
printf("\n");
//输出小球上面的空行 x次
for(i=1;i<=high;i++){
if(i!=x){//小球上面的行 和 下面的行
for(k=0;k<right+1;k++){
if(k==0)printf("%d",(1+i)%10);//每行的左边界
else if(k==right)printf("%d",(1+i)%10);//每行的右边界
else printf(" ");
}
printf("\n");
}
else{//小球所在的行
printf("%d",(1+i)%10);//左边界
//输出y-1次空格 即小球左边的空格
for(int n=0;n<y-1;n++)
printf(" ");
printf("o");
//输出wide-y+1-1次空格 即小球右边的空格
for(int n=0;n<wide-y;n++)
printf(" ");
printf("%d",(1+i)%10);//右边界
printf("\n");
}
}
//输出下边界
for(i=0;i<right+1;i++)
printf("%d",(1+i)%10);
printf("\n");
//输出参数信息
test[1]=x-1;//小球上方空行
test[2]=high-x;
test[3]=x;
test[4]=y;
printf("\n上下边界长:%-3d\n",test[0]);
printf("上方空行:%-3d\n",test[1]);
printf("下方空行:%-3d\n",test[2]);
printf("x:%-3d\n",test[3]);
printf("y:%-3d\n",test[4]);
printf("高:%-3d宽:%-2d\n",high,wide);
printf("上边界:%-3d左边界:%d\n",1,1);
printf("下边界:%-3d右边界:%d\n",high+2,wide+2);
printf("velocity_x:%d\n",velocity_x);
printf("velocity_y:%d\n",velocity_y);
printf("\n\n");
//上下弹跳
if(x==top || x==high){//上下反弹
velocity_x = -velocity_x;
}
if(y==left || y==wide){//左右反弹
velocity_y = -velocity_y;
}
system("pause");
x = x + velocity_x;
y = y + velocity_y;
// Sleep(50);
}
return 0;
}
使用请点赞!