0% found this document useful (0 votes)
10 views8 pages

C# Snake Game Code Implementation

The document is a C# code file for a Snake game, defining the game's logic, controls, and UI elements. It includes methods for starting, pausing, and managing the game state, as well as handling user input for snake movement. The game tracks the score and high score, and checks for collisions to determine game over conditions.

Uploaded by

majraazeem4567
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

C# Snake Game Code Implementation

The document is a C# code file for a Snake game, defining the game's logic, controls, and UI elements. It includes methods for starting, pausing, and managing the game state, as well as handling user input for snake movement. The game tracks the score and high score, and checks for collisions to determine game over conditions.

Uploaded by

majraazeem4567
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C:\Users\FriendS\Downloads\Form1.

cs 1
1 using [Link];
2 using [Link];
3
4 namespace snakegame
5 {
6 public partial class Snakegame : Form
7 {
8 List<Point> Snake = new List<Point>();
9 Point food;
10 int p1 = 1; int p2 = 0;
11 int score = 0;
12 int highscore = 0;
13 int cellsize = 20;
14 int cols = 30;
15 int rows = 25;
16 bool gameruning = false;
17 Random random = new Random();
18 bool paused = false;
19
20
21
22 public Snakegame()
23 {
24 InitializeComponent();
25 [Link] += gamearea_Paint;
26
27
28 [Link] = true;
29 [Link] += Snakegame_KeyDown;
30
31
32 [Link] += Gametimer_Tick;
33
34
35 [Link] += btnstart_Click;
36 [Link] += btnpause_Click;
37
38
39 [Link] = false;
40 [Link] = false;
41 [Link] = false;
42
43
44 [Link] = null;
45 [Link]();
46 }
47
48 private void btnup_Click(object sender, EventArgs e)
49 {
C:\Users\FriendS\Downloads\[Link] 2
50 if (!gameruning)
51 {
52 return;
53 }
54 else
55 {
56 if (p2 != 1)
57 {
58 p1 = 0;
59 p2 = -1;
60 }
61 }
62
63 }
64
65 private void Snakegame_Load(object sender, EventArgs e)
66 {
67
68
69
70
71 }
72
73 private void lblhighscore_Click(object sender, EventArgs e)
74 {
75
76 }
77
78 private void gamearea_Paint(object sender, PaintEventArgs e)
79 {
80 if (!gameruning)
81 {
82 return;
83 }
84 else
85 {
86 Brush Snakecolor = [Link];
87 Brush foodcolor = [Link];
88 foreach (var part in Snake)
89 {
90 [Link](Snakecolor,
91 part.X * cellsize, part.Y * cellsize,
92 cellsize,
93 cellsize
94
95 );
96
97
98 }
C:\Users\FriendS\Downloads\[Link] 3
99 [Link](foodcolor,
100 food.X * cellsize, food.Y * cellsize,
101 cellsize,
102 cellsize
103
104 );
105
106 }
107
108 }
109
110 private void Gametimer_Tick(object sender, EventArgs e)
111 {
112 if (!gameruning)
113 return;
114 snakemove();
115 [Link]();
116
117 }
118 void snakemove()
119 {
120 Point head = Snake[[Link] - 1];
121 Point newhead = new Point(head.X + p1, head.Y + p2);
122 if (newhead == food)
123 {
124 [Link](newhead);
125 score += 10;
126 [Link] = "score :" + score;
127 if (score > highscore)
128 {
129 highscore = score;
130 [Link] = "highscore" + highscore;
131 }
132 Generatefood();
133 }
134 else
135 {
136 [Link](0);
137 [Link](newhead);
138
139 }
140 checkcollision();
141 }
142 void checkcollision()
143 {
144 Point head = Snake[[Link] - 1];
145 if (head.X < 0 || head.Y < 0 || head.X >= cols || head.Y >=
rows)
146 {
C:\Users\FriendS\Downloads\[Link] 4
147 gameover();
148 }
149 for (int i = 0; i < [Link] - 1; i++)
150 {
151 if (Snake[i] == head)
152 {
153 gameover();
154
155 }
156
157 }
158 }
159 void gameover()
160 {
161 [Link]();
162 gameruning = false;
163 paused = false;
164
165 [Link]("Game Over!\nYour Score: " + score);
166
167 [Link] = true;
168 [Link] = false;
169 [Link] = "Pause";
170 }
171 private void Snakegame_KeyDown(object sender, KeyEventArgs e)
172 {
173 if (!gameruning)
174 {
175 return;
176
177 }
178 else
179 {
180 switch ([Link])
181 {
182
183 case [Link]:
184 if (p2 != 1)
185 {
186 p1 = 0;
187 p2 = -1;
188 }
189 break;
190 case [Link]:
191 if (p2 != -1)
192 {
193 p1 = 0;
194 p2 = 1;
195 }
C:\Users\FriendS\Downloads\[Link] 5
196 break;
197 case [Link]:
198 if (p1 != 1)
199 {
200 p1 = -1;
201 p2 = 0;
202 }
203 break;
204 case [Link]:
205 if (p1 != -1)
206 {
207 p1 = 1;
208 p2 = 0;
209
210 }
211 break;
212
213 }
214
215
216
217
218
219
220 }
221
222 }
223
224 private void btnstart_Click(object sender, EventArgs e)
225 {
226 if (gameruning) return;
227 startgame();
228
229 }
230
231 void startgame()
232 {
233 [Link]();
234 [Link]();
235
236 [Link](new Point(7, 10));
237 [Link](new Point(8, 10));
238 [Link](new Point(9, 10));
239
240 p1 = 1;
241 p2 = 0;
242
243 score = 0;
244 [Link] = "Score: 0";
C:\Users\FriendS\Downloads\[Link] 6
245
246 gameruning = true;
247 paused = false;
248
249 Generatefood();
250
251 [Link] = false;
252 [Link] = true;
253 [Link] = "Pause";
254
255 [Link] = 600;
256 [Link]();
257
258 [Link]();
259 [Link]();
260
261 }
262 void Generatefood()
263 {
264 Point p;
265 do
266 {
267 p = new Point([Link](0, cols), [Link](0, rows));
268 }
269 while ([Link](p));
270
271 food = p;
272
273
274
275
276 }
277
278 private void btnpause_Click(object sender, EventArgs e)
279 {
280 if (!gameruning)
281 return;
282
283 if (!paused)
284 {
285 [Link]();
286 paused = true;
287 [Link] = "Resume";
288 }
289 else
290 {
291 [Link]();
292 paused = false;
293 [Link] = "Pause";
C:\Users\FriendS\Downloads\[Link] 7
294 [Link]();
295 }
296
297 }
298
299 private void btnright_Click(object sender, EventArgs e)
300 {
301 if (!gameruning)
302 {
303 return;
304 }
305 else
306 {
307 if (p1 != -1)
308 {
309 p1 = 1;
310 p2 = 0;
311
312 }
313 }
314
315 }
316
317 private void btnleft_Click(object sender, EventArgs e)
318 {
319 if (!gameruning)
320 {
321 return;
322 }
323 else
324 {
325 if (p1 != 1)
326 {
327 p1 = -1;
328 p2 = 0;
329
330 }
331 }
332
333 }
334
335 private void btndown_Click(object sender, EventArgs e)
336 {
337 if (!gameruning)
338 {
339 return;
340 }
341 else
342 {
C:\Users\FriendS\Downloads\[Link] 8
343 if (p2 != -1)
344 {
345 p1 = 0;
346 p2 = 1;
347
348 }
349 }
350
351 }
352 }
353 }

You might also like