Ejemplo n.º 1
0
        public int JudgeSafeSatate(float[] FallOffPosition, Cube nowCube, Cube nextCube)
        {
            float Xp = FallOffPosition[0];
            float Yp = FallOffPosition[1];

            if (nextCube.RefreshDirection == 1)
            {
                if (Xp >= nowCube.MinX && Xp <= nowCube.MidX)
                {
                    if (Yp >= nowCube.y0FROMx0(Xp) && Yp <= nowCube.y1FROMx1(Xp))
                    {
                        return(30);
                    }
                    else
                    {
                        return(31);
                    }
                }
                if (Xp > nowCube.MidX && Xp <= nowCube.MaxX)
                {
                    if (Yp >= nowCube.y2FROMx2(Xp) && Yp <= nowCube.y3FROMx3(Xp))
                    {
                        return(30);
                    }
                    else
                    {
                        return(31);
                    }
                }

                if (Xp > nowCube.MaxX && Xp < nextCube.MinX)
                {
                    return(31);
                }
                if (Xp >= nextCube.MinX && Xp <= nextCube.MidX)
                {
                    if (Yp >= nextCube.y0FROMx0(Xp) && Yp <= nextCube.y1FROMx1(Xp))
                    {
                        return(32);
                    }
                    else
                    {
                        return(31);
                    }
                }
                if (Xp > nextCube.MidX && Xp <= nextCube.MaxX)
                {
                    if (Yp >= nextCube.y2FROMx2(Xp) && Yp <= nextCube.y3FROMx3(Xp))
                    {
                        return(32);
                    }
                    return(33);
                }
                if (Xp > nextCube.MaxX)
                {
                    return(33);
                }
            }
            if (nextCube.RefreshDirection == -1)
            {
                if (Xp >= nowCube.MinX && Xp <= nowCube.MidX)
                {
                    if (Yp >= nowCube.y0FROMx0(Xp) && Yp <= nowCube.y1FROMx1(Xp))
                    {
                        return(30);
                    }
                    else
                    {
                        return(31);
                    }
                }
                if (Xp > nowCube.MidX && Xp <= nowCube.MaxX)
                {
                    if (Yp >= nowCube.y2FROMx2(Xp) && Yp <= nowCube.y3FROMx3(Xp))
                    {
                        return(30);
                    }
                    else
                    {
                        return(31);
                    }
                }

                if (Xp > nextCube.MaxX && Xp < nowCube.MinX)
                {
                    return(31);
                }
                if (Xp >= nextCube.MidX && Xp <= nextCube.MaxX)
                {
                    if (Yp >= nextCube.y2FROMx2(Xp) && Yp <= nextCube.y3FROMx3(Xp))
                    {
                        return(32);
                    }
                    else
                    {
                        return(31);
                    }
                }
                if (Xp >= nextCube.MinX && Xp < nextCube.MidX)
                {
                    if (Yp >= nextCube.y0FROMx0(Xp) && Yp <= nextCube.y1FROMx1(Xp))
                    {
                        return(32);
                    }
                    else
                    {
                        return(33);
                    }
                }
                if (Xp < nextCube.MinX)
                {
                    return(33);
                }
            }

            return(39);//还有情况没有考虑到,坐标以及方程之间的判断出错。
        }
Ejemplo n.º 2
0
        //游戏逻辑
        public void RunLogic()
        {
            switch (nStateIndex)
            {
            case -1:    //小人下落状态,只在游戏开始时运行一次,但是需要运行好几帧,此后循环只从nStateIndex=0开始
            {
                drawStateIndex = -11;
                //当最后一帧运行动画时,进入if,因此在倒数第二针帧的动画结束时,RunDrawStateNegative11EndFlag赋值true,以至于最后一帧时进入此判断
                if (RunDrawStateNegative11EndFlag)
                {
                    RunDrawStateNegative11EndFlag = false;
                    nStateIndex = 0;
                }
                break;
            }

            case 0:    //空闲状态,等待按压
            {
                textBlockbb.Text    = "State 0 " + $"第{CountOfLevel}个方块" + " counts= " + CountOfAnimationFrame.ToString();
                ScoreNumber.Content = CountOfLevel.ToString();

                //计算安全区,计算新的nowcube和nextcube表面的四条函数方程
                NowCube.ComputeParameterOfSafeZone();
                NextCube.ComputeParameterOfSafeZone();
                player.MoveDirection = NextCube.RefreshDirection;
                drawStateIndex       = 10;
                break;
            }

            case 1:    //蓄力状态
            {
                textBlockbb.Text = "State 1";
                drawStateIndex   = 11;
                break;
            }

            case 2:    //判断状态
            {
                textBlockbb.Text = "State 2";

                //根据按键时间换算移动距离
                player.distanceOfMove = player.PressTimeToDistance(dPressTime);

                //计算出小人跳跃后落点的坐标。
                player.FallOffPosition = player.ComputeFallOutPositon(player.distanceOfMove, player.Position, NextCube.Position, NextCube.RefreshDirection);

                //计算小人跳跃函数的参数
                player.parameterXYofJump(player.Position, NextCube.Position);

                int isSafeFlag = player.JudgeSafeSatate(player.FallOffPosition, NowCube, NextCube);
                nStateIndex    = isSafeFlag;
                drawStateIndex = 12;
                break;
            }

            case 30:    //跳在原Cube的情况
            {
                textBlockbb.Text = "State 30";
                drawStateIndex   = 130;

                //只有当动画结束才执行的操作
                if (RunDrawState130EndFlag)
                {
                    //当RunDrawState130EndFlag=true进入此后,立刻置false,不然动画只能执行一次。
                    RunDrawState130EndFlag = false;

                    //当动画结束后,更新person现在的坐标,因为person跳跃的动画需要用到起跳的位置,所以只能在跳跃完成后更新person现在的坐标
                    player.Position = player.FallOffPosition;
                    //当动画结束后,进入0状态等待命令
                    nStateIndex    = 0;
                    drawStateIndex = 10;
                }
                break;
            }

            case 31:    //失败,跳在两个Cube之间的情况
            {
                textBlockbb.Text = "State 31 失败,跳在两个Cube之间的情况";
                drawStateIndex   = 131;

                //只有当动画结束才执行的操作
                if (RunDrawState131EndFlag)
                {
                    //当RunDrawState130EndFlag=true进入此后,立刻置false,不然动画只能执行一次。
                    RunDrawState131EndFlag = false;

                    bGameOver = true;
                }

                break;
            }

            case 32:    //成功跳到下一Cube的情况
            {
                textBlockbb.Text       = "State 32";
                drawStateIndex         = 132;
                CountOfAnimationFrame += 1;        //第几个播放动画的帧数

                if (RunDrawState132EndFlag)
                {
                    //当RunDrawState130EndFlag=true进入此后,立刻置false,不然动画只能执行一次。
                    RunDrawState132EndFlag = false;

                    //当动画结束后,更新person现在的坐标,因为person跳跃的动画需要用到起跳的位置,所以只能在跳跃完成后更新person现在的坐标
                    player.Position = player.FallOffPosition;

                    //跳跃成功,之前的nextcube要就是现在的nowcube,所以要赋值一次,之前的nextcube重新初始化,当成下一个cube
                    NowCube = NextCube;

                    NextCube = new Cube();
                    NextCube.RefreshDistance = 200;        //这个距离应该是随机出现的,现在暂时固定
                    NextCube.SideLength      = 100;

                    Random rng = new Random();
                    int    s   = rng.Next(2);
                    if (s == 0)
                    {
                        NextCube.RefreshDirection = -1;
                    }
                    else
                    {
                        NextCube.RefreshDirection = 1;         //这个方向应该是在-1和1中随机出现的,现在暂时固定
                    }
                    //计算新出现的cube的位置
                    NextCube.Position = NextCube.ComputCubePositon(NextCube.RefreshDirection, NextCube.RefreshDistance, NowCube.Position);

                    //当动画结束后,进入0状态等待命令,重新开始新的游戏循环
                    nStateIndex    = 0;
                    drawStateIndex = 1320;
                    cubeFallIndex  = 1;



                    //跳到了第几个方块
                    CountOfLevel++;
                }
                break;
            }

            case 33:    //失败,跳过了下一个Cube的情况
            {
                textBlockbb.Text = "State 33 失败,跳过了下一个Cube的情况";
                drawStateIndex   = 133;
                if (RunDrawState133EndFlag)
                {
                    //当RunDrawState130EndFlag=true进入此后,立刻置false,不然动画只能执行一次。
                    RunDrawState133EndFlag = false;
                    bGameOver = true;
                }
                break;
            }
            }
        }