Help with a couple of things in play snake in c # [duplicated]

-1

I'm doing a snake game in c #, and I would need a couple of things that I do not know very well how to do:

  

I need the game to send a message similar to "you have died" as soon as the snake touches the wall or touches itself, but before this, I need to know how I can make the snake move forward with an established initial size, for example 2 or 3 rings

I can say that I have some idea of how to do the wall, I was thinking of something like:

if (abs (cx) == 30 or abs (cy) == 100) then it takes you out of the program

But I do not know if this would be the most correct way, so if someone can give me some further guidance, I will be immensely grateful. I leave the code I have so far

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


namespace snake_dia_1
{
    class Program
    {

        public struct Coords
        {
            public int cX;
            public int cY;
        }

        public struct Cuc
        {
            public int cX;
            public int cY;
            //public coords cap; seria mes correcte
            public char direccio;
        }

        static void Main(string[] args)
        {

            Cuc cuc = new Cuc();
            bool sortir = false;
            ConsoleKeyInfo cki;

            pintaMarc();
            cuc.direccio = 'd';
            pintaCuc(ref cuc.cX, ref cuc.cY);

            while (!sortir)
            { 
                if (Console.KeyAvailable)
                {
                    cki = Console.ReadKey();
                    if (cki.Key == ConsoleKey.UpArrow)
                    {
                        cuc.direccio = 'w';
                    }
                    if (cki.Key == ConsoleKey.DownArrow)
                    {
                        cuc.direccio = 's';
                    }
                    if (cki.Key == ConsoleKey.LeftArrow)
                    {
                        cuc.direccio = 'a';
                    }
                    if (cki.Key == ConsoleKey.RightArrow)
                    {
                        cuc.direccio = 'd';
                    }
                    if (cki.Key == ConsoleKey.Escape)
                    {
                        exit();
                    }
                }

                mouCuc(ref cuc.cX, ref cuc.cY, cuc.direccio);
                System.Threading.Thread.Sleep(150);

            }


        } //end main()

        static void pintaMarc()
        {
            //caracters marc 186 ║, 187 ╗, 188 ╝, 200 ╚, 201 ╔, 205 ═

            Console.SetCursorPosition(1, 1);
            Console.WriteLine("╔");
            Console.SetCursorPosition(1, 30);
            Console.WriteLine("╚");
            Console.SetCursorPosition(100, 1);
            Console.WriteLine("╗");
            Console.SetCursorPosition(100, 30);
            Console.WriteLine("╝");


            for (int y = 2; y < 100; y++)
            {
                int x = 1;
                Console.SetCursorPosition(y, x);
                Console.WriteLine("═");
            }

            for (int x = 2; x < 30; x++)
            {
                int y = 1;
                Console.SetCursorPosition(y, x);
                Console.WriteLine("║");

                y = 100;
                Console.SetCursorPosition(y, x);
                Console.WriteLine("║");
            }

            for (int y = 2; y < 100; y++)
            {
                int x = 30;
                Console.SetCursorPosition(y, x);
                Console.WriteLine("═");
            }


         }

        static void pintaCuc(ref int cx, ref int cy)
        {

            cx = 30;
            cy = 10;

                Console.SetCursorPosition(cx, cy);
                Console.Write("█");
        }

        static void mouCuc(ref int cx, ref int cy, char direccio)
        {
            if(direccio == 'w')
            {
                Console.SetCursorPosition(cx, cy);
                Console.Write("█");
                cy--;
                Console.SetCursorPosition(cx, cy);
                Console.Write("0");
            }
            if (direccio == 'a')
            {
                Console.SetCursorPosition(cx, cy);
                Console.Write("█");
                cx--;
                Console.SetCursorPosition(cx, cy);
                Console.Write("0");
            }
            if (direccio == 's')
            {
                Console.SetCursorPosition(cx, cy);
                Console.Write("█");
                cy++;
                Console.SetCursorPosition(cx, cy);
                Console.Write("0");
            }
            if (direccio == 'd')
            {
                Console.SetCursorPosition(cx, cy);
                Console.Write("█");
                cx++;
                Console.SetCursorPosition(cx, cy);
                Console.Write("0");
            }
        }

        static void exit ()
        {
            Environment.Exit(0);
        }

    } //end class
}//end namespace
    
asked by THR4SH3RP0L0 04.04.2017 в 12:30
source

1 answer

0

If the wall is always in a fixed position, the best you can do is what you pose.

if (cx>=100 || cy>=30 || cy==0 || cx==0) {
  // Sortir de l'aplicació
}

Where 100, 30 and 0 appear, they should be the absolute positions of the X wall, and

Greetings,

    
answered by 04.04.2017 / 12:49
source