代码实现最简单的游戏————空中飞机

以下代码简单的实现了:
1,控制飞机的移动
2,飞机发射子弹
3,敌机的生成与移动
4,随着分数的增大,地敌机的移动速度加快
5,防止飞机越界
6,实现游戏的暂停和结束

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


int position_x, position_y;//飞机位置
int bullet_x, bullet_y;//子弹位置
int enemy_x, enemy_y;//敌人位置
int high, width;//游戏地图的尺寸
int score;//得分
int velocity = 35;//速度

void HideCursor()//隐藏光标的函数
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};//第二个值为零表示隐藏光标,如果好奇的伙伴把0变为1,就会发现光标在狂闪
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()//初始化游戏数据
{
	high = 20;//初始化地图
	width = 30;
	position_x = high / 2;//初始化飞机的位置
	position_y = width / 2;
	bullet_x = 0;//子弹的位置
	bullet_y = position_y;//因为子弹在飞机头的上方所以横坐标相同
	enemy_x = 0;//敌机的位置
	enemy_y = position_y;
}

void gotoxy(int x, int y)//将光标移动到(x,y)的位置,进行从新画图,相当与代替了system("cls");避免了画面狂闪。
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);

}

void show()//显示画面
{
	gotoxy(0, 0);//光标移动到原点
	int i, j;
	for (i = 0; i < high; i++)
	{
		for (j = 0; j < width; j++)
		{
			if ((i == position_x) && (j == position_y))

				printf("*");//输出飞机
			else if ((i == position_x + 1) && (j == position_y - 2))
				printf("*****");
			else if ((i == position_x + 2) && (j == position_y - 2))
				printf(" * * ");
			else if ((i == enemy_x) && (j == enemy_y))
				printf("@");//输出敌机
			else if ((i == bullet_x) && (j == bullet_y))
				printf("|");//输出子弹
			else
				printf(" ");//输出空格
		}
		printf("\n");
	}
	printf("得分:%d", score);

}

void updateWithoutInput() //与用户输入无关的数据
{
	if (bullet_x - 1)//子弹的移动
		bullet_x--;
	
	if ((bullet_x == enemy_x) && (bullet_y == enemy_y))
	{
		score++;
		velocity -= 1;//随着击落一架敌机分数增加,下以辆敌机速度加快;
		enemy_x = -1;//从新初始化敌机坐标
		enemy_y = rand() % width;//随机生成位置
	}
	if (enemy_x > high)
	{
		enemy_x = -1;//如果敌机已经出戒线则重新生成敌机
		enemy_y = rand() % width;//随机生成坐标
	}
	
	static int speed1 = 0;//避免更新数据影响用户的输入
	if (speed1 < velocity)
		speed1++;
	if (speed1 == velocity)
	{
		enemy_x ++;//敌机的向下移动
		speed1 = 0;
		

	}
	
	
}

void updateWithInput()//与用户输入有关的数据
{
	char input,j;
	if (_kbhit())
	{
		input = _getch();//——getch()直接获取字符,不用按回车
		switch (input)
		{
		case 'a':if (position_y - 2 == 0);else position_y--; break;//以下if()语句都加入了防止越界
		case 'd':if (position_y + 2 == width); else position_y++; break;
		case 'w':if (position_x == 0);else position_x--; break;
		case 's':if (position_x + 3 == high);else  position_x++; break;
		case ' ':bullet_x = position_x - 1; bullet_y = position_y; break;//发射的子弹初始位置在飞机的上空,
		case 'p':j = _getch(); if (j == 27)exit(0); //当输入‘p’暂停游戏,输入除ESC的按键继续游戏,如果为ESC结束程序,ESC的对应的ascill是27,exit()为结束程序的函数
		}
	}
}

int main()
{
	HideCursor();//隐藏光标
	startup();//初始化游戏数据
	while (1)
	{
		show(); //显示画面
		updateWithoutInput();//与用户无关的数据更新
		updateWithInput();   //与用户输入有关的数据更新
	}
	return 0;
}
  • 9
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值