Bonjour,
Je travaille sur un Pac-Man depuis un bon mois et je suis coincé au niveau des déplacements et des collisions du Pac-Man. J'ai utilisé une image de fond pour la map et crée un tableau de dimension 2 pour quadriller cette image tous les 4 pixels.
Mais lorsque je lance le jeux et je me retrouve avec l'erreur suivante : "L'index se trouve en dehors des limites du tableau". Et on me signale l'erreur au niveau du switch "Déplacement" et de la case "Direction.Up".
Voici mon code, pour recevoir un peu d'aide car là je craque
Et voici un court extrait de mon tableau car il a pour dimension [62,56] Sinon les 1 représente les murs et les 0 représentent les couloirs qui Pac-Man peut emprunter.
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; using Pac_Man_Game1; using Pac_Man_Ressources; using Pac_Man_Player; namespace Pac_Man_Player { public enum Direction { Up, Down, Left, Right } class Player { //FIELD Rectangle Hitbox; Rectangle Carte; SpriteEffects Effect; Direction Direction; Direction Déplacement; int Frameline; int FrameColumn; int CoordoPacX; int CoordoPacY; int CoordoSpeed = 60; int CoordoTimer = 0; int Temps; int CoordScoreX = 365; int CoordScoreY = 15 ; int Score ; bool Animation; int Timer = 0; int Animationspeed = 7; int TabY = 25; int TabX = 46; //CONSTRUCTOR public Player() { this.Hitbox = new Rectangle((this.TabX*4), (this.TabY * 4), 30, 28); this.Carte = new Rectangle(0, 50, 800, 600); this.Frameline = 0; this.FrameColumn = 0; this.Direction = Direction.Up; this.Effect = SpriteEffects.None; this.Animation = true; this.Timer = 0; } //METHOD public void Animate() { this.Timer++; if (this.Timer == this.Animationspeed) { this.Timer = 0; if (this.Animation) { if (this.FrameColumn == 2) this.FrameColumn = 1; else this.FrameColumn = 2; } } } //UPDATE & DRAW public void Update(KeyboardState keyboard) { if (keyboard.IsKeyDown(Keys.Up)) { if (this.Timer == this.CoordoSpeed) { this.Timer++; this.Timer = 0; this.Déplacement = Direction.Up; } this.Direction = Direction.Up; } else if (keyboard.IsKeyDown(Keys.Down)) { if (this.Timer == this.CoordoSpeed) { this.Timer++; this.Timer = 0; this.Déplacement = Direction.Down; } this.Direction = Direction.Down; } else if (keyboard.IsKeyDown(Keys.Left)) { if (this.Timer == this.CoordoSpeed) { this.Timer++; this.Timer = 0; this.Déplacement = Direction.Left; } this.Direction = Direction.Left; } else if (keyboard.IsKeyDown(Keys.Right)) { if (this.Timer == this.CoordoSpeed) { this.Timer++; this.Timer = 0; this.Déplacement = Direction.Right; } this.Direction = Direction.Right; } switch (this.Déplacement) { case Direction.Up: { this.Temps = this.TabX; TabX++; if (TabMap[TabX, TabY] == 0) { this.CoordoPacX = TabX * 4; this.CoordoPacY = TabY * 4; this.Hitbox.X = this.CoordoPacX; this.Hitbox.Y = this.CoordoPacY; } else { this.Hitbox.X = (this.Temps * 4); this.Hitbox.Y = this.TabY; } } break; case Direction.Down: { this.Temps = this.TabX; TabX--; if (TabMap[TabX, TabY] == 0) { this.CoordoPacX = TabX * 4; this.CoordoPacY = TabY * 4; this.Hitbox.X = this.CoordoPacX; this.Hitbox.Y = this.CoordoPacY; } else { this.Hitbox.X = (this.Temps * 4); this.Hitbox.Y = this.TabY; } } break; case Direction.Left: { this.Temps = this.TabY; TabY--; if (TabMap[TabX, TabY] == 0) { this.CoordoPacX = TabX * 4; this.CoordoPacY = TabY * 4; this.Hitbox.X = this.CoordoPacX; this.Hitbox.Y = this.CoordoPacY; } else { this.Hitbox.X = this.CoordoPacX; this.Hitbox.Y = (this.Temps * 4); } } break; case Direction.Right: { this.Temps = this.TabY; TabY++; if (TabMap[TabX, TabY] == 0) { this.CoordoPacX = TabX * 4; this.CoordoPacY = TabY * 4; this.Hitbox.X = this.CoordoPacX; this.Hitbox.Y = this.Temps; } else { this.Hitbox.X = this.CoordoPacX; this.Hitbox.Y = (this.Temps * 4); } } break; } switch (this.Direction) { case Direction.Up: this.Frameline = 1; this.Effect = SpriteEffects.None; this.Animate(); break; case Direction.Down: this.Frameline = 2; this.Effect = SpriteEffects.None; this.Animate(); break; case Direction.Left: this.Frameline = 3; this.Effect = SpriteEffects.FlipHorizontally; this.Animate(); break; case Direction.Right: this.Frameline = 3; this.Effect = SpriteEffects.None; this.Animate(); break; } } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(Ressources.Map, this.Carte, Color.White); spriteBatch.Draw(Ressources.Player, this.Hitbox, new Rectangle(((this.FrameColumn -1) * 15), ((this.Frameline -1) * 14), 15, 14), Color.White, 0f, new Vector2(0, 0), this.Effect, 0f); string P_Score = string.Format("Score : "+Score); spriteBatch.DrawString(Ressources.police, P_Score, new Vector2(this.CoordScoreX, this.CoordScoreY), Color.Yellow); } } }
Je vous remercie de votre aide =D
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 *public static int[,] TabMap = new int[62, 56] { ********{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1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