ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-10-04 12:34:49  阅读:227  来源: 互联网

标签:enemy canvas bullet C语言 二维 plane 数组 col row


#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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有