C语言游戏编程之飞机大战

本文介绍了一个基于C语言的简单飞机大战游戏,该游戏利用基本C语法,并着重讲解全局变量的正确使用方法。开发环境为DevC++,确保游戏能够顺利运行。

这是一个最简单的飞机大战游戏,只涉及到少量的c语法知识,理解起来不难。
需要使用大量全局变量,重点把握如何正确使用全局变量
采用devc++编写,该编译软件可完美运行

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define console 20
int width,high,position_x,position_y,bullet_x,bullet_y,score;
int enemy_x,enemy_y,enemy_x1,enemy_y1;
void updatewithoutinput();
void updatewithinput(); 
void startup();
void show();
void gotoxy(int x,int y);//需要调用windows.h里面的函数定义该函数 
void hidecursor();//同上 
int main(void){
	hidecursor();//隐藏光标 
	startup();
	while(1){
		show();
		updatewithoutinput();
		updatewithinput();
	}
	
	return 0;
} 
void hidecursor(){//隐藏光标 
	CONSOLE_CURSOR_INFO cursor_info={1,0};//第二个值为0表示隐藏光标 
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
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(){
	high=20;width=40;
	position_x=width/2;position_y=high/2;
	bullet_x=-1;bullet_y=position_y-1;
	enemy_x=width/2;enemy_y=0; 
	enemy_x1=15;enemy_y1=0;
}
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==position_y+1&&j==position_x-2)//画出机身 
				printf("*****");
			else if(i==position_y+2&&j==position_x-1)//画出机尾 
				printf("* *");
			else if(i==bullet_y&&j==bullet_x)//画出激光 
				printf("|");
			else if(i==enemy_y&&j==enemy_x)//画出敌机 
				printf("@");
			else if(i==enemy_y1&&j==enemy_x1)
				printf("+");
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("得分:%d\n",score);
} 
void updatewithoutinput(){
	if(bullet_y>-1)
		bullet_y--;
	
	if(enemy_y==bullet_y&&enemy_x==bullet_x){//当激光击中敌机时,敌机消失,子弹消失 
		score++;
		bullet_y=-1;
		enemy_x=rand()/width;
		enemy_y=-1;
	}
	if(enemy_y1==bullet_y&&enemy_x1==bullet_x){//当激光击中敌机时,敌机消失,子弹消失 
		score++;
		bullet_y=-1;
		enemy_x1=rand()%width;
		enemy_y1=-1;
	}
	if(enemy_y>high){//当敌机飞出边界时 
		enemy_y=-1;
		enemy_x=rand()%width; 
	}
	if(enemy_y1>high){//当敌机飞出边界时 
		enemy_y1=-1;
		enemy_x1=rand()%width; 
	}
	static int speed;
	if(speed<10)//延缓敌机下落速度 
		speed++;
	if(speed==10){
		enemy_y++;
		enemy_y1++;
		speed=0;
	} 
	//当敌机撞到飞机时,游戏结束 
	if(enemy_y==position_y&&enemy_x==position_x){
		printf("Game Over!\n");
		system("pause");
	} 
	if(enemy_y1==position_y&&enemy_x1==position_x){
		printf("Game Over!\n");
		system("pause");
	} 
	if(enemy_y==position_y+1&&enemy_x>=position_x-2&&enemy_x<=position_x-1){
		printf("Game Over!\n");
		system("pause");
	}
	if(enemy_y==position_y+1&&enemy_x>=position_x+1&&enemy_x<=position_x+2){
		printf("Game Over!\n");
		system("pause");
	}
	if(enemy_y1==position_y+1&&enemy_x1>=position_x-2&&enemy_x1<=position_x-1){
		printf("Game Over!\n");
		system("pause");
	}
	if(enemy_y1==position_y+1&&enemy_x1>=position_x+1&&enemy_x1<=position_x+2){
		printf("Game Over!\n");
		system("pause");
	}
	 
}
void updatewithinput()  // 与用户输入有关的更新
{
	if(kbhit()){
		char input;
		
		input=getch();
		if(input=='a')//飞机位置左移 
			position_x--;
		else if(input=='d')//飞机位置右移 
			position_x++;
		else if(input=='w')//飞机位置上移 
			position_y--;
		else if(input=='s')//飞机位置上移 
			position_y++;
		else if(input==' '){//发射激光 
			bullet_x=position_x;
			bullet_y=position_y-1;
		}
			
	} 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值