Пример #1
0
        /// <summary>
        /// 当たり判定を行う
        /// </summary>
        /// <param name="ch">当たり判定を行うキャラクタ</param>
        private void HitTest(GameCharacter ch)
        {
            // 当たり判定
            var checkChara = hitTestArray[ch.GetIndex()];

            // 何にも当たっていなかった場合
            if (checkChara == null)
            {
                hitTestArray[ch.GetIndex()] = ch;
                return;
            }

            // 当たっていた場合
            checkChara.UpdateHitPoint(ch);

            // もともとindexの位置にあったキャラクタ
            if (!checkChara.IsAlive())
            {
                hitTestArray[ch.GetIndex()] = null;
            }

            // 当たり判定対象のキャラクタ
            if (ch.IsAlive())
            {
                hitTestArray[ch.GetIndex()] = ch;
            }

            // indexの位置のキャラクタのHPが残っていた場合、同じ位置に描画しなおす
            if (hitTestArray[ch.GetIndex()] != null)
            {
                hitTestArray[ch.GetIndex()].UpdateDrawing();
            }
        }
Пример #2
0
        /// <summary>
        /// 自機の移動、自弾の発射の処理を行う
        /// </summary>
        private void RunShipEvent()
        {
            while (!isGameOver)
            {
                lock (ship)
                {
                    if (!Console.KeyAvailable)
                    {
                        continue;
                    }
                    if (isGameOver)
                    {
                        break;
                    }

                    var key = Console.ReadKey(true).Key;
                    switch (key)
                    {
                    case ConsoleKey.LeftArrow:
                        ship.MoveLeft();
                        if (ship.HitTest(hitTestArray))
                        {
                            isGameOver = true;
                            break;
                        }
                        continue;

                    case ConsoleKey.RightArrow:
                        ship.MoveRight();
                        if (ship.HitTest(hitTestArray))
                        {
                            isGameOver = true;
                            break;
                        }
                        continue;

                    case ConsoleKey.Spacebar:
                        if (shipBullet != null)
                        {
                            break;;
                        }
                        shipBullet = ship.ShotBullet();

                        // 弾を作成した位置に敵・敵弾があった場合は対消滅させる
                        if (hitTestArray[shipBullet.GetIndex()] != null)
                        {
                            hitTestArray[shipBullet.GetIndex()].UpdateHitPoint(shipBullet);
                            shipBullet = null;
                            continue;;
                        }
                        gameCharacters.Add(shipBullet);
                        continue;
                    }
                }
            }

            // ゲームオーバー後の処理
            while (isGameOver)
            {
                if (!Console.KeyAvailable)
                {
                    continue;
                }
                var key = Console.ReadKey(true).Key;

                switch (key)
                {
                case ConsoleKey.Q:     // ゲームをやめる
                    return;

                case ConsoleKey.Enter:     // 新しくゲームを始める
                    GameStart();
                    return;

                default:
                    continue;
                }
            }
        }