摘要:本文主要向大家介绍了C/C++知识点之C语言-飞机游戏,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之C语言-飞机游戏,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
整体思路
main函数
在这里,主要是使用一个简易的游戏框架,来减小开发游戏时的难度
int main(){ startup();//初始化
while (1) //游戏循环进行
{ show();//显示画面
updateWitoutIput();//与用户输入无关的更新
updateWithInput(); //与用户输入有关的更新
} system("pause");
return 0;
}123456789101112
全局变量的定义
为了方便之后代码的书写,在这里,我们使用宏定义代替部分全局变量
//全局变量的定义#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; //得分123456789
全局变量的初始化
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;
}1234567891011121314151617
显示函数show()
输出飞机、敌机、子弹等
使用循环语句,在满足飞机、敌机、子弹位置条件时输出对应的图案(飞机: * 敌机: @ 子弹: | ),其余位置输出‘ ’或‘\n’
//输出每一行每一列for (i = 0; i < HIGH; i++) //行
{ for (j = 0; j < WIDTH; 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");
}123456789101112131415161718192021222324
输出得分
printf("得分:%d", score);1
清屏
在输出输出飞机等图标时,需要使用清屏函数使得光标回到 (0,0) 处,方便下一次输入
清屏函数
#include <windows.h>system("cls");12
使光标回到 (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);
}12345678910
隐藏光标显示
为了防止光标闪烁的过于频繁,我们使用隐藏光标显示函数隐藏光标
void HideCursor()//隐藏光标显示函数{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}//该函数只需调用一次即可123456
与用户输入无关的部分
子弹上落
if (bullet_x > -1) //让子弹向上落{
bullet_x--;}1234
敌机下落,到达底部后,生成新的敌机
//创建一个静态变量,当静态变量满足一定条件时,敌机下落一次,这样可以控制敌机下降的速度同时不影响飞机移动的速度static int speed = 0; //控制敌机下落速度if (speed < NUM) //每进行NUM次敌机下落一次{
speed++;
}else{
enemy_x++; //敌机下落一次
speed = 0;
}if (enemy_x > HIGH) //敌机一直下落到底部{
enemy_x = -1; //生成新的飞机
enemy_y = rand() % WIDTH;
}12345678910111213141516
命中敌机,得分+1,同时生成新的敌机
if (bullet_x == enemy_x && bullet_y == enemy_y) //命中{
score++; //得分+1
enemy_x = -1; //生成新的飞机
enemy_y = rand() % WIDTH;
bullet_x = -1; //让子弹直接出现屏幕外,直到下一次发射子弹}123456789
与用户输入有关的部分
在这里,我们可以使用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;
}1234567891011121314
getch函数
if (_kbhit()) //kbhit()函数:检查当前是否有键盘输入,若有则返回一个非0值,否则返回0,头文件<conio.h>{
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;
}
}1234567891011121314151617
在这里,我们选择使用getch()函数,从而使得程序更加方便(减少用户回车的输入及屏幕上的显示)
注意事项
在实现代码时需要注意在使用各个函数时对头文件的调用
尽量减少对全局变量的创建
完整代码
#include <stdio.h>#include <windows.h>#include <stdlib.h>#include <conio.h>#include <time.h>//全局变量的定义#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 < HIGH; i++) //行
{ for (j = 0; j < WIDTH; 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 < NUM) //每进行NUM次敌机下落一次
{
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;
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号