Код:
namespace Snake
{public partial class Snake : Form
{SoundPlayer sp = new SoundPlayer();
Timer myTimer = new Timer();
Timer myTimer2 = new Timer();
//Snake def
PictureBox[] snakeParts;
int snakesize = 3;
Point location = new Point(120, 120);
string direction = "Right";
bool changingDirection = false;
//fruit def
PictureBox food = new PictureBox();
Point foodlocation = new Point(0, 0);
PictureBox[] wallparts;
double a = 15;
int level = 1;
//database
static String path = Path.GetFullPath(Environment.CurrentDirectory);
static String dataBaseName = "date.mdf";
string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=" + path + @"\" + dataBaseName + "; Integrated Security=True";
public Snake()
{InitializeComponent();}
void myTimer_Tick(object sender, EventArgs e)
{int currentScores = Int32.Parse(Score_count.Text);
int newScore = currentScores + 1;
Score_count.Text = newScore + "";
int currenttime = Int32.Parse(timelabel.Text);
int newtime = currenttime - 1;
timelabel.Text = newtime + "";}
void myTimer2_Tick(object sender, EventArgs e)
{GamePanel.Controls.Remove(food);
drawfood();}
private void button2_Click(object sender, EventArgs e)
{stopgame();}
private void Start_Click(object sender, EventArgs e){
//play again
GamePanel.Controls.Clear();
snakeParts = null;
Score_count.Text = "0";
level_count.Text = "1";
fruit_count.Text = "0";
snakesize = 3;
direction = "Right";
location = new Point(120, 120);
//start game
drawsnake();
drawfood();
level_count.Text = "1";
timer1.Start();
//control
trackBar1.Enabled = false;
Start.Enabled = false;
namebox.Enabled = false;
//enable pause
Pause.Enabled = true;
// timer +1 on
myTimer.Interval = 1000;
myTimer.Enabled = true;
myTimer.Tick += myTimer_Tick;
myTimer.Start();
//snake long label
longlabel.Text = snakesize + "";}
private void drawsnake()
{snakeParts = new PictureBox[snakesize];
//one part after another
for (int i = 0; i < snakesize; i++)
{snakeParts[i] = new PictureBox();
snakeParts[i].Size = new Size(15, 15);
snakeParts[i].BackColor = Color.LawnGreen;
snakeParts[i].BorderStyle = BorderStyle.FixedSingle;
snakeParts[i].Location = new Point(location.X - (15 * i), location.Y);
GamePanel.Controls.Add(snakeParts[i]);}}
private void drawfood()
{Random rnd = new Random();
int Xrand = rnd.Next(38) * 15;
int Yrand = rnd.Next(30) * 15;
bool isOnSnake = true;
//chek food on snake
while (isOnSnake)
{for (int i = 0; i < snakesize; i++)
{if (snakeParts[i].Location == new Point(Xrand, Yrand))
{Xrand = rnd.Next(38) * 15;
Yrand = rnd.Next(30) * 15;} else{isOnSnake = false;}}}
//draw food
if (isOnSnake == false)
{ foodlocation = new Point(Xrand, Yrand);
food.Size = new Size(15, 15);
food.BackColor = Color.Red;
food.BorderStyle = BorderStyle.FixedSingle;
food.Location = foodlocation;
GamePanel.Controls.Add(food);
myTimer2.Interval = 30000;
myTimer2.Enabled = true;
myTimer2.Tick += myTimer2_Tick;
myTimer2.Start();
timelabel.Text = "30";}}
private void trackBar1_Scroll(object sender, EventArgs e)
{
//change time interval
timer1.Interval = 501 - (5 * trackBar1.Value);}
private void timer1_Tick(object sender, EventArgs e)
{move();}
private void move(){
Point point = new Point(0, 0);
//moving each part of snake
for (int i = 0; i < snakesize; i++)
{if (i == 0)
{point = snakeParts[i].Location;
if (direction == "Left"){
snakeParts[i].Location = new Point(snakeParts[i].Location.X - 15, snakeParts[i].Location.Y);}
if (direction == "Right"){
snakeParts[i].Location = new Point(snakeParts[i].Location.X + 15, snakeParts[i].Location.Y);}
if (direction == "Top")
{snakeParts[i].Location = new Point(snakeParts[i].Location.X, snakeParts[i].Location.Y - 15);}
if (direction == "Down")
{snakeParts[i].Location = new Point(snakeParts[i].Location.X, snakeParts[i].Location.Y + 15);}}
else
{Point newPoint = snakeParts[i].Location;
snakeParts[i].Location = point;
point = newPoint;}}
//hit food
if (snakeParts[0].Location == foodlocation)
{eatfood(); drawfood();}
//if snake hits wall
if (snakeParts[0].Location.X < 0 || snakeParts[0].Location.X >= 850 || snakeParts[0].Location.Y < 0 || snakeParts[0].Location.Y >= 480){stopgame();}
// snake eat itself
for (int i = 3; i < snakesize; i++){
if(snakeParts[0].Location==snakeParts[i].Location)
{stopgame();}}
changingDirection = false;}
//control snake
protected override bool ProcessCmdKey(ref Message msg, Keys keyData){
if (keyData == (Keys.Up) && direction != "Down" && changingDirection != true)
{direction = "Top";
changingDirection = true;}
if (keyData == (Keys.Down) && direction != "Top" && changingDirection != true){
direction = "Down";
changingDirection = true;}
if (keyData == (Keys.Left) && direction != "Right" && changingDirection != true){
direction = "Left";
changingDirection = true;}
if (keyData == (Keys.Right) && direction != "Left" && changingDirection != true){
direction = "Right";
changingDirection = true;}
return base.ProcessCmdKey(ref msg, keyData);}
private void eatfood()
{Random rnd = new Random();
int abrand = rnd.Next(1, 3);
for (int ba = 1; ba <= abrand; ba++)
{snakesize++;
PictureBox[] oldsnake = snakeParts;
GamePanel.Controls.Clear();
snakeParts = new PictureBox[snakesize];
for (int i = 0; i < snakesize; i++){
snakeParts[i] = new PictureBox();
snakeParts[i].Size = new Size(15, 15);
snakeParts[i].BackColor = Color.LawnGreen;
snakeParts[i].BorderStyle = BorderStyle.FixedSingle;
if (i == 0)
{ snakeParts[i].Location = foodlocation;}
else
{snakeParts[i].Location = oldsnake[i - 1].Location;}
GamePanel.Controls.Add(snakeParts[i]);}
myTimer2.Enabled = false;
myTimer2.Stop();
if (snakesize >= a){
GamePanel.Controls.Clear();
snakeParts = null;
level ++;
level_count.Text = level + "";
snakesize = 3;
direction = "Right";
location = new Point(120, 120);
drawsnake();
drawfood();
double a1 = a + a / 3;
a = a1;}}
int currentScores = Int32.Parse(Score_count.Text);
int newScore = currentScores + 50;
Score_count.Text = newScore+"";
int currentfruits = Int32.Parse(fruit_count.Text);
int newScorefr = currentfruits + 1;
fruit_count.Text = newScorefr + "";
int currentlong = Int32.Parse(longlabel.Text);
int newScoreln = snakesize;
longlabel.Text = newScoreln + "";}
private void stopgame()
{timer1.Stop();
Start.Enabled = true;
trackBar1.Enabled = true;
Pause.Enabled = false;
namebox.Enabled = true;
myTimer.Stop();
//game over text
Label over = new Label();
over.Text = "Game\nOver";
over.ForeColor = Color.Maroon;
over.Font = new Font("AgitProp", 100);
over.Size = over.PreferredSize;
over.TextAlign = ContentAlignment.MiddleCenter;
int X = GamePanel.Width / 2 - over.Width / 2;
int Y = GamePanel.Height / 2 - over.Height / 2;
over.Location = new Point(X, Y);
GamePanel.Controls.Add(over);
over.BringToFront();
//add current score update score board
addCurrentScoresToDateBase();
UpdateScoreBoard();}