c语言飞机借位,使用c语言实现飞机游戏

在这里,我主要使用scanf函数和printf函数来实现一个简单的飞机游戏,并且通过函数的形式实现。

整体思路:main函数

在这里,主要是使用一个简易的游戏框架,来减小开发游戏时的难度int main(){

startup();//初始化

while (1) //游戏循环进行

{

show();//显示画面

updateWitoutIput();//与用户输入无关的更新

updateWithInput(); //与用户输入有关的更新

}

system('pause');

return 0;

}

全局变量的定义

为了方便之后代码的书写,在这里,我们使用宏定义代替部分全局变量//全局变量的定义

#define HIGH 20  //游戏界面高度

#define WIDTH 30  // 游戏界面宽度

#define NUM 20  //敌机下落速度

int position_x, position_y;  //飞机位置

int bullet_x, bullet_y;  //子弹位置

int enemy_x, enemy_y;  //敌机位置

int score;  //得分

全局变量的初始化void startup()//数据的初始化{

//初始化飞机

position_x = HIGH / 2; //高度

position_y = WIDTH / 2; //宽度

//初始化子弹

bullet_x = -1;  //使子弹出现在屏幕外

bullet_y = position_y;

//初始化敌机

enemy_x = 0;

enemy_y = position_y;

//初始化得分

score = 0;

}

显示函数show()

输出飞机、敌机、子弹等

使用循环语句,在满足飞机、敌机、子弹位置条件时输出对应的图案(飞机: * 敌机: @ 子弹: | ),其余位置输出‘ ’或‘\n’//输出每一行每一列

for (i = 0; i 

{

for (j = 0; j 

{

if (i == position_x && j == position_y)//输出飞机

{

printf('*');

}

else if (i == bullet_x && j == bullet_y)//输出子弹

{

printf('|');

}

else if (i == enemy_x && j == enemy_y)//输出敌机

{

printf('@');

}

else

{

printf(' ');

}

}

printf('\n');

}

输出得分printf('得分:%d', score);

清屏

在输出输出飞机等图标时,需要使用清屏函数使得光标回到 (0,0) 处,方便下一次输入

清屏函数#include 

system('cls');

使光标回到 (0,0)gotoxy(0, 0);

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);

}

隐藏光标显示

为了防止光标闪烁的过于频繁,我们使用隐藏光标显示函数隐藏光标oid HideCursor()//隐藏光标显示函数{

CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}

//该函数只需调用一次即可

与用户输入无关的部分

子弹上落if (bullet_x > -1) //让子弹向上落

{

bullet_x--;

}

敌机下落,到达底部后,生成新的敌机//创建一个静态变量,当静态变量满足一定条件时,敌机下落一次,这样可以控制敌机下降的速度同时不影响飞机移动的速度

static int speed = 0; //控制敌机下落速度

if (speed 

{

speed++;

}

else

{

enemy_x++;  //敌机下落一次

speed = 0;

}

if (enemy_x > HIGH)  //敌机一直下落到底部

{

enemy_x = -1;   //生成新的飞机

enemy_y = rand() % WIDTH;

}

命中敌机,得分+1,同时生成新的敌机if (bullet_x == enemy_x && bullet_y == enemy_y)  //命中

{

score++;  //得分+1

enemy_x = -1;   //生成新的飞机

enemy_y = rand() % WIDTH;

bullet_x = -1;  //让子弹直接出现屏幕外,直到下一次发射子弹

}

与用户输入有关的部分

在这里,我们可以使用scanf函数或者getch函数实现在用户输入 ‘w’ ‘s’ ‘a’ ‘d’ 时对上下左右的控制

scanf函数scanf('%c', &input);

if (input == 'w')

position_x--;

if (input == 's')

position_x++;

if (input == 'a')

position_y--;

if (input == 'd')

position_y++;

if (input == ' ')

{

bullet_x = position_x - 1;

bullet_y = position_y;

}

getch函数if (_kbhit())  //kbhit()函数:检查当前是否有键盘输入,若有则返回一个非0值,否则返回0,头文件

{

input = _getch();  //getch()是一个不回显函数,当用户按下某个字符时,函数自动读取,但是不会显示在屏幕上,无需按回车,

if (input == 'w')

position_x--;

if (input == 's')

position_x++;

if (input == 'a')

position_y--;

if (input == 'd')

position_y++;

if (input == ' ')

{

bullet_x = position_x - 1;

bullet_y = position_y;

}

}

在这里,我们选择使用getch()函数,从而使得程序更加方便(减少用户回车的输入及屏幕上的显示)

注意事项

在实现代码时需要注意在使用各个函数时对头文件的调用尽量减少对全局变量的创建

完整代码#include 

#include 

#include 

#include 

#include 

//全局变量的定义

#define HIGH 20  //游戏界面高度

#define WIDTH 30  // 游戏界面宽度

#define NUM 10  //敌机下落速度

int position_x, position_y;  //飞机位置

int bullet_x, bullet_y;  //子弹位置

int enemy_x, enemy_y;  //敌机位置

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 HideCursor()  //隐藏光标显示{

CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);

}

void startup()//数据的初始化{

//初始化飞机

position_x = HIGH / 2; //高度

position_y = WIDTH / 2; //宽度

//初始化子弹

bullet_x = -1;

bullet_y = position_y;

//初始化敌机

enemy_x = 0;

enemy_y = position_y;

//初始化得分

score = 0;

}

void show()//显示画面{

//system('cls'); //清屏函数

gotoxy(0, 0); //使光标回到0,0

int i, j;

for (i = 0; i 

{

for (j = 0; j 

{

if (i == position_x && j == position_y)//输出飞机

{

printf('*');

}

else if (i == bullet_x && j == bullet_y)//输出子弹

{

printf('|');

}

else if (i == enemy_x && j == enemy_y)//输出敌机

{

printf('@');

}

else

{

printf(' ');

}

}

printf('\n');

}

printf('得分:%d', score);

}

void updateWitoutIput()//与用户输入无关的更新{

if (bullet_x > -1) //让子弹向上落

{

bullet_x--;

}

if (bullet_x == enemy_x && bullet_y == enemy_y) //命中敌机

{

score++;  //得分+1

enemy_x = -1;   //生成新的飞机

enemy_y = rand() % WIDTH;

bullet_x = -1;  //让子弹直接出现屏幕外,直到下一次发射子弹

}

static int speed = 0; //控制敌机下落速度

if (speed 

{

speed++;

}

else

{

enemy_x++;

speed = 0;

}

if (enemy_x > HIGH)  //敌机一直下落到底部

{

enemy_x = -1;

enemy_y = rand() % WIDTH;

}

}

void updateWithInput()//与用户输入有关的更新{

//用户输入

char input;

if (_kbhit())

{

input = _getch();

if (input == 'w')

position_x--;

if (input == 's')

position_x++;

if (input == 'a')

position_y--;

if (input == 'd')

position_y++;

if (input == ' ')

{

bullet_x = position_x - 1;

bullet_y = position_y;

}

}

}

int main(){

startup();//初始化

HideCursor();

srand((unsigned)time(NULL));

while (1)

{

show();//显示画面

updateWitoutIput();//与用户输入无关的更新  //更新数据

updateWithInput(); //与用户输入有关的更新  //输入分析

}

system('pause');

return 0;

}

程序效果

4788f178b923cc650c8e44ba7bc30038.png

总结

虽然完成了一个简单的飞机游戏,但是很多的功能都未能实现,如改变飞机的形状,增加游戏的难度,飞机生命的减少等等,任需要继续的努力。

---------------------

作者:木头i

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值