コード例 #1
0
        private void Canvas1_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args)
        {
            #region Menu
            if (Stage == 0)
            {
                Rect[] MenuRectangle = new Rect[2];
                for (int i = 0; i < 2; i++)
                {
                    MenuRectangle[i] = new Rect((int)((Canvas1.ActualWidth / 2) - 23), (int)((Canvas1.ActualHeight / 2) - 47 + (50 * i)), 50, 25);
                    if (!MenuRectangle[i].Contains(MouseCords))
                    {
                        args.DrawingSession.DrawRectangle(MenuRectangle[i], Colors.Black);
                    }
                    else
                    {
                        args.DrawingSession.DrawRectangle(MenuRectangle[i], Colors.Gold);
                    }
                }

                args.DrawingSession.DrawText("Train", (int)((Canvas1.ActualWidth / 2) - 20), (int)((Canvas1.ActualHeight / 2) - 50 + (50 * 0)), Colors.LightGreen);
                args.DrawingSession.DrawText("Fight", (int)((Canvas1.ActualWidth / 2) - 20), (int)((Canvas1.ActualHeight / 2) - 50 + (50 * 1)), Colors.LightGreen);

                if (MouseDown[0] && MenuRectangle[0].Contains(MouseCords))
                {
                    Stage        = 1;
                    MouseDown[0] = false;
                }
                if (MouseDown[0] && MenuRectangle[1].Contains(MouseCords))
                {
                    Stage        = 2;
                    MouseDown[0] = false;
                }
            }
            #endregion

            args.DrawingSession.DrawText(TrainerCount + "", 10, 10, Colors.LightGreen);
            #region Trainer
            if (Stage == 1)
            {
                #region BoardControl
                if (GameOver)
                {
                    if (Tie)
                    {
                        args.DrawingSession.DrawText("You both win!!!", (int)((Canvas1.ActualWidth / 2) - 40), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                    }
                    else if (Turn)
                    {
                        args.DrawingSession.DrawText("O Won", (int)((Canvas1.ActualWidth / 2) - 30), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                    }
                    else if (!Turn)
                    {
                        args.DrawingSession.DrawText("X Won", (int)((Canvas1.ActualWidth / 2) - 30), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                    }
                }
                else if (Turn)
                {
                    args.DrawingSession.DrawText("X's Turn", (int)((Canvas1.ActualWidth / 2) - 30), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                }
                else if (!Turn)
                {
                    args.DrawingSession.DrawText("O's Turn", (int)((Canvas1.ActualWidth / 2) - 30), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                }



                args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 50), (int)((Canvas1.ActualHeight / 2) - 150), (int)((Canvas1.ActualWidth / 2) - 50), (int)((Canvas1.ActualHeight / 2) + 150), Colors.Black);
                args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) + 50), (int)((Canvas1.ActualHeight / 2) - 150), (int)((Canvas1.ActualWidth / 2) + 50), (int)((Canvas1.ActualHeight / 2) + 150), Colors.Black);
                args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 150), (int)((Canvas1.ActualHeight / 2) - 50), (int)((Canvas1.ActualWidth / 2) + 150), (int)((Canvas1.ActualHeight / 2) - 50), Colors.Black);
                args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 150), (int)((Canvas1.ActualHeight / 2) + 50), (int)((Canvas1.ActualWidth / 2) + 150), (int)((Canvas1.ActualHeight / 2) + 50), Colors.Black);

                Rect[] GameRectangle = new Rect[9];
                for (int i = 0; i < 9; i++)
                {
                    GameRectangle[i] = new Rect((int)((Canvas1.ActualWidth / 2) - 150 + (100 * (i % 3))), (int)((Canvas1.ActualHeight / 2) - 150 + (100 * (Math.Floor((double)(i / 3))))), 100, 100);
                    if (!GameOver)
                    {
                        if (MouseDown[0] && GameRectangle[i].Contains(MouseCords) && GameState[i] == 0 && Turn)
                        {
                            OldGameState[i] = GameState[i];
                            GameState[i]    = 1;
                            MoveMemory      = i;
                            Turn            = !Turn;
                        }
                        else if (MouseDown[0] && GameRectangle[i].Contains(MouseCords) && GameState[i] == 0 && !Turn)
                        {
                            //OldGameState[i] = GameState[i];
                            GameState[i] = 2;
                            Turn         = !Turn;
                        }
                    }

                    if (GameState[i] == 1)
                    {
                        args.DrawingSession.DrawLine((int)(GameRectangle[i].X + 5), (int)(GameRectangle[i].Y + 5), (int)(GameRectangle[i].X + GameRectangle[i].Width - 5), (int)(GameRectangle[i].Y + GameRectangle[i].Height - 5), Colors.Black);
                        args.DrawingSession.DrawLine((int)(GameRectangle[i].X + 5), (int)(GameRectangle[i].Y + GameRectangle[i].Height - 5), (int)(GameRectangle[i].X + GameRectangle[i].Width - 5), (int)(GameRectangle[i].Y + 5), Colors.Black);
                    }
                    if (GameState[i] == 2)
                    {
                        args.DrawingSession.DrawEllipse((int)(GameRectangle[i].X + 50), (int)(GameRectangle[i].Y + 50), 45, 45, Colors.Black);
                    }
                }

                if (GameOver)
                {
                    args.DrawingSession.DrawText("Rematch", (int)((Canvas1.ActualWidth / 2) - 20), (int)((Canvas1.ActualHeight / 2) - 50 + (50 * 4)), Colors.LightGreen);
                    args.DrawingSession.DrawText("Go Home", (int)((Canvas1.ActualWidth / 2) - 20), (int)((Canvas1.ActualHeight / 2) - 50 + (50 * 5)), Colors.LightGreen);
                    Rect[] MenuRectangle = new Rect[2];
                    for (int i = 0; i < 2; i++)
                    {
                        MenuRectangle[i] = new Rect((int)((Canvas1.ActualWidth / 2) - 23), (int)((Canvas1.ActualHeight / 2) - 47 + (50 * (i + 4))), 90, 25);
                        if (!MenuRectangle[i].Contains(MouseCords))
                        {
                            args.DrawingSession.DrawRectangle(MenuRectangle[i], Colors.Black);
                        }
                        else
                        {
                            args.DrawingSession.DrawRectangle(MenuRectangle[i], Colors.Gold);
                        }
                    }
                    if (MouseDown[0] && MenuRectangle[0].Contains(MouseCords))
                    {
                        for (int i = 0; i < 9; i++)
                        {
                            GameState[i] = 0;
                        }
                        GameOver = false;
                        Tie      = false;
                    }
                    else if (MouseDown[0] && MenuRectangle[1].Contains(MouseCords))
                    {
                        for (int i = 0; i < 9; i++)
                        {
                            GameState[i] = 0;
                        }
                        GameOver = false;
                        Stage    = 4;
                        Tie      = false;
                    }
                }

                #region GameCheck
                for (int i = 0; i < 3; i++)
                {
                    if (GameState[i] != 0 && GameState[i] == GameState[i + 3] && GameState[i] == GameState[i + 6])
                    {
                        GameOver = true;
                        args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 100 + (100 * (i % 3))), (int)((Canvas1.ActualHeight / 2) - 150), (int)((Canvas1.ActualWidth / 2) - 100 + (100 * (i % 3))), (int)((Canvas1.ActualHeight / 2) + 150), Colors.Red);
                    }
                    else if (GameState[i * 3] != 0 && GameState[i * 3] == GameState[(i * 3) + 1] && GameState[i * 3] == GameState[(i * 3) + 2])
                    {
                        GameOver = true;
                        args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 150), (int)((Canvas1.ActualHeight / 2) - 100 + (100 * (i % 3))), (int)((Canvas1.ActualWidth / 2) + 150), (int)((Canvas1.ActualHeight / 2) - 100 + (100 * (i % 3))), Colors.Red);
                    }
                    else if (GameState[i * 2] != 0 && i != 2 && GameState[i * 2] == GameState[4] && GameState[i * 2] == GameState[8 - (i * 2)])
                    {
                        GameOver = true;
                        args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - (150 + (-300 * i))), (int)((Canvas1.ActualHeight / 2) - 150), (int)((Canvas1.ActualWidth / 2) + (150 + (-300 * i))), (int)((Canvas1.ActualHeight / 2) + 150), Colors.Red);
                    }
                    else if (!GameState.Contains(0))
                    {
                        GameOver = true;
                        Tie      = true;
                    }
                }
                #endregion
                #endregion

                if (GameState != OldGameState)
                {
                    TrainerCount++;
                    for (int i = 0; i < 9; i++)
                    {
                        Input[TrainerCount][i] = OldGameState[i];
                    }
                    DesiredOutput[TrainerCount][0] = MoveMemory;
                    OldGameState = GameState;
                }
            }
            #endregion

            if (Stage == 4)
            {
                double[] Output = new double[1];
                double   Error = 0.0, ErrorResult = 1.0, ErrorSum = 1.0, ErrorAverage = 0.0, PreviousErrorAverage = 0.0;
                int      Unstuck = 0;
                while (ErrorResult > 0.001)
                {
                    Error = 0.0;
                    for (int i = 0; i < TrainerCount; i++)
                    {
                        Error += bp.Train(ref Input[i], ref DesiredOutput[i], 0.15, 0.1);

                        ErrorResult = Error;
                        ErrorSum   += Error;


                        if ((PreviousErrorAverage / ErrorSum) < 1.001)  // Only run if error is increasing on average
                        {
                            bp.Stuck(0.1);
                            Unstuck++;
                        }
                        ErrorSum             = 0.0;
                        PreviousErrorAverage = ErrorAverage;
                    }
                }
                Stage = 0;
            }

            #region Fight
            if (Stage == 2)
            {
                double[] SingleInput = new double[9];
                for (int i = 0; i < 9; i++)
                {
                    SingleInput[i] = OldGameState[i];
                }

                double[] Output = new double[1];
                bp.Run(ref SingleInput, out Output);
                #region BoardControl
                if (GameOver)
                {
                    if (Tie)
                    {
                        args.DrawingSession.DrawText("You both win!!!", (int)((Canvas1.ActualWidth / 2) - 40), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                    }
                    else if (Turn)
                    {
                        args.DrawingSession.DrawText("O Won", (int)((Canvas1.ActualWidth / 2) - 30), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                    }
                    else if (!Turn)
                    {
                        args.DrawingSession.DrawText("X Won", (int)((Canvas1.ActualWidth / 2) - 30), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                    }
                }
                else if (Turn)
                {
                    args.DrawingSession.DrawText("X's Turn", (int)((Canvas1.ActualWidth / 2) - 30), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                }
                else if (!Turn)
                {
                    args.DrawingSession.DrawText("O's Turn", (int)((Canvas1.ActualWidth / 2) - 30), (int)((Canvas1.ActualHeight / 2) - 50 - (50 * 4)), Colors.LightGreen);
                }

                args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 50), (int)((Canvas1.ActualHeight / 2) - 150), (int)((Canvas1.ActualWidth / 2) - 50), (int)((Canvas1.ActualHeight / 2) + 150), Colors.Black);
                args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) + 50), (int)((Canvas1.ActualHeight / 2) - 150), (int)((Canvas1.ActualWidth / 2) + 50), (int)((Canvas1.ActualHeight / 2) + 150), Colors.Black);
                args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 150), (int)((Canvas1.ActualHeight / 2) - 50), (int)((Canvas1.ActualWidth / 2) + 150), (int)((Canvas1.ActualHeight / 2) - 50), Colors.Black);
                args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 150), (int)((Canvas1.ActualHeight / 2) + 50), (int)((Canvas1.ActualWidth / 2) + 150), (int)((Canvas1.ActualHeight / 2) + 50), Colors.Black);

                Rect[] GameRectangle = new Rect[9];
                for (int i = 0; i < 9; i++)
                {
                    GameRectangle[i] = new Rect((int)((Canvas1.ActualWidth / 2) - 150 + (100 * (i % 3))), (int)((Canvas1.ActualHeight / 2) - 150 + (100 * (Math.Floor((double)(i / 3))))), 100, 100);
                    if (!GameOver)
                    {
                        if (MouseDown[0] && GameRectangle[i].Contains(MouseCords) && GameState[i] == 0 && Turn)
                        {
                            OldGameState[i] = GameState[i];
                            GameState[i]    = 1;
                            Turn            = !Turn;
                        }
                        else if (!Turn)
                        {
                            if (Output[0] > -0.5 && Output[0] < 8.5)
                            {
                                GameState[Convert.ToInt32(Output[0])] = 2;
                            }
                            Turn         = !Turn;
                            MouseDown[0] = false;
                            args.DrawingSession.DrawText(Output[0] + "", 10, 10, Colors.LightGreen);
                        }
                    }

                    if (GameState[i] == 1)
                    {
                        args.DrawingSession.DrawLine((int)(GameRectangle[i].X + 5), (int)(GameRectangle[i].Y + 5), (int)(GameRectangle[i].X + GameRectangle[i].Width - 5), (int)(GameRectangle[i].Y + GameRectangle[i].Height - 5), Colors.Black);
                        args.DrawingSession.DrawLine((int)(GameRectangle[i].X + 5), (int)(GameRectangle[i].Y + GameRectangle[i].Height - 5), (int)(GameRectangle[i].X + GameRectangle[i].Width - 5), (int)(GameRectangle[i].Y + 5), Colors.Black);
                    }
                    if (GameState[i] == 2)
                    {
                        args.DrawingSession.DrawEllipse((int)(GameRectangle[i].X + 50), (int)(GameRectangle[i].Y + 50), 45, 45, Colors.Black);
                    }
                }

                if (GameOver)
                {
                    args.DrawingSession.DrawText("Rematch", (int)((Canvas1.ActualWidth / 2) - 20), (int)((Canvas1.ActualHeight / 2) - 50 + (50 * 4)), Colors.LightGreen);
                    args.DrawingSession.DrawText("Go Home", (int)((Canvas1.ActualWidth / 2) - 20), (int)((Canvas1.ActualHeight / 2) - 50 + (50 * 5)), Colors.LightGreen);
                    Rect[] MenuRectangle = new Rect[2];
                    for (int i = 0; i < 2; i++)
                    {
                        MenuRectangle[i] = new Rect((int)((Canvas1.ActualWidth / 2) - 23), (int)((Canvas1.ActualHeight / 2) - 47 + (50 * (i + 4))), 90, 25);
                        if (!MenuRectangle[i].Contains(MouseCords))
                        {
                            args.DrawingSession.DrawRectangle(MenuRectangle[i], Colors.Black);
                        }
                        else
                        {
                            args.DrawingSession.DrawRectangle(MenuRectangle[i], Colors.Gold);
                        }
                    }
                    if (MouseDown[0] && MenuRectangle[0].Contains(MouseCords))
                    {
                        for (int i = 0; i < 9; i++)
                        {
                            GameState[i] = 0;
                        }
                        GameOver = false;
                    }
                    else if (MouseDown[0] && MenuRectangle[1].Contains(MouseCords))
                    {
                        for (int i = 0; i < 9; i++)
                        {
                            GameState[i] = 0;
                        }
                        GameOver = false;
                        Stage    = 0;
                    }
                }

                #region GameCheck
                for (int i = 0; i < 3; i++)
                {
                    if (GameState[i] != 0 && GameState[i] == GameState[i + 3] && GameState[i] == GameState[i + 6])
                    {
                        GameOver = true;
                        args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 100 + (100 * (i % 3))), (int)((Canvas1.ActualHeight / 2) - 150), (int)((Canvas1.ActualWidth / 2) - 100 + (100 * (i % 3))), (int)((Canvas1.ActualHeight / 2) + 150), Colors.Red);
                    }
                    else if (GameState[i * 3] != 0 && GameState[i * 3] == GameState[(i * 3) + 1] && GameState[i * 3] == GameState[(i * 3) + 2])
                    {
                        GameOver = true;
                        args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - 150), (int)((Canvas1.ActualHeight / 2) - 100 + (100 * (i % 3))), (int)((Canvas1.ActualWidth / 2) + 150), (int)((Canvas1.ActualHeight / 2) - 100 + (100 * (i % 3))), Colors.Red);
                    }
                    else if (GameState[i * 2] != 0 && i != 2 && GameState[i * 2] == GameState[4] && GameState[i * 2] == GameState[8 - (i * 2)])
                    {
                        GameOver = true;
                        args.DrawingSession.DrawLine((int)((Canvas1.ActualWidth / 2) - (150 + (-300 * i))), (int)((Canvas1.ActualHeight / 2) - 150), (int)((Canvas1.ActualWidth / 2) + (150 + (-300 * i))), (int)((Canvas1.ActualHeight / 2) + 150), Colors.Red);
                    }
                    else if (!GameState.Contains(0))
                    {
                        GameOver = true;
                    }
                }
                #endregion
                #endregion
            }
            #endregion

            /*
             * double[][] Input = new double[4][];
             * double[][] DesiredOutput = new double[4][];
             * for (int i = 0; i < 4; i++)
             * {
             *  Input[i] = new double[2];
             *  DesiredOutput[i] = new double[1];
             * }
             * Input[0][0] = 0.0; Input[0][1] = 0.0; DesiredOutput[0][0] = 0.0;
             * Input[1][0] = 1.0; Input[1][1] = 0.0; DesiredOutput[1][0] = 1.0;
             * Input[2][0] = 0.0; Input[2][1] = 1.0; DesiredOutput[2][0] = 1.0;
             * Input[3][0] = 1.0; Input[3][1] = 1.0; DesiredOutput[3][0] = 2.0;
             *
             *
             * double[] Output = new double[1];
             * double Error = 0.0, ErrorResult = 1.0, ErrorSum = 1.0, ErrorAverage = 0.0, PreviousErrorAverage = 0.0;
             * int Count = 0, Unstuck = 0;
             * while (ErrorResult > 0.001)
             * {
             *  Count++;
             *  Error = 0.0;
             *  for (int j = 0; j < 4; j++)
             *  {
             *      Error += bp.Train(ref Input[j], ref DesiredOutput[j], 0.15, 0.1);
             *      // bp.Run(ref Input[j], out Output);
             *  }
             *  ErrorResult = Error;
             *  ErrorSum += Error;
             *  if (Count % 1000 == 0)
             *  {
             *      //Console.WriteLine("Trial:" + Count + " Error:" + Math.Round(Error, 3));
             *  }
             *  //Compareing average error change to detect being stuck
             *  if (Count % 100 == 0)
             *  {
             *      ErrorAverage = ErrorSum / 100;
             *      ErrorSum = 0.0;
             *      //  Console.WriteLine("ErrorChange: " + PreviousErrorAverage / ErrorAverage);
             *
             *      if (Count != 100 && (PreviousErrorAverage / ErrorAverage) < 1.001) // Only run if error is increasing on average
             *      {
             *          bp.Stuck(0.1);
             *          Unstuck++;
             *
             *      }
             *      PreviousErrorAverage = ErrorAverage;
             *  }
             *
             * }
             * //Console.WriteLine("Finished on Trial:" + Count + " Error:" + Math.Round(Error, 3) + " TimesStuck:" + Unstuck);
             *
             * for (int i = 0; i < 4; i++)
             * {
             *  bp.Run(ref Input[i], out Output);
             *  //  Console.WriteLine(Input[i][0] + " + " + Input[i][1] + " = " + Output[0]);
             * }
             *
             *
             * // Console.ReadKey();
             */
            Canvas1.Invalidate();
        }
コード例 #2
0
 private void IconListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     Canvas1.Invalidate();
 }
コード例 #3
0
        private void Canvas1_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args)
        {
            CanvasDrawingSession ds = args.DrawingSession;

            if (!ListBoxItem1.IsSelected)
            {
                //Drawing 1

                //Sun
                ds.FillEllipse(0, 0, 150, 100, Colors.Yellow);
                //Grass
                ds.FillRectangle(new Rect(0, 700, 5000, 200), Colors.Green);

                //Tree
                ds.FillRectangle(new Rect(200, 325, 80, 400), Colors.SaddleBrown);
                ds.FillEllipse(240, 325, 100, 100, Colors.Green);
                ds.FillEllipse(200, 350, 100, 100, Colors.Green);
                ds.FillEllipse(260, 350, 100, 100, Colors.Green);

                //House
                ds.FillRectangle(new Rect(800, 475, 350, 300), Colors.DarkRed);
                ds.DrawLine(new Vector2(800, 475), new Vector2(1150, 775), Colors.White, 10);
                ds.DrawLine(new Vector2(800, 775), new Vector2(1150, 475), Colors.White, 10);

                Vector2[]         roof        = { new Vector2(800, 475), new Vector2(975, 375), new Vector2(1150, 475) };
                CanvasPathBuilder pathBuilder = new CanvasPathBuilder(Canvas1);
                pathBuilder.AddGeometry(CanvasGeometry.CreatePolygon(Canvas1, roof));
                ds.FillGeometry(CanvasGeometry.CreatePath(pathBuilder), Colors.SaddleBrown);
            }
            else
            {
                Color[] rainbow = { Colors.Red, Colors.Orange, Colors.Yellow, Colors.Green, Colors.Blue, Colors.Indigo, Colors.Violet };

                //Drawing 2
                Vector2 startingPoint = new Vector2((float)(ActualWidth / 2), (float)(ActualHeight / 2));

                int angleIncrement = 3;

                int sideLength = 60;
                int angle      = 0;

                hexagonsDrawn++;

                if (hexagonsDrawn == 1000)
                {
                    //Draw hexagons
                    for (int i = 0; i < hexagonsDrawn; i++)
                    {
                        for (int j = 0; j < 6; j++)
                        {
                            startingPoint = DrawLine(ds, startingPoint, angle, sideLength, rainbow[i % rainbow.Length]);
                            angle        += 60;
                        }

                        angle += angleIncrement;
                        sideLength++;
                    }

                    hexagonsDrawn = 0;
                }
                else
                {
                    //Draw hexagons
                    for (int i = 0; i < hexagonsDrawn; i++)
                    {
                        for (int j = 0; j < 6; j++)
                        {
                            startingPoint = DrawLine(ds, startingPoint, angle, sideLength, rainbow[i % rainbow.Length]);
                            angle        += 60;
                        }

                        angle += angleIncrement;
                        sideLength++;
                    }

                    Task.Delay(10).Wait();
                    Canvas1.Invalidate();
                }
            }
        }
コード例 #4
0
        private void Canvas1_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args)
        {
            CanvasDrawingSession ds = args.DrawingSession;

            if (gameOver)
            {
                ds.DrawText("Game Over, You Win!!!", (float)Canvas1.ActualWidth / 2, (float)Canvas1.ActualHeight / 2, Colors.Green);
                ds.DrawText("Restarting in " + restartCountdown / 60 + "s.", (float)Canvas1.ActualWidth / 2, (float)(Canvas1.ActualHeight / 2) + 50, Colors.Lime);

                if (restartCountdown <= 0)
                {
                    //Starting positions
                    mainCharacter.x = 40;
                    mainCharacter.y = Canvas1.ActualHeight - 115;

                    mainCharacter.box = new Rect(mainCharacter.x, mainCharacter.y, 40, 40);
                    gameOver          = false;
                    restartCountdown  = 600;
                }
                else
                {
                    restartCountdown--;
                }

                Canvas1.Invalidate();
                return;
            }

            if (mainCharacter == null)
            {
                mainCharacter = new GameCharacter(Canvas1);
            }
            if (mainCharacter.y > Canvas1.ActualHeight)
            {
                mainCharacter.y = 0;
            }
            if (mainCharacter.x > Canvas1.ActualWidth)
            {
                mainCharacter.x = 0;
            }
            else if (mainCharacter.x < 0)
            {
                mainCharacter.x = Canvas1.ActualWidth - mainCharacter.box.Width;
            }

            //true when the box intersects with a single box.
            mainCharacter.onGround = !shapes.Keys.ToList().TrueForAll(rect => !checkIntersect(mainCharacter.box, rect));

            //Handle character horizontal acceleration
            if (Window.Current.CoreWindow.GetKeyState(VirtualKey.D).HasFlag(CoreVirtualKeyStates.Down))
            {
                if (mainCharacter.hSpeed < 10)
                {
                    mainCharacter.hSpeed++;
                }
            }
            else if (Window.Current.CoreWindow.GetKeyState(VirtualKey.A).HasFlag(CoreVirtualKeyStates.Down))
            {
                if (mainCharacter.hSpeed > -10)
                {
                    mainCharacter.hSpeed--;
                }
            }
            else
            {
                if (mainCharacter.hSpeed > 0)
                {
                    mainCharacter.hSpeed--;
                }
                else if (mainCharacter.hSpeed < 0)
                {
                    mainCharacter.hSpeed++;
                }
            }

            if (!mainCharacter.onGround)
            {
                mainCharacter.box.Y -= mainCharacter.vSpeed;

                foreach (Rect rect in shapes.Keys)
                {
                    if (checkIntersect(mainCharacter.box, rect))
                    {
                        mainCharacter.y        = rect.Y - mainCharacter.box.Height;
                        mainCharacter.onGround = true;
                    }
                }

                if (!mainCharacter.onGround)
                {
                    mainCharacter.y -= mainCharacter.vSpeed;
                    mainCharacter.vSpeed--;
                }
                else
                {
                    mainCharacter.vSpeed = -2;
                }
            }
            else
            {
                mainCharacter.box.Y -= mainCharacter.vSpeed;

                foreach (Rect rect in shapes.Keys)
                {
                    if (checkIntersect(mainCharacter.box, rect) && mainCharacter.y - mainCharacter.box.Height > rect.Y)
                    {
                        mainCharacter.y = rect.Y - mainCharacter.box.Height;
                    }
                }
            }

            //Character Movement adjustments
            mainCharacter.x += mainCharacter.hSpeed;

            //Draw Rectangles
            foreach (KeyValuePair <Rect, Color> item in shapes)
            {
                ds.FillRectangle(item.Key, item.Value);
            }

            mainCharacter.draw(ds);

            //Check win condition
            if (mainCharacter.y < 300 && mainCharacter.x > 1000 && mainCharacter.x < 1140)
            {
                gameOver = true;
            }

            Canvas1.Invalidate();
        }