1:自由落体的小鸟
做游戏的第一步当然要输出主要控制对象,即小鸟,学了之前的弹球游戏我们可以很轻松的写出来一个下落的小鸟,无非就是进行坐标间的判断,以及纵坐标的递增,保证效果上等同于小鸟的下落。
int x,y; //x,y分别表示小鸟的横纵坐标,全局变量
int high=10,width=20;
void show()
{
for(int i=0;i<high;i++)
{
for(int j=0;j<width;j++)
{
if((x==j)&&(i==y)) //判断该坐标值是符合,是小鸟的坐标时输出@,不是时输出空格
printf("@");
else
printf(" ");
}
printf("\n");
}
}
void update()
{
y++;
}
这就是包含了屏幕输出和小鸟下落的函数部分。完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
int high,width;
int bird_x,bird_y;
int bar1_x,bar1_ytop,bar1_ydown;
void gotoxy(int x,int y) //光标移动到x,y处
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
void startup()
{
high=15;
width=20;
bird_y=0;
bird_x=width/3;
}
void show()
{
gotoxy(0,0);
int i,j;
for(i=0;i<high;i++)
{
for(j=0;j<width;j++)
{
if((i==bird_y)&&(j==bird_x))
printf("@");
else
printf(" ");
}
printf("\n");
}
}
void updatewithoutinput()
{
bird_y++;
Sleep(150);
}
void updatewithinput()
{
}
int main()
{
startup();
while(1)
{
show();
updatewithoutinput();
updatewithinput();
}
return 0;
}
输出效果如图:
2:飞鸟的飞行
所谓飞鸟当然要不能只会自由落体,那么如何实现飞行呢。我们知道,该物体的下落是纵坐标y的增加,那么我们就可以通过键盘输入一个字符或空格,接收到指令后使小鸟的坐标减去2(数字无所谓,看你想让小鸟飞多高)。那么小鸟的位置就升高了两格。
首先就是接收键盘的功能,在上面代码的基础上再updatewithinput()函数中增加了接受机制:
void updatewithinput()
{
char input;
if(kbhit())
{
input=getch();
if(input==' ')
bird_y=bird_y-2; //小鸟高度升高2格
}
}
当我们输入空格时,y坐标-2得到了新坐标继续输出。(这一步显示的障碍物在下一步)
3:显示障碍物
显示障碍物无非就是断了口子的一条符号墙,判定机制也是输出的时候看看坐标是否符合,符合的话输出 * ,不符合判断其他的。当然还要增加有关围墙的全局变量。
void show()
{
gotoxy(0,0);
int i,j;
for(i=0;i<high;i++)
{
for(j=0;j<width;j++)
{
if((i==bird_y)&&(j==bird_x))
printf("@");
else if((j==bar1_x)&&((i<bar1_ydown)||(i>bar1_ytop)))
printf("*");
else
printf(" ");
}
printf("\n");
}
}
输出结果如上图所示。
4:移动的障碍物
这一步与上一步有异曲同工之妙,上一步判定了障碍物并输出,这一步同理,不过墙的横坐标随时间变化,也就形成了移动,就差不多等同于小鸟的下落,方向变了一下而已。
void updatewithoutinput()
{
bird_y++;
Sleep(150);
bar1_x--; //围墙移动
}
啊对,就是这么简单。
5:判断是否碰撞
判断是否碰撞就可以看坐标之间的大小,如当小鸟的 x 坐标等于移动的墙壁的 x 坐标时,判断小鸟的 y 是否 > 下面墙的最上面或者 < 上面墙的最下面即可判断为小鸟从中间传过去。
void updatewithoutinput()
{
bird_y++;
bar1_x--;
if(bird_x==bar1_x)
{
if((bird_y>=bar1_ydown)&&(bird_x<=bar1_ytop))
score++;
else
printf("游戏失败\n");
system("pause");
exit(0);
}
Sleep(150);
}
此时还增加了一个加分的变量,当传过去的时候score+1,视为得一分。
6:循环生成障碍物
机理为当墙壁的 x 坐标==0时,重置或者增加墙壁的 x 坐标即可。
void updatewithoutinput()
{
bird_y++;
bar1_x--;
if(bird_x==bar1_x)
{
if((bird_y>=bar1_ydown)&&(bird_x<=bar1_ytop))
score++;
else
printf("游戏失败\n");
system("pause");
exit(0);
}
if(bar1_x<=0)
{
bar1_x=width;
int temp=rand()%int (high*0.8);
bar1_ydown=temp-high/10;
bar1_ytop=temp+high/10;
}
Sleep(150);
}
这样一个小游戏就好了。最后的动态图就不上了,参数设置有问题要么就是我太菜,最多得了 1 分。
完整的附上:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
int high,width;
int bird_x,bird_y;
int bar1_x,bar1_ytop,bar1_ydown;
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=15;
width=45;
bird_y=0;
bird_x=width/4;
bar1_x=width/2;
bar1_ydown=high/4;
bar1_ytop=high/2;
score=0;
}
void show()
{
gotoxy(0,0);
int i,j;
for(i=0;i<high;i++)
{
for(j=0;j<width;j++)
{
if((i==bird_y)&&(j==bird_x))
printf("@");
else if((j==bar1_x)&&((i<bar1_ydown)||(i>bar1_ytop)))
printf("*");
else
printf(" ");
}
printf("\n");
}
printf("得分:%d\n",score);
}
void updatewithoutinput()
{
bird_y++;
bar1_x--;
if(bird_x==bar1_x)
{
if((bird_y>=bar1_ydown)&&(bird_x<=bar1_ytop))
score++;
else
printf("游戏失败\n");
system("pause");
exit(0);
}
if(bar1_x<=0)
{
bar1_x=width;
int temp=rand()%int (high*0.8);
bar1_ydown=temp-high/10;
bar1_ytop=temp+high/10;
}
Sleep(150);
}
void updatewithinput()
{
char input;
if(kbhit())
{
input=getch();
if(input==' ')
bird_y=bird_y-2;
}
}
int main()
{
startup();
while(1)
{
show();
updatewithoutinput();
updatewithinput();
}
return 0;
}
完。。。。