这篇文章主要为大家详细介绍了C语言实现像素鸟游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C语言实现像素鸟游戏的具体代码,供大家参考,具体内容如下
在进入更复杂的学习之前,我们最后实现一个小游戏——像素鸟。
下落的小鸟
首先我们写好游戏代码框架并实现小鸟下落和上升(按空格)的功能:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>
//全局变量
int high,width; //画面尺寸
int bird_x,bird_y; //小鸟坐标
int barl_y,barl_xTop,barl_xDowm; //障碍物
void gotoxy(int x, int y) //移动光标便于清屏重画
{
HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
CROOD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup() //数据初始化
{
high = 15;
width = 20;
bird_x = 0;
bird_y = width/3;
}
void show() //显示画面
{
gotoxy(0,0);
int i,j;
for(i=0; i<high; i++)
{
for(j=0; j<width; j++)
{
if((i==bird_x)&&(j==bird_y))
printf("@");
else
printf(" ");
}
print("\n"); //每经一次行循环就换行
}
}
void updateWithoutInput()
{
bird_x ++;
sleep(150);
}
void updateWithInput()
{
char input;
if(kbhit()) //判断是否有输入
{
input = getch();
if(input==' ')
bird_x = bird_x - 2;
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
显示障碍物
我们在上一步的基础上完成障碍物的绘制,使用全局变量barl_y, barl_xTop, barl_xDown描述相关量,如图:
加入以下代码片段:
数据初始化
barl_y = width/2;
barl_xDown = high/2;
barl_xTop = high/3;
输出循环
...
else if ((j==barl_y)&&((i>barl_xDown)||(i<barl_xTop)))
printf("*");
...
障碍物移动(在updateWithoutInput里)
barl_y --;
判定碰撞
接下来判定当障碍物 y 坐标到达小鸟位置时是否有碰撞发生,若有,则游戏失败,反之则得分加一。
加入如下代码段:
int score; //全局变量,得分
void startup()
{
...
score = 0;
...
}
void updateWithoutInput()
{
...
if(bird_y == barl_y)
{
if((bird_x>=barl_xTop)&&(bird_x<=barl_xDown))
score ++;
else
{
printf("GG\n");
system("pause");
exit(0);
}
}
...
}
循环障碍物
到这里我们就要是障碍物循环出现,因为不管怎么样也不应该只有一个障碍物吧!同时,在此还将利用 rand() 随机障碍物空隙坐标。
加入以下代码段:
if(barl_y <= 0)
{
barl_y = width;
int temp =rand() % int(high * 0.8); //使用临时变量储存障碍物坐标信息
barl_xDown = temp + high/10;
barl_xTop = temp - high/10;
}
这里对临时变量加减高度除以十的操作是为了防止生成新障碍物的空隙在太过边缘的位置。
小结
完整代码如下(我是打字机器):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cwindow.h>
//全局变量
int high,width; //画面尺寸
int bird_x,bird_y; //小鸟坐标
int barl_y,barl_xTop,barl_xDowm; //障碍物
int score; //得分
void gotoxy(int x, int y) //移动光标便于清屏重画
{
HANDLE handle = GetStdHandle(STD_UOTPUT_HANDLE);
CROOD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void startup() //数据初始化
{
high = 15;
width = 20;
score = 0;
bird_x = 0;
bird_y = width/3;
barl_y = width/2;
barl_xDown = high/2;
barl_xTop = high/3;
}
void show() //显示画面
{
gotoxy(0,0);
int i,j;
for(i=0; i<high; i++)
{
for(j=0; j<width; j++)
{
if((i==bird_x)&&(j==bird_y))
printf("@");
else if ((j==barl_y)&&((i>barl_xDown)||(i<barl_xTop)))
printf("*");
else
printf(" ");
}
print("\n"); //每经一次行循环就换行
}
}
void updateWithoutInput()
{
bird_x ++;
barl_y --;
if(bird_y == barl_y)
{
if((bird_x>=barl_xTop)&&(bird_x<=barl_xDown))
score ++;
else
{
printf("GG\n");
system("pause");
exit(0);
}
}
if(barl_y <= 0)
{
barl_y = width;
int temp =rand() % int(high * 0.8); //使用临时变量储存障碍物坐标信息
barl_xDown = temp + high/10;
barl_xTop = temp - high/10;
}
sleep(150);
}
void updateWithInput()
{
char input;
if(kbhit()) //判断是否有输入
{
input = getch();
if(input==' ')
bird_x = bird_x - 2;
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:C语言实现像素鸟游戏


猜你喜欢
- Qt计时器使用方法详解 2023-05-30
- C语言qsort()函数的使用方法详解 2023-04-26
- C++ 数据结构超详细讲解顺序表 2023-03-25
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- C语言详解float类型在内存中的存储方式 2023-03-27
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- ubuntu下C/C++获取剩余内存 2023-09-18
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- Easyx实现扫雷游戏 2023-02-06