简陋版C语言打飞机游戏代码

简陋版C语言打飞机游戏代码

代码实例qingyu2020-09-19 3:29:391078A+A-

  C语言DOS下的黑屏简陋版打飞机游戏

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
//合理的游戏框架:

//函数外全局变量定义
int positon_x,position_y;//飞机位置
int bullet_x,bullet_y;  //子弹位置
int high,width;       //游戏画面尺寸
int enemy_x,ememy_y;  //敌机位置
int score;

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() //隐藏光标 
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut, &cci);
cci.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &cci);
}

void  startup()// 数据初始化
{
	high = 28;
	width = 30;

	positon_x = high/2;
	position_y = width/2;

	bullet_x = -1;
	bullet_y = position_y;

	enemy_x = 0;
	ememy_y = width/2;
	HideCursor();
}


void show()   //显示画面
{
	int i,j;
	gotoxy(0,0);
	
	for(i=0;i<high;i++)
	{
		for(j=0;j<width;j++)
		{

			if((i==positon_x)&&(j==position_y))
				printf("*"); //输出飞机
			else if((i==bullet_x)&&(j==bullet_y))
				printf("|"); //输出子弹
			else if((i==enemy_x)&&(j==ememy_y))
				printf("@"); //输出敌机
			else
				printf(" "); //输出空格

		}
		printf("\n");
	}
	printf("得分:%d",score);
}

void updateWithoutInput() //与用户无关的更新
{

	static int speed = 0;
	if((bullet_x==enemy_x)&&(bullet_y==ememy_y))
	{
		score++;
		enemy_x = 0;
		ememy_y = rand() % width;
		bullet_x =-1;
	}
	if(speed<10)
		speed++;
	if(bullet_x>-1)
		bullet_x--;

	if(enemy_x>high)
	{	
		enemy_x = 0;
		ememy_y = rand() % width;
	}
	else
	{  
		if(speed==10)
		{
			enemy_x++;
			speed = 0;
		}
	}
}
void updateWithInput()   //与用户输入有关的更新
{
	char input;
	if(kbhit())           //当键盘按键时执行
	{
		input = getch();
		if(input=='a')
			position_y--;
		if(input=='d')
			position_y++;
		if(input=='w')
			positon_x--;
		if(input=='s')
			positon_x++;
		if(input==' ')
		{
			bullet_x = positon_x - 1;
			bullet_y =position_y;
		}
	}
}



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


点击这里复制本文地址 欢迎来到大黄鸡源码分享网
qrcode

大黄鸡源码编程网 © All Rights Reserved.  
网站备案号:闽ICP备18012015号-4
Powered by Z-BlogPHP
联系我们| 关于我们| 广告联系| 网站管理