用C语言怎么做小球弹跳游戏
1、设置游戏界面:
做游戏界面大家先把头函数确定:#include <stdio.h>#include <Windows.h>。
然后在main函数上面自己设定一个输出字符串的函数:
void drawstring(char ch,int n){ int i=0; for(i=0;i<n;i++) { printf("%c",ch); }}

2、设置小球具体在哪里弹跳函数:
也就是在游戏界面内的定位显示:
void gotoxy(int y,int x){ COORD scrn; HANDLE hOuput=GetStdHandle(STD_OUTPUT_HANDLE); scrn.X=x; scrn.Y=y; SetConsoleCursorPosition(hOuput,scrn); }这个函数也是放在mian函数上面的。

3、让小球开始弹跳:
这个函数得放在main函数里面。
gotoxy(y,x);printf("O\n"); Sleep(200); gotoxy(y,x);printf(" \n");。这几行代码就是让小球弹跳起来,但是得用一个while循坏将他们括起来,然后才会实现一直弹跳。

4、 x=x+xSpeed; y=y+ySpeed; if(x>=78||x<=1) { xSpeed=-xSpeed; } if(y>=20||y<=1) { ySpeed=-ySpeed; }
这几行代码就是让小球弹跳过程中位置不断变化,且当碰壁得时候沿着他的反方向弹回。(这个函数也是放在main函数里面的)

5、最后就将所有的代码给大家方便大家参考:
#include <stdio.h>#include <Windows.h>
void gotoxy(int y,int x){ COORD scrn; HANDLE hOuput=GetStdHandle(STD_OUTPUT_HANDLE); scrn.X=x; scrn.Y=y; SetConsoleCursorPosition(hOuput,scrn); }void drawstring(char ch,int n){ int i=0; for(i=0;i<n;i++) { printf("%c",ch); }}void drawinterface(){ int width=80; int height=22; int i=0; drawstring('*',width); printf("\n"); for(i=0;i<height-2;i++) { printf("*"); drawstring(' ',width-2); printf("*\n"); } drawstring('*',width); printf("\n"); }main(){ drawinterface(); int x=20; int y=10; int xSpeed=1; int ySpeed=1; while(1) { gotoxy(y,x);printf("O\n"); Sleep(200); gotoxy(y,x);printf(" \n"); x=x+xSpeed; y=y+ySpeed; if(x>=78||x<=1) { xSpeed=-xSpeed; } if(y>=20||y<=1) { ySpeed=-ySpeed; } }
}

