c++小游戏:飞机游戏

游戏说明:能够通过上下左右来控制飞机的位置,按空格建能够发射子弹,敌机随机生成并从顶端下落,若子弹击中飞机,那么子弹和飞机一起消失,若敌机与我方飞船碰撞或到达地面,则life减一,击中敌机三次后,敌机速度会提高;
可玩性不高,因为用清屏函数做的闪屏太厉害
主要是easyx还没整明白…
是跟网课一老师学的,老师用c写的,我用了c++不过我也没改动太多,在原有的基础上增加了随机下落,生命值,速度等级什么的
实验运行图如下:(@是敌机*是我方飞船哈)
在这里插入图片描述
以下是游戏的大框架

#include<iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
int main()
{
	startup();					//确定一堆初始值
	while (true)
	{
		show();					//显示屏幕
		updatewithoutinput();	//该函数是指在没有键盘输入时要做的
		updatewithinput();		//该函数时有键盘输入时要做的
		if (speed_level <= 0)	//速度等级降为0时,胜利,游戏结束
		{
			cout << "win" << endl;
			break;
		}
		if (life == 0)			//生命值为0时,失败,游戏结束
		{
			cout << "lose" << endl;
			break;
		}
	}
	cout << "******game over*****" << endl;
	return 0;
}

首先第一步要写的时显示屏幕函数
大部分函数都是定义成了全局变量,并且在startup函数里定义了初值,不然要传的参数也太多了摔

void show()//显示画面
{
	//system("cls");
	int i, j;
	for (i = 0; i <= high; i++)		//high是屏幕高度
	{
		for (j = 0; j <= width; j++)//width是屏幕宽度
		{
			if (i == position_x && j == position_y)		//position_x ,position_y分别是飞船的对应屏幕的高和长(应该刚好是yx坐标了)
				cout << '*';
			else if (i == bullet_x && j == bullet_y)
			//bullet_x bullet_y分别是子弹的对应屏幕的高和长
				cout << '|';
			else if (i == target_x && j == target_y)
			//target_x target_y分别是敌机的对应屏幕的高和长
				cout << '@';
			else if (i == high)			//输出边界
					cout << '#';
			else if (j==width)
				cout << '#';
			else
				cout << ' ';
		}
		cout << endl;
	}
	cout << "score:" << score  << " life:"<<life<<endl;
	cout << "speed_level:" << speed_level << " speed:" << speed << endl;
}

如果运行上面的代码看到的应该是刷屏的屏幕框框
所以要引入清屏函数system(“cls”);
位置是放在show函数的第一行
加上了以后应该就会看到静止的飞船敌机边框 还有一堆数据(其实是闪烁的
哦对 别忘了给他们都附上初始值,不然肯定编译都过不了,大家这个自己看着来,下面是我的初值,下面这个函数要放在上面那个上面,要不然就提前声明,并且这些变量都定义了全局变量

void startup()
{
	high = 15;				//屏幕高度
	width = 20;				//屏幕宽度
	position_x = high - 1;	//飞船把他放在最后一行中间
	position_y = width / 2;
	bullet_x = -1;			//子弹在隐藏
	bullet_y = position_y;	//横坐标应该和飞船一致
	target_x = 0;
	target_y = rand() % width;	//随机生成敌机
}

我们按键盘是控制上下左右和发射子弹的,那么这些步骤我们就要写在updatewithinput()函数中

void updatewithinput()
{
	char ch;
	if (_kbhit())			//按键时(_kbhit函数可以检测键盘是否有动静)
	{
		ch = _getch();		//_getch函数可以不需要按回车就知道你按的什么值		
		//这里是通过按awsd控制上下左右,当然还可以把大写的也加上,上下左右箭头也加上,很容易,当然也可以用一个switch语句来写
		if (ch == 'a')
			position_y--;
		if (ch == 'd')
			position_y++;
		if (ch == 'w')
			position_x--;
		if (ch == 's')
			position_x++;
		if (ch == ' ')			//发射子弹时我们要重新规定一下子弹的横纵坐标
		{
			bullet_x = position_x - 1;
			bullet_y = position_y;
		}
	}
}

现在运行一下会发现飞船已经可以上下左右移动了,但是发射的子弹发射不出去啊,并且敌机也没有下降,因为这些动作都是在没有按键的时候进行的。所以我们现在要写的是我们最后一个函数

void updatewithoutinput()
{
	if(bullet_x>-1)							//若未隐藏子弹,子弹要一直向上
		bullet_x--;
		/* 速度#1 */
	if (bullet_x == target_x && bullet_y == target_y)		//击中
	{
		bullet_x = -1;					//隐藏子弹
		target_x = 0;					//重新生成敌机
		target_y = rand() % width;
		/*速度#2*/
	}
	if (position_x == target_x && position_y == target_y||target_x==high-1)										//如果敌机和飞船相遇,或者是敌机降落
	{
		life--;
		target_x = 0;
		target_y = rand() % width;		//生命值减一并且在随机生成一个敌机
	}
}

如果现在运行的话,子弹已经可以发射了,击中敌机也会换一台,但是我们会发现敌机没有下落啊,是因为下落的太快了 我就把它和下面的调节速度写在一起了。

			/*速度#1*/
	if(speed < speed_level)//让函数循环speed_level次后在进行一步动作,也就是该动作放慢了这么多倍
		speed++;
	if (speed == speed_level)
	{
		if (target_x < high)			//飞船下落
		{
			target_x++;
		}
		else							//若触底则重新生成
		{
			target_x = 0;
			target_y = rand() % width;
		}
		speed = 0;						//重设speed值,让循环重来
	}
	/*速度#2*/
		score++;				//射中多一个飞船
		if (score % 3 == 0)		//射中三个等级提高,速度循环次数少一点,也就是更快一点
		{
			level++;
			speed_level -= 1;
		}

至此该游戏已经完成,一般操作都可以进行,温馨提示不要忘记设初值~
下面是完整代码废话比较多写了150行叭
是在VS2019上运行的

#include<iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int high;
int width ;
int target_y ;
int target_x ;
int position_x ;
int position_y;
int bullet_x;
int bullet_y;
int score;
int level;
int speed=0;
int speed_level=9;
int life = 3;

void startup()
{
	high = 15;
	width = 20;
	target_y = 4;
	target_x = 0;
	position_x = high - 1;
	position_y = width / 2;
	bullet_x = -1;
	bullet_y = position_y;
	target_x = 0;
	target_y = rand() % width;
	score = 0;
	level = 0;
}

void show()//显示画面
{
	system("cls");
	int i, j;
	for (i = 0; i <= high; i++)
	{
		for (j = 0; j <= width; j++)
		{
			if (i == position_x && j == position_y)
				cout << '*';
			else if (i == bullet_x && j == bullet_y)
				cout << '|';
			else if (i == target_x && j == target_y)
				cout << '@';
			else if (i == high)
					cout << '#';
			
			else if (j==width)
				cout << '#';
			else
				cout << ' ';
		}
		cout << endl;
	}
	cout << "score:" << score  << " life:"<<life<<endl;
	cout << "speed_level:" << speed_level << " speed:" << speed << endl;
}

void updatewithoutinput()
{
	if(bullet_x>-1)
		bullet_x--;
	
	if(speed < speed_level)
		speed++;
	if (speed == speed_level)
	{
		if (target_x < high)
		{
			target_x++;
		}
		else
		{
			target_x = 0;
			target_y = rand() % width;
		}
		speed = 0;
	}
	
	if (bullet_x == target_x && bullet_y == target_y)//击中
	{
		bullet_x = -1;
		target_x = 0;
		target_y = rand() % width;
		score++;
		if (score % 3 == 0)
		{
			level++;
			speed_level -= 1;
		}
	}
	if (position_x == target_x && position_y == target_y||target_x==high-1)
	{
		life--;
		target_x = 0;
		target_y = rand() % width;

	}
	
}
void updatewithinput()
{
	char ch;
	if (_kbhit())//按键时
	{
		ch = _getch();
		if (ch == 'a')
			position_y--;
		if (ch == 'd')
			position_y++;
		if (ch == 'w')
			position_x--;
		if (ch == 's')
			position_x++;
		if (ch == ' ')
		{
			bullet_x = position_x - 1;
			bullet_y = position_y;
		}
	}
}

int main()
{
	startup();
	while (true)
	{
		show();
		updatewithoutinput();
		updatewithinput();


		if (speed_level <= 0)
		{
			cout << "win" << endl;
			break;
		}
		if (life == 0)
		{
			cout << "lose" << endl;
			break;
		}
	}
	cout << "******game over*****" << endl;
	return 0;
}
  • 12
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值