目录
声明:本游戏参考《c语言课程设计与游戏开发实践教程》。
前言
视频效果如下
game2.3小鸟
小鸟的图案非常简单,可以通过空格来控制小鸟上下飞行并且穿过墙壁缝隙,通过一个缝隙记一分,撞墙了就game over。
本游戏知识点
基础知识点:printf输出,for语句,if语句,while循环,函数的自定义和引用,各种运算符,定义全局变量。
(可能的)拓展知识点:gotoxy函数,rand函数,方便的getch函数,kbhit函数,隐藏光标函数,sleep函数。
很多拓展知识在我上一篇文章http://t.csdn.cn/4XvHr都有介绍。
上一篇没讲解的放在这一篇来讲。
制作一个可控制上下飞行的小鸟
画面效果概述:小鸟的图案可以是任意一个符号,小鸟在没收到操作时会自由下落,按空格键可以让小鸟往上飞。
Sleep函数
- 属于windows.h函数库。根据不同系统和编译器,有时候也写做cwindow.h。头文件加上#include <windows.h>或者#include<cwindow.h>。
- 作用:在此处让程序运行暂缓***毫秒。
- 使用:Sleep();S是大写,括号内数字默认单位是毫秒。(根据不同系统和编译器,有时候也有可能是小写s)。
下面是代码和详细解析
#include <stdlib.h> //rand函数库
#include <stdio.h>
#include <windows.h> //sleep函数库
#include <conio.h> //kbhit函数,rand函数库
//定义全局变量
int high,width;//画面大小
int x,y;//鸟的位置坐标
void gotoxy(int x,int y) //作用类似是清屏函数,写法固定
{
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
void start() //赋值
{
high=15;
width=32;
x=width/8;//小鸟横坐标
y=0;
}
void show() //显示画面
{
gotoxy(0,0); //每次光标回到(0,0),我们认为起到了清屏作用
int i,n;
for(i=0;i<high;i++){
for(n=0;n<width;n++){
if((i==y)&&(n==x))
printf("@"); //显示小鸟
else
if(i==high-1)
printf("-"); //显示画面最低底线
else
printf(" ");
}
printf("\n");
}
}
void unput()
{
y++; //小鸟每循环一次下落一行
Sleep(200); //降低小鸟下落速度
}
void input() //可通过输入控制小鸟的函数
{
char put;
if(kbhit())
{
put=getch();
if(put==' ') //按空格让小鸟向上移动
y-=2;
}
}
void HideCursor() //隐藏光标函数
{
CONSOLE_CURSOR_INFO cursor_info= {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
int main() //主函数
{
start();
while(1) //无限循环
{
show();
unput();
input();
HideCursor();
}
return 0;
}
添加障碍墙,并且让障碍墙移动、重现
画面效果概述:这一步会添加一个有缝隙的障碍墙,墙体自动匀速向左移动,玩家通过控制小鸟上下飞行穿过空隙。墙体消失后可在初始位置重现,中间空隙的位置每次都随机出现。
- 这个函数出现的rand函数用法比较特殊,只有文件名后缀是.cpp才能正常运行,是.c的话会报错
下面展示代码🐎
#include <stdlib.h> //rand函数
#include <stdio.h>
#include <windows.h> //sleep函数
#include <conio.h> //kbhit函数,getch函数
//定义全局变量
int high,width;//画面大小
int x,y;//鸟的位置
int a,b,c;//障碍物
int bar_x,bar_down,bar_top;//障碍物相关坐标。
//bar是障碍物的意思。bar_x是障碍物初始位置横坐标,后两个是空隙上下限
void gotoxy(int x,int y)//清屏
{
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
void start()
{
high=15;
width=32;
x=width/8;
y=0;
bar_x=width/2;
bar_down=high/2;
bar_top=high/3;
}
void show()
{
gotoxy(0,0);//conio.h函数
int i,n;
for(i=0;i<high;i++){
for(n=0;n<width;n++){
if((i==y)&&(n==x))
printf("@");
else
if(i==high-1)
printf("-");
else
if((n==bar_x)&&((i<bar_top)||(i>bar_down)))
printf("*");
else
printf(" ");
}
printf("\n");
}
}
void unput()
{
y++;
bar_x--;
Sleep(200);//windows.h函数
if(x==bar_x)
{
if((y>bar_down)||(y<bar_top)) //如果小鸟撞墙
{
printf("game over!\n");
system("pause");
exit(0); //程序会被强制结束
}
}
//这段代码让墙体小时候在原位置重现,并且空隙位置会随机变化
if(bar_x<=0){
bar_x=width;
int temp=rand()%int(high*0.8); //这种格式的rand函数只有文件后缀是.cpp才正常运行
bar_down=temp+high/10;
bar_top=temp-high/10;
}
}
void input()
{
char put;
if(kbhit())
{
put=getch();
if(put==' ')
y-=2;
}
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info= {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
int main() //主函数
{
start();
while(1)
{
show();
unput();
input();
HideCursor();
}
return 0;
}
补充计分功能&&呈现完整代码
画面效果概述:小鸟每成功通过一次空隙,下方计数器数字+1。
如下是完整代码
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <conio.h>
//定义全局变量
int high,width;//画面大小
int x,y;//鸟的位置
int a,b,c;//障碍物
int score;
int bar_x,bar_down,bar_top;//障碍物相关坐标
void gotoxy(int x,int y)//清屏
{
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}
void start()
{
high=15;
width=32;
x=width/8;
y=0;
bar_x=width/2;
bar_down=high/2;
bar_top=high/3;
score=0;
}
void show()
{
gotoxy(0,0);
int i,n;
for(i=0;i<high;i++){
for(n=0;n<width;n++){
if((i==y)&&(n==x))
printf("@");
else
if(i==high-1)
printf("-");
else
if((n==bar_x)&&((i<bar_top)||(i>bar_down)))
printf("*");
else
printf(" ");
}
printf("\n");
}
printf("穿过障碍物次数为%d次",score);
}
void unput()
{
y++;
bar_x--;
Sleep(200);
if(x==bar_x)
{
if((y>bar_down)||(y<bar_top))
{
printf("game over!\n");
system("pause");
exit(0);
}
else
score++;
}
//这段代码暂时不理解
if(bar_x<=0){
bar_x=width;
int temp=rand()%int(high*0.8);
bar_down=temp+high/10;
bar_top=temp-high/10;
}
}
void input()
{
char put;
if(kbhit())
{
put=getch();
if(put==' ')
y-=2;
}
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info= {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
int main() //主函数
{
start();
while(1)
{
show();
unput();
input();
HideCursor();
}
return 0;
}
大家学废了吗?
如果有不懂的地方,随时评论区或者私信问我😉
就酱,拜拜~