太空大战

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

#define high 40 //不加;
#define width 20

int position_x = width/2;
int position_y = high/2;
int enemy_x = width/2;
int enemy_y = 0;
int bullet_x = -1;
int bullet_y = high/2;
int score = 0;

void show();
void update_with_input();
void update_without_input();
void gotoxy(int x, int y);//VS中未包括gotoxy函数,必须自己实现;使光标定位到指定位置
void HideCursor();//隐藏光标

int main()
{
	
	while (1)
	{
		HideCursor();
		show();
		update_with_input();
		update_without_input();
	}
	return 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 show()
{
	int i, j;
	gotoxy(0, 0);
	for (i = 0; i <= high; i++)
	{
		for (j = 0; j <= width; j++)
		{
			if (i == position_y && j == position_x) printf("*");
			else if (i == enemy_y && j == enemy_x) printf("@");
			else if (i == bullet_y && j == bullet_x) printf("|");
			else printf(" ");
		}
		printf("\n");
	}
	printf("score:%d\n", score);
}

void update_with_input()
{
	char input;
	if (_kbhit())//有输入为1,没输入为0
	{
		input = _getch();
		switch (input)
		{
		case 'a': //单引号而不是双引号
			position_x--;
			break;
		case'd':
			position_x++;
			break;
		case'w':
			position_y--;
			break;
		case's':
			position_y++;
			break;
		case' ':
			bullet_x = position_x;
			bullet_y = position_y - 1;
		default:
			break;
		}
	}
}

void update_without_input()
{
	if (bullet_y > -1) bullet_y--;
	if (bullet_x == enemy_x && enemy_y == bullet_y)
	{
		score++;
		enemy_x = rand() % width;
		enemy_y = 0;
		bullet_x = -1;
	}
	if (enemy_y > high)
	{
		enemy_x = rand() % width;
		enemy_y = 0;
	}
	static int delay = 0;
	if (delay < 10) delay++;
	else if (delay == 10)
	{
		enemy_y++;
		delay = 0;
	}
	if (position_x > width) position_x = width;
	if (position_x < 0)position_x = 0;
	if (position_y < 0)position_y = 0;
	if (position_y > high)position_y = high;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值