【四、像素鸟】

在进入更复杂的学习之前,我们最后实现一个小游戏——像素鸟。

下落的小鸟

首先我们写好游戏代码框架并实现小鸟下落和上升(按空格)的功能:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>

//全局变量
int high,width;	//画面尺寸
int bird_x,bird_y;	//小鸟坐标
int barl_y,barl_xTop,barl_xDowm;	//障碍物

void gotoxy(int x, int y)	//移动光标便于清屏重画
{
	HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
	CROOD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void startup()		//数据初始化
{
	high = 15;
	width = 20;
	bird_x = 0;
	bird_y = 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_x)&&(j==bird_y))
				printf("@");
			else
				printf(" ");
		}
		print("\n");	//每经一次行循环就换行
	}
}

void updateWithoutInput()
{
	bird_x ++;
	sleep(150);
}

void updateWithInput()
{
	char input;
	if(kbhit())	//判断是否有输入
	{
		input = getch();
		if(input==' ')
			bird_x = bird_x - 2;
	}
}

int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

显示障碍物

我们在上一步的基础上完成障碍物的绘制,使用全局变量barl_y, barl_xTop, barl_xDown描述相关量,如图:
障碍物相关量表示
加入以下代码片段:

  • 数据初始化
barl_y = width/2;
barl_xDown = high/2;
barl_xTop = high/3;
  • 输出循环
...
else if ((j==barl_y)&&((i>barl_xDown)||(i<barl_xTop)))
	printf("*");
...
  • 障碍物移动(在updateWithoutInput里)
barl_y --;

判定碰撞

接下来判定当障碍物 y 坐标到达小鸟位置时是否有碰撞发生,若有,则游戏失败,反之则得分加一。

加入如下代码段:

int score; //全局变量,得分

void startup()
{
	...
	score = 0;	
	...
}

void updateWithoutInput()
{
	...
	if(bird_y == barl_y)
	{
		if((bird_x>=barl_xTop)&&(bird_x<=barl_xDown))
			score ++;
		else 
		{
			printf("GG\n");
			system("pause");
			exit(0);
		}
	}
	...
}

循环障碍物

到这里我们就要是障碍物循环出现,因为不管怎么样也不应该只有一个障碍物吧!同时,在此还将利用 rand() 随机障碍物空隙坐标。

加入以下代码段:

if(barl_y <= 0)
{
	barl_y = width;
	int temp =rand() % int(high * 0.8);	//使用临时变量储存障碍物坐标信息
	barl_xDown = temp + high/10;
	barl_xTop = temp - high/10;
}

这里对临时变量加减高度除以十的操作是为了防止生成新障碍物的空隙在太过边缘的位置

小结

完整代码如下(我是打字机器):

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>

//全局变量
int high,width;	//画面尺寸
int bird_x,bird_y;	//小鸟坐标
int barl_y,barl_xTop,barl_xDowm;	//障碍物
int score; 		//得分

void gotoxy(int x, int y)	//移动光标便于清屏重画
{
	HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
	CROOD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}

void startup()		//数据初始化
{
	high = 15;
	width = 20;
	score = 0;	
	bird_x = 0;
	bird_y = width/3;
	barl_y = width/2;
	barl_xDown = high/2;
	barl_xTop = high/3;
}

void show()		//显示画面
{
	gotoxy(0,0);
	int i,j;

	for(i=0; i<high; i++)
	{
		for(j=0; j<width; j++)
		{
			if((i==bird_x)&&(j==bird_y))
				printf("@");
			else if ((j==barl_y)&&((i>barl_xDown)||(i<barl_xTop)))
				printf("*");
			else
				printf(" ");
		}
		print("\n");	//每经一次行循环就换行
	}
}

void updateWithoutInput()
{
	bird_x ++;
	barl_y --;
	if(bird_y == barl_y)
	{
		if((bird_x>=barl_xTop)&&(bird_x<=barl_xDown))
			score ++;
		else 
		{
			printf("GG\n");
			system("pause");
			exit(0);
		}
	}
	if(barl_y <= 0)
	{
		barl_y = width;
		int temp =rand() % int(high * 0.8);	//使用临时变量储存障碍物坐标信息
		barl_xDown = temp + high/10;
		barl_xTop = temp - high/10;
	}
	sleep(150);
}

void updateWithInput()
{
	char input;
	if(kbhit())	//判断是否有输入
	{
		input = getch();
		if(input==' ')
			bird_x = bird_x - 2;
	}
}

int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

对没错我又想起CSDN要更的事了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值