Create Snake Game C# - playproduction.de

Create Snake Game C#

Ahmad Kandil
Views: 52740
Like: 752
Snake Game C# Step by Step.
Download All Games :
You Can Find All Projects Here :

31 Comments

  1. All codes;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;

    namespace snake_oyunu
    {
    public partial class Form1 : Form
    {
    int cols = 50, rows = 25, score = 0, dx = 0, dy = 0, front = 0, back = 0;
    Piece[] snake = new Piece[1250];
    List<int> available = new List<int>();
    bool[,] visit;

    Random rand = new Random();

    Timer timer = new Timer();
    public Form1()
    {
    InitializeComponent();
    initial();
    LaunchTimer();
    }

    private void LaunchTimer()
    {
    timer.Interval = 100;
    timer.Tick += move;
    timer.Start();
    }

    private void move(object sender, EventArgs e)
    {
    int x = snake[front].Location.X, y = snake[front].Location.Y;
    if (dx == 0 && dy == 0) return;
    if (game_over(x + dx, y + dy))
    {
    timer.Stop();
    MessageBox.Show("Game Over");
    return;
    }
    if (collisionFood(x + dx, y + dy))
    {
    score += 1;
    lblScore.Text = "Score: " + score.ToString();
    if (hits((y + dy) / 20, (x + dx) / 20)) return;
    Piece head = new Piece(x + dx, y + dy);
    front = (front – 1 + 1250) % 1250;
    snake[front] = head;
    visit[head.Location.Y / 20, head.Location.X / 20] = true;
    Controls.Add(head);
    randomFood();
    }
    else
    {
    if (hits((y + dy) / 20, (x + dx) / 20)) return;
    visit[snake[back].Location.Y / 20, snake[back].Location.X / 20] = false;
    front = (front – 1 + 1250) % 1250;
    snake[front] = snake[back];
    snake[front].Location = new Point(x + dx, y + dy);
    back = (back – 1 + 1250) % 1250;
    visit[(y + dy) / 20, (x + dx) / 20] = true;
    }
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    dx = dy = 0;
    switch (e.KeyCode)
    {
    case Keys.Right:
    dx = 20;
    break;
    case Keys.Left:
    dx = -20;
    break;
    case Keys.Up:
    dy = -20;
    break;
    case Keys.Down:
    dy = 20;
    break;
    }
    }

    private void randomFood()
    {
    available.Clear();
    for (int i = 0; i < rows; i++)
    for (int j = 0; j < cols; j++)
    if (!visit[i, j]) available.Add(i * cols + j);
    int idx = rand.Next(available.Count) % available.Count;
    lblFood.Left = (available[idx] * 20) % Width;
    lblFood.Top = (available[idx] * 20) / Width * 20;
    }

    private bool hits(int x, int y)
    {
    if (visit[x, y])
    {
    timer.Stop();
    MessageBox.Show("Snake hit his Body");
    return true;
    }
    return false;
    }

    private bool collisionFood(int x, int y)
    {
    return x == lblFood.Location.X && y == lblFood.Location.Y;
    }

    private bool game_over(int x, int y)
    {
    return x < 0 || y < 0 || x > 980 || y > 480;
    }

    private void initial()
    {
    visit = new bool[rows, cols];
    Piece head = new Piece((rand.Next() % cols) * 20, (rand.Next() % rows) * 20);
    lblFood.Location = new Point((rand.Next() % cols) * 20, (rand.Next() % rows) * 20);
    for (int i = 0; i < rows; i++)
    for (int j = 0; j < cols; j++)
    {
    visit[i, j] = false;
    available.Add(i * cols + j);
    }
    visit[head.Location.Y / 20, head.Location.X / 20] = true;
    available.Remove(head.Location.Y / 20 * cols + head.Location.X / 20);
    Controls.Add(head);
    snake[front] = head;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    }
    }========================================================================= New Piece's class codes;using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace snake_oyunu
    {
    class Piece : Label
    {
    public Piece(int x , int y)
    {
    Location = new System.Drawing.Point(x, y);
    Size = new System.Drawing.Size(20, 20);
    BackColor = Color.Orange;
    Enabled = false;
    }
    }
    }

  2. i need the code for work i am a big gyoutuber with 100000k subs i need the pastabiain code

  3. This is a really cool video! I’ve been learning C# for a little bit now, but I can’t fully understand everything you are doing. Can you make a lengthier video where you explain what you do?

  4. When I create a windows forms it pops up a script instead of a blank page like yours so I'm lost from the start

  5. 1:45 what is that menu. I’ve been going crazy over it. What did you open there

  6. if you want to run it first download c# form visual studio because then it cannot run on chrome or edge they are the preinstalled running apps so download that and use terminal to run

  7. Brother can you tell me how to access the code on my visual studio

  8. why when i go to change the with, it goes back to 0 may i ask ?

  9. Can someone say what is the use of this available list? What does it do?

  10. I encode it properly but the snake is not moving when a press da arrows keys, Anyone who can help me? Thanks

  11. Anyone know why i get CS1520?
    code is:

    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;

    namespace snake1

    {

    public partial class Form1 : Form

    {

    int cols = 50, rows = 25, score = 0, dx = 0, dy = 0, front = 0, back = 0;

    Piece[] snake = new Piece[1250];

    List<int> available = new List<int>();

    bool[,] visit;

    Random rand = new Random();

    Timer timer = new Timer();

    public form1()

    {

    InitializeComponent();

    Initial();

    launchTimer();

    }

    private void launchTimer()

    {

    timer.Interval = 50;

    timer.Tick += move;

    timer.Start();

    }

    private void Snake_KeyDown(object sender, KeyEventArgs e)

    {

    dx = dy = 0;

    switch(e.KeyCode)

    {

    case Keys.Right:

    dx = 20;

    break;

    case Keys.Left:

    dx = -20;

    break;

    case Keys.Up:

    dy = -20;

    break;

    case Keys.Down:

    dy = 20;

    break;

    }

    }

    private void move(object sender, EventArgs e)

    {

    int x = snake[front].Location.X, y = snake[front].Location.Y;

    if (dx == 0 && dy == 0) return;

    if (game_over(x + dx, y + dy))

    {

    timer.Stop();

    MessageBox.Show("Game Over!");

    return;

    }

    if (collisionFood(x + dx, y + dy))

    {

    score += 1;

    lblScore.Text = "Score: " + score.ToString();

    if (hits((y + dy) / 20, (x + dx) / 20)) return;

    Piece head = new Piece(x + dx, y + dy);

    front = (front – 1 + 1250) % 1250;

    snake[front] = head;

    visit[head.Location.Y / 20, head.Location.X / 20] = true;

    Controls.Add(head);

    randomFood();

    }

    else

    {

    if (hits((y + dy) / 20, (x + dx) / 20)) return;

    visit[snake[back].Location.Y / 20, snake[back].Location.X / 20] = false;

    front = (front – 1 + 1250) % 1250;

    snake[front] = snake[back];

    snake[front].Location = new Point(x + dx, y + dy);

    back = (back – 1 + 1250) % 1250;

    visit[(y + dy) / 20, (x + dx) / 20] = true;

    }

    }

    private void randomFood()

    {

    available.Clear();

    for (int i = 0; i < rows; i++)

    for (int j = 0; j < cols; j++)

    if (!visit[i, j]) available.Add(i * cols + j);

    int idx = rand.Next(available.Count) % available.Count;

    lblFood.Left = (available[idx] * 20) % Width;

    lblFood.Top = (available[idx] * 20) / Width * 20;

    }

    private bool hits(int x, int y)

    {

    if (visit[x, y])

    {

    timer.Stop();

    MessageBox.Show("Snake hit his Body");

    return true;

    }

    return false;

    }

    private bool collisionFood(int x, int y)

    {

    return x == lblFood.Location.X && y == lblFood.Location.Y;

    }

    private bool game_over(int x, int y)

    {

    return x < 0 || y < 0 || x > 980 || y > 480;

    }

    private void Initial()

    {

    visit = new bool[rows, cols];

    Piece head

    = new Piece((rand.Next() % cols) * 20, (rand.Next() % rows) * 20);

    lblFood.Location

    = new Point((rand.Next() % cols) * 20, (rand.Next() % rows) * 20);

    for (int i = 0; i < rows; i++)

    for (int j = 0; j < cols; j++)

    {

    visit[i, j] = false;

    available.Add(i * cols + j);

    }

    visit[head.Location.Y / 20, head.Location.X / 20] = true;

    available.Remove(head.Location.Y / 20 * cols + head.Location.X / 20);

    Controls.Add(head); snake[front] = head;

    }

    }

    }

  12. Got an error –
    error while trying to run project could not load file or assembly 'snake' or one of its dependencies. The module was expected to contain an assembly manifest.
    Please solve it

  13. i didnt understand single thing. i only know syntax of c#

  14. Hi, could you make something like restart button, so when you lose, you can play next one?

  15. hi, great video. for me a beginner programmer it goes a little fast tho.

  16. how do you move it? for some reason for me it doesnt move. i tried the whole keyboard and everything

  17. Thanks @Ahmad Kandil. A nice project, and easy to follow video. And a good result – works great, exactly as it should.

  18. Hands down one of the best codes I have seen. I mean it has a small bug or two but it's really easy to understand and to change it however I want to. Thanks!

  19. tanks bro 🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰👏

  20. StupidThingsYouWatchWhenYou'reBored! says:

    how can i improve the controls and how can i add a restart function

  21. hi, could you explain what it is boll[,] = visit ?

  22. Can u help me?? When food spaws second time my worm can`t pick it.

Leave a Reply

Your email address will not be published.