C语言游戏---飞机大战(完整代码)

前言

本文基于C语言,使用简单的语法,编写了飞机大战的小游戏,能够用户控制飞机来击落敌机群获取得分,随着得分提高敌机的移动的速度也会提高,玩家得分提高也会升级飞机的子弹的范围,从而提高命中率。

编译环境

visual studio

代码

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

// 光标移到(X, Y)位置
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 };  //第二个值为0,表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

//全局变量
#define High 25
#define Width 50			//游戏画面尺寸
#define EnemyNum 5			//敌机数量

int position_x, position_y;		//飞机的位置 坐标
int canvas[High][Width] = { 0 };		//画布数组
int enemy_x[EnemyNum], enemy_y[EnemyNum];			//敌机的位置

int score;				
int BulletWidth;			//子弹宽度
int EnemyMoveSpeed;			//敌机的移动速度

//数据的初始化
void startup()
{
	position_x = High - 1;
	position_y = Width / 2;
	canvas[position_x][position_y] = 1;
	int k;
	for (k = 0; k < EnemyNum; k++)
	{
		enemy_x[k] = rand() % 2;
		enemy_y[k] = rand() % Width;
		canvas[enemy_x[k]][enemy_y[k]] = 3;
	}
	score = 0;
	BulletWidth = 0;
	EnemyMoveSpeed = 20;

}

//显示画面
void show()
{
	gotoxy(0, 0);
	int i, j;
	for (i = 0; i < High; i++)
	{
		for (j = 0; j < Width; j++)
		{
			if (canvas[i][j] == 0)
				printf(" ");			//输出空格
			else if (canvas[i][j] == 1)
				printf("*");			//输出飞机
			else if (canvas[i][j] == 2)
				printf("|");			//输出子弹
			else if (canvas[i][j] == 3)
				printf("@");			//输出敌机
		}
		printf("\n");
	}
	printf("得分:%3d\n", score);
	Sleep(20);
}

//与用户输出无关的更新
void updateWithoutInput()
{
	int i, j, k;
	for (i = 0; i < High; i++)
	{
		for (j = 0; j < Width; j++)
		{
			if (canvas[i][j] == 2)	
			{
				for (k = 0; k < EnemyNum; k++)
				{
				if (i == enemy_x[k] & j == enemy_y[k])
				{
					score++;			//得分加1
					printf("\a");
					if (score % 5 == 0 && EnemyMoveSpeed > 3)		//达到一定积分敌机变换
						EnemyMoveSpeed--;
					if (score % 5 == 0)//达到一定分数子弹变得厉害
						BulletWidth++;
					canvas[enemy_x[k]][enemy_y[k]] == 0;
					enemy_x[k] = 0;
					enemy_y[k] = rand() % Width;		//产生新的飞机
					canvas[enemy_x[k]][enemy_y[k]] = 3;
					canvas[i][j] = 0;		//子弹消失
				}
				}
				//子弹上移
				canvas[i][j] = 0;
				if (i > 0)
					canvas[i - 1][j] = 2;
			}
		}
	}
	static int speed = 0;
	if (speed <EnemyMoveSpeed)
	{
		speed++;
	}
	for (k = 0; k < EnemyNum; k++)
	{
		if (position_x == enemy_x[k] && position_y == enemy_y[k])//敌机与飞机相撞
		{
			printf("游戏失败!\n");
			printf("\a\a\a");
			Sleep(3000);
			system("pause");
			exit(0);
		}
		if (enemy_x[k] > High)
		{
			score--;
			canvas[enemy_x[k]][enemy_y[k]] = 0;
			enemy_x[k] = 0;
			enemy_y[k] = rand() % Width;
			canvas[enemy_x[k]][enemy_y[k]] = 3;
		}
		if (speed == EnemyMoveSpeed)
		{
			for (k = 0; k < EnemyNum; k++)
			{
				canvas[enemy_x[k]][enemy_y[k]] = 0;
				enemy_x[k]++;
				speed = 0;
				canvas[enemy_x[k]][enemy_y[k]] = 3;
			}
		}
	}
}

//与用户输出有关的更新
void updateWithInput()
{
	char input;
	if (_kbhit())
	{
		input = _getch();
			if (input == 'a'&& position_y>0)
			{
				canvas[position_x][position_y] = 0;
				position_y--;			//位置左移
				canvas[position_x][position_y] = 1;
			}
			else if (input == 'd'&& position_y<Width)
			{
				canvas[position_x][position_y] = 0;
				position_y++;			//位置右移
				canvas[position_x][position_y] = 1;
			}
			else if (input == 'w'&& position_x>1)
			{
				canvas[position_x][position_y] = 0;
				position_x--;			//位置上移
				canvas[position_x][position_y] = 1;
			}
			else if (input == 's'&& position_x<High-1)
			{
				canvas[position_x][position_y] = 0;
				position_x++;			//位置下移
				canvas[position_x][position_y] = 1;
			}
			else if (input = ' ')
			{
				int left = position_y - BulletWidth;
				int right = position_y + BulletWidth;
				if (left < 0)
					left = 0;
				if (right > Width - 1)
					right = Width - 1;
				int k;
				for(k=left;k<=right;k++)
				canvas[position_x-1][k] = 2;
			}
	}
}

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

后续

对于本文感兴趣,可关注作者其他的博客。

### 简易 C语言实现飞机大战游戏 #### 游戏概述 该游戏是一个简单的命令行版本的飞机大战,玩家通过键盘输入来控制一架战斗机在屏幕上移动并发射子弹击落敌机。此项目不依赖任何第三方库[^2]。 #### 主要功能模块 - **检测碰撞** #### 完整代码示例 ```c #include <stdio.h> #include <conio.h> /* _getch */ #include <windows.h> #define WIDTH 40 // 屏幕宽度 #define HEIGHT 20 // 屏幕高度 // 移动方向枚举定义 enum Direction { UP, DOWN, LEFT, RIGHT, }; typedef struct { int x; int y; } Point; void gotoxy(int column, int line) { // 设置光标位置 COORD coord; coord.X = column; coord.Y = line; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } void clearScreen() { // 清屏函数 system("cls"); } void drawBorder() { // 绘制边界线 for (int i = 0; i <= WIDTH; ++i) printf("-"); printf("\n"); for (int j = 0; j < HEIGHT; ++j) { for (int k = 0; k <= WIDTH; ++k) { if (k == 0 || k == WIDTH) putchar('|'); else putchar(' '); } printf("\n"); } for (int i = 0; i <= WIDTH; ++i) printf("-"); printf("\n"); } Point playerPos = {WIDTH / 2, HEIGHT - 1}; // 初始化玩家坐标 void movePlayer(enum Direction dir) { // 处理玩家移动逻辑 switch (dir) { case UP: if (playerPos.y > 1) --playerPos.y; break; case DOWN: if (playerPos.y < HEIGHT - 1) ++playerPos.y; break; case LEFT: if (playerPos.x > 1) --playerPos.x; break; case RIGHT: if (playerPos.x < WIDTH - 1) ++playerPos.x; break; } } char getCharAtXY(short int x, short int y) { // 获取指定坐标的字符 CHAR_INFO ci; COORD xy = {0, 0}; SMALL_RECT rect = {(short)x, (short)y, (short)(x + 1), (short)(y + 1)}; DWORD n; ReadConsoleOutputA( GetStdHandle(STD_OUTPUT_HANDLE), &ci, 1, xy, &rect ); return ci.Char.AsciiChar; } void shootBullet(Point bullet[]) { // 子弹射击逻辑 static int count = 0; char ch; while (_kbhit()) { ch = _getch(); if(ch == ' ') { bullet[count].x = playerPos.x; bullet[count++].y = playerPos.y - 1; if(count >= MAX_BULLETS) count = 0; } } for(int i=0;i<count;++i){ if(bullet[i].y != 0 && getCharAtXY(bullet[i].x,bullet[i].y-1)==' ') --bullet[i].y; else{ bullet[i].x = 0; bullet[i].y = 0; } } } void mainLoop() { // 主循环 enum Direction direction; char input; Point bullets[MAX_BULLETS]; do { clearScreen(); drawBorder(); // 显示玩家位置 gotoxy(playerPos.x, playerPos.y); putchar('@'); // 更新子弹的位置 shootBullet(bullets); // 打印所有活动中的子弹 for(int i=0;i<MAX_BULLETS;++i){ if(bullets[i].y!=0){ gotoxy(bullets[i].x,bullets[i].y); putchar('*'); } } Sleep(75); // 控制帧率 if(_kbhit()){ input=_getch(); // 接收按键 if(input=='w') // 上移 direction = UP; else if(input=='s') // 下移 direction = DOWN; else if(input=='a') // 左移 direction = LEFT; else if(input=='d') // 右移 direction = RIGHT; movePlayer(direction); } } while (input != 'q'); // 按下 q 键退出程序 } ``` 上述代码实现了基本的游戏框架,包括了玩家角色、敌人以及子弹之间的交互机制。为了简化起见,这里省略了一些细节上的优化和额外的功能扩展。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值