【黑马程序员】一个简单的小游戏——骑士飞行棋

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

这是一个简单的小游戏——骑士飞行棋的实现方法。本游戏由两名玩家对战。运用的是C#的基础知识,是对基础知识的一个总结。下面是本游戏的效果图:

先上本游戏实现方法的代码:

namespace 骑士飞行棋
{
    /// <summary>
    /// 玩家类
    /// </summary>
    class Player
    {
        public string name;      //玩家姓名

        public int position;     //玩家当前所处位置 

        public int statue;    //玩家当前所处的状态,即当前玩家是否可以投掷骰子,1表示可以,0表示不可以

        public bool isPause;   //指示玩家是否暂停
    }

    /// <summary>
    /// 飞行棋类(本游戏限两人对战)
    /// </summary>
    class FlyingCheese
    {
        int[] Map = new int[100];          //表示地图

        public Player playerA = new Player();    //A玩家

        public Player playerB = new Player();     //B玩家

        Random random = new Random();   //掷骰子的随机数对象

        Player winner;        //胜利的玩家

        //下面是用于存储地图中特殊位置的下标
        int[] LuckyTurn;//幸运转盘
        int[] Landmine;//地雷
        int[] Pause;//暂停
        int[] TimeTunnel;//时空隧道

        /// <summary>
        /// 程序输出的开头,用于显示游戏的名称(由于要重复输出,因此形成一个单独的方法)
        /// </summary>
        public void ShowGame()
        {
            Console.WriteLine("********************************************************");
            Console.WriteLine("*     *      *                            *      *     *");
            Console.WriteLine("**************   骑   士   飞   行   棋   **************");
            Console.WriteLine("*     *      *                            *      *     *");
            Console.WriteLine("********************************************************");
        }

        /// <summary>
        /// 初始化有关地图的数据
        /// </summary>
        public void InitialMap()
        {
            LuckyTurn = new int[] { 6, 23, 40, 55, 69, 83 };//幸运转盘
            Landmine = new int[] { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷
            Pause = new int[] { 9, 27, 60, 93 };//暂停
            TimeTunnel = new int[] { 20, 25, 45, 63, 72, 88, 90 };//时空隧道

            for (int i = 0; i < LuckyTurn.Length; i++)  //幸运转盘在Map中用1表示
            {
                Map[LuckyTurn[i]] = 1;
            }
            for (int i = 0; i < Landmine.Length; i++)  //地雷在Map中用2表示
            {
                Map[Landmine[i]] = 2;
            }
            for (int i = 0; i < Pause.Length; i++)  //暂停在Map中用3表示
            {
                Map[Pause[i]] = 3;
            }
            for (int i = 0; i < TimeTunnel.Length; i++)  //时光隧道在Map中用4表示
            {
                Map[TimeTunnel[i]] = 4;
            }
        }

        /// <summary>
        /// 画地图(要分成五个部分来画)
        /// 我们总共画100个位置,其中横三行分别为30个位置,竖二行分别为5个位置
        /// </summary>
        public void DrawMap()
        {
            //在正式画地图之前,如果玩家的位置下标小于0,就让他的下标为0
            if (playerA.position < 0)
            {
                playerA.position = 0;
            }
            if (playerB.position < 0)
            {
                playerB.position = 0;
            }
            //在正式画地图之前,如果玩家的位置下标大于99,就让他的下标为99
            if (playerA.position > 99)
            {
                playerA.position = 99;
            }
            if (playerB.position > 99)
            {
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的基于Java飞行棋小游戏代码示例: ``` import java.util.*; public class FlyChess { private static final int WINNING_SCORE = 30; // 获胜分数 private static final int BOARD_SIZE = 30; // 棋盘大小 private static final int MAX_PLAYERS = 4; // 最大玩家数 private static final String[] PLAYER_COLORS = {"红色", "黄色", "绿色", "蓝色"}; // 玩家颜色 private static final int[] PLAYER_START_POSITIONS = {0, 10, 20, 30}; // 玩家起始位置 private static final int[] DICE_VALUES = {1, 2, 3, 4, 5, 6}; // 骰子点数 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("欢迎来玩飞行棋游戏!"); // 创建玩家 System.out.print("请输入玩家数(最多" + MAX_PLAYERS + "人):"); int numPlayers = scanner.nextInt(); if (numPlayers > MAX_PLAYERS) { numPlayers = MAX_PLAYERS; } Player[] players = new Player[numPlayers]; for (int i = 0; i < numPlayers; i++) { System.out.print("请输入玩家" + (i + 1) + "的名称:"); String name = scanner.next(); players[i] = new Player(name, PLAYER_COLORS[i], PLAYER_START_POSITIONS[i]); } // 创建棋盘 Board board = new Board(BOARD_SIZE); // 开始游戏 int currentPlayerIndex = 0; while (true) { Player currentPlayer = players[currentPlayerIndex]; System.out.println("轮到玩家 " + currentPlayer.getName() + ",颜色为 " + currentPlayer.getColor() + ",当前得分为 " + currentPlayer.getScore() + " 分。"); System.out.print("按 Enter 键开始掷骰子:"); scanner.nextLine(); // 读取 Enter 键 scanner.nextLine(); // 等待用户输入 int diceValue = DICE_VALUES[(int) (Math.random() * DICE_VALUES.length)]; System.out.println("掷出了 " + diceValue + " 点。"); // 玩家移动 int newPosition = currentPlayer.getPosition() + diceValue; if (newPosition > board.getSize()) { System.out.println("超出了棋盘范围,不能前进。"); } else { currentPlayer.setPosition(newPosition); int score = board.getScore(newPosition); currentPlayer.addScore(score); System.out.println("前进了 " + diceValue + " 步,获得了 " + score + " 分。"); } // 判断游戏是否结束 if (currentPlayer.getScore() >= WINNING_SCORE) { System.out.println("游戏结束,玩家 " + currentPlayer.getName() + " 获胜!"); break; } // 切换下一个玩家 currentPlayerIndex = (currentPlayerIndex + 1) % numPlayers; } } } class Player { private String name; private String
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值