Author Topic: [C#][.NET] Creating a Snake Game  (Read 60790 times)

Offline G_G

  • Green Gmod Game_Guy AKA G4 AKA hyper-G AKA G-force
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 6580
  • LV: 407
  • Gender: Male
    • View Profile
Re: [C#][.NET] Creating a Snake Game
« Reply #20 on: November 16, 2012, 12:56:03 PM »
I know XNA and Android aren't engines themselves, but you can create engines with them. I've tried Unity several times but I never get anywhere with it just for the lack of 3D knowledge. I eventually want to get into 3D development, but every time I try, I always end up giving up, mostly over silly things. I think I may take another shot at Unity.

Offline Apidcloud

  • Ethereal Devastator
  • ****
  • Posts: 258
  • LV: -10
  • Gender: Male
  • Epic pony is epic'
    • View Profile
Re: [C#][.NET] Creating a Snake Game
« Reply #21 on: November 16, 2012, 02:18:15 PM »
Ah yes, im aware of that, but when i suggested android I wasnt specifically talking about game, even though its the funniest thing xd about unity, i havent tried it either but a new version has been released like 2 days ago or something, it would be interesting to work with it
Instead of wanting to be somebody else, rather become somebody else



"I will treasure the knowledge like a squirrel treasures acorns."

Gibbo Glast 2D Engine - The sky is no longer a limit

DaJoker

  • Guest
Re: [C#][.NET] Creating a Snake Game
« Reply #22 on: May 11, 2014, 10:45:11 AM »
hello gameus,
your Snake Game Tutorial is wonderful i can't thank you enough  :-* for making one of the best tutorials i have seen on the internet for this game.
but i was wondering  :???: and trying to find a way to modify the game to Generate more Food and have maybe some way to add difficulty to change snake speed when you press one of 3 buttons (easy, medium, and hard) to make the game more interesting and challenging.

and i am sorry if i revived such a dead post in anyway  :^_^':

Offline G_G

  • Green Gmod Game_Guy AKA G4 AKA hyper-G AKA G-force
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 6580
  • LV: 407
  • Gender: Male
    • View Profile
Re: [C#][.NET] Creating a Snake Game
« Reply #23 on: May 11, 2014, 03:52:03 PM »
Here's what I'd do.

For the food:
  • Create a food array or a food list, and in the generate food method, generate how ever many pieces
  • In the collision check method, you'll have to iterate through the array of food instead of checking just one piece of food
  • You can even randomize colors if you'd like, just add a Color variable to the Food (snake piece) class

Making the Snake move faster
  • In the tutorial, I only used one timer, it'd be easier if I had used two timers. One for the Game logic/rendering and one for moving the snake
  • I'd create another timer for snake movement only, and everytime it ticks, it'll move the snake. Allowing you to modify the tick speed, the lower it is, the faster the snake.
  • Modify the first timer so it runs at a constant FPS (such as 30 or 60)

These are good things to learn and to practice your programming skills.

DaJoker

  • Guest
Re: [C#][.NET] Creating a Snake Game
« Reply #24 on: May 12, 2014, 04:56:55 PM »
 oh wow thanks mate cant be more grateful for your ideas and advice's on the topic  :bow:  but if u can show me your ways of implementing the codes with your codes
because some how i am not able to achieve it :negative:

Offline G_G

  • Green Gmod Game_Guy AKA G4 AKA hyper-G AKA G-force
  • Global Moderator
  • Chaos Ultimate
  • ****
  • Posts: 6580
  • LV: 407
  • Gender: Male
    • View Profile
Re: [C#][.NET] Creating a Snake Game
« Reply #25 on: May 12, 2014, 07:18:28 PM »
I don't really have the time or want to create add-on code. This tutorial is meant for people to work on and improve the code themselves. It's as simple as that.

leona313

  • Guest
Re: [C#][.NET] Creating a Snake Game
« Reply #26 on: July 31, 2014, 08:28:44 AM »
i heart the barcode can be used in games visual basic 2d bar code generator
« Last Edit: July 31, 2014, 08:30:40 AM by leona313 »

Offline PhoenixFire

  • Moderator
  • Astral Trancist
  • ***
  • Posts: 918
  • LV: 6
  • Gender: Female
  • ~That's an interesting notion, in it's own way~
    • View Profile
Re: [C#][.NET] Creating a Snake Game
« Reply #27 on: July 31, 2014, 12:57:44 PM »
Next off, how to create a first person shooter using microsoft excel.

<Remember when computers had turbo buttons?

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19961
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: [C#][.NET] Creating a Snake Game
« Reply #28 on: July 31, 2014, 01:09:29 PM »
Well noticed. *deletes account*
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


Quote from: winkio
I do not speak to bricks, either as individuals or in wall form.

Quote from: Barney Stinson
When I get sad, I stop being sad and be awesome instead. True story.

dobzik

  • Guest
Re: [C#][.NET] Creating a Snake Game
« Reply #29 on: June 23, 2015, 03:15:25 PM »
Quick Navigation
1. Understanding the Concept
2. Setting Up
3. Creating Necessary Classes
4. Creating an Input Manager
5. The Game Loop
6. Rendering Graphics
7. Creating Game Logic

6. Rendering Graphics

The Paint Event
We'll need to take advantage of the picture box's "Paint" event. This event will get fired when the control needs to be painted or re-painted. Go ahead and create a method for this event. Inside the method, we'll be grabbing the Graphics value from our event arguments. With an instance of the Graphics class, we'll be able to draw on the picture box itself.

Code: [Select]
       private void pbCanvas_Paint(object sender, PaintEventArgs e)
        {
            Graphics canvas = e.Graphics;
        }

And now we have a complete game loop. Let's go ahead and test it out shall we?

Experimenting
Create two new variables in our Form class, "square_x" and "square_y". These will just be integers. Set them to 0 or whatever preferred number I suppose. In our Paint method, add this line.

Code: [Select]
           canvas.FillRectangle(Brushes.Red, new Rectangle(square_x, square_y, 16, 16));
Now let's go up to our Update method and test out our Input manager we created. We're going to check Input from the Arrow Keys and then change the values of square_x and square_y. Here is where we use our Input manager "Pressed" method.

Code: [Select]
       private void Update(object sender, EventArgs e)
        {
            if (Input.Pressed(Keys.Right))
                square_x += 4;
            if (Input.Pressed(Keys.Left))
                square_x -= 4;
            if (Input.Pressed(Keys.Up))
                square_y -= 4;
            if (Input.Pressed(Keys.Down))
                square_y += 4;
            pbCanvas.Invalidate();
        }

Go ahead and run the project. You should end up with something like this, you should be able to move the square around. Be sure to remove the experimental code before moving on.
(click to show/hide)

Hi, I have followed the code and I could not get the red square in the middle, can you please help me? Here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Snake
{
    public partial class Form1 : Form
    {
        private static Hashtable keys = new Hashtable();
        private int square_x=5;
        private int square_y=5;
        public Form1()
                 {
           
            InitializeComponent();
            timer1.Start();
            timer1.Interval = 1000 / 60;
            timer1.Tick += new EventHandler(Update);
        }
        class snake
        {

            public int x { get; set; }
            public int y { get; set; }
            public snake(int x, int y)
            {
                x = 0;
                y = 0;
            }
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Input.ChangeState(e.KeyCode, true);
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            Input.ChangeState(e.KeyCode, false);
        }

   
        class Input
        {
     
            public static void ChangeState(Keys key, bool state)
            {
                keys[key] = state;
            }
            public static bool Pressed(Keys key)
            {
                if (keys[key] == null)
                    return false;
                return (bool)keys[key];
            }
        }

     
       
        private void pbCanvas_Paint(object sender, PaintEventArgs e)
        {
            Graphics canvas = e.Graphics;
            canvas.FillRectangle(Brushes.Red, new Rectangle(square_x, square_y, 16, 16));
        }
        private void Update(object sender, EventArgs e)
        {

            if (Input.Pressed(Keys.Right))
                square_x += 4;
            if (Input.Pressed(Keys.Left))
                square_x -= 4;
            if (Input.Pressed(Keys.Up))
                square_y -= 4;
            if (Input.Pressed(Keys.Down))
                square_y += 4;
            pbCanvas.Invalidate();
        }

    }
}