IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言简易飞机游戏 -> 正文阅读

[C++知识库]C语言简易飞机游戏

//简易飞机大战游戏代码
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
//函数外全局变量定义
int high, width;				//游戏画面尺寸
int position_x, position_y;		//飞机位置
int bullet_x, bullet_y;			//子弹位置
int enemy_x,enemy_y;			//敌机位置
int score;						//得分

void HideCursor()				//隐藏光标函数
{
	_CONSOLE_CURSOR_INFO cursor_info = { 1,0 };		//第二值为0表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)		//光标移动到(x,y)位置,清屏函数
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
void starup()					//数据初始化
{
	high = 18;
	width = 30;

	position_x = high / 2;
	position_y = width / 2;

	bullet_x = -1;
	bullet_y = position_y;

	enemy_x = 0;
	enemy_y = width / 2;

	HideCursor();		//隐藏光标
}

void show()			//显示画面
{
	gotoxy(0, 0);	//光标移动到原点位置,一下重画清屏
	int i, j;
	//system("cls");	//清屏
	for (i = 0; i < high; i++) {
		for (j = 0; j < width; j++) {
			if ((i == position_x) && (j == position_y))
				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\n", score);
}
void UpdateWithoutInput()		//与用户输入无关的更新
{
	if ((bullet_x == enemy_x) && (bullet_y == enemy_y))
	{
		score++;
		enemy_x = 0;
		enemy_y = rand() % width;
		bullet_x = -1;
	}
	static int speed = 0;		//用于控制敌机下落速度
	if (speed < 10)
		speed++;
	if(bullet_x > -1)			//子弹的移动
	bullet_x--;

	if (enemy_x > high)
	{
		enemy_x = 0;
		enemy_y = rand() % width;
	}
	else {
		if (speed == 10) {
			enemy_x++;
			speed = 0;
		}
	}
}
void UpdateWithInput()			//与用户输入有关的更新
{
	char input;
	if (_kbhit())				//当按下按键后执行
	{
		input = _getch();
		if (input == 'a')
			position_y--;
		if (input == 'd')
			position_y++;
		if (input == 'w')
			position_x--;
		if (input == 's')
			position_x++;
		if (input == ' ')
		{
			bullet_x = position_x - 1;
			bullet_y = position_y;
		}
	}
}
int main()
{
	starup();			//数据初始化
	while (1) {			//游戏执行
		show();				//显示画面
		UpdateWithoutInput();		//与用户输入无关的更新
		UpdateWithInput();			//与用户输入有关的更新
	}
	return 0;
}










生命周期游戏代码
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define Hight 40		//定义框架
#define Width 70
//函数外全局变量定义
int cells[Hight][Width];


void HideCursor()				//隐藏光标函数
{
	_CONSOLE_CURSOR_INFO cursor_info = { 1,0 };		//第二值为0表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)		//光标移动到(x,y)位置,清屏函数
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
void starup()					//数据初始化
{
	int i, j;
	for (i = 0; i < Hight; i++)
		for (j = 0; j < Width; j++)
			cells[i][j] = rand() % 2;	//细胞初始


	HideCursor();		//隐藏光标
}

void show()			//显示画面
{
	gotoxy(0, 0);	//光标移动到原点位置,一下重画清屏
	int i, j;
	for (i = 0; i < Hight; i++) 
	{
		for (j = 0; j < Width; j++) 
		{
			if (cells[i][j] == 1)
				printf("*");
			else
				printf(" ");
		}
		printf("\n");
	}
		

}
void UpdateWithoutInput()		//与用户输入无关的更新
{
	int i, j;
	int tempCells[Hight][Width];		//临时存储的二维数组
	for (i = 0; i < Hight; i++)
		for (j = 0; j < Width; j++)
			tempCells[i][j] = cells[i][j];
	int NeibourNumber = 0;
	for (i = 1; i < Hight - 1; i++)
	{
		for (j = 1; j < Width - 1; j++)
		{
			NeibourNumber = cells[i - 1][j - 1] + cells[i - 1][j] + cells[i - 1][j + 1]
				+ cells[i][j - 1] + cells[i][j] + cells[i][j + 1]
				+ cells[i + 1][j - 1] + cells[i + 1][j] + cells[i + 1][j + 1];
			if (NeibourNumber == 3)
				tempCells[i][j] = 1;
			else if (NeibourNumber == 2 || NeibourNumber == 4)
				tempCells[i][j] = cells[i][j];
			else
				tempCells[i][j] = 0;
		}
	}
	for (i = 0; i < Hight; i++)
		for (j = 0; j < Width; j++)
			cells[i][j] = tempCells[i][j];
	Sleep(150);
}
void UpdateWithInput()			//与用户输入有关的更新
{
	
}
int main()
{
	starup();			//数据初始化
	while (1) {			//游戏执行
		show();				//显示画面
		UpdateWithoutInput();		//与用户输入无关的更新
		UpdateWithInput();			//与用户输入有关的更新
	}
	return 0;
}

飞机大战1.0
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define High 18
#define Width 40
#define EnemyNum 5
//函数外全局变量定义
int canvas[High][Width] = { 0 };	//二维数组记录游戏画面中对应的元素
									//0输出空格 1输出飞机 2输出子弹 3输出敌机
int position_x, position_y;			//飞机位置
int enemy_x[EnemyNum], enemy_y[EnemyNum];	//敌机位置
int score;							//得分

void HideCursor()					//隐藏光标函数
{
	_CONSOLE_CURSOR_INFO cursor_info = { 1,0 };		//第二值为0表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)		//光标移动到(x,y)位置,清屏函数
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
void starup()					//数据初始化
{
	position_x = High / 2;
	position_y = Width / 2;
	canvas[position_x][position_y] = 1;
	int k;
	for (k = 0; k < EnemyNum; k++)
	{
		enemy_x[k] = rand() % 3;
		enemy_y[k] = rand() % Width;
		canvas[enemy_x[k]][enemy_y[k]] = 3;
	}
	score = 0;
	HideCursor();		//隐藏光标
}

void show()			//显示画面
{
	gotoxy(0, 0);	//光标移动到原点位置,一下重画清屏
	int i, j;
	//system("cls");	//清屏
	for (i = 0; i < High - 1; i++) {
		for (j = 0; j < Width - 1; j++) {
			if (canvas[i][j] == 1)
				printf("*");	//输出飞机
			else if (canvas[i][j] == 2)
				printf("|");	//输出子弹
			else if (canvas[i][j] == 3)
				printf("@");	//输出敌机
			else
				printf(" ");	//输出空格
		}
		printf("\n");
	}
	printf("得分 %d\n", score);
}
void UpdateWithoutInput()		//与用户输入无关的更新
{
	int i, j, k;
	for (i = 0; i < High - 1; i++) {
		for (j = 0; j < Width - 1; j++) {
			if (canvas[i][j] == 2)
			{
				for (k = 0; k < EnemyNum; k++)
				{
					if (i == enemy_x[k] && j == enemy_y[k])		//击中敌机判定
					{
						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;
						score++;
					}
				}
				
				//使子弹自动向上移动
				canvas[i][j] = 0;
				if (i > 0)
					canvas[i - 1][j] = 2;
			}
		}
	}
	for (k = 0; k < EnemyNum; k++)
	{
		if (enemy_x[k] > High)			//敌机跑出下边界 
		{
			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;
			score--;
		}
	}
	//敌机自动下落
	static int speed = 0;		//用于控制敌机下落速度
	if (speed < 10)
		speed++;
	if (speed == 10)			//子弹的移动
	{
		for (k = 0; k < EnemyNum; k++)
		{
			canvas[enemy_x[k]][enemy_y[k]] = 0;
			enemy_x[k]++;
			canvas[enemy_x[k]][enemy_y[k]] = 3;
		}
		speed = 0;
	}
}
void UpdateWithInput()			//与用户输入有关的更新
{
	char input;
	if (_kbhit())				//当按下按键后执行
	{
		input = _getch();
		if (input == 'a') {
			canvas[position_x][position_y] = 0;
			position_y--;
			canvas[position_x][position_y] = 1;
		}
		if (input == 'd') {
			canvas[position_x][position_y] = 0;
			position_y++;
			canvas[position_x][position_y] = 1;
		}
		if (input == 'w') {
			canvas[position_x][position_y] = 0;
			position_x--;
			canvas[position_x][position_y] = 1;
		}
		if (input == 's') {
			canvas[position_x][position_y] = 0;
			position_x++;
			canvas[position_x][position_y] = 1;
		}
		if (input == ' ')
		{
			canvas[position_x - 1][position_y] = 2;
		}
	}
}
int main()
{
	starup();			//数据初始化
	while (1) {			//游戏执行
		show();				//显示画面
		UpdateWithoutInput();		//与用户输入无关的更新
		UpdateWithInput();			//与用户输入有关的更新
	}
	return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-02 10:37:50  更:2021-08-02 10:40:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/9 22:20:31-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码