C语言实现像素鸟游戏

Willow ·
更新时间:2024-09-20
· 1884 次阅读

本文实例为大家分享了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语言 像素

需要 登录 后方可回复, 如果你还没有账号请 注册新账号