comment ne pas réecrire tout le code pour un autre level?

salut, j ai besoin d 'aide urgent svp.
j ai passé toute la nuit a chercher la solution mais je trouve rien.
ce code est un level d 'un jeu.
quand j 'ai essayé de faire un level 2, j ai vu que je dois tout réecrire pour changer la map[20][20].
si je veux faire beaucoup d autre level, mon proramme aura des milliers de lignes et prendra beaucoup de temps.
ce que je veux est comment juste changer la map[20][20] et ne pas réecrire le "switch(map[y][x])" des millers de fois?
merci :)

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
#include <iostream>
#include <windows.h>

using namespace std;
char map [20][20] = {"###################",
                     "#                 #",
                     "#                 #",
                     "#                 #",
                     "#                 #",
                     "#                 #",
                     "# *############## #",
                     "#  #              #",
                     "#  #              #",
                     "#  #              #",
                     "#  #######        #",
                     "#        #        #",
                     "#        #        #",
                     "#        #        #",
                     "#        #        #",
                     "# ########        #",
                     "# #               #",
                     "#@#     !         #",
                     "###################"};


int gamespeed = 100;
bool gamerunning = true;
int level =1;
int health = 100, maxhealth = 100;


int main()
{

    while (gamerunning == true && level == 1)
    {
        system ("cls");

    for (int i=0; i<20; i++)
    {
        cout << map[i] << endl;
    }
    cout << "Health :" << health << " / " <<  maxhealth << endl;
    for (int y=0; y<20; y++)
    {
        for (int x=0; x<20; x++)
        {
            switch (map[y][x])
            {

            case '#':
                {
                    map[y][x] = char(219);
                }break;
            case '@':
                {
                    

                if (GetAsyncKeyState(VK_UP)!= 0)
                {
                    int y2 = (y-1);
                    switch (map [y2][x])
                    {
                    case ' ':
                        {

                        map[y][x] = ' ';
                        y-=1;
                        map[y2][x] = '@';
                        }break;


                    case '!':
                        {
                            level = 2;
                        }break;

                    case '*':
                        {
                            health -= 20;
                            map[y][x] = ' ';
                            y -= 1;
                            map[y2][x] = '@';
                        }break;
                    }
                }
                if (GetAsyncKeyState(VK_DOWN) != 0)
                {
                    int y2 = (y+1);
                    switch(map [y2][x])
                    {
                    case ' ':
                        {
                            map [y][x] = ' ';
                            y+=1;
                            map[y2][x] = '@';
                        }break;

                    case '!':
                        {
                            level = 2;
                        }break;

                    case '*':
                        {
                            health -= 20;
                            map[y][x] = ' ';
                            y += 1;
                            map[y2][x] = '@';
                        }break;
                    }
                }
                if (GetAsyncKeyState(VK_RIGHT) != 0)
                {
                    int x2 = (x+1);
                    switch (map[x2][y])
                    {
                    case ' ':
                        {
                            map[y][x] = ' ';
                            x+=1;
                            map[y][x2] = '@';
                        }break;

                    case '!':
                        {
                            level = 2;
                        }break;

                    case '*':
                        {
                            health -= 20;
                            map[y][x] = ' ';
                            x += 1;
                            map[y][x2] = '@';
                        }break;
                    }
                }
                if (GetAsyncKeyState(VK_LEFT))
                {
                    int x2 = (x-1);
                    switch (map[y][x2])
                    {
                    case ' ':
                        {
                            map[y][x] = ' ';
                            x-=1;
                            map[y][x2] = '@';

                        }
                    break;

                    case '!':
                        {
                            level = 2;
                        }break;

                    case '*':
                        {
                            health -= 20;
                            map[y][x] = ' ';
                            x -= 1;
                            map[y][x2] = '@';
                        }break;
                    }
                }
                }
            }


        }
    }
Sleep (gamespeed);
    }
    while (gamerunning == true && level == 2)
    {
        system ("cls");
        cout << "level 2 coming soon..." << endl;
        system ("pause");
        return 0;
    }
    return 0;
}
hi, I have need of emergency help please.
I spent all night searching for the solution but I find nothing.
This code is a level of a play.
When I have tried to make a level 2, I saw that I must rewrite to change the map [20] [20].
if I want to make a lot of different level, my proramme will be thousands of lines and take a long time.
what I want is just how to change the map [20] [20] and do not rewrite the "switch (map [y] [x])" of times Millers?
thank you :)


Suggestion: read your map in from a file. Your file can store many different maps.
Or you can have a file per level. Or a map per file. The choice is up to you.

The mechanics of the game (the loops and switch statement) remain the same regardless of the map read in from the file.
Last edited on
Topic archived. No new replies allowed.