【二、飞机游戏(2)】

Source

【二、飞机游戏(2)】

本节我们将在上一节的基础上对飞机游戏进行改造完善。

基本框架

从本节起,为了避免把所有代码都放进main函数而使得代码看起来臃肿,我们将通过以下基本框架来实现游戏内容。

//全局变量定义
int main(){
    
      
	startup(); //游戏初始化
	while(1) //游戏循环体
	{
    
      
		show(); //显示界面
		updateWithoutInput(); //与输入无关的更新
		updateWithInput(); //与输入有关的更新
	}
	return 0;
}

代码重构

现在我们要对上一节的代码通过基本框架来重构一遍。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
//全局变量
int position_x,position_y; //飞机坐标
int high,width; //画面尺寸

void startup() //数据初始化
{
    
      
	high = 20;
	width = 30;
	position_x = high/2;
	position_y = width/2;
}

void show() //显示画面
{
    
      
	system("cls"); //清屏函数,linux使用clear命令
	int i,j;
	for(i=0; i<high; i++){
    
      
		for(j=0; j<width; j++){
    
      
			if((i==position_x)&&(j==position_y))
				printf("*"); //输出飞机 * 
			else
				printf(" "); //输出空格
		}
		printf("\n");
	}
}

void updateWithoutInput() //与输入无关的更新
{
    
      
}

void updateWithInput() //与输入有关的更新
{
    
      
	char input;
	if(kbhit()) //判断是否有输入
	{
    
      
		input = getch();
		if(input=='a')
			position_y --;
		if(input=='d')
			position_y ++;
		if(input=='w')
			position_x --;
		if(input=='s')
			position_x ++;		
	}
}

int main(){
    
      
	startup();
	while(1){
    
      
		show();
		updateWithoutInput();
		updateWithInput();
	}
	return 0;
}

新款子弹

之前我们实现的激光是覆盖整条线的,对于敌人来说比较不友好(反正挨打的是敌人),那么为了保证游戏体验(并没有),我们在此给游戏加一个新款子弹。

可以设置初始化子弹坐标:

// 子弹在飞机上方
bullet_x = position_x - 1;
// 子弹 y 坐标不变
bullet_y = position_y;

// 每次循环子弹往上飞 1
bullet_x --; 

所以我们需要在 show() 函数 与 update 中加入控制子弹的代码:

// 定义子弹坐标变量
int bullet_x,bullet_y;

//初始化子弹
bullet_x = -1;
bullet_y = position_y;

// 写在输出循环中
if ...
else if ((i==bullet_x)&&(j==bullet_y))
	printf("|"); //输出子弹
else ...

// 子弹前进
void updateWithoutInput()
{
    
      
	if(bullet_x > -1)
		bullet_x --;
}

// 写在用户输入判断中
if(input == ' ')
{
    
      	
	// 子弹在飞机上方
	bullet_x = position_x - 1;
	// 子弹 y 坐标不变
	bullet_y = position_y;
}

敌机【终于来了】

用“@”表示敌机,用 enemy_xenemy_y 表示敌机坐标,使用和加入子弹大致一样的方法加入敌机。

注意:敌机的移动不能和子弹那样,不然每回你只能看到一个大黑耗子跑过去了。

// 敌机位置
int enemy_x,enemy_y;

// 数据初始化
enemy_x = 0;
enemy_y = position_y;

// 加入输出循环
if ...
else if ((i==enemy_x)&&(j==enemy_y))
	printf("@");
else ...

// 控制敌机移动
static int speed = 0;
if(speed<10)
	speed ++;
if(speed == 10){
    
      
	enemy_x ++;
	speed = 0;
}

可以看到,在实现敌机移动时,我们为了避免出现大黑耗子嗖一下就过去的问题使用了 static 关键字来定义一个静态变量 speed 从而实现敌机缓慢移动。

static 定义的局部变量在每次循环不会重新定义它的值,第一次循环会根据语句把 speed 定义为 0 ,而在之后的循环中不会重新执行该赋值语句。

每次循环判断当其值小于 10 时则加一,并继续以后语句。若其值等于 10 ,则将敌机 x 坐标加一(也就是敌机移动),并重新将 speed 归零。

击杀与得分

没错,在完成以上代码之后你就会发现:
屏幕上出现了两个锁血挂!!
怎么打都打不死,而且一个飞着飞着就没了!

所以我们需要增加击杀功能,当然还有得分(击杀获得)。

而且在每次击杀以后都要生成新的敌机(其实就是敌机坐标重新赋值)。

// 加入updateWithoutInput更新循环
if((bullet_x == enemy_x)&&(bullet_y == enemy_y))
{
    
       //子弹击中敌机
	score ++; //得分加一
	// 敌机刷新
	enemy_x = -1;
	enemy_y = rand() % width;
	bullet_x = -2; //子弹无效,如果你希望子弹能穿透的话可以去掉
}
if(enemy_x > high) // 敌机飞出边界
{
    
      
	// 敌机刷新
	enemy_x = -1;
	enemy_y = rand() % width;
	// 可选,敌机出界得分减一
	score --;
}

// 定义得分并显示
int score;
// 显示部分加在 show() 函数尾部
printf("得分: %d\n", score);

清屏升级

在实现以上代码后,我们发现游戏时屏幕闪烁很严重,这里提供优化清屏的方法。

//清屏前移动光标
#include <windows.h>

void gotoxy(int x, int y){
    
      
	HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
	CROOD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
//解决光标闪烁
void HideCursor(){
    
      
	CONSOLE_CURSOR_INFO cursor_info = {
    
      1, 0}; //第二个值为0表示隐藏光标
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

感谢阅读,喜欢就点个赞吧~

在这里插入图片描述