Salut a tous !


Je suis en train de developper un metal slug a ma sauce en C# avec le framework XNA. Mais j'ai un problème pour faire les balles, j'aimerais simplicialement les afficher.

Ce n'est pas vraiment un probleme de JEU 2D, c'est plutot un probleme de class en C# pur.

(Je m'aide de tuto et j'essaye d'apprendre par moi même, le code est fait par moi et moi seul)

Ici mon personnage a les bonnes animations etc il court, tire etc, mais les balles qu'il tire (dans la class personnage) ne sont pas pris en compte.

Je ne comprends pas bien le systeme des class ici. Je creer une class Bullet et une Main pour facilité la tache, mais le problème c'est qu'enfin de compte elle ne me servent a rien. Car si je veux afficher mes Balle quand je tire, je suis obliger de tout mettre dans ma Class Personnage (Update & Draw) alors que je sais qu'on peut le faire autrement (en passant par ma class Main et Bullet)

Probleme : je n'y arrive pas


EDIT : ce que j'aimerais faire c'est enlever dans ma Class PERSONNAGE : la ligne 19, 43, mais si je fais ca la ligne 259 ne fonctionne plus. Et même si je venais a laisser tout cela, l'affichage ne se fait pas. Je suis obliger de faire un grsd copier coller de ma Class Main (Update et Draw)

Si vous n'arrivez pas a comprendre ce que je dis, dites le moi, je tacherais de mieux m'expliquer



Je vous montre mon code :


CLASS PERSONNAGE :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SlugRemix
{
 
    class ClassPersonnage
    {
 
        // VARIABLES
 
        public Vector2 Position, Velocity;
 
        List<ClassBullet> Bullets;
 
        Texture2D TexLegsActuel, TexTopActuel;
 
        int TimerLeg, AnimationSpeedLeg, FrameColoneLeg, HFrameLeg, LFrameLeg;
        int TimerTop, AnimationSpeedTop, FrameColoneTop, HFrameTop, LFrameTop;
 
        SpriteEffects Effect;
 
        float Speed = 3f;
        int XPosDynTop, YPosDynTop;
 
        public bool Idle, Jump, Shoot, Left, Right, ActualShoot;
 
        // INITIALISATION
        public ClassPersonnage()
        {
            Position.Y = 300;
            Position.X = 100;
 
            XPosDynTop = 9;
 
            Right = true;
 
            Bullets = new List<ClassBullet>();
        }
 
        // METHODE
 
        //
        //
        //
        //
        // ----------------- ANIMATION LEGS
        public void AnimateLeg()
        {
            this.TimerLeg++;
 
            if (this.TimerLeg >= this.AnimationSpeedLeg)
            {
                this.TimerLeg = 0;
                this.FrameColoneLeg++;
 
                if (Idle == false && Jump == false) if (FrameColoneLeg == 12) FrameColoneLeg = 0;
 
                if (Idle == true && Jump == false) if (FrameColoneLeg == 1) FrameColoneLeg = 0;
 
                if (Idle == false && Jump == true) if (FrameColoneLeg == 5) FrameColoneLeg = 4;
            }
        }
 
        //
        //
        //
        //
        // ----------------- ANIMATION TOP
        public void AnimateTop()
        {
            this.TimerTop++;
 
            if (this.TimerTop >= this.AnimationSpeedTop)
            {
                this.TimerTop = 0;
                this.FrameColoneTop++;
 
                if (Shoot == false) if (FrameColoneTop == 4) FrameColoneTop = 0;
 
                if (Shoot == true) 
                    if (FrameColoneTop == 7) 
                    { 
                        FrameColoneTop = 0;
                        Shoot = false;
                    }
            }
        }
 
 
 
 
 
        // UPDATE & DRAW
 
        public void Update(MouseState mouse, KeyboardState keyboard)
        {
 
            this.AnimateLeg();
            this.AnimateTop();
 
            this.Position += this.Velocity;
 
            //
            //
            //
            //
            // ----------------- MOUV DROITE
            if (keyboard.IsKeyDown(Keys.Right))
            {
                Right = true;
                Left = false;
                Idle = false;
 
                this.Velocity.X = Speed;
                this.Effect = SpriteEffects.None;
 
                if (Shoot == false)
                    XPosDynTop = 9;
            }
 
            //
            //
            //
            //
            // ----------------- MOUV GAUCHE
            if (keyboard.IsKeyDown(Keys.Left))
            {
                Right = false;
                Left = true;
                Idle = false;
 
                this.Velocity.X = -Speed;
                this.Effect = SpriteEffects.FlipHorizontally;
 
                if (Shoot == false)
                    XPosDynTop = -9;
            }
 
            //
            //
            //
            //
            //
            // ----------------- METHODE LEFT | RIGHT
            if (Idle == false)
            {
                if (Jump == false) TexLegsActuel = ClassRessources.legsMouv;
 
                    this.AnimationSpeedLeg = 3;
                    LFrameLeg = 33;
                    HFrameLeg = 21;
            }
 
            //
            //
            //
            //
            // ----------------- MOUV JUMP
            if(keyboard.IsKeyDown(Keys.Space) && Jump == false)
            {
                this.Position.Y -= 10f;
                this.Velocity.Y = -7f;
 
                this.FrameColoneLeg = 0;
 
                Jump = true;
                Idle = false;
            }
 
            //
            //
            //
            //
            // ----------------- METHODE JUMP
            if (Jump == true && Idle == false)
            {
                float i = 1;
                this.Velocity.Y += 0.35f * i;
 
                TexLegsActuel = ClassRessources.legsJump;
 
                this.AnimationSpeedLeg = 5;
                LFrameLeg = 23;
                HFrameLeg = 25;
            }
            if (this.Jump == true && keyboard.IsKeyUp(Keys.Left) && keyboard.IsKeyUp(Keys.Right))
                this.Velocity.X = 0f;
 
            if (this.Position.Y >= 300) Jump = false;
            if (this.Jump == false) Velocity.Y = 0f;
 
            //
            //
            //
            //
            // ----------------- MOUV IDLE
            if (keyboard.IsKeyUp(Keys.Left) && keyboard.IsKeyUp(Keys.Right) && Idle == false && Jump == false)
            {
                this.Velocity.X = 0f;
                this.FrameColoneLeg = 0;
 
                this.Idle = true;
                Jump = false;
            }
 
            //
            //
            //
            //
            // ----------------- METHODE IDLE
            if (Jump == false && Idle == true)
            {
                TexLegsActuel = ClassRessources.legsIdle;
 
                this.AnimationSpeedLeg = 15;
                LFrameLeg = 21;
                HFrameLeg = 17;
            }
 
                if (Shoot == false)
                {
                    TexTopActuel = ClassRessources.topIdle;
                    YPosDynTop = -21;
 
                    AnimationSpeedTop = 10;
                    HFrameTop = 30;
                    LFrameTop = 33;
 
 
                    if (Left == true) XPosDynTop = -9;
 
                    else if (Right == true) XPosDynTop = 9;
 
                }
 
 
            //
            //
            //
            //
            // ----------------- METHODE SHOOT
            if (keyboard.IsKeyDown(Keys.D) && ActualShoot == false)
            {
                AnimationSpeedTop = 2;
                TimerTop = 0;
 
                Shoot = true;
                ActualShoot = true;
                FrameColoneTop = 0;
 
                ClassRessources.pistolSound.Play();
 
                Bullets.Add(new ClassBullet((int)Math.Round(Position.X+45), (int)Math.Round(Position.Y)-36, ClassRessources.pistolBullet));
 
 
            }
            if (keyboard.IsKeyUp(Keys.D)) ActualShoot = false;
 
            if (Shoot == true)
            {
                TexTopActuel = ClassRessources.topPistolShoot;
                LFrameTop = 54;
                HFrameTop = 27;
 
                if(Left == true)
                {
                    XPosDynTop = -25; 
                    if (Jump == false) YPosDynTop = -27;
                }
                else if(Right == true)
                {
                    XPosDynTop = 25;
                    if(Jump == false) YPosDynTop = -27;
                }
                if (Jump == true) YPosDynTop = -30;
 
            }
 
 
 
 
 
        }
 
 
        public void Draw(SpriteBatch spriteBatch)
        {
            //
            //
            //
            //
            // ----------------- DESSIN LEGS
            spriteBatch.Draw(
                TexLegsActuel,
                Position,
                new Rectangle((this.FrameColoneLeg) * LFrameLeg, 0 * HFrameLeg, LFrameLeg, HFrameLeg),
                Color.White,
                0f,
                new Vector2(LFrameLeg / 2, HFrameLeg / 2),
                1.5f,
                this.Effect,
                0f
                );
 
            //
            //
            //
            //
            // ----------------- DESSIN TOP
            spriteBatch.Draw(
                TexTopActuel,
                new Vector2((Position.X + XPosDynTop), (Position.Y + YPosDynTop)),
                new Rectangle((this.FrameColoneTop) * LFrameTop, 0 * HFrameTop, LFrameTop, HFrameTop),
                Color.White,
                0f,
                new Vector2(LFrameTop / 2, HFrameTop / 2),
                1.5f,
                this.Effect,
                0f
                );
 
 
            //
            //
            //
            //
            // ----------------- AFFICHAGE
            spriteBatch.DrawString(
                ClassRessources.font,
                "COUNT BULLET : " + Bullets.Count,
                new Vector2(20, 10),
                Color.Red
                );
        }
    }
}


CLASS BULLET :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SlugRemix
{
    class ClassBullet
    {
 
        Rectangle Hitbox;
        Texture2D Texture;
 
        public ClassBullet(int x, int y, Texture2D texture)
        {
            this.Texture = texture;
            this.Hitbox = new Rectangle(x, y, this.Texture.Width, this.Texture.Height);
        }
 
 
        public void Update(MouseState mouse, KeyboardState keyboard)
        {
 
        }
 
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(this.Texture, this.Hitbox, Color.White);
        }
    }
}

CLASS MAIN :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SlugRemix
{
    class ClassMain
    {
        // CHAMP
        public ClassPersonnage Personnage;
 
        public List<ClassBullet> Bullets;
 
        // CONSTRUCTEUR
        public ClassMain()
        {
            Personnage = new ClassPersonnage();
            Bullets = new List<ClassBullet>();
        }
 
        // METHODE
 
        // UPDATE & DRAW
        public void Update(MouseState mouse, KeyboardState keyboard)
        {
 
            //
            //
            //
            //
            // ----------------- UPDATE PERSONNAGE
            Personnage.Update(mouse, keyboard);
 
            //
            //
            //
            //
            // ----------------- UPDATE BALLE
            foreach (ClassBullet bullet in Bullets)
                bullet.Update(mouse, keyboard);
 
        }
 
        public void Draw(SpriteBatch spriteBatch)
        {
            Personnage.Draw(spriteBatch);
 
            foreach (ClassBullet bullet in Bullets)
                bullet.Draw(spriteBatch);
 
        }
    }
}

Merci !