#打飞机游戏

打飞机游戏

  这个游戏的原理比较简单,我们要实现这个游戏,首先要实现的就是怎么打印出飞机,和怎么操控飞机的移动,显然这些用简单的循环语句和分支语句就能实现。而敌机出现的话就更简单了,只要随机生成敌机的水平坐标,竖直坐标就让敌机从顶部落下(当然,你也可以随机生成竖直坐标),我们只需要控制随机数的范围就好了  例如 rand()%30; 随机生成0-29范围内的随机数。
  其次,我们要做的就是如何攻击,当飞机发射子弹时,bullet_x会等于plane_x,bullet_y = plane_y - 1;我们要想子弹往上移动,只需要bullet_y --,就能实现。
  最后,我们就只需要判断,子弹攻击到敌机,和飞机碰到敌机。当子弹坐标等于敌机坐标时,敌机死亡,当飞机碰到敌机时,游戏结束。
  原理很简单,这里就不多做解释了,上代码。
#include<stdio.h>
#include<windows.h>
#include<conio.h>

int plane_x, plane_y;			//飞机坐标
int bullet_x, bullet_y;			//子弹坐标
int target_x, target_y;			//靶子坐标
int height = 21;	//界面尺寸
int width = 41;	
int speed;			//控制敌机下降速度
int kill;			//判断靶子是否被击中
int score;			//得分
int flag;			//判断是否死亡

void start() {		//初始化
	int i;
	for (i = 0; i < height - 3; i++) {
		if (i == height / 2 - 1) {
			printf("     WASD分别控制\n");
			printf("     上下左右移动\n");
			printf("       空格射击\n");
			printf("    (按任意键继续)");
		}
		else
			printf("\n");
	}
	plane_x = width / 2;
	plane_y = height / 2;
	target_x = rand() % 41;
	target_y = 0;
	speed = 15;
	kill = 0;
	score = 0;
	flag = 0;
	_getch();
}
void hideCursor() {
	CONSOLE_CURSOR_INFO cursor_info = { 1,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 show() {			//展示界面
	int i, j;
	gotoxy(0, 0);
	for (j = 0; j < height; j++) {			//打印出游戏界面
		for (i = 0; i < width; i++) {
			if (i == plane_x && j == plane_y)	//判断在什么位置输出飞机
				printf("*");
			else if (i == target_x && j == target_y)	//输出敌机
				printf("@");
			else if (i == bullet_x && j == bullet_y) {	//输出子弹
				printf("|");							
				bullet_y--;					
			}	
			else
				printf(" ");
		}
		printf("|\n");
	}
	for (i = 0; i <= width / 2; i++) {
		printf(" ̄");
	}
	printf("\n");
	printf("当前得分:%d\n", score);
}
void updateWithInput() {		//飞机移动和射击
	char input;
	if (_kbhit()) {
		input = _getch();
		switch (input) {
		case 's':
		case 'S': plane_y++;
			break;
		case 'w':
		case 'W': plane_y--;
			break;
		case 'a':
		case 'A': plane_x--;
			break;
		case 'd':
		case 'D': plane_x++;
			break;
		case ' ':
			bullet_x = plane_x;
			bullet_y = plane_y - 1;
			break;
		}
		if (plane_x < 0)		//判断飞机是否超出界面
			plane_x = 0;
		else if (plane_x > width - 1)
			plane_x = width - 1;
		if (plane_y < 0)
			plane_y = 0;
		else if (plane_y > height - 1)
			plane_y = height - 1;
	}
}

void updateWithoutInput() {	//子弹和靶子移动
	if (plane_x == target_x && plane_y == target_y)		//飞机碰到敌机,死亡
		flag = 1;
	if (bullet_x == target_x && bullet_y == target_y) {	//子弹碰到敌机
		score += 5;
		kill = 1;
		bullet_y = -1;
	}
	if (kill == 1) {				//敌机死亡,重新生成敌机
		target_x = rand() % 20;
		target_y = 0;
		kill = 0;
	}
	static int t = 0;				//静态常量,用来控制敌机的速度
	if (t == speed) {
		target_y++;
		t = 0;
	}
	else
		t++;
	if (target_y > height)
		kill = 1;
}

int main() {
	system("color 0A");
	hideCursor();
	char input;
	int k = 1;
	while (1) {
		if (k == 1) {
			start();
			k = 0;
		}
		else {
			gotoxy(0, 0);
			for (int i = 0; i < 10; i++) {
				printf("\n");
			}
			printf("游戏结束,最终得分:%d(按任意键继续)", score);
			_getch();
			system("cls");		//清屏
			start();
		}
		while (!flag) {
				show();
				updateWithoutInput();
				updateWithInput();
		}
	}
	return 0;
}

发布了3 篇原创文章 · 获赞 2 · 访问量 116

猜你喜欢

转载自blog.csdn.net/Dreamer_ZC/article/details/105273475