服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言实现飞机大战

C语言实现飞机大战

2022-12-20 16:47Object_in_java C/C++

这篇文章主要为大家详细介绍了C语言实现飞机大战,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现飞机大战的具体代码,供大家参考,具体内容如下

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
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<Windows.h>
 
int score = 0;
int plane_col, plane_row;//·É»úλÖÃ
int bullet_col,bullet_row;//×Óµ¯µÄλÖÃ
int area_height, area_width;//ÓÎÏ·ÇøÓò  0-n-1
int enemy_col, enemy_row;
int enemy_vh, enemy_vv;
int a[100][100] = { 0 };
 
void gotoxy(int x, int y) {//ˢР
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}
void HideCursor() {
    CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
 
void startup()//³õʼ»¯ 
{
    area_height = 20;
    area_width = 30;
 
    plane_col = 14;
    plane_row = 10;
    
    bullet_col = 0;
    bullet_row = -1;
 
    enemy_col = rand() % area_width;
    enemy_row = 0;
    enemy_vh = 0;
    enemy_vv = 1;
}
 
//int[][] planeArray() {
//    
//    a[plane_col][plane_row] = 1;
//    for (int i = plane_col - 2; i < plane_col + 2; i++)
//        a[i][plane_row + 1] = 1;
//    a[plane_col - 1][plane_row + 2] = 1; a[plane_col + 1][plane_row + 2] = 1;
//
//    return a;
//}
 
void show()//Öð¸ö×Ö·ûɨÃè²¢´òÓ¡ 
{
    gotoxy(0, 0);
    int i, j;
    //ÏÔʾ
    //system("cls");
    for (i = 0; i < area_height; i++)//ÐбéÀú
    {
        for (j = 0; j < area_width; j++)//ÁбéÀú
        {
            if (i == plane_row && j == plane_col)
            {
                printf("*");
            }
            else if (i == bullet_row && j == bullet_col)
                printf("|");
            else if (i == enemy_row && j == enemy_col)
                printf("@");
            else printf(" ");
        }
        printf("\n");
    }
    printf("score:%d\n",score);
}
void updateWithInput()//½»»¥£¬¿ØÖÆ·É»úÒƶ¯£¬Éä»÷
    char input;
    //µÈ´ýÓû§µÄÊäÈ룬½»»¥
    if (kbhit()) {
        input = getch();
        switch (input)
        {
        case 'w':
            if(plane_row != 0)
            plane_row--; break;
        case 'a':
            if(plane_col != 0)
            plane_col--; break;
        case 'd':
            if(plane_col != area_width)
            plane_col++; break;
        case 's':
            if(plane_row != area_height)
            plane_row++; break;
        case ' ':
            if (bullet_row < 0)//ÆÁÄ»ÀïûÓÐ×Óµ¯
            {
                bullet_row = plane_row - 1;
                bullet_col = plane_col;
            }
            break;
        default:
            break;
        }
    }
}
 
int IsCrash() {
    //ÅжÏÎÒ·Å·É»úÊÇ·ñ×¹»Ù 
    if (enemy_col == plane_col && enemy_row == plane_row) {
        return 1;
    }
    return 0;
}
 
void updateWithourInput()//×Óµ¯Òƶ¯ÓëµÐÈËÒƶ¯ 
{
    //¸üÐÂ
    bullet_row--;
    static int count = 0;
    count ++;
    if (count == 40) {
        enemy_row += enemy_vv;
        enemy_col += enemy_vh;
        count = 0;
    }
    
}
 
void crack() {//»÷»ÙµÐÈË 
 
    if(enemy_row > area_height){
        bullet_row = -1;
        enemy_row = -1;
        enemy_col = rand() % area_height;
    }
    else if (bullet_col == enemy_col && bullet_row == enemy_row) {
        score += 10;
        bullet_row = -1;
        enemy_row = -1;
        enemy_col = rand() % area_height;
    }
 
}
 
int IsFinish() {//ÓÎÏ·ÊÇ·ñ½áÊø 
    if (score == 100) {
        system("cls");
        printf("congretulations!!!");
        score = 0;
        _sleep(500);//ÏÈÔÝÍ£ÔÚÏÖʵ·ûºÏÈËÐÔ»¯ 
        system("pause");
        return 1;
    }
    else if (IsCrash() == 1) {
        system("cls");
        printf("you have lost!!!");
        score = 0;
        _sleep(500);
        system("pause");
        return 1;
    }          
    
    return 0;
}
 
int main()
{
    HideCursor();
    startup();
    while (1)
    {
        show();
        updateWithInput();
        updateWithourInput();
        crack();
        if(IsFinish()==1){
            startup();
            continue;
        }
    }
    return 0;
}

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

原文链接:https://blog.csdn.net/m0_45311187/article/details/120305204

延伸 · 阅读

精彩推荐