C语言小游戏之小熊跳板功能的实现

时间:2022-11-29 08:58:54

上篇文章给大家介绍了C语言实现桌面贪吃蛇小游戏,感兴趣的朋友可以点击查看,今天给大家介绍c语言实现小熊跳板功能。

C语言代码,有点简陋,还有许多可以优化的地方,注释也没写全,后期会补充,后期也可能添加更多功能,我会去尽量的完善代码。

测试工具:VS2019,语言: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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
#include<string.h>
 
#define High 40
#define Width 60 //画布大小
 
int people_x, people_y; //小球坐标
int canvas[High + 1][Width + 1]; //标记
int b1_left[3]; //横线板板起点
int b1_right[3]; //横线板板终点
int score = 0; //得分
int vv = 15, v_b = 8; //vv大小可以改变板板上升速度
 
void HideCursor()
{
    CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x, int y)
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
 
}
 
void startup()
{
    int i, j, k;
    srand(time(NULL));
    for (i = 3; i <= High; i += 3)
    {
        int t = 0;
        
        int k;
        for (k = 0; k < 3; k++)
        {
            b1_left[k] = rand() % (Width - 6);
            b1_right[k] = b1_left[k] + rand()%3+5;
 
        }
        t = 3;
        while (t--) {
            for (j = b1_left[t]; j < b1_right[t]; j++)
            {
                canvas[i][j] = 2; //标记为横线板板
            }
        }
 
    }
    people_x = 2;
    people_y = 6; // 小球横纵坐标
    canvas[people_x][people_y] = 1; // 小球所在不能为横线板板
    for (i = 0; i < High; i++) // 左右边框为束线
    {
        canvas[i][0] = -2;
        canvas[i][Width - 1] = -2;
    }
    for (j = 0; j < Width; j++) //上下边框为T
    {
        canvas[0][j] = -1;
        canvas[High - 1][j] = -1;
    }
}
 
void show()
{
    gotoxy(0, 0);//把光标启动到(0,0)点,在这里功能为清屏
    int i, j;
    for (i = 0; i < High; i++)
    {
        for (j = 0; j < Width; j++)
        {
            if (canvas[i][j] == 2)
                printf("-");
            else if (canvas[i][j] == 1)
                printf("o");
            else if (canvas[i][j] == -1)
                printf("T");
            else if (canvas[i][j] == -2)
                printf("|");
            else printf(" ");
        }
        printf("\n");
    }
    printf("得分:%d\n", score);
}
 
int updataWithoutInput()
{
 
    int i, j, k;
    static int v_bord = 0, v_ball = 0;
    int flag = 0;
    int oldps_x, oldps_y;
    if (v_bord < vv) //设置更新各个部件的位置
        v_bord++;
    if (v_bord == vv) {
        for (i = 1; i <= High - 2; i++)
        {
            for (j = 1; j <= Width - 2; j++)
            {
                if (canvas[i][j] == 2 && i - 1 != 0)
                {
                    canvas[i][j] = 0;
                    if (canvas[i - 1][j] == 1)
                    {
                        people_x = i - 2; people_y = j;
                        canvas[people_x][people_y] = 1;
                        canvas[i - 1][j] = 2;
                    }
                    else
                        canvas[i - 1][j] = 2;
                }
                else
                {
                    if (i - 1 == 0 && canvas[i][j] == 2) {
                        canvas[i][j] = 0;
                        flag = 1;
                    }
 
                }
            }
        }
        v_bord = 0;
    }
    for (i = 1; i <= Width - 2; i++)
    {
        if (canvas[people_x][i] == 2)
        {
            score++;
            break;
        }
    }
 
    if (v_ball < v_b)
        v_ball++;
 
    if (v_ball == v_b && canvas[people_x + 1][people_y] == 0)
    {
        canvas[people_x][people_y] = 0;
        people_x++;
        canvas[people_x][people_y] = 1;
        v_ball = 0;
    }
 
    int cnt = 0;//判断游戏是否结束
    if (people_x == 0 || people_x == High - 2)
    {
        printf("GAMEOVER!\n");
        system("pause");
        cnt = 1;
        goto restart;
    }
 
    if (flag)
    {
        int t = 0;
        int k;
        for (k = 0; k < 3; k++)
        {
            b1_left[k] = rand() % (Width - 7) + 1;
            b1_right[k] = b1_left[k] + rand() % 3 + 5;
        }
        t = 3;
        while (t--) {
            for (j = b1_left[t]; j < b1_right[t]; j++)
            {
                canvas[High/3*3-3][j] = 2;
            }
        }
 
    }
    //Sleep(200);
restart:
    return cnt;
}
 
void updataWithInput()
{
    char key;
    if (_kbhit())
    {
        key = _getch();
        if ((key == 'a' || key == 75) && canvas[people_x][people_y-1] == 0)
        {
            canvas[people_x][people_y] = 0;
            people_y--;
            canvas[people_x][people_y] = 1;
        }
        if ((key == 'd' || key == 77) && canvas[people_x][people_y+1] == 0)
        {
            canvas[people_x][people_y] = 0;
            people_y++;
            canvas[people_x][people_y] = 1;
        }
 
    }
 
}
 
void init() {
    system("cls");
    memset(canvas, 0, sizeof canvas);
    memset(b1_left, 0, sizeof b1_left);
    memset(b1_right, 0, sizeof b1_right);
    score = 0;
    startup(); //初始化界面
}
 
int main()
{
    HideCursor();
start:
    init();
    while (1)
    {
        show();//画界面
        int cnt = updataWithoutInput();
        updataWithInput();
        if (cnt == 1) goto start;
    }
}

运行效果:

C语言小游戏之小熊跳板功能的实现

到此这篇关于C语言小游戏之小熊跳板功能的实现的文章就介绍到这了,更多相关C语言小熊跳板内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/MengYiKeNan/article/details/110454103