在主机游戏Snake中保存/加载Snake位置

时间:2018-08-25 01:44:07

标签: c# console-application

我正在尝试学习如何在主机游戏中保存和加载。目前,我正在尝试从文件加载蛇的位置。蛇是具有每个元素的X和Y坐标的列表。

我的问题是蛇没有从注册位置加载。我想问题出在阅读上。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.IO;

namespace MySnake
{

    struct Position
    {
        public int Row;

        public int Col;

        public Position(int row, int col)
        {
            this.Row = row;
            this.Col = col;
        }

    }
    class Program
    {
        static void Main()
        {
            byte right = 0;
            byte left = 1;
            byte down = 2;
            byte up = 3;

            Position[] directions =
            {
                new Position(0,1), // right
                new Position(0,-1), // left
                new Position(1,0), // down
                new Position(-1,0), // up

            };

            int direction = 0;

            Console.SetBufferSize(100, 100);
            Console.SetWindowSize(Console.BufferWidth, Console.BufferHeight);

            List<Position> snake = new List<Position>();
            for (int i = 0; i < 6; i++)
            {
                snake.Add(new Position(0, 1));
            }

            Draw(snake,"*");

            while (true)
            {

                if (Console.KeyAvailable)
                {

                    ConsoleKeyInfo userImput = Console.ReadKey();

                    if (userImput.Key == ConsoleKey.LeftArrow)
                    {
                        if (direction != right)
                        {
                            direction = 1;
                        }

                    }
                    if (userImput.Key == ConsoleKey.RightArrow)
                    {
                        if (direction != left)
                        {
                            direction = 0;
                        }

                    }
                    if (userImput.Key == ConsoleKey.UpArrow)
                    {
                        if (direction != down)
                        {
                            direction = 3;
                        }

                    }
                    if (userImput.Key == ConsoleKey.DownArrow)
                    {
                        if (direction != up)
                        {
                            direction = 2;
                        }

                    }
                    if (userImput.Key == ConsoleKey.S)
                    {
                        SaveGame(snake);
                    }
                    if (userImput.Key == ConsoleKey.L)
                    {
                        LoadGame(snake);
                    }

                }

                Position snakeHead = snake.Last();
                Position snakeNewHead = new Position(snakeHead.Row + directions[direction].Row, snakeHead.Col + directions[direction].Col);


                snake.Add(snakeNewHead);
            Draw(snakeNewHead,"*");

                Console.SetCursorPosition(snake[0].Col, snake[0].Row);
                Console.Write(' ');
                snake.RemoveAt(0); // take off at the back!!


                Thread.Sleep(100);//100ms
            }
        }      
        private static void Draw(List<Position> smth,string str)
        {
            foreach (var position in smth)
            {
                Console.SetCursorPosition(position.Col, position.Row);
                Console.Write(str);
            }
        }
        private static void Draw(Position smth,string str)
        {
                Console.SetCursorPosition(smth.Col, smth.Row);
                Console.Write(str);
        }
        private static void LoadGame(List<Position> snake)
        {
            int snakeCount = snake.Count;
            snake.Clear();

            StreamReader reader = new StreamReader("savegame.jcn");
            using (reader)
            {

                for (int i = 0; i < snakeCount; i++)
                {
                    string[] text = reader.ReadLine().Split(' ');
                    snake.Add(new Position(int.Parse(text[0]), int.Parse(text[1])));
                }

            }
        }
        private static void SaveGame(List<Position>snake)
        {
            StreamWriter writer = new StreamWriter("savegame.jcn");
            using (writer)
            {
                foreach (var element in snake)
                {
                    writer.WriteLine(element.Col + " " + element.Row);
                }
            }
        }
    }

}

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试用该版本:

struct Position
{
    public int Row;
    public int Col;
    public Position(int row, int col)
    {
        this.Row = row;
        this.Col = col;
    }
}

class Program
{
    static void Main()
    {
        byte right = 0;
        byte left = 1;
        byte down = 2;
        byte up = 3;

        Position[] directions =
        {
            new Position(0, 1), // right
            new Position(0, -1), // left
            new Position(1, 0), // down
            new Position(-1, 0), // up
        };

        int direction = right;

        Console.SetBufferSize(100, 100);
        Console.SetWindowSize(Console.BufferWidth, Console.BufferHeight);

        List<Position> snake = new List<Position>();
        for (int i = 0; i < 6; i++)
        {
            snake.Add(new Position(0, 1));
        }

        Draw(snake, "*");

        while (true)
        {

            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo userImput = Console.ReadKey();

                var moves = new []
                {
                    new { key = ConsoleKey.LeftArrow, no = right, yes = left },
                    new { key = ConsoleKey.RightArrow, no = left, yes = right },
                    new { key = ConsoleKey.UpArrow, no = down, yes = up },
                    new { key = ConsoleKey.DownArrow, no = up, yes = down },
                }

                moves
                    .Where(x => x.key == userImput.Key)
                    .Where(x => x.no != direction)
                    .ToList()
                    .ForEach(x => direction = x.yes);

                if (userImput.Key == ConsoleKey.S)
                {
                    SaveGame(snake);
                }

                if (userImput.Key == ConsoleKey.L)
                {
                    LoadGame(snake);
                }
            }

            Position snakeHead = snake.Last();
            Position snakeNewHead = new Position(snakeHead.Row + directions[direction].Row, snakeHead.Col + directions[direction].Col);

            snake.Add(snakeNewHead);
            Draw(snakeNewHead, "*");

            Console.SetCursorPosition(snake[0].Col, snake[0].Row);
            Console.Write(' ');
            snake.RemoveAt(0); // take off at the back!!

            Thread.Sleep(TimeSpan.FromMilliseconds(100.0));
        }
    }

    private static void Draw(List<Position> smth, string str)
    {
        foreach (var position in smth)
        {
            Console.SetCursorPosition(position.Col, position.Row);
            Console.Write(str);
        }
    }

    private static void Draw(Position smth, string str)
    {
        Console.SetCursorPosition(smth.Col, smth.Row);
        Console.Write(str);
    }

    private static void LoadGame(List<Position> snake)
    {
        snake.Clear();
        snake.AddRange(
            File
                .ReadAllLines("savegame.jcn")
                .Select(x => x.Split(' '))
                .Select(x => new Position(int.Parse(x[0]), int.Parse(x[1]))));
    }

    private static void SaveGame(List<Position> snake)
    {
        File.WriteAllLines("savegame.jcn", snake.Select(x => $"{x.Col} {x.Row}"));
    }
}

这里的关键是使用File.ReadAllLinesFile.WriteAllLines