服务器之家

服务器之家 > 正文

C语言简易版flappy bird小游戏

时间:2021-07-14 16:54     来源/作者:叫我小红

假期在家无聊,想随便码点东西,故有此简陋的小游戏诞生。觉着可能对初学C语言的小伙伴练习有点帮助,故写此博客。游戏界面如下:

C语言简易版flappy bird小游戏

首先,先画出整个小游戏实现的流程图,如下:

C语言简易版flappy bird小游戏

思路很简单,整个游戏界面是由一个大的char类型数组构成,更新数组的值然后不停的打印出来就形成了动态效果。

由上图看,大循环是保证游戏一直不断的进行下去,小循环是让小鸟的速度大于游戏界面里背景(由#构成的柱子)的速度(小鸟动四下柱子才动一下)。

下面是具体代码(水平有限大家多多见谅,但是效果还是有的!)

Bird.c文件

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <windows.h>
#include "Interface.h"
 
int main(void)
{
 InitialInterface();
 for(;;)
 {
  newinterface();
  scoring();//过一个柱子计一次分,所以和柱子更新速度一致
  for (int i = 0; i < 4; i++)//小鸟的速度是柱子的4倍
  {
   birdmove();
   draw();
   Sleep(50);
  }
 }
 return 0;
}

Interface.h文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef INTERFACE_H
#define INTERFACE_H
 
#define M 20
#define N 36
 
void InitialInterface(void);
void newinterface(void);
void birdmove(void);
void scoring(void);
void draw(void);
 
#endif

Interface.c文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include "interface.h"
 
 
char interf[M][N] = {{ 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,32,32 },
     { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,32,32 },
     { 38,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32 },
     { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 },
     { 32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35,32,32,32,32,32,32,32,32,32,32,35,35 }, };
//初始界面矩阵,ASCII码中“ ”是32,“&”是38表示小鸟,“#”是35用来画柱子
 
int num = 0;//用于计数输出并排两列黑柱子同一位置
int black;//黑方块位置
int p= M/2 ;//小鸟初始位置
int score = 0;//分数
 
/*初始化界面*/
void InitialInterface(void)
{
  printf("\n   作者:xhyang,博客地址:http://blog.csdn.net/weixin_39449570\n");
  printf("   按\"w\"使小鸟跳起来,别落地,顺利穿过尽可能多的柱子!\n");
  for (int i = 0; i < M; i++)
  {
   printf("   ");
   for (int j = 0; j < N; j++)
   {
    printf("%c", interf[i][j]);
   }
   printf("\n");
  }
}
 
 
/*更新界面各个柱子*/
void newinterface(void)
{
 
 if (interf[0][1] == 35 && num==0)//当矩阵第二列为黑色方块时,计算出下一次黑柱子上半部分的位置
 {
  black = 5 + rand() % 5;
  num = 2;//黑柱子是两列#组成,第二列与第一列位置一样,用num保证两列位置一致
 }
 for (int i = 0; i < M; i++)
 {
 
  for (int j = 0; j < N - 1; j++)
  {
   interf[i][j] = interf[i][j + 1];
  }
  if (interf[0][0] == 35 && (i < black || i>(black + 5)))//此时上面的第二列变成了第一列,更新下一个黑柱子,有了黑柱子上半部分位置+5即是下半部分的起始位置
  {
   interf[i][N-1] = 35;
  }
  else
  {
   interf[i][N-1] = 32;
  }
 }
 if (num > 0)
  num--;
}
 
 
/*更新小鸟位置*/
void birdmove(void)
{
 for (int a = 0; a < 3; a++)
 {
  if (a == 2 && p > 0)//减缓鸟的速度,使按键上跳速度是下落的4倍
  {
   p = p + 1;
  }
  if (_kbhit())
  {
   if (_getch() == 'w' || _getch() == 'W')
   {
    p = p - 3;
   }
  }
 }
}
 
/*计分*/
void scoring(void)
{
 if (p > 20 || interf[p][0] == 35)
 {
  system("cls");
  printf("\n\n   游戏结束!\n\n");
  printf("   最终得分:%d\n\n\n", score);
  system("pause");
 }
 
 if (interf[0][0] == 35 && interf[0][1] == 32 )
  score++;
}
 
/*重画界面*/
void draw(void)
{
 system("cls");
 printf("\n   作者:xhyang,博客地址:http://blog.csdn.net/weixin_39449570\n");
 printf("   按\"w\"使小鸟跳起来,别落地,顺利穿过尽可能多的柱子!\n");
 for (int i = 0; i < M; i++)
 {
  printf("   ");
  for (int j = 0; j < N; j++)
  {
   if (i == p && j == 0 && interf[p][0] != 35)
    printf("%c", 38);
   else
    printf("%c", interf[i][j]);
  }
  printf("\n");
 
 }
 printf("   得分:%d \n", score);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_39449570/article/details/79307447

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部