简单打飞机游戏

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


//x是列坐标  y是行坐标
int screen_x , screen_y ;   //画板大小
int plane_x, plane_y;		//飞机位置
int bullet_x, bullet_y;     //子弹位置
int machine_x, machine_y;   //敌人位置
int bullet_flag;            //判定是否发射子弹
int speed ;					//控制敌人速度
int score;                  //玩家分数



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 startup()  // 数据初始化
{
	srand(time(0));
	srand(time(0));
	screen_x = 20;
	screen_y = 20;
	plane_x = screen_x -5;
	plane_y = screen_y -5;
	bullet_x = plane_x ;
	bullet_y = -1;
	machine_x = rand()%20;
	speed = 0;
	score = 0;
	
	machine_y = -1;
	bullet_flag = 0;
}

void show()   // 显示画面
{
	gotoxy(0, 0);
	for (int i = 0; i < screen_y; i++)
	{
		for (int j = 0; j < screen_x; j++)
		{
			if (machine_y == i&&machine_x == j)
				printf("@");

			if (plane_y == i&& plane_x == j)
			{
				printf("*");
			}
			else if (bullet_x == j&&bullet_y == i)       //在这的判断会决定x, y哪个是行坐标,那个是列坐标
			{
				printf("|");
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
	}
	printf("玩家所获分数为%d", score);
}

void updateWithoutInput()  // 与用户输入无关的更新
{
	if (machine_y <= screen_y)   //更新敌人
	{
		speed++;                 //通过这样来控制敌人移动速度,而且可以不影响玩家移动的速度
		if (speed == 10)
		{
			machine_y++;
			speed = 0;
		}
		
	}
	else
	{
		machine_y = -1; 
		machine_x = rand() % 20;
	}

	if (bullet_flag == 1)        //更新子弹
	{
		if (bullet_y > -1)
		{
			bullet_y--;
		}
		else
		{
			bullet_flag = 0;
		}
	}

	if (machine_x == bullet_x&&machine_y == bullet_y)
	{
		score++;
		machine_y = -1;
		machine_x = rand() % 20;
	}

}

void updateWithInput()   // 与用户输入有关的更新
{
	char move_key;
	if (_kbhit())          //判断是否有输入(如果没有这行语句,则每次都需要输入后程序才继续执行,即不输入会卡在getch()这)
	{
		move_key = _getch();

		if (move_key == 'w')
		{
			plane_y--;
		}
		if (move_key == 's')
		{
			plane_y++;
		}
		if (move_key == 'a')
		{
			plane_x--;
		}
		if (move_key == 'd')
		{
			plane_x++;
		}
		if (move_key == ' ')
		{
			bullet_y = plane_y;
			bullet_x = plane_x;
			bullet_flag = 1;
		}
	}
	
}


void main()
{
	startup();    // 数据初始化
	while (1)  //  游戏循环执行
	{
		show();   // 显示画面
		updateWithoutInput();  // 与用户输入无关的更新
		updateWithInput();    // 与用户输入有关的更新
	}
	system("pause");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值