温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用C语言实现开发飞机游戏

发布时间:2022-02-07 15:50:16 来源:亿速云 阅读:175 作者:iii 栏目:开发技术

这篇“怎么用C语言实现开发飞机游戏”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么用C语言实现开发飞机游戏”文章吧。

一、前言

[设计难度 : ★☆☆☆☆

[参考书籍:《C语言课程设计与游戏开发实践教程》

[主要涉及知识:函数封装 + 循环判断语句

[程序运行效果图:

怎么用C语言实现开发飞机游戏

[主要的游戏功能:

  • 通过按键’w’,‘s’,‘a’,'d’分别实现飞机的上下左右移动

  • 按空格键发射子弹

  • 按ESC实现游戏暂停

  • 按q键返回菜单界面

  • 实现子弹和敌机位置的自动更新

  • 敌机的生成速度和下落速度随分数的增加而变快

  • 实时打印得分和生命值。生命值为0时游戏结束

以下为飞机游戏全部的代码,大家可以直接拷贝运行:

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <windows.h> #include <conio.h> #include <time.h> #define height 25   //设置游戏边界 #define width 50 #define enemy_max 5 enum Option	//枚举增加代码可读性 {	EXIT,	PLAY,	GUIDE, }; enum Condition      //表示游戏幕布上的情况 {	backspace,	enemy,	bullet, }; int canvas[height][width]; //游戏幕布存储对应的信息 int score; int x, y;	   //飞机头部坐标 int Std_Speed;	       //敌机标准下落速度 int Std_Time;	       //敌机生成的标准速度 int HP;                    //玩家生命值 int enemy_num; int times; void gotoxy(int x, int y)	//清屏函数 {	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);	COORD pos;	pos.X = x;	pos.Y = y;	SetConsoleCursorPosition(handle, pos); } void HideCursor()	   //光标隐藏函数 {	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void Initgame() {	for (int i = 0; i < height; i++)	{	for (int j = 0; j < width; j++)	//将幕布上先初始化为空格	canvas[i][j] = backspace;	}	HP = 3;	score = 0;	x = width / 2;	//初始化飞机位置	y = height / 2;	enemy_num = 0;	Std_Speed = 60;	Std_Time = 60; } void show() {	gotoxy(0, 0);	for (int i = 0; i < height; i++)	{	for (int j = 0; j < width; j++)	{	if (i == y && j == x)	//打印飞机	printf("*");	else if (i == y + 1 && j == x - 2)	{	printf("*****");	j += 4;	}	else if (i == y + 2 && j == x - 1)	{	printf("* *");	j += 2;	}	else if (canvas[i][j] == bullet)	// 打印子弹	printf("|");	else if (canvas[i][j] == enemy)	printf("@");	else	printf(" ");	}	printf("|\n");	//打印游戏边框	}	for (int j = 0; j < width; j++)   //打印游戏边框	printf("-");	printf("\n[得分:>%d\n", score);	  //打印游戏分数和血量	printf("[生命值:>%d\n", HP); } int updateWithinput() {	if (_kbhit())	{	int input = _getch();	switch (input)	{	case 'w': if (y > 0)	//防止飞机飞出游戏边界	y--;	break;	case 's': if (y < height - 3)	y++;	break;	case 'a': if (x > 2)	x--;	break;	case 'd': if (x < width - 3)	x++;	break;	case 27: system("pause"); break;	//ESC的ascll码值为27	case ' ': if (y > 0)	canvas[y - 1][x] = bullet;	break;	case 'q': return 1;	//退出游戏	}	}	return 0; } int enemy_update() {	static int enemy_speed = 0;	static int enemy_time = 0;	int flag = 0;	if (enemy_speed < Std_Speed)	//依靠循环来减速	enemy_speed++;	if (enemy_time < Std_Time)	enemy_time++;	if (enemy_num < enemy_max && enemy_time >= Std_Time)	{	int i, j;	do	{	i = rand() % (height / 5);	j = rand() % (width - 4) + 2;	//j的范围:[2, width - 3]	} while (canvas[i][j] != backspace);	canvas[i][j] = enemy;	enemy_num++;	enemy_time = 0;	}	if (enemy_speed >= Std_Speed)	{	flag = 1;	enemy_speed = 0;	}	for (int i = height - 1; i >= 0; i--)	{	for (int j = width - 1; j >= 0; j--)	{	if (canvas[i][j] == enemy)	//遇到敌机的情况	{	if (i == height - 1)	//敌机飞到边界	{	score--;	HP--;	if (HP == 0)	return 1;	enemy_num--;	canvas[i][j] = backspace;	}	else if (i < height - 1 && canvas[i + 1][j] == bullet)//检测是否被子弹击中	{	score++;	printf("\a");	enemy_num--;	if (score % 5 == 0 && Std_Speed >= 12) //分数到达一定程度后下落加快,生成加快	{	Std_Speed -= 3;	//下落加快	Std_Time -= 3;	//生成速度加快	}	canvas[i][j] = backspace;	}	else if (flag)	//flag为1更新敌机位置	{	canvas[i + 1][j] = enemy;	canvas[i][j] = backspace;	}	}	}	}	return 0; } void bullet_update() {	for (int i = 0; i < height; i++)	//控制子弹的移动	{	for (int j = 0; j < width; j++)	{	if (canvas[i][j] == bullet)	{	if (i > 0 && canvas[i - 1][j] == enemy)	{	score++;	printf("\a");	enemy_num--;	if (score % 5 == 0 && Std_Speed >= 6) //分数到达一定程度后下落加快,生成加快	{	Std_Speed -= 3;	//下落加快	Std_Time -= 3;	//生成速度加快	}	canvas[i - 1][j] = bullet;	}	else if (i > 0)	canvas[i - 1][j] = bullet;	canvas[i][j] = backspace;	}	}	} } void gamebody() {	system("cls");	Initgame();	HideCursor();	srand((unsigned int)time(NULL));	while (1)	{	show();	bullet_update();	if (updateWithinput() || enemy_update())	{	show();	printf("[本次游戏结束:>");	system("pause");	break;	}	} } void menu() {	printf("*****************\n");	printf("**  飞机游戏   **\n");	printf("**-------------**\n");	printf("**   1.PLAY    **\n");	printf("**   2.GUIDE   **\n");	printf("**   0.EXIT    **\n");	printf("*****************\n"); } void guide() {	printf("******************\n");	printf("** 游戏操作指南 **\n");	printf("**--------------**\n");	printf("**    w->上移   **\n");	printf("**    s->下移   **\n");	printf("**    a->左移   **\n");	printf("**    d->右移   **\n");	printf("**    q->返回   **\n");	printf("**   ESC->暂停  **\n");	printf("**   空格->射击 **\n");	printf("******************\n\n\n"); } int main() {	int input = 0;	do	{	menu();	printf("[请选择:>");	scanf("%d", &input);	switch (input)	{	case PLAY: gamebody(); break;	case GUIDE: guide(); break;	case EXIT: printf("成功退出游戏!\n"); break;	default: printf("输入错误,请重新选择\n");	}	} while (input);	return 0; }

如果觉得还挺有意思的,那就继续保持着轻松的心情看下去吧!

二、从设计初始菜单界面开始

一个基本的游戏初始选择框架:

int main() {	int input = 0;	do	{	menu();	printf("[请选择:>");	scanf("%d", &input);	switch (input)	{	case xxx:	case xxx:	case xxx:	default: 	}	}while (input);	return 0; }

我们根据游戏所包含的功能设计好相应的menu选项以及其对应的case事件即可。作为我们飞机游戏的第一个简单版本,我们先不考虑其他的模式和功能,仅包含PLAY(游戏)功能、GUIDE(操作说明)、EXIT(退出游戏)三种功能。根据这个思路,我们写下这样的menu函数

void menu() {	printf("*****************\n");	printf("**  飞机游戏   **\n");	printf("**-------------**\n");	printf("**   1.PLAY    **\n");	printf("**   2.GUIDE   **\n");	printf("**   0.EXIT    **\n");	printf("*****************\n"); }

为了增加代码的可读性,我们在头文件处创建枚举变量。

enum Option	//枚举增加代码可读性 {	EXIT,           // printf("%d", EXIT);的结果为 0	PLAY,	// printf("%d", PLAY);的结果为 1	GUIDE,	// printf("%d", GUIDE);的结果为 2 };

每个枚举常量都是有值的,第一个枚举成员的值默认为0(不人为修改的话),之后的随前一个递增。这恰好与我们的menu中功能序号相对应,于是我们可以用枚举变量作为case的整形常量表达语句,最终写出的主函数是这样的:

int main() {	int input = 0;	srand((unsigned int)time(NULL));  //初始化rand函数,只需要初始化一次即可,所以放在主函数内	do	{	menu();	printf("[请选择:>");	scanf("%d", &input);	switch (input)	{	case PLAY: gamebody(); break;	case GUIDE: guide(); break;	case EXIT: printf("成功退出游戏!\n"); break;	default: printf("输入错误,请重新选择\n");	}	}while (input);	return 0; }

三、游戏操作指南&mdash;&mdash;guide函数

说明按键对应的功能,很简单就不赘述了

void guide() {	printf("******************\n");	printf("** 游戏操作指南 **\n");	printf("**--------------**\n");	printf("**    w->上移   **\n");	printf("**    s->下移   **\n");	printf("**    a->左移   **\n");	printf("**    d->右移   **\n");	printf("**    q->返回   **\n");	printf("**   ESC->暂停  **\n");	printf("**   空格->射击 **\n");	printf("******************\n\n\n"); }

四、游戏的主体gamebody()

①简化通用的游戏框架

void gamebody() {	Initgame();	//初始化游戏函数	while(1)	{	show();	 //展示函数	updateWithInput();	 //与用户输入有关的更新,	updateWithoutInput();//与用户输入无关的更新,如子弹、敌机的移动	} }

以这个游戏框架为基础,我们建立起我们的设计逻辑

②头文件一览

在正式介绍gamebody函数之前,我们先看看定义在头文件的全局变量以及他们的作用

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include <windows.h> #include <time.h> #include <conio.h>	//需要用到的函数头文件 #define height 25       //宏定义游戏边界的高度 #define width 50        //宏定义游戏边界的宽度 #define enemy_max 5     //宏定义敌人的最多数量 enum Option	    //枚举增加代码可读性 {	EXIT,	PLAY,	GUIDE, }; enum Condition      //表示游戏幕布上的情况 {	backspace,	    //空	enemy,	//敌人	bullet,	//子弹 }; int canvas[height][width]; //游戏幕布存储对应位置上的Condition信息 int score;	   //记录游戏分数 int x, y;	   //飞机头部的xy坐标 int Std_Speed;	       //敌机标准下落速度,与之后的加速下落有关 int Std_Time;	       //敌机生成的标准速度,与之后的加速生成有关 int HP;                    //玩家生命值 int enemy_num;	   //此时的敌机数量 void gamebody();

③清屏函数的实现

我们的游戏画面完全是靠printf函数打印出来的,因此清屏函数是必不可少的。

直接使用system("cls")函数会造成屏幕画面闪烁严重,因此我们可以自行封装一个gotoxy函数,函数的功能是将光标移到原点,从原点开始重新绘制,相当于实现清屏的效果。虽然还是会闪烁,但防屏闪效果有了显著提升。

首先给大家介绍几个平时不常用的函数:

①SetConsoleCursorPosition

怎么用C语言实现开发飞机游戏

  • 头文件:#include<windows.h>

  • 参数①:hConssoleOutput &rarr; 指向屏幕缓冲区的句柄

  • 参数②:dwCursorPosition &rarr; 指定包含新光标位置的COORD结构

  • 函数功能:设置光标在指定的控制台屏幕缓冲区中的位置

  • COORD结构体:

怎么用C语言实现开发飞机游戏

②GetStdHandle函数

怎么用C语言实现开发飞机游戏

  • 头文件:#include<windows.h>

  • 参数①:nStdHandle 指定返回句柄的标准设备,nStdHandle有以下三种选择

valMeanig
STD_INPUT_HANDLEStandard input handle
STD_OUTPUT_HANDLEStandard output handle
STD_ERROR_HANDLEStandard error handle
  • 函数功能:获取标准输入、标准输出或标准错误设备的句柄

什么是句柄?

将①②函数组合后就可以构造出我们需要的gotoxy函数了

void gotoxy(int x, int y) {	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);	COORD pos;	pos.X = x;	pos.Y = y;	SetConsoleCursorPosition(handle, pos); }

④光标隐藏函数

光标一直闪烁会影响我们的游戏体验,所以我们封装一个HideCursor函数。光标的信息定义在CONSOLE_CURSOR_INFO结构体中,其具体定义如下:

怎么用C语言实现开发飞机游戏

dwSize结构体成员指定这光标的大小,bVisible决定光标是否可见,因此我们只需对将它设置为false即可。实际的修改还需要借助SetConsoleCursorInfo函数

怎么用C语言实现开发飞机游戏

  • 参数①:标准输出设备的句柄,我们可以用上面提到的GetStdHandle函数获取

  • 参数②:CONSOLE CURSOR INFO结构体类型的指针,该结构体包含屏幕缓冲区新规范 有了上面的知识,我们可以写下这样的代码:

void HideCursor() {	CONSOLE_CURSOR_INFO cursor_info = {1, 0};//将“是否可见”设置为false	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); }

⑤Initgame函数

因为我们使用了全局变量,并且要求设计出来的游戏能能够重复的play,所以我们在每次游戏开始时都要对全局变量进行必要的 初始化

void Initgame() {	for (int i = 0; i < height; i++)	{	for (int j = 0; j < width; j++)	//将幕布首先初始化为空格	canvas[i][j] = backspace;	}	HP = 3;	score = 0;	x = width / 2;	//初始化飞机位置	y = height / 2;	enemy_num = 0;	Std_Speed = 60;	//初始化“标准下落速度”	Std_Time = 60;	//初始化“标准生成速度” }

⑥show函数的实现

遍历canvas数组,根据canvas数组中的内容决定打印什么:

  • backspace &rarr; 空格

  • enemy &rarr; 敌机(@)

  • bullet &rarr; 子弹(|)

void show() {	gotoxy(0, 0);                   	//在打印之前先清屏	for (int i = 0; i < height; i++)	{	for (int j = 0; j < width; j++)	{	if (i == y && j == x)	//打印飞机	printf("*");	else if (i == y + 1 && j == x - 2)	{	printf("*****");	j += 4;	}	else if (i == y + 2 && j == x - 1)	{	printf("* *");	j += 2;	}	else if (canvas[i][j] == bullet)	// 打印子弹	printf("|");	else if (canvas[i][j] == enemy)	printf("@");	else	printf(" ");	}	printf("|\n");	//打印游戏右边框	}	for (int j = 0; j < width; j++)   //打印游戏底边框	printf("-");	printf("\n[得分:>%d\n", score);	  //打印游戏分数和血量	printf("[生命值:>%d\n", HP); }

⑦与用户输入有关的更新- updateWithinput

[设计难点:

  • 当我们键盘没有输入的时候,函数不执行效果&middot;;

  • 当我们按下相应的游戏按键而不需要按下回车时,数据就可以被读取

现在介绍两个大家平时可能不常用到的函数来满足我们上面的设计要求:

_kbhit函数用来监测键盘是否有输入,如果有输入则返回一个非0值。

怎么用C语言实现开发飞机游戏

即使没有按下回车键,_getch函数可以从控制台中读取字符

怎么用C语言实现开发飞机游戏

有了上面的基础知识储备,我们来实现updateWithinput函数

int updateWithinput() {	if (_kbhit())	{	int input = _getch();	switch (input)	{	case 'w': if(y > 0)	//先要加以判断,防止飞机飞出游戏边界	y--; 	  break;	case 's': if (y < height - 3)	y++;	  break;	case 'a': if (x > 2)	x--;	  break;	case 'd': if (x < width - 3)	x++;	  break;	case 27: system("pause"); break;	//ESC的ascll码值为27	case ' ': if(y > 0)	canvas[y - 1][x] = bullet; 	  break;	case 'q': return 1;	//退出游戏	}	}	return 0; 	//我们根据返回值判断是否需要退出游戏 }

⑧与用户输入无关的更新-updateWithoutinput

我们将updateWithoutinput函数拆分成对子弹位置更新的函数和对敌机位置更新的函数。子弹的位置是实时更新的:

void bullet_update() {	for (int i = 0; i < height; i++)	{	for (int j = 0; j < width; j++)	{	if (canvas[i][j] == bullet)	{	if (i > 0 && canvas[i - 1][j] == enemy)	//检测是否击中敌机	{	score++;	printf("\a");	enemy_num--;	if (score % 5 == 0 && Std_Speed >= 6) //分数到达一定程度后下落加快,生成加快	{	Std_Speed -= 3;	//下落加快	Std_Time -= 3;	//生成速度加快	}	canvas[i - 1][j] = bullet;	}	else if (i > 0)	canvas[i - 1][j] = bullet;	canvas[i][j] = backspace;	}	}	} }

与子弹稍微不同的一点是敌机的位置更新受“标准速度”的限制,我们通过循环实现敌机的速度控制,但每次仍需要检测是否和子弹相撞。由于敌机是向y增大的方向上运动的,若for正向循环则,则敌机一直被往前推,视觉上是“瞬移”的效果,所以我们需要反向遍历。

int enemy_update() {	static int enemy_speed = 0;	static int enemy_time = 0;	int flag = 0;	if (enemy_speed < Std_Speed)	//依靠循环来减速	enemy_speed++;	if (enemy_time < Std_Time)	enemy_time++;	if (enemy_num < enemy_max && enemy_time >= Std_Time)	{	int i, j;	do	{	i = rand() % (height / 5);	j = rand() % (width - 4) + 2;	//j的范围:[2, width - 3]	} while (canvas[i][j] != backspace);	canvas[i][j] = enemy;	enemy_num++;	enemy_time = 0;	}	if (enemy_speed >= Std_Speed)	{	flag = 1;	enemy_speed = 0;	}	for (int i = height - 1; i >= 0; i--)	{	for (int j = width - 1; j >= 0; j--)	{	if (canvas[i][j] == enemy)	//遇到敌机的情况	{	if (i == height - 1)	//敌机飞到边界	{	score--;	HP--;	if (HP == 0)	return 1;	enemy_num--;	canvas[i][j] = backspace;	}	else if (i < height - 1 && canvas[i+1][j] == bullet)//检测是否被子弹击中	{	score++;	printf("\a");	enemy_num--;	if (score % 5 == 0 && Std_Speed >= 12) //分数到达一定程度后下落加快,生成加快	{	Std_Speed -= 3;	//下落加快	Std_Time -= 3;	//生成速度加快	}	canvas[i][j] = backspace;	}	else if (flag)	//flag为1更新敌机位置	{	canvas[i + 1][j] = enemy;	canvas[i][j] = backspace;	}	}	}	}	return 0; }

⑨组合而成的gamebody函数

void gamebody() {	system("cls");	Initgame();	HideCursor();	while (1)	{	show();	bullet_update();	if (updateWithinput() || enemy_update())	{	show();	printf("[本次游戏结束:>");	system("pause");	break;	}	} }

以上就是关于“怎么用C语言实现开发飞机游戏”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI