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
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
using System.Threading;
namespace Cs_LaboFinal
{
public partial class Snake : Form
{
private int points;
private int record;
private String nomJoueur;
private int positionTeteX;
private int positionTeteY;
private int positionPommeX;
private int positionPommeY;
private Label pomme;
private List<Label> labelSnake;
private char direction; // 'D' = 'DOWN', 'U' = 'UP', 'L' = 'LEFT', 'R' = 'RIGHT'.
private bool perdu;
private static int taille = 10; // Taille (longueur ou largeur) d'un element du serpent.
Label scoreLabel;
Label nomJoueurLabel;
public Snake()
{
positionTeteX = 150;
positionTeteY = 150;
points = 0;
record = 0;
direction = 'D';
// Création label Score
scoreLabel = new System.Windows.Forms.Label();
scoreLabel.Location = new System.Drawing.Point(200, 350);
Controls.Add(scoreLabel);
scoreLabel.Visible = true;
scoreLabel.Size = new System.Drawing.Size(60, 20);
scoreLabel.Text = "Score : " + points;
scoreLabel.BackColor = Color.Silver;
scoreLabel.BorderStyle = BorderStyle.FixedSingle;
// Création label Joueur
nomJoueur = "Adrian";
nomJoueurLabel = new System.Windows.Forms.Label();
nomJoueurLabel.Location = new System.Drawing.Point(50, 350);
Controls.Add(nomJoueurLabel);
nomJoueurLabel.Visible = true;
nomJoueurLabel.Size = new System.Drawing.Size(100, 20);
nomJoueurLabel.Text = "Score : " + points;
nomJoueurLabel.BackColor = Color.Silver;
nomJoueurLabel.BorderStyle = BorderStyle.FixedSingle;
nomJoueurLabel.Text = "Joueur : " + nomJoueur;
labelSnake = new List<Label>();
pomme = new System.Windows.Forms.Label();
InitializeComponent();
Text = "Snake";
setPomme();
creerTete();
perdu = false;
}
public void creerTete()
{
Label l = new System.Windows.Forms.Label();
l.Location = new System.Drawing.Point(positionTeteX, positionTeteY);
Controls.Add(l);
l.Visible = true;
l.Size = new System.Drawing.Size(taille, taille);
l.BackColor = Color.Blue;
l.BorderStyle = BorderStyle.FixedSingle;
labelSnake.Add(l);
}
public void ajouterQueue(int x, int y)
{
Label l = new System.Windows.Forms.Label();
l.Location = new System.Drawing.Point(x, y);
Controls.Add(l);
l.Visible = true;
l.Size = new System.Drawing.Size(taille, taille);
l.BackColor = Color.GhostWhite;
l.BorderStyle = BorderStyle.FixedSingle;
labelSnake.Insert(0, l);
}
public void setPomme()
{
Random r = new Random();
decimal i = r.Next(10, 300);
i = Math.Round((i / 10));
i *= 10;
positionPommeX = (int)i;
i = r.Next(10, 300);
i = Math.Round((i / 10));
i *= 10;
positionPommeY = (int)i;
Controls.Add(pomme);
pomme.Visible = true;
pomme.Size = new System.Drawing.Size(10, 10);
pomme.BackColor = Color.Red;
pomme.BorderStyle = BorderStyle.FixedSingle;
pomme.Location = new System.Drawing.Point(positionPommeX, positionPommeY);
Refresh();
}
public bool verifierPomme()
{
if (positionTeteX <= positionPommeX && positionTeteY <= positionPommeY
&& (positionTeteX + taille) >= positionPommeX && (positionTeteY + taille) >= positionPommeY)
{
points++;
setPomme();
setScore();
return true;
}
else
return false;
}
public void setScore()
{
scoreLabel.Text = "Score : " + points;
}
public void setPosition()
{
for (int i = 0; i < labelSnake.Count(); ++i)
{
switch (direction)
{
case 'U':
positionTeteY -= taille;
if (verifierPomme())
ajouterQueue(positionTeteX, positionTeteY - taille);
if (positionTeteY < 10 || positionTeteY > 300)
gameOver();
break;
case 'D':
positionTeteY += taille;
if (verifierPomme())
ajouterQueue(positionTeteX, positionTeteY + taille);
if (positionTeteY < 10 || positionTeteY > 300)
gameOver();
break;
case 'R':
positionTeteX += taille;
if (verifierPomme())
ajouterQueue(positionTeteX + taille, positionTeteY);
if (positionTeteX < 10 || positionTeteX > 300)
gameOver();
break;
case 'L':
positionTeteX -= taille;
if (verifierPomme())
ajouterQueue(positionTeteX - taille, positionTeteY);
if (positionTeteX < 10 || positionTeteX > 300)
gameOver();
break;
}
labelSnake.ElementAt(i).Location = new System.Drawing.Point(positionTeteX, positionTeteY);
}
}
public void gameOver()
{
perdu = true;
DialogResult resultat = MessageBox.Show("GAME OVER \n\nRejouer?", "Game Over", MessageBoxButtons.YesNo);
if (resultat == DialogResult.No)
{
this.Close();
}
else
{
this.Invalidate();
this.Update();
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
if (direction == 'L')
setPosition();
else
{
direction = 'R';
setPosition();
}
}
else if (e.KeyCode == Keys.Left)
{
if (direction == 'R')
setPosition();
else
{
direction = 'L';
setPosition();
}
}
else if (e.KeyCode == Keys.Up)
{
if (direction == 'D')
setPosition();
else
{
direction = 'U';
setPosition();
}
}
else if (e.KeyCode == Keys.Down)
{
if (direction == 'U')
setPosition();
else
{
direction = 'D';
setPosition();
}
}
}
public bool getPerdu()
{
return perdu;
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics graph = pea.Graphics;
graph.DrawLine(Pens.Orange, 10, 310, 310, 310); // Axes
graph.DrawLine(Pens.Orange, 10, 10, 10, 310);
graph.DrawLine(Pens.Orange, 310, 10, 310, 310);
graph.DrawLine(Pens.Orange, 310, 10, 10, 10);
}
}
} |
Partager