lectri / Snake

A simple snake game made using Python. To practice my programming skill.
1 stars 0 forks source link

Optimize Snake Movement #15

Closed lectri closed 3 years ago

lectri commented 3 years ago

While I was learning to use Unity, I found a way to make a slightly better move function. See this:

void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow) | snakeDirection == "U") 
        {
            y++;
            snakeDirection = "U";
        }

        if (Input.GetKeyDown(KeyCode.DownArrow) | snakeDirection == "D") 
        {
            y--;
            snakeDirection = "D";
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow) | snakeDirection == "L") 
        {
            x--;
            snakeDirection = "L";
        }

        if (Input.GetKeyDown(KeyCode.RightArrow) | snakeDirection == "R") 
        {
            x++;
            snakeDirection = "R";
        }

Although this is C# code, we can still make use of this. If the key is pressed OR when the snake direction is one of four directions, update the sprite direction. Instead of having two different functions handle this.

lectri commented 3 years ago

I've decided I wont fix this. It might be more efficient, but with the way Pyglet handles key presses, the code will likely turn very ugly. If I get an idea I will reopen the issue, but for now I won't fix.