Sign in to follow this  
similar

The problem with the movement of the serpent (snake game)

Recommended Posts

I am a beginner,Unfortunately, the snake's movement can not write I do not understand how to do it? Do you like the idea?

note:In fact, I mean, flexibility is a must have when moving in different directions. This is my idea:I've used several picturbox But I do not know how to join these To be flexible.Is this idea is wrong?

 

 

 

Share this post


Link to post
Share on other sites

You're question seems very unclear. Are you asking for a Snake Game Tutorial?

If you are looking for help about creating a snake game with C# and Windows Forms then here is classical snake game, you can have it for reference.

 

Off-topic:

 

It seems that you are using Windows Forms and C# for game development.

I find that the community for Windows Forms and C# for developing games is less and should be avoided (at least for getting started). Also I think (my personal opinion) that Windows Forms is for creating applications and user interfaces (in which case WPF is better), why go for creating games with it?

 

Even the name says that its used for creating forms (certainly not games, correct me if I'm wrong). Just an advice: Trying to create games using it can be a learning exercise but why not go with something like:

  • C++ and SDL/Allegro/SFML
  • Java and Java2d/Slick/lwjgl
  • C# and OpenTk/SlimDX
  • Python and pyGame

Share this post


Link to post
Share on other sites

Unfortunately, the snake's movement can not write I do not understand how to do it?
This is expected. Programming is mostly explaining to a computer what it must do. If you don't know, you cannot explain it to others.

 

In cases like this, stop writing code (you don't know what to write anyway), and pickup a paper and pen, and start thinking.

 

What must you do each time the snake changes position?

How can I store that in the computer?

Can you write receipt in steps that must be done?

 

This is my idea:I've used several picturbox But I do not know how to join these To be flexible.Is this idea is wrong?
I do not understand what "join" means, so I cannot give you an answer here.

 

 

(you learn much more if you first ponder about solutions yorself, before peeking at solutions written by others)

Share this post


Link to post
Share on other sites

the basic idea with the snake is you track the location of each body section. to move the snake, you move the head, then for each section, you move it to where the section before it was. if you keep the sections in a zero based list (array, vector, etc), the head is section zero, and so on. so you move the head, then move section 1 to where the head was, and in general, move section N to where section N-1 was. the result is the parts follow the head like a choo-choo-train.

 

FYI: its probably a bad idea to use a business app graphics package (windows forms) for game development work. they're not really designed for it, and can make things harder than they have to be.

 

instead, look for a simple 2D graphics package that supports your language of preference (C# in your case, apparently).

Share this post


Link to post
Share on other sites

I think I am the worst person that could answer this, because I'm a beginner too and I recently failed to create a snake game so I put it on hold, but I think by flexibility you mean making it continue moving in a direction and making it turn around to make it possible to bite itself while moving, is that right? Correct me if I'm wrong, I can't help you with the programming part, but you need to make it so that every snake block takes the position of the previous snake block, say you have snake block in position. n, when it's moved, you want to move the next in the position n was, so if direction n is to say the right, when the snake moves once, you need to make the position of the first block n+1 next block = x+1 where x is the initial position of that block, as to the continuous motion, I'm not sure, but I think you could add a timer and set it and move the snake at the timer event in the direction the user sets using keyboard keys, I hope you understand what I mean and I hope I helped even if a little. (I just realized I just repeated what Norman Barrows said, lol, sorry for that)

Edited by Luna Wu

Share this post


Link to post
Share on other sites

Take a look at the following. hopefully it will give you some idea on how to approach it.

  class Snake {
    static readonly SnakePart Head = new SnakePart ();
    const int SIZE = 20;

    public void Draw(Graphics g) {
        g.Clear(Color.White);
        Head.Draw(g);
    }

    public void Update(int dx, int dy) {
        var location = Head.Location;
        location.X += (SIZE * dx);
        location.Y += (SIZE * dy);
        Head.Update(location);
    }

    public void AddPart() { // Head --> NextPart --> NextPart --> null
        var p = Head;
        while (p.NextPart != null) p = p.NextPart; // loop to end
        p.NextPart = new SnakePart() {Location = p.Location };
    }

    class SnakePart {
        public SnakePart NextPart { get; set; }
        public Point Location { get; set; }

        public void Draw(Graphics g) {
            NextPart?.Draw(g);
            Brush b = (this == Head) ? Brushes.OrangeRed : Brushes.GreenYellow;
            g.FillRectangle(b, Location.X, Location.Y, SIZE, SIZE);
            g.DrawRectangle(Pens.Black, Location.X, Location.Y, SIZE, SIZE);
        }

        public void Update(Point newLocation) {
            NextPart?.Update(Location); // C#5 and under use, if(NextPart != null) NextPart.Update(Location);
            Location = newLocation;
        }
    }
}

public class Game : Form {
    public Game() {
        var Snake = new Snake();
        var r = new Random();
        this.ClientSize = new Size(800, 500);
        Paint += (_, e) => Snake.Draw(e.Graphics);
        Load  += (_, e) => {
            this.DoubleBuffered = true;
            new Thread(() => {
                while (true) {
                    var dx = r.Next(2); //Simulate some Game Movement
                    var dy = dx == 0 ? 1 : 0;
                    if (r.Next(20) < 4) Snake.AddPart(); // Simulate snake eating something yum yum
                    Snake.Update(dx, dy);
                    Invalidate();
                    Thread.Sleep(500);
                }
            }).Start();
        };
    }
        
    static void Main() {
        Application.Run(new Game());
    }
}
Edited by ClearZB

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this