M
M
Maxim K2020-04-19 22:46:16
C++ / C#
Maxim K, 2020-04-19 22:46:16

How to fix the program - a snake?

Hello.
After reading the book, I decided to repeat the project - the snake program, only in c#. I am doing this lesson https://itproger.com/course/cpp-snake . Why doesn't this design work?

Declared like this:

public enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
   public eDirection dir;


I call like this:
static void Input()
        {
            if (Console.KeyAvailable)
            {
                var keyInfo = Console.ReadKey();
                switch (keyInfo.KeyChar)
                {
                    case 'a':
                        dir = LEFT;
                        break;
                    case 'd':
                        dir = RIGHT;
                        break;
                    case 'w':
                        dir = UP;
                        break;
                    case 's':
                        dir = DOWN;
                    case 'x':
                        gameOver = true;
                        break;
                }
            }

        }


I get an error:
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Program.dir'

How can I fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2020-04-19
@mkvmaks

1) A static method cannot change a local variable. the variable should then also be static
2)

dir = LEFT;
->
dir = eDirection.LEFT;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question