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);
}
}
}
}
}