[C#][.NET] Creating a Snake Game

Started by G_G, August 05, 2012, 10:42:12 am

Previous topic - Next topic

G_G

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.

Apidcloud

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

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  :^_^':

G_G

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

 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:

G_G

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

July 31, 2014, 02:28:44 am #26 Last Edit: July 31, 2014, 02:30:40 am by leona313
i heart the barcode can be used in games visual basic 2d bar code generator

PhoenixFire

Quote from: Subsonic_Noise on July 01, 2011, 02:42:19 amNext off, how to create a first person shooter using microsoft excel.

Quote from: Zeriab on September 09, 2011, 02:58:58 pm<Remember when computers had turbo buttons?

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


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

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

dobzik

Quote from: gameus on August 05, 2012, 10:44:23 am
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.

        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.

            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.

        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.
Spoiler: ShowHide



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();
        }

    }
}