c#,写一个推箱子游戏,要求用到继承,类,枚举,结构体

 c#,写一个推箱子游戏,要求用到继承,类,枚举,结构体

using System;

namespace SokobanGame
{
    // 枚举表示方向
    public enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }

    // 结构体表示位置
    public struct Position
    {
        public int X;
        public int Y;

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

    // 基本游戏对象类
    public abstract class GameObject
    {
        public Position Position { get; set; }

        public GameObject(Position position)
        {
            Position = position;
        }

        public abstract char GetSymbol();
    }

    // 玩家类继承自GameObject
    public class Player : GameObject
    {
        public Player(Position position) : base(position) { }

        public override char GetSymbol()
        {
            return 'P';
        }

        public void Move(Direction direction)
        {
            switch (direction)
            {
                case Direction.Up:
                    Position = new Position(Position.X, Position.Y - 1);
                    break;
                case Direction.Down:
                    Position = new Position(Position.X, Position.Y + 1);
                    break;
                case Direction.Left:
                    Position = new Position(Position.X - 1, Position.Y);
                    break;
                case Direction.Right:
                    Position = new Position(Position.X + 1, Position.Y);
                    break;
            }
        }
    }

    // 箱子类继承自GameObject
    public class Box : GameObject
    {
        public Box(Position position) : base(position) { }

        public override char GetSymbol()
        {
            return 'B';
        }
    }

    // 墙壁类继承自GameObject
    public class Wall : GameObject
    {
        public Wall(Position position) : base(position) { }

        public override char GetSymbol()
        {
            return '#';
        }
    }

    // 地图类,包含所有游戏对象
    public class GameMap
    {
        private int width;
        private int height;
        private GameObject[,] map;
        private Player player;

        public GameMap(int width, int height)
        {
            this.width = width;
            this.height = height;
            map = new GameObject[width, height];
        }

        public void AddObject(GameObject obj)
        {
            map[obj.Position.X, obj.Position.Y] = obj;
            if (obj is Player)
                player = (Player)obj;
        }

        public void Draw()
        {
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (map[x, y] == null)
                        Console.Write('.');
                    else
                        Console.Write(map[x, y].GetSymbol());
                }
                Console.WriteLine();
            }
        }

        public void MovePlayer(Direction direction)
        {
            Position newPosition = player.Position;
            switch (direction)
            {
                case Direction.Up:
                    newPosition.Y -= 1;
                    break;
                case Direction.Down:
                    newPosition.Y += 1;
                    break;
                case Direction.Left:
                    newPosition.X -= 1;
                    break;
                case Direction.Right:
                    newPosition.X += 1;
                    break;
            }

            if (CanMoveTo(newPosition))
            {
                map[player.Position.X, player.Position.Y] = null;
                player.Move(direction);
                map[player.Position.X, player.Position.Y] = player;
            }
        }

        private bool CanMoveTo(Position position)
        {
            if (position.X < 0 || position.X >= width || position.Y < 0 || position.Y >= height)
                return false;

            return map[position.X, position.Y] == null;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            GameMap gameMap = new GameMap(10, 10);

            // 添加玩家
            gameMap.AddObject(new Player(new Position(1, 1)));

            // 添加一些墙壁
            gameMap.AddObject(new Wall(new Position(0, 0)));
            gameMap.AddObject(new Wall(new Position(0, 1)));
            gameMap.AddObject(new Wall(new Position(0, 2)));

            // 添加一个箱子
            gameMap.AddObject(new Box(new Position(2, 2)));

            // 游戏循环
            while (true)
            {
                Console.Clear();
                gameMap.Draw();

                ConsoleKeyInfo key = Console.ReadKey();
                Direction? direction = null;

                if (key.Key == ConsoleKey.W) direction = Direction.Up;
                if (key.Key == ConsoleKey.S) direction = Direction.Down;
                if (key.Key == ConsoleKey.A) direction = Direction.Left;
                if (key.Key == ConsoleKey.D) direction = Direction.Right;

                if (direction.HasValue)
                {
                    gameMap.MovePlayer(direction.Value);
                }
            }
        }
    }
}

using System;

namespace SokobanGame
{
    // 枚举表示方向
    public enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }

    // 结构体表示位置
    public struct Position
    {
        public int X;
        public int Y;

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

    // 基本游戏对象类
    public abstract class GameObject
    {
        public Position Position { get; set; }

        public GameObject(Position position)
        {
            Position = position;
        }

        public abstract char GetSymbol();
    }

    // 玩家类继承自GameObject
    public class Player : GameObject
    {
        public Player(Position position) : base(position) { }

        public override char GetSymbol()
        {
            return 'P';
        }

        public void Move(Direction direction)
        {
            switch (direction)
            {
                case Direction.Up:
                    Position = new Position(Position.X, Position.Y - 1);
                    break;
                case Direction.Down:
                    Position = new Position(Position.X, Position.Y + 1);
                    break;
                case Direction.Left:
                    Position = new Position(Position.X - 1, Position.Y);
                    break;
                case Direction.Right:
                    Position = new Position(Position.X + 1, Position.Y);
                    break;
            }
        }
    }

    // 箱子类继承自GameObject
    public class Box : GameObject
    {
        public Box(Position position) : base(position) { }

        public override char GetSymbol()
        {
            return 'B';
        }
    }

    // 墙壁类继承自GameObject
    public class Wall : GameObject
    {
        public Wall(Position position) : base(position) { }

        public override char GetSymbol()
        {
            return '#';
        }
    }

    // 地图类,包含所有游戏对象
    public class GameMap
    {
        private int width;
        private int height;
        private GameObject[,] map;
        private Player player;

        public GameMap(int width, int height)
        {
            this.width = width;
            this.height = height;
            map = new GameObject[width, height];
        }

        public void AddObject(GameObject obj)
        {
            map[obj.Position.X, obj.Position.Y] = obj;
            if (obj is Player)
                player = (Player)obj;
        }

        public void Draw()
        {
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (map[x, y] == null)
                        Console.Write('.');
                    else
                        Console.Write(map[x, y].GetSymbol());
                }
                Console.WriteLine();
            }
        }

        public void MovePlayer(Direction direction)
        {
            Position newPosition = player.Position;
            switch (direction)
            {
                case Direction.Up:
                    newPosition.Y -= 1;
                    break;
                case Direction.Down:
                    newPosition.Y += 1;
                    break;
                case Direction.Left:
                    newPosition.X -= 1;
                    break;
                case Direction.Right:
                    newPosition.X += 1;
                    break;
            }

            if (CanMoveTo(newPosition))
            {
                map[player.Position.X, player.Position.Y] = null;
                player.Move(direction);
                map[player.Position.X, player.Position.Y] = player;
            }
        }

        private bool CanMoveTo(Position position)
        {
            if (position.X < 0 || position.X >= width || position.Y < 0 || position.Y >= height)
                return false;

            return map[position.X, position.Y] == null;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            GameMap gameMap = new GameMap(10, 10);

            // 添加玩家
            gameMap.AddObject(new Player(new Position(1, 1)));

            // 添加一些墙壁
            gameMap.AddObject(new Wall(new Position(0, 0)));
            gameMap.AddObject(new Wall(new Position(0, 1)));
            gameMap.AddObject(new Wall(new Position(0, 2)));

            // 添加一个箱子
            gameMap.AddObject(new Box(new Position(2, 2)));

            // 游戏循环
            while (true)
            {
                Console.Clear();
                gameMap.Draw();

                ConsoleKeyInfo key = Console.ReadKey();
                Direction? direction = null;

                if (key.Key == ConsoleKey.W) direction = Direction.Up;
                if (key.Key == ConsoleKey.S) direction = Direction.Down;
                if (key.Key == ConsoleKey.A) direction = Direction.Left;
                if (key.Key == ConsoleKey.D) direction = Direction.Right;

                if (direction.HasValue)
                {
                    gameMap.MovePlayer(direction.Value);
                }
            }
        }
    }
}
 

相关推荐

  1. c#---结构

    2024-06-16 14:24:03       10 阅读
  2. C++箱子游戏开发

    2024-06-16 14:24:03       42 阅读
  3. 箱子游戏C++

    2024-06-16 14:24:03       11 阅读
  4. C#基础——字典、结构

    2024-06-16 14:24:03       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-16 14:24:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-16 14:24:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-16 14:24:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-16 14:24:03       20 阅读

热门阅读

  1. yocto根文件系统如何配置静态IP地址

    2024-06-16 14:24:03       9 阅读
  2. web前端网上私活:探索、挑战与成长的独特之旅

    2024-06-16 14:24:03       8 阅读
  3. Web前端网页滚动效果:深度解析与创意实践

    2024-06-16 14:24:03       8 阅读
  4. c++题目_第K小的数(进阶)

    2024-06-16 14:24:03       6 阅读
  5. [AIGC] 深入浅出 Python中的`enumerate`函数

    2024-06-16 14:24:03       9 阅读
  6. 刷题 ——反转链表(若有其它解法,继续补充)

    2024-06-16 14:24:03       11 阅读
  7. wordpress站群搭建1需求分析

    2024-06-16 14:24:03       9 阅读
  8. git子模块应用和常用用法

    2024-06-16 14:24:03       7 阅读
  9. MySQL每日备份

    2024-06-16 14:24:03       6 阅读