C语言 飞机大战2.0 黑窗口版本

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

int score = 0;
int plane_col, plane_row;//飞机位置
int bullet_col,bullet_row;//子弹的位置
int area_height, area_width;//游戏区域  0-n-1
int enemy_col, enemy_row;
int enemy_vh, enemy_vv;
int a[100][100] = { 0 };

void gotoxy(int x, int y) {//刷新 
	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()//初始化 
{
	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 = 0;
	enemy_vv = 1;
}

//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++)//行遍历
	{
		for (j = 0; j < area_width; j++)//列遍历
		{
			if (i == plane_row && j == plane_col)
			{
				printf("*");
			}
			else if (i == bullet_row && j == bullet_col)
				printf("|");
			else if (i == enemy_row && j == enemy_col)
				printf("@");
			else printf(" ");
		}
		printf("\n");
	}
	printf("score:%d\n",score);
}
void updateWithInput()//交互,控制飞机移动,射击
{ 
	char input;
	//等待用户的输入,交互
	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 (bullet_row < 0)//屏幕里没有子弹
			{
				bullet_row = plane_row - 1;
				bullet_col = plane_col;
			}
			break;
		default:
			break;
		}
	}
}

int IsCrash() {
	//判断我放飞机是否坠毁 
	if (enemy_col == plane_col && enemy_row == plane_row) {
		return 1;
	}
	return 0;
}

void updateWithourInput()//子弹移动与敌人移动 
{
	//更新
	bullet_row--;
	static int count = 0;
	count ++;
	if (count == 40) {
		enemy_row += enemy_vv;
		enemy_col += enemy_vh;
		count = 0;
	}
	
}

void crack() {//击毁敌人 

	if(enemy_row > area_height){
		bullet_row = -1;
		enemy_row = -1;
		enemy_col = rand() % area_height;
	}
	else if (bullet_col == enemy_col && bullet_row == enemy_row) {
		score += 10;
		bullet_row = -1;
		enemy_row = -1;
		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;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值