Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    10
  • comments
    133
  • views
    2318

c# console snake game

phil67rpg

1264 views

I have decided to work on a console based snake game, I am doing this because I want to learn basic c# skills before move on to c#  graphics programming.

I am able to move the snake from the center to the edge of the screen, it is just a start, but I am learning.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace snakegame
{
    class Program
    {
        static void Main(string[] args)
        {
            const int board_size = 17;
            char[,] board = new char[board_size, board_size];
            int horizontal = 5, vertical = 8, count = 0;

            for (int i = 0; i < board_size; i++) 
            {
                for (int j = 0; j < board_size; j++) 
                {
                    board[i, j] = '.';
                }
            }

            void draw_board()
            {
                for (int i = 0; i < board_size; i++)
                {
                    for (int j = 0; j < board_size; j++)
                    {
                        Console.Write(board[i, j]);
                    }
                    Console.WriteLine();
                }
            }

            while (count < 6)
            {
                board[vertical, horizontal] = '*';
                board[vertical, horizontal + 1] = '*';
                board[vertical, horizontal + 2] = '*';
                board[vertical, horizontal + 3] = '*';
                board[vertical, horizontal + 4] = '*';
                board[vertical, horizontal + 5] = '*';
                board[vertical, horizontal + 6] = '*';

                draw_board();

                horizontal++;
                count++;

                System.Threading.Thread.Sleep(1000);
                Console.Clear();
            }
        }
    }
}

 



34 Comments


Recommended Comments



1 hour ago, phil67rpg said:

I want to draw the food on the screen but what I get is the food is drawn randomly all over the screen.

Because you're randomly drawing it all over the screen... Well technically in the ranges of 330 - 340 + 0 - 770 and 280 + 0 - 550.  What were you expecting random to do?  Don't call the randomize function Form1_Paint() each frame and you won't get a new food items each frame.

Share this comment


Link to comment

Paint gets called all the time. And you randomize the placement. That's why you get many all over the place.

Also use e.Graphics

Edited by duke_meister

Share this comment


Link to comment
10 hours ago, phil67rpg said:

I want to draw the food on the screen but what I get is the food is drawn randomly all over the screen.


        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            int food_x = rnd.Next(0, 770);
            int food_y = rnd.Next(0, 550);
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
            Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);
            Rectangle rect_red = new Rectangle(food_x , food_y , 10, 10);
            g.FillRectangle(greenBrush, rect_green);
            g.FillRectangle(blackBrush, rect_black);
            g.FillRectangle(redBrush, rect_red);
            redBrush.Dispose();
            blackBrush.Dispose();
            greenBrush.Dispose();
            g.Dispose();
        }

 

Let's structure the code a little bit. I think it is more convenient to separate drawing and action. This code is just for example. The code below is bad because the snake changes a move direction with changing a food position. Could you explain how do you want to control the snake? By mouse click? Or the snake must move yourself with simple AI (path finding)?

        int food_x = 0;
        int food_y = 0;

        private void timer2_Tick(object sender, EventArgs e)
        {
            move = rnd.Next(1, 5);
            food_x = rnd.Next(0, 200);
            food_y = rnd.Next(0, 200);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            //Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
            //Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);
            Rectangle rect_green = new Rectangle(150 + x, 150 + y, 10, 10);
            Rectangle rect_black = new Rectangle(140 + x, 150 + y, 10, 10);
            Rectangle rect_red = new Rectangle(food_x, food_y, 10, 10);
            g.FillRectangle(greenBrush, rect_green);
            g.FillRectangle(blackBrush, rect_black);
            g.FillRectangle(redBrush, rect_red);
            redBrush.Dispose();
            blackBrush.Dispose();
            greenBrush.Dispose();
            g.Dispose();
        }

 

Edited by 8Observer8

Share this comment


Link to comment

I do not understand where and when the food must be drawn. Please, explain me. It is better to write it in Paint. For example, you can generate next food when the snake eat one. Or do you want to generate food periodically? For example, every 5 seconds?

Share this comment


Link to comment

yeah!! observer I put the food in the form_load method and it works, thanks for all the input.

Edited by phil67rpg

Share this comment


Link to comment

@phil67rpg what do you think about this improving of your code?

Let's to rewrite this method:

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            SolidBrush blackBrush = new SolidBrush(Color.Black);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            Rectangle rect_green = new Rectangle(340 + x, 280 + y, 10, 10);
            Rectangle rect_black = new Rectangle(330 + x, 280 + y, 10, 10);
            Rectangle rect_red = new Rectangle(food_x, food_y, 10, 10);
            g.FillRectangle(greenBrush, rect_green);
            g.FillRectangle(blackBrush, rect_black);
            g.FillRectangle(redBrush, rect_red);
            redBrush.Dispose();
            blackBrush.Dispose();
            greenBrush.Dispose();
            g.Dispose();
        }

I think it looks better:

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            DrawRectangle(g, 340 + x, 280 + y, Color.Green);
            DrawRectangle(g, 330 + x, 280 + y, Color.Black);
            DrawRectangle(g, food_x, food_y, Color.Red);
            g.Dispose();
        }

        private void DrawRectangle(Graphics g, int x, int y, Color color)
        {
            SolidBrush brush = new SolidBrush(color);
            Rectangle rect = new Rectangle(x, y, 10, 10);
            g.FillRectangle(brush, rect);
            brush.Dispose();
        }

 

Edited by 8Observer8

Share this comment


Link to comment

If you add DrawRactange() method then drawing food will be easier:

        struct Food { public int x; public int y; };

        List<Food> foods = new List<Food>() { new Food() };

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            DrawRectangle(g, 340 + x, 280 + y, Color.Green);
            DrawRectangle(g, 330 + x, 280 + y, Color.Black);

            foreach (var f in foods)
            {
                DrawRectangle(g, f.x, f.y, Color.Red);
            }

            g.Dispose();
        }

 

How it looks now:

SnakeByPhil_ListOfFood.gif.8863c0bdf92ab1637a75fec61e0ef281.gif

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace SnakeByPhil_WinFormsGDI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CenterToScreen();

            timer1.Interval = 200;

            timer2.Interval = 500;
            timer2.Start();
        }

        int x = 0, y = 0;

        Random rnd = new Random();

        int move = 1;

        struct Food { public int x; public int y; };

        List<Food> foods = new List<Food>() { new Food() };

        private void timer2_Tick(object sender, EventArgs e)
        {
            move = rnd.Next(1, 5);
            int food_x = rnd.Next(0, 290);
            int food_y = rnd.Next(0, 290);
            foods.Add(new Food() { x = food_x, y = food_y });
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            DrawRectangle(g, 340 + x, 280 + y, Color.Green);
            DrawRectangle(g, 330 + x, 280 + y, Color.Black);

            foreach (var f in foods)
            {
                DrawRectangle(g, f.x, f.y, Color.Red);
            }

            g.Dispose();
        }

        private void DrawRectangle(Graphics g, int x, int y, Color color)
        {
            SolidBrush brush = new SolidBrush(color);
            Rectangle rect = new Rectangle(x, y, 10, 10);
            g.FillRectangle(brush, rect);
            brush.Dispose();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (move)
            {
                case 1:
                    for (int i = 0; i <= 20; i++)
                    {
                        x++;
                    }
                    break;
                case 2:
                    for (int i = 0; i <= 20; i++)
                    {
                        x--;
                    }
                    break;
                case 3:
                    for (int i = 0; i <= 20; i++)
                    {
                        y++;
                    }
                    break;
                case 4:
                    for (int i = 0; i <= 20; i++)
                    {
                        y--;
                    }
                    break;
            }

            if (MouseButtons == MouseButtons.Left)
            {

            }
            Invalidate();
        }
    }
}

 

Edited by 8Observer8

Share this comment


Link to comment

Did you take anything from my console snake blog? At least see how you can structure your code.

Food doesn't have x,y values. A position (i.e. screen position) does though.

public class Pos
{
    public int X { get; set; }
    public int Y { get; set; }

    public Pos(int x, int y)
    {
        X = x;
        Y = y;
    }
}

Then you can have a position for your food (there is only one in my version of the game).

static Pos FoodPos = new Pos(0, 0);

As I already mentioned, don't create your own Graphics object. There's one for you in the Paint event.

e.Graphics

But I would first try the most basic thing, make a square move when the user presses an arrow key. That's it. No timers, randomized things, etc. Then move on.

Edited by duke_meister

Share this comment


Link to comment

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
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!