小游戏之雷霆战机

话不多说先上代码:

本代码兼容多种编译器,C和C++语言项目多可以运行。

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

#define map_x 39 //定义可移动范围的长和宽
#define map_y 76
#define plane_x 6 //定义爱心的长和宽
#define plane_y 8
#define tortoise_x 8
#define tortoise_y 11

#define ball1 '+'

void setcolor(int, int);
void gotoxy(int, int);
void Begin();
void Init();
void CreateFrame();
void GetContral();
void PlaneMove();
void TortoiseMove();
void Print();
void PlaneShoot();
void TortoiseShoot();
void PlaneBlast();

char map[map_x][map_y], newmap[map_x][map_y];
char plane1[plane_x][plane_y + 1] = {
	"   **",
	"********",
	"********",
	"   **",
	"   **",
	"  ****"
};
char tortoise[tortoise_x][tortoise_y + 1] = {
	"     #",
	" #  ###  #",
	" #########",
	"#  # # #  #",
	"###########",
	"#  # # #  #",
	" #########",
	"#    ##   #"
};
int plane_start_x = map_x - plane_x, plane_start_y = (map_y - plane_y) / 2, tortoise_start_x = 0, tortoise_start_y = (map_y - plane_y) / 2;
int plane_move_y, plane_move_x, tortoise_move_y = 1, tortoise_move_x = 1;
int game_time;
int life1 = 20, life2 = 999;
int is_blast;

/********************************************************************/
int main()
{
	Begin();
	Init();
	while (1)
	{
		PlaneMove();
		if (game_time % 6 == 0)TortoiseMove();
		PlaneShoot();
		TortoiseShoot();
		Print();
		Sleep(20);
	}
	system("pause");
	return 0;
}
/********************************************************************/


void Print()
{
	for (int i = 0; i < map_x; i++){
		for (int j = 0; j < map_y; j++){
			if (map[i][j] != newmap[i][j]){
				gotoxy(i + 1, j + 2);
				if (newmap[i][j] == '*' || newmap[i][j] == '|' || newmap[i][j] == '^'){
					setcolor(1, 14);
				}
				else{
					setcolor(1, 10);
				}
				printf("%c", newmap[i][j]);
				map[i][j] = newmap[i][j];
			}
		}
	}
	gotoxy(map_x + 2, map_y + 3);

	game_time++;
}

void PlaneMove()
{
	GetContral();
	if (plane_start_y != map_y - plane_y&&plane_move_y == 1)plane_start_y += 2;
	if (plane_start_y != 0 && plane_move_y == -1)plane_start_y -= 2;
	if (plane_start_x != map_x - plane_x&&plane_move_x == 1)plane_start_x += 1;
	if (plane_start_x != map_x - plane_x - 15 && plane_move_x == -1)plane_start_x -= 1;


	for (int i = 0; i < map_x; i++){
		for (int j = 0; j < map_y; j++){
			if (newmap[i][j] == '*')newmap[i][j] = ' ';
		}
	}
	for (int i = 0; i < plane_x; i++){
		for (int j = 0; j < plane_y; j++){
			if (plane1[i][j] == '*'){
				if (newmap[plane_start_x + i][plane_start_y + j] == '+'){
					life1--;
					gotoxy(map_x + 1, 7);
					setcolor(1, 12);
					printf("%03d", life1);
				}
				newmap[plane_start_x + i][plane_start_y + j] = plane1[i][j];
			}
		}
	}
}

void TortoiseMove()
{
	tortoise_start_x += tortoise_move_x;
	tortoise_start_y += tortoise_move_y;

	if (rand() % 91 == 0)tortoise_move_y *= -1;
	if (rand() % 29 == 0)tortoise_move_x *= -1;

	if (tortoise_start_x == 0)tortoise_move_x = 1;
	if (tortoise_start_x == 7)tortoise_move_x = -1;
	if (tortoise_start_y == 0)tortoise_move_y = 1;
	if (tortoise_start_y == map_y - tortoise_y)tortoise_move_y = -1;


	for (int i = 0; i < map_x; i++)
		for (int j = 0; j < map_y; j++)
			if (newmap[i][j] == '#')newmap[i][j] = ' ';

	for (int i = 0; i < tortoise_x; i++){
		for (int j = 0; j < tortoise_y; j++){
			if (tortoise[i][j] == '#'){
				newmap[tortoise_start_x + i][tortoise_start_y + j] = tortoise[i][j];
			}
		}
	}
}

void PlaneShoot()
{
	int oldlife = life2;
	PlaneBlast();
	for (int i = 0; i < map_x - plane_x; i++){
		for (int j = 0; j < map_y; j++){
			if (newmap[i][j] == '|'){
				newmap[i][j] = ' ';
				if (newmap[i - 1][j] == ' '&&i)newmap[i - 1][j] = '|';
				if (newmap[i - 1][j] == '#')life2--;
			}
		}
	}

	if (oldlife > life2){
		gotoxy(0, 7);
		setcolor(1, 12);
		printf("%03d", life2);
	}

	if (game_time % 2)
	{
		newmap[plane_start_x - 1][plane_start_y + 3] = '|';
		newmap[plane_start_x - 1][plane_start_y + 4] = '|';
	}
}

void TortoiseShoot()
{
	if (rand() % 10 == 0){
		newmap[tortoise_start_x + tortoise_x][tortoise_start_y + 5] = ball1;
		newmap[tortoise_start_x + tortoise_x][tortoise_start_y + 6] = ball1;
	}
	if (game_time % 4 == 0){
		for (int i = 0; i < map_y; i++){
			if (newmap[map_x - 1][i] == ball1)newmap[map_x - 1][i] = ' ';
		}
		for (int i = map_x - 2; i >= 0; i--){
			for (int j = 0; j < map_y; j++){
				if (newmap[i][j] == ball1){
					newmap[i][j] = ' ';
					if (newmap[i + 1][j] != '^')newmap[i + 1][j] = ball1;
				}
			}
		}
	}
}

void PlaneBlast()
{
	if (is_blast == 0)return;
	static int blastmap[map_x][map_y] = { 0 }, depth;
	int walkx[8] = { 0, 0, 1, -1 }, walky[8] = { 1, -1, 0, 0 }, flag = 0;
	depth++;
	if (is_blast == 1){
		depth = 3;
		newmap[plane_start_x - 1][plane_start_y + 3] = '^';
		newmap[plane_start_x - 1][plane_start_y + 4] = '^';
		blastmap[plane_start_x - 1][plane_start_y + 3] = depth;
		blastmap[plane_start_x - 1][plane_start_y + 4] = depth;
		is_blast = 2;
		return;
	}
	for (int i = 0; i < map_x; i++){
		for (int j = 0; j < map_y; j++){
			if (blastmap[i][j] == depth - 3 && newmap[i][j] == '^'){
				newmap[i][j] = ' ';
				flag = 1;
			}
			if (blastmap[i][j] == depth - 1){
				if (newmap[i][j] == '#')life2--;
				for (int k = 0; k < 4; k++){
					if (blastmap[i + walkx[k]][j + walky[k]] == 0 && i + walkx[k] >= 0 && i + walkx[k] < map_x&&j + walky[k] >= 0 && j + walky[k] < map_y){
						blastmap[i + walkx[k]][j + walky[k]] = depth;
						if (newmap[i + walkx[k]][j + walky[k]] == ' ' || newmap[i + walkx[k]][j + walky[k]] == '+')newmap[i + walkx[k]][j + walky[k]] = '^';
					}
				}
				flag = 1;
			}
		}
	}
	if (!flag)
	{
		is_blast = 0;
		memset(blastmap, 0, sizeof(blastmap));
	}

}

void GetContral()
{
	plane_move_x = 0;
	plane_move_y = 0;
	if (_kbhit())
	{
		int temp = _getch();
		if (temp == 'f'&&is_blast == 0) is_blast = 1;
		if (temp == 224)
		{
			temp = _getch();
			switch (temp)
			{
			case 72://上
				plane_move_x = -1;
				break;
			case 80://下
				plane_move_x = 1;
				break;
			case 75://左
				plane_move_y = -1;
				break;
			case 77://右
				plane_move_y = 1;
				break;
			}
		}
	}
}

void CreateFrame()
{
	for (int i = 0; i < map_y + 4; i += 2){
		gotoxy(0, i);
		printf("─");
		gotoxy(map_x + 1, i);
		printf("─");
	}
	for (int i = 1; i < map_x + 1; i++){
		gotoxy(i, 0);
		printf("│");
		gotoxy(i, map_y + 2);
		printf("│");
	}
	gotoxy(0, map_y + 2);
	printf("┐");
	gotoxy(0, 0);
	printf("┌");
	gotoxy(map_x + 1, map_y + 2);
	printf("┘");
	gotoxy(map_x + 1, 0);
	printf("└");
}

void Init()
{
	srand(time(0));

	system("color 1f");
	CreateFrame();

	setcolor(1, 13);
	gotoxy(0, map_y / 2 + 1);
	printf("雷霆战机");

	setcolor(1, 12);
	gotoxy(0, 2);
	printf("生命:%03d", life2);
	gotoxy(map_x + 1, 2);
	printf("生命:%03d", life1);
	memset(map, ' ', sizeof(map));
	memset(newmap, ' ', sizeof(newmap));

	for (int i = 0; i < plane_x; i++)
	{
		for (int j = 0; j < plane_y; j++)
		{
			if (plane1[i][j] == '*')newmap[plane_start_x + i][plane_start_y + j] = plane1[i][j];
		}
	}
	for (int i = 0; i < tortoise_x; i++)
	{
		for (int j = 0; j < tortoise_y; j++)
		{
			if (tortoise[i][j] == '#')newmap[tortoise_start_x + i][tortoise_start_y + j] = tortoise[i][j];
		}
	}
	Print();
	Sleep(20);
}

void Begin()
{
	system("color F0");
	gotoxy(6, 28);
	printf("抵制不良游戏,拒绝盗版游戏。");
	gotoxy(8, 28);
	printf("注意自我保护,谨防受骗上当。");
	gotoxy(10, 28);
	printf("适度游戏益脑,沉迷游戏伤身。");
	gotoxy(12, 28);
	printf("合理安排时间,享受健康生活。");
	Sleep(2000);
	system("cls");


	system("color e9");
	gotoxy(2, 32);
	printf("雷霆战机");
	Sleep(500);
	gotoxy(5, 28);
	setcolor(14, 0);
	printf("请把界面调成最大化!");
	Sleep(500);
	gotoxy(10, 50);
	printf("制作人:圣诞老头");
	Sleep(1000);
	gotoxy(22, 29);
	system("pause");
	system("cls");
}

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

void setcolor(int BackColor, int ForeColor)
{
	HANDLE winHandle; //句柄 
	winHandle = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置文字颜色
	SetConsoleTextAttribute(winHandle, ForeColor + BackColor * 0x10);
	// 0-黑 1-蓝 2-绿 3-浅绿 4-红 5-紫 6-黄 7-白 8-灰 9-淡蓝 
	//10-淡绿 11-淡浅绿  12-淡红 13-淡紫 14-淡黄 15-亮白 
}

 

以下是一个简单的雷霆战机游戏的C语言程序,仅供参考: ``` #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <conio.h> #include <windows.h> #define WIDTH 50 #define HEIGHT 25 #define PLANE_WIDTH 5 #define PLANE_HEIGHT 3 void gotoxy(int x, int y); void draw_plane(int x, int y); void draw_enemy(int x, int y); void clear_bullet(int x, int y); void draw_bullet(int x, int y); void clear_enemy(int x, int y); void move_enemy(int *x, int *y); void move_bullet(int *x, int *y, int *state); bool is_hit(int x1, int y1, int x2, int y2); void game_over(); int main() { int score = 0; int plane_x = WIDTH / 2 - PLANE_WIDTH / 2; int plane_y = HEIGHT - 3; int enemy_x = rand() % (WIDTH - 2) + 1; int enemy_y = 1; bool is_game_over = false; // 隐藏光标 CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); // 绘制边框 for (int i = 0; i < WIDTH; i++) { gotoxy(i, 0); printf("#"); gotoxy(i, HEIGHT - 1); printf("#"); } for (int i = 1; i < HEIGHT - 1; i++) { gotoxy(0, i); printf("#"); gotoxy(WIDTH - 1, i); printf("#"); } // 绘制飞机 draw_plane(plane_x, plane_y); while (!is_game_over) { // 移动敌机 move_enemy(&enemy_x, &enemy_y); // 绘制敌机 draw_enemy(enemy_x, enemy_y); // 发射子弹 if (kbhit()) { int key = getch(); if (key == ' ') { int bullet_x = plane_x + PLANE_WIDTH / 2; int bullet_y = plane_y - 1; int bullet_state = 1; draw_bullet(bullet_x, bullet_y); while (bullet_y > 0) { clear_bullet(bullet_x, bullet_y); move_bullet(&bullet_x, &bullet_y, &bullet_state); draw_bullet(bullet_x, bullet_y); // 判断是否击中敌机 if (is_hit(bullet_x, bullet_y, enemy_x, enemy_y)) { score++; gotoxy(WIDTH + 2, 2); printf("Score: %d", score); clear_enemy(enemy_x, enemy_y); enemy_x = rand() % (WIDTH - 2) + 1; enemy_y = 1; break; } } } } // 判断是否被撞击 if (is_hit(plane_x, plane_y, enemy_x, enemy_y)) { is_game_over = true; } Sleep(50); } game_over(); return 0; } void gotoxy(int x, int y) { COORD pos = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } void draw_plane(int x, int y) { gotoxy(x, y); printf(" %c ", 219); gotoxy(x, y + 1); printf(" %c%c%c ", 219, 219, 219); gotoxy(x, y + 2); printf("%c %c %c", 219, 219, 219); } void draw_enemy(int x, int y) { gotoxy(x, y); printf("%c%c%c", 30, 40, 30); gotoxy(x, y + 1); printf("%c%c%c", 94, 88, 94); gotoxy(x, y + 2); printf("%c%c%c", 94, 88, 94); } void clear_bullet(int x, int y) { gotoxy(x, y); printf(" "); } void draw_bullet(int x, int y) { gotoxy(x, y); printf("|"); } void clear_enemy(int x, int y) { gotoxy(x, y); printf(" "); gotoxy(x, y + 1); printf(" "); gotoxy(x, y + 2); printf(" "); } void move_enemy(int *x, int *y) { clear_enemy(*x, *y); (*y)++; if (*y >= HEIGHT - 1) { *x = rand() % (WIDTH - 2) + 1; *y = 1; } } void move_bullet(int *x, int *y, int *state) { if (*state == 1) { (*y)--; if (*y <= 0) { *state = 0; } } } bool is_hit(int x1, int y1, int x2, int y2) { return (x1 >= x2 && x1 <= x2 + 2 && y1 >= y2 && y1 <= y2 + 2); } void game_over() { system("cls"); gotoxy(WIDTH / 2 - 5, HEIGHT / 2); printf("Game Over!"); getch(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值