Bonjour,
Je travaille actuellement sur une version perso du Snake. Je débute en C# et j'ai un petit problème au niveau de l'orienté objet il me semble.
Tout d'abord voici une partie de mon code:
La fenêtre principale, qui contient le jeu à proprement parlé dans un pannel.
Ma classe snake, qui crée un objet 'Snake'
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 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Timers; using System.Threading; /* SNAKE: (version by ******) * ----------------------------------- * * * */ namespace Snake { public partial class MainFrame : Form { snake the_snake = new snake(); Human the_human = new Human(); Random randNum = new Random(); public static int score = 0; public static int flagTimer = 0; public MainFrame() { InitializeComponent(); panel1.Hide(); label2.Hide(); label4.Hide(); label5.Hide(); panel1.Paint += new PaintEventHandler(Paint_Event); (panel1 as Control).KeyDown +=new KeyEventHandler(MainFrame_KeyDown); } private void DrawingPanel(System.Drawing.Graphics formGraphics) { the_snake.drawSnake(formGraphics); the_human.drawHuman(formGraphics); } private void Paint_Event(object sender, System.Windows.Forms.PaintEventArgs e) { DrawingPanel(e.Graphics); } //-----------------------------------------------------------------------------CLIC--- private void button1_Click(object sender, EventArgs e) { snake.setX(100); snake.setY(100); snake.right = true; snake.down = false; snake.left = false; snake.up = false; flagTimer = 0; score = 0; snake.longg = 4; for (int i = 3; i < snake.the_snake.Count(); i++) { snake.the_snake.RemoveAt(i); panel1.Refresh(); } button1.Hide(); label2.Hide(); panel1.Show(); pictureBox1.Hide(); panel1.Show(); label4.Show(); label5.Show(); timer1.Start(); } //-----------------------------------------------------------------------------TIC---- private void timer1_Tick(object sender, EventArgs e) { if (flagTimer == 1) { timer1.Stop(); button1.Text = "PLAY AGAIN"; button1.Show(); label2.Show(); panel1.Hide(); pictureBox1.Show(); } label4.Text = Convert.ToString(score); int x = snake.getX(); int y = snake.getY(); snake.upSnake(x, y); if (snake.right) { snake.setX(x + 10); } if (snake.up) { snake.setY(y - 10); } if (snake.left) { snake.setX(x - 10); } if (snake.down) { snake.setY(y + 10); } Collisions.Collision(); panel1.Invalidate(); if (snake.the_snake.ElementAt(0).IntersectsWith(Human.the_human)) { if (score >= 0 && score < 10) { score += 1; } if (score >= 10 && score < 20) { score += 2; } if (score >= 20 && score < 40) { score += 4; } if (score >= 40 && score < 80) { score += 8; } if (score >= 80 && score < 160) { score += 16; } if (score >= 160 && score < 10000) { score += 20; } if (score >= 10000) { score += 40; } // pour avoir un systeme de points un peu plus évolué que +1 snake.growSnake(); Human the_human = new Human(randNum); } } //------------------------------------------------------------------------------END---- public static void restart() { flagTimer = 1; // pour signaler au timer } //-----------------------------------------------------------------------------KEY---- private void MainFrame_KeyDown(object sender, KeyEventArgs e) { if (e.KeyData == Keys.NumPad4) // left { if (snake.right == false && snake.left == false) { int x = snake.getX(); int y = snake.getY(); snake.upSnake(x, y); snake.moveLeft(); } } if (e.KeyData == Keys.NumPad5) // down { if (snake.up == false && snake.down == false) { int x = snake.getX(); int y = snake.getY(); snake.upSnake(x, y); snake.moveDown(); } } if (e.KeyData == Keys.NumPad6) // right { if (snake.left == false && snake.right == false) { int x = snake.getX(); int y = snake.getY(); snake.upSnake(x, y); snake.moveRight(); } } if (e.KeyData == Keys.NumPad8) // up { if (snake.down == false && snake.up == false) { int x = snake.getX(); int y = snake.getY(); snake.upSnake(x, y); snake.moveUp(); } } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if(comboBox1.Text.Equals("easy")) { timer1.Interval = 150; } if(comboBox1.Text.Equals("medium")) { timer1.Interval = 100; } if(comboBox1.Text.Equals("hard")) { timer1.Interval = 45; } if (comboBox1.Text.Equals("insane")) { timer1.Interval = 10; } }
Donc ma question :
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 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace Snake { public class snake { public static List<Rectangle> the_snake = new List<Rectangle>(); private static SolidBrush brush1 = new System.Drawing.SolidBrush(System.Drawing.Color.Black); public static bool right; public static bool left; public static bool up; public static bool down; public static int longg; // longueur du snake private static int x, y; private static int hauteur=10, largeur=10; private static int otherSnake = 0; public snake() // fonction qui permet d'initialiser le snake en début de partie { if (otherSnake == 0) { x = 100; y = 100; down = false; right = true; // on commence par aller vers la droite left = false; up = false; } if (otherSnake == 1) { x = 300; y = 100; down = false; right = false; // on commence par aller vers la gauche left = true; up = false; } longg = 4; // on commence avec un snake de 4 cases for (int i = 0; i < longg; i++) // on fait un snake de 3 carrés pour commencer { the_snake.Add(new Rectangle(x, y, hauteur, largeur)); x += 10; } } public static void upSnake(int x, int y) // fonction qui permet de bouger le snake en jeu { the_snake.Add(new Rectangle(x, y, hauteur, largeur)); the_snake.RemoveAt(0); } public void drawSnake(Graphics dessin) { for (int i = 0 ; i < the_snake.Count; i++) { dessin.FillRectangle(brush1, getSnake(i)); } } public void drawSnake() { for (int i = the_snake.Count() - 1; i > 0; i--) { the_snake[i] = the_snake[i - 1]; } } public static Rectangle getSnake(int j) { return the_snake[j]; } public static int getX() { return x; } public static int getY() { return y; } public static int setX(int val) { return x = val; } public static int setY(int val) { return y = val; } public static void moveDown() { down = true; right = false; left = false; up = false; } public static void moveUp() { down = false; right = false; left = false; up = true; } public static void moveLeft() { down = false; right = false; left = true; up = false; } public static void moveRight() { down = false; right = true; left = false; up = false; } public static void growSnake() { the_snake.Add(new Rectangle(x, y, hauteur, largeur)); longg++; } } }
J'aimerais créer un 2eme objet 'snake' dans mon pannel, afin de les voir se déplacer simultanément, chacun controlé par un joueur différent.
Malheureusement en créant un nouveau timer et ce que je suppose être un nouvel objet 'snake' je n'arrive qu'a doubler la vitesse du premier snake.
Ceci signifie donc que mon objet n'est pas créé une 2eme fois, qu'il s'agit toujours du même objet.
(Dans le code ci dessus je n'ai pas mis mes différents essais, je ne les ai pas sauvegardé)
Je suppose que mon erreur provient du mot clef statique que je ne maîtrise pas bien... Si quelqu'un pouvait me débloquer je lui en serait très reconnaissant.
Partager