Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8136391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:44:46+00:00 2026-06-06T10:44:46+00:00

How do I make the snake move in all directions continuosly without having to

  • 0

How do I make the snake move in all directions continuosly without having to press the buttons always

public partial class Form1 : Form
{
    Rectangle rectangle;
    Size recSize;
    Point firstPoint;
    Point[,] grid;
    Graphics graphics;
    Point[] snake;
    Random rng;
    Pen pen;
    int width = 0;
    int height = 0;

    public Form1()
    {
        InitializeComponent();

        firstPoint = new Point(0, 0);
        recSize = new Size(this.ClientSize.Width, this.ClientSize.Height);
        rectangle = new Rectangle(firstPoint, recSize);
        graphics = this.CreateGraphics();
        width = rectangle.Width;
        height = rectangle.Height;
        grid = new Point[width/4, height/4];
        snake = new Point[400];
        rng = new Random();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        pen = new Pen(new SolidBrush(Color.Green));
        //e.Graphics.DrawRectangle(pen, rectangle);
        e.Graphics.FillRectangle(new SolidBrush(Color.Green), rectangle);

    }

    private void GameButton_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < width / 4; i++)
        {
            for (int j = 0; j < height / 4; j++)
            {
                grid[i, j] = new Point();
                grid[i, j].X = firstPoint.X + (i * 4);
                grid[i, j].Y = firstPoint.Y + (j * 4);
                graphics.DrawEllipse(new Pen(new SolidBrush(Color.FromArgb(255, 0,0,0))), new Rectangle(grid[i, j], new Size(2, 2)));   
            }
        }

        snake[0] = new Point();
        snake[0] = grid[width /4/ 2 , height /4/ 2 ];
        graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
        graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));

    }


    private void moveSnake(KeyEventArgs e)
    {

           switch (e.KeyData)
           {
               case Keys.Up:

                       this.graphics.Clear(Color.Green);
                       snake[0].Y -= 4;
                       graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
                       graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));
                       graphics.Flush();
                       this.Invalidate();
                       System.Threading.Thread.Sleep(500);
                       // this.Refresh();
                       //moveSnake(e);


                        break;
                    case Keys.Down:
                        this.graphics.Clear(Color.Green);
                        snake[0].Y += 4;

                        graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
                        graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));
                        this.Invalidate();
                        System.Threading.Thread.Sleep(500);
                        //this.Refresh();
                        break;
                    case Keys.Left:
                        this.graphics.Clear(Color.Green);
                        snake[0].X -= 4;

                        graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
                        graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));
                        this.Invalidate();
                        System.Threading.Thread.Sleep(500);
                        //this.Refresh();
                        break;
                    case Keys.Right:
                        this.graphics.Clear(Color.Green);
                        snake[0].X += 4;

                        graphics.DrawEllipse(new Pen(new SolidBrush(Color.Black)), new Rectangle(snake[0], new Size(4, 4)));
                        graphics.FillEllipse(new SolidBrush(Color.Black), new Rectangle(snake[0], new Size(4, 4)));
                        this.Invalidate();
                        System.Threading.Thread.Sleep(500);
                        //this.Refresh();
                        break;
                }





    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {

        moveSnake(e);
        this.Refresh();
    }


    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        MessageBox.Show(e.KeyChar.ToString());
    }
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);
        MessageBox.Show(e.KeyChar.ToString());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:44:47+00:00Added an answer on June 6, 2026 at 10:44 am

    you need a timer on the form and for every tick move in last direction one square.

    With this you can speed up the snake a higher difficulty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make the movement of my snake smooth. Right now the
I'm trying to make a basic snake game in C++, I made a basic
i am trying to figure our how to make a snake like tail for
I have some homework for my school and I have to make a snake
Found some nice Open Source code of a snake game, but wanting to make
I wanted to make a class GradeBook, then create an object for each student,
Demo I am trying to make a snake game in html 5 canvas. But
I'm trying to make a snake game in javascript, but I am struggling with
As part of my self-education of programming I decided to make a snake in
For simplicities sake, I'll make up a similar example to what I have: Let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.