其他分享
首页 > 其他分享> > C语言飞机大战 二维数组版

C语言飞机大战 二维数组版

作者:互联网

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

#define WIDTH	40
#define HEIGHT	20

#define SPACE 0
#define PLANE 1
#define ENEMY 2
#define BULLET 3
#define VEDGE 4
#define HEDGE 5

#define MOVEUP	-1
#define MOVEDOWN 1
#define MOVELEFT -1
#define MOVERIGHT 1

int score = 0;
int plane_col, plane_row;//?¡è¡§|?¡§2????
int bullet_col, bullet_row;//?¨¢¡§?|¨¬?£¤|¨¬?????
int area_height, area_width;//¡§????¡è??¡§?¡§¡ã  0-n-1
int enemy_col, enemy_row;
int enemy_vh, enemy_vv;
int canvas[HEIGHT + 1][WIDTH + 1] = { 0 };
bool exitBullet; 
bool exitEnemy;

void gotoxy(int x, int y) {//??¨¦D? 
	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 startup()//3?¡§o???£¤ 
{
	area_height = 20;
	area_width = 30;

	plane_col = 14;
	plane_row = 10;

	bullet_col = 0;
	bullet_row = -1;

	enemy_col = rand() % area_width;
	enemy_row = 0;
	enemy_vh = MOVEDOWN;
	enemy_vv = 0;

	//init array2d
	int i, j;
	for (i = 0; i <= area_height; i++)
		for (j = 0; j <= area_width; j++)
		{
			canvas[i][j] = SPACE;
			if (j == area_width)
				canvas[i][j] = VEDGE;
			if (i == area_height)
				canvas[i][j] = HEDGE;
		}
	canvas[plane_row][plane_col] = PLANE;
	canvas[enemy_row][enemy_col] = ENEMY;
	exitBullet = false;
	exitEnemy = false;
}

//int[][] planeArray() {
//	
//	a[plane_col][plane_row] = 1;
//	for (int i = plane_col - 2; i < plane_col + 2; i++)
//		a[i][plane_row + 1] = 1;
//	a[plane_col - 1][plane_row + 2] = 1; a[plane_col + 1][plane_row + 2] = 1;
//
//	return a;
//}

void show()
{
	gotoxy(0, 0);
	int i, j;
	//system("cls");
	for (i = 0; i <= area_height; i++)//DD?¨¤¡§|¡§¡è¡§2
	{
		for (j = 0; j <= area_width; j++)//¡§¡éD?¨¤¡§|¡§¡è¡§2
		{
			if (canvas[i][j] == PLANE)
				printf("*");//plane
			else if (canvas[i][j] == HEDGE)
				printf("-");//?¨¤???
			else if (canvas[i][j] == VEDGE)
				printf("|");//?¨¤???
			else if (canvas[i][j] == ENEMY)
				printf("@");//enemy
			else if (exitBullet == true && canvas[i][j] == BULLET)
				printf("^");
			else printf(" ");
		}
		printf("\n");
	}
	printf("score:%d\n", score);

}
void updateWithInput()
{
	char input;
	canvas[plane_row][plane_col] = SPACE;

	if (kbhit()) {
		input = getch();
		switch (input)
		{
		case 'w':
			if (plane_row != 0)
				plane_row--; break;
		case 'a':
			if (plane_col != 0)
				plane_col--; break;
		case 'd':
			if (plane_col != area_width)
				plane_col++; break;
		case 's':
			if (plane_row != area_height)
				plane_row++; break;
		case ' ':
			if (exitBullet == false)//?¡§¡é??¡§¡è???¡§?D?¨¢¡§?|¨¬?£¤
			{
				exitBullet = true;
				bullet_row = plane_row - 1;
				bullet_col = plane_col;
				canvas[bullet_row][bullet_col] = BULLET;
			}
			break;
		default:
			break;
		}
	}
	canvas[plane_row][plane_col] = PLANE;
}

int IsCrash() {
	if (enemy_col == plane_col && enemy_row == plane_row) {
		return 1;
	}
	return 0;
}

void updateWithourInput()
{
	if (exitBullet == true) {
		canvas[bullet_row][bullet_col] = SPACE;
		
		if (bullet_row == 0) {
			exitBullet = false;
			canvas[bullet_row][bullet_col] = SPACE;
		}
		if (exitBullet == true) {
			bullet_row--;
			canvas[bullet_row][bullet_col] = BULLET;
		}

	}
	

	static int count = 0;
	count++;

	if (count == 40) {
		count = 0;
		canvas[enemy_row][enemy_col] = SPACE;
		enemy_row += enemy_vv;
		enemy_col += enemy_vh;
		canvas[enemy_row][enemy_col] = ENEMY;

	}

}

void crack() {

	if (enemy_row == area_height) {
		if(exitBullet == true)
			canvas[bullet_row][bullet_col] = SPACE;
		exitBullet == false;
		
		
		canvas[enemy_row][enemy_col] = SPACE;
		exitEnemy = false;

		enemy_col = rand() % area_height;
	}
	else if (bullet_col == enemy_col && bullet_row == enemy_row + 1) {
		score += 10;
		if (exitBullet == true)
			canvas[bullet_row][bullet_col] = SPACE;
		exitBullet == false;

		canvas[enemy_row][enemy_col] = SPACE;
		exitEnemy = false;

		enemy_col = rand() % area_height;
	}

}

int IsFinish() {
	if (score == 100) {
		system("cls");
		printf("congretulations!!!");
		score = 0;
		_sleep(500);
		system("pause");
		return 1;
	}
	else if (IsCrash() == 1) {
		system("cls");
		printf("you have lost!!!");
		score = 0;
		_sleep(500);
		system("pause");
		return 1;
	}

	return 0;
}

int main()
{
	HideCursor();
	startup();
	while (1)
	{
		show();
		updateWithInput();
		updateWithourInput();
		crack();
		if (IsFinish() == 1) {
			startup();
			continue;
		}
	}
	return 0;
}

标签:enemy,canvas,bullet,C语言,二维,plane,数组,col,row
来源: https://blog.csdn.net/m0_45311187/article/details/120602799