Snake Game in C#

Page 1 of 1

4 Replies - 606 Views - Last Post: 24 May 2012 - 03:46 AM Rate Topic: -----

#1 navy4422  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 12

Snake Game in C#

Posted 22 May 2012 - 10:26 PM

Hi all,

So I'm trying to self teach myself some C# and after some tutorials and some other project I thought why not make the classic game of snake. Well its proving to be a little more difficult than I though and I am completely confused on one section of the game, making the bends. I intended to use picture boxes to make the snake. I have one box with the head and one with the tail that should be easy to navigate. The body section is what is confusing to me. Each picture box is 16 x 16 and I would like some help on the following:

1) Is there a way to create more picture boxes when the snake eats an apple?

2) If I store all of the parts information in a large array lets say 1000 and make it 2d where rows are each section of the snake and columns store (the name of the section, x position , y position, is it at a bend, and direction) is there a way to loop the array and access the appropriate picture box from the array?

Any help would be greatly appreciated. If you would like to see what I have I'll post it but its not much because I don't want to code a lot and have to start over if what I want to do doesn't work.

-Thomas

Is This A Good Question/Topic? 0
  • +

Replies To: Snake Game in C#

#2 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Snake Game in C#

Posted 23 May 2012 - 08:32 PM

I'm not really a game developer, but for the body part, i would probably make a grid.
Then fill each column and row with pictureboxes.
To set the images to the correct picturebox, you could just use something like the good old coordination system that we all know so well.
x = row, y = column

But, i would use C# WPF for that, because WinForm dose not have the same performance as WPF when it comes to standard graphical stuff. At least not from my experience.


Hope this helps

Cheers
Was This Post Helpful? 1
  • +
  • -

#3 navy4422  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 12

Re: Snake Game in C#

Posted 23 May 2012 - 08:41 PM

View PostEndLessMind, on 23 May 2012 - 08:32 PM, said:

I'm not really a game developer, but for the body part, i would probably make a grid.
Then fill each column and row with pictureboxes.
To set the images to the correct picturebox, you could just use something like the good old coordination system that we all know so well.
x = row, y = column

But, i would use C# WPF for that, because WinForm dose not have the same performance as WPF when it comes to standard graphical stuff. At least not from my experience.


Hope this helps

Cheers


Thanks I actually figured it out but now everything seems to work besides keyboard input ,I don't understand why it doesn't work.

here's the section of code

        private void SnakeGameForm_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show("test");

            if (e.KeyData == Keys.Down && (direction != 0))
            {
                direction = 2;

            }
            if (e.KeyData == Keys.Up && (direction != 2))
            {
                direction = 0;
            }
            if (e.KeyData == Keys.Right && (direction != 1))
            {
                direction = 3;
            }
            if (e.KeyData == Keys.Left && (direction != 3))
            {
                direction = 1;


            }
        }



        private void SnakeGameForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            MessageBox.Show("test");

            switch (e.KeyCode)
            {
                case Keys.Up:
                    direction = 0;
                    break;
                case Keys.Down:
                    direction = 2;
                    break;
                case Keys.Left:
                    direction = 1;
                    break;
                case Keys.Right:
                    direction = 3;
                    break;
            }

            
        }



I've been trying with both of these methods
Was This Post Helpful? 0
  • +
  • -

#4 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 193
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Snake Game in C#

Posted 24 May 2012 - 03:17 AM

hm, have you tried to make a print to the console on each KeyDown?
You are only going to use "KeyDown" not "PreviewKeyDown".

But this worked for me

    Private Sub MainWindow_KeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs) Handles Me.KeyDown
        If e.Key = Key.Down Then
            Console.WriteLine("Down")
        ElseIf e.Key = Key.Up Then
            Console.WriteLine("Up")
        ElseIf e.Key = Key.Left Then
            Console.WriteLine("Left")
        ElseIf e.Key = Key.Right Then
            Console.WriteLine("Right")
        End If
    End Sub


It's basically the same as you have
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,429
  • Joined: 23-August 08

Re: Snake Game in C#

Posted 24 May 2012 - 03:46 AM

Do NOT open up multiple topics.

Closed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1