private void canvas_CreateResources(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args) //Yet more boilerplate code, this runs before canvas_Draw and is meant to be used to load any assets that need to be loaded from files { Board = LoadAsset(sender, "Chess Board"); //These are all basically identical; they set the value of each variable to whatever LoadAsset returns given the file name. PawnBlack = LoadAsset(sender, "PawnBlack"); PawnWhite = LoadAsset(sender, "PawnWhite"); KnightBlack = LoadAsset(sender, "KnightBlack"); KnightWhite = LoadAsset(sender, "KnightWhite"); BishopBlack = LoadAsset(sender, "BishopBlack"); BishopWhite = LoadAsset(sender, "BishopWhite"); RookBlack = LoadAsset(sender, "RookBlack"); RookWhite = LoadAsset(sender, "RookWhite"); QueenBlack = LoadAsset(sender, "QueenBlack"); QueenWhite = LoadAsset(sender, "QueenWhite"); KingBlack = LoadAsset(sender, "KingBlack"); KingWhite = LoadAsset(sender, "KingWhite"); PlaceholderBlack = CanvasSvgDocument.LoadFromXml(sender, PawnBlack.GetXml().Replace("opacity=\"1\"", "opacity=\"0.5\"")); PlaceholderWhite = CanvasSvgDocument.LoadFromXml(sender, PawnWhite.GetXml().Replace("opacity=\"1\"", "opacity=\"0.5\"")); translator = new RenderTranslator(PawnBlack, PawnWhite, KnightBlack, KnightWhite, BishopBlack, BishopWhite, RookBlack, RookWhite, QueenBlack, QueenWhite, KingBlack, KingWhite, PlaceholderBlack, PlaceholderWhite); pieceSize = new Size(sender.Size.Width / 16, sender.Size.Height / 16); pawn = new Pawn(new Position(3, 3), true); MoveHighlight = new Color(); MoveHighlight.A = 100; MoveHighlight.R = 255; MoveHighlight.G = 255; MoveHighlight.B = 255; }
public static void Move(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { for (int j = 0; j < MainPage.enemyXPOS.Count; ++j) { if (MainPage.enemySHIP[j] == 1) { MainPage.SHIP_IMG = MainPage.Enemy1; } if (MainPage.enemySHIP[j] == 2) { MainPage.SHIP_IMG = MainPage.Enemy2; } if (MainPage.enemyDIR[j] == "left") { MainPage.enemyXPOS[j] -= 3; } else { MainPage.enemyXPOS[j] += 3; } MainPage.enemyYPOS[j] += 3; args.DrawingSession.DrawImage(Scaling.img(MainPage.SHIP_IMG), MainPage.enemyXPOS[j], MainPage.enemyYPOS[j]); } }
private void CanvasControl_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { GaussianBlurEffect gbe = new GaussianBlurEffect() { Source = cb, BlurAmount = 10 }; Rect rect = new Rect(50, 50, Width, Height); args.DrawingSession.DrawEllipse(500, 500, 80, 56, Colors.Red); args.DrawingSession.DrawImage(offscreen, 23, 34); }
private void Canv_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { args.DrawingSession.Units = CanvasUnits.Pixels; var size = cbi.Size; Rect rect = new Rect(0, 0, size.Width * 1.5, size.Height * 1.5); if (isBlurred) { args.DrawingSession.DrawImage(blur); } }
private void CanFaceRect_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { if (fRectangles != null) { if (fRectangles.Length > 0) { foreach (var faceRect in fRectangles) { args.DrawingSession.DrawRectangle(faceRect.Left, faceRect.Top, faceRect.Width, faceRect.Height, Colors.Red); } } } }
private void BackCanvasControl_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { var gaussionBlurEffect = new GaussianBlurEffect() { BlurAmount = 20, BorderMode = EffectBorderMode.Hard, Source = _coverCanvasBitmap }; args.DrawingSession.DrawImage(gaussionBlurEffect); // scaling var canvasViusal = ElementCompositionPreview.GetElementVisual(this.backCanvasControl); float d = (float)_coverCanvasBitmap.Size.Width; float w = (float)this.scrollViewer.ActualWidth; float scale = w / d; canvasViusal.Scale = new System.Numerics.Vector3(scale); }
private async void canvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { if (GameBoard != null) { await RenderService.AwaitResources(); RenderService.SharedBoardControlState = new BoardControlState(Rectangle.GetBoundingRectangle(GameBoard)); RenderService.ShowCoordinates = false; RenderService.SimpleRenderService = true; RenderService.Draw(sender, sender.Size.Width, sender.Size.Height, args.DrawingSession, new GameTreeNode() { BoardState = GameBoard }); } }
private void canvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) //This is where I put the code that actually draws anything on screen. { // The actual renderer (currently Win2D) args.DrawingSession.DrawSvg(Board, sender.Size); //It works a lot like processing actually, but it's in C# and hardware accelerated (it runs on the GPU not the CPU), which is ideal and why I chose it foreach (Tuple <Position, byte> piece in game.RenderInterface()) //Iterate over each of the Tuples returned by game.RenderInterface() { args.DrawingSession.DrawSvg(translator.map[piece.Item2], pieceSize, (float)(piece.Item1.Column * pieceSize.Width), (float)(piece.Item1.Row * pieceSize.Height)); //The function call Have the translator find the svg The size Calculate on-screen location based on board position by multiplying board positions by the width of each tile } foreach (Position move in game.Moves) { args.DrawingSession.DrawCircle((float)(move.Column * pieceSize.Width + pieceSize.Width / 2), (float)(move.Row * pieceSize.Height + pieceSize.Height / 2), (float)(pieceSize.Height / 4), MoveHighlight); } }
private void _view_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { var layoutChange = ArrangeChildenLayout(false); if (layoutChange) { ReRebulidDataResources(); } using (CanvasCommandList cl = new CanvasCommandList(sender)) { using (CanvasDrawingSession cds = cl.CreateDrawingSession()) { OnDraw(cds); } args.DrawingSession.DrawImage(cl); } }
private void mySurface_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { cds = args.DrawingSession; int j = 1; if (block.list.Count > 5000) { for (int i = block.list.Count - 1000; i < block.list.Count - 1; i++) { cds.DrawLine(j - 1, block.list[i].deg / 2, j, block.list[i + 1].deg / 2, Windows.UI.Colors.White); cds.DrawLine(j - 1, block.list[i].sin * 50 + 75, j, block.list[i + 1].sin * 50 + 75, Windows.UI.Colors.Red); cds.DrawLine(j - 1, block.list[i].cos * 50 + 75, j, block.list[i + 1].cos * 50 + 75, Windows.UI.Colors.Blue); j++; } } //temp +=1; }
private void viewPane_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { //CanvasDrawingSession drawSurface = args.DrawingSession; GlobalNodeHandler.viewNode.DrawRepresentationAt(args.DrawingSession, 10 + (int)LeftStackP.Width, 10 + (int)TopStackP.Height); GlobalNodeHandler.viewNode.drawView(sender, args.DrawingSession); if (mouse.dragRec) { args.DrawingSession.DrawRectangle(mouse.selectionRec, Colors.Blue, 5); } GlobalNodeHandler.pick1.draw(args.DrawingSession); //drawSurface.DrawText(status, 10, 400, Colors.White); //args.DrawingSession.DrawText(status2, 800, 0, Colors.Black); }
private void Canv_CreateResources(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args) { args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction()); }
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(); }
private void Canv_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { args.DrawingSession.Units = CanvasUnits.Pixels; args.DrawingSession.DrawImage(canvasEffect); }
/// <summary> /// Graphical Logic, creating visuals in responses to events, and incorporating edge collision logic /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void GameCanvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { GSM.gsm(); //Call GSM on first draw args.DrawingSession.DrawImage(Scaling.Img(BG)); //Generate Background upon method call if (gameState == 1) //If we are playing the game { CanvasTextLayout textScore = new CanvasTextLayout(args.DrawingSession, myScore.ToString(), new CanvasTextFormat() { FontFamily = "", FontSize = (40 * scaledHeight), WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); CanvasTextLayout textLevel = new CanvasTextLayout(args.DrawingSession, myLevel.ToString(), new CanvasTextFormat() { FontFamily = "Times New Roman", FontSize = (50 * scaledHeight), WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); CanvasTextLayout textLevelUp = new CanvasTextLayout(args.DrawingSession, "Level " + myLevel.ToString() + " Speed Increasing!", new CanvasTextFormat() { FontFamily = "Times New Roman", FontSize = 60 * scaledHeight, WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); args.DrawingSession.DrawTextLayout(textScore, (140 * scaledWidth), (50 * scaledHeight), Color.FromArgb(255, 255, 255, 255)); //Draw score args.DrawingSession.DrawTextLayout(textLevel, (80 * scaledWidth), (45 * scaledHeight), Color.FromArgb(255, 0, 0, 0)); //Draw Level if (levelUp) // Level Up Notifier { for (int i = 0; i < 3; i++) { args.DrawingSession.DrawTextLayout(textLevelUp, centerX - 250, centerY - 100, Color.FromArgb(255, 30, 25, 150));//Draw Level Up Sequence } } if (roundEnded == true) //Results Screen Drawing Events { accuracy = arrowsHit / arrowsFired * 100; CanvasTextLayout textAccuracy = new CanvasTextLayout(args.DrawingSession, "Accuracy: " + accuracy.ToString() + "% ", new CanvasTextFormat() { FontSize = 40 * scaledHeight, WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); CanvasTextLayout textFinalScore = new CanvasTextLayout(args.DrawingSession, "Final Score: " + myScore.ToString(), new CanvasTextFormat() { FontFamily = "", FontSize = 40 * scaledHeight, WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); args.DrawingSession.DrawTextLayout(textFinalScore, (790 * scaledWidth), (645 * scaledHeight), Color.FromArgb(255, 150, 5, 5)); args.DrawingSession.DrawTextLayout(textAccuracy, (790 * scaledWidth), (685 * scaledHeight), Color.FromArgb(255, 150, 5, 5)); } //Draw Death animation first so death animation superimposes the enemy image if ((deathAnimationX > 0) && (deathAnimationY > 0) && (deathAniFrames > 0))//If we have a 'dead enemy' draw death animation { args.DrawingSession.DrawImage(Scaling.Img(blood), deathAnimationX - 80, deathAnimationY - 80); deathAniFrames -= 1; } else//Reset death animaiton variables { deathAniFrames = 60; deathAnimationX = 0; deathAnimationY = 0; } //Drawing Enemies second so projectiles superimpose the enemy images for (int j = 0; j < enemyXPOS.Count; j++) { //If an enemy makes it to the tower GAME OVER if ((centerX >= enemyXPOS[j]) && (centerX <= (enemyXPOS[j] + (100 * scaledWidth))) && (centerY >= enemyYPOS[j]) && (centerY <= (enemyYPOS[j] + (75 * scaledHeight)))) { roundEnded = true; countDown = 0; break; } //Enemy Image Select if (enemyImageLL[j] == 1) { enemyImage = enemy1; } else if (enemyImageLL[j] == 2) { enemyImage = enemy2; } //Enemy Direction Select if (enemyDirection[j] == 1)//Start top, move down { enemyYPOS[j] += 1 + (float)(myLevel * 0.1); } else if (enemyDirection[j] == 2)//Start left, move right { enemyXPOS[j] += 2 + (float)(myLevel * 0.2); } else if (enemyDirection[j] == 3)//Start right, move left { enemyXPOS[j] -= 2 + (float)(myLevel * 0.2); } else if (enemyDirection[j] == 4)//Start down, move up { enemyYPOS[j] -= 1 + (float)(myLevel * 0.1); } args.DrawingSession.DrawImage(Scaling.Img(enemyImage), enemyXPOS[j], enemyYPOS[j]); } //Display arrow for (int i = 0; i < arrowXPOS.Count; i++) { //Linear Interpolation to calculate the projectile path between two known coordinates pointX = (centerX + (arrowXPOS[i] - centerX) * percent[i]); pointY = (centerY + (arrowYPOS[i] - centerY) * percent[i]); args.DrawingSession.DrawImage(Scaling.Img(arrow), pointX - (19 * scaledWidth), pointY - (20 * scaledHeight));//19 and 20 come from half of the width and height of the arrow.jpg percent[i] += (0.1f * scaledHeight); //Every time a projectile is fired check for collision for (int j = 0; j < enemyXPOS.Count; j++)//For each projectile check collision cooridantes of every enemy { //Note: 100 and 95 in the following if statement are relative to the blood spatter dimension image //Compare current projectile x/y coordinate with every enemy x/y position to check for a collision if ((pointX >= enemyXPOS[j]) && (pointX <= (enemyXPOS[j] + (100 * scaledWidth))) && (pointY >= enemyYPOS[j]) && (pointY <= (enemyYPOS[j] + (95 * scaledHeight)))) { //Update death animation locaiton for 'blood splatter' deathAnimationX = pointX - (50 * scaledWidth); deathAnimationY = pointY - (47 * scaledHeight); //Remove the enemy off screen enemyXPOS.RemoveAt(j); enemyYPOS.RemoveAt(j); enemyImageLL.RemoveAt(j); enemyDirection.RemoveAt(j); //Remove the current projectile off screen arrowXPOS.RemoveAt(i); arrowYPOS.RemoveAt(i); percent.RemoveAt(i); myScore += 100; arrowsHit++; break; } } if (pointY < 0f)//If arrow trails off screen, remove the arrow { arrowXPOS.RemoveAt(i); arrowYPOS.RemoveAt(i); percent.RemoveAt(i); } } } GameCanvas.Invalidate(); }
/// <summary> /// This is where all the drawings actually take place. By going through for loops and constantly changing the values /// of x and y of the images we can make it appear that the ships, bullets and aliens are moving. We also use this method /// to show the highscore, current score, lives and levels. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void GameCanvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { GSM.gameLevel(); //intializes the backgrounds args.DrawingSession.DrawImage(Scaling.img(BG)); //draws the backgrounds Alien.createAliens(); //additional things to draw if the game is over if (RoundEnded == true) { if (lives == 0) { Storage.UpdateScore(); } //When a new Highscore is reached if (Storage.update == true) { Storage.ReadFile(); CanvasTextLayout textLayout1 = new CanvasTextLayout(args.DrawingSession, "Score\n" + MyScore.ToString(), new CanvasTextFormat() { FontSize = (35 * scaleHeight), WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); //Positions the current high score after game CanvasTextLayout textLayout2 = new CanvasTextLayout(args.DrawingSession, "High Score\n" + Storage.highScore.ToString(), new CanvasTextFormat() { FontSize = (35 * scaleHeight), WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); //Displays if a new high score is reached CanvasTextLayout textLayout3 = new CanvasTextLayout(args.DrawingSession, "NEW HIGHSCORE!!!!!", new CanvasTextFormat() { FontSize = (50 * scaleHeight), WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); args.DrawingSession.DrawTextLayout(textLayout1, ((DesignWidth * scaleWidth) / 2) - ((float)textLayout1.DrawBounds.Width / 2), 400 * scaleHeight, Colors.White); args.DrawingSession.DrawTextLayout(textLayout2, ((DesignWidth * scaleWidth) / 2) - ((float)textLayout1.DrawBounds.Width / 2), 560 * scaleHeight, Colors.White); args.DrawingSession.DrawTextLayout(textLayout3, ((float)bounds.Width / 2) - 160, 50 * scaleHeight, Colors.Red); } //Every other time else { Storage.ReadFile(); CanvasTextLayout textLayout1 = new CanvasTextLayout(args.DrawingSession, "Score\n" + MyScore.ToString(), new CanvasTextFormat() { FontSize = (35 * scaleHeight), WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); //Positions the current high score after game CanvasTextLayout textLayout2 = new CanvasTextLayout(args.DrawingSession, "High Score\n" + Storage.highScore.ToString(), new CanvasTextFormat() { FontSize = (35 * scaleHeight), WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); args.DrawingSession.DrawTextLayout(textLayout1, ((DesignWidth * scaleWidth) / 2) - ((float)textLayout1.DrawBounds.Width / 2), 400 * scaleHeight, Colors.White); args.DrawingSession.DrawTextLayout(textLayout2, ((DesignWidth * scaleWidth) / 2) - ((float)textLayout1.DrawBounds.Width / 2), 560 * scaleHeight, Colors.White); } } else { if (GameState > 1) { //Positions the level number during game args.DrawingSession.DrawText("Level: " + GSM.level.ToString(), (float)bounds.Width / 2 - 185, (float)bounds.Height - 35, Color.FromArgb(255, 255, 255, 255)); // Positions the score board during game args.DrawingSession.DrawText("Score: " + MyScore.ToString(), (float)bounds.Width / 2 - 40, (float)bounds.Height - 35, Color.FromArgb(255, 255, 255, 255)); // Positions the highscore board during game args.DrawingSession.DrawText("High Score: " + Storage.STRHighScore, (float)bounds.Width / 2 - 430, (float)bounds.Height - 35, Color.FromArgb(255, 255, 255, 255)); myShip.MoveShip(); //Displaying life count args.DrawingSession.DrawText("Lives: ", (float)bounds.Width / 2 + 150, (float)bounds.Height - 35, Color.FromArgb(255, 255, 255, 255)); for (int i = 0; i < lives; i++) { args.DrawingSession.DrawImage(Scaling.img(Heart), (float)bounds.Width / 2 + (210 + (50 * i)), (float)bounds.Height - 40); } //displays the explosion of ship and alien or bullet and alien if (boomX > 0 && boomY > 0 && boomCount > 0) { args.DrawingSession.DrawImage(Scaling.img(Boom), boomX, boomY); boomCount--; } //otherwise resets coordinates else { boomCount = 60; boomX = 0; boomY = 0; } //moving alien fleet if (alienList.Count > 0) { alienList[0].MoveFleet(); } //Enemies for (int j = 0; j < alienList.Count; j++) { if (alienList[j].AlienType == 1) { ALIEN_IMG = Enemy1; } if (alienList[j].AlienType == 2) { ALIEN_IMG = Enemy2; } //moving aliens and drawing alienList[j].MoveAlien(); args.DrawingSession.DrawImage(Scaling.img(ALIEN_IMG), alienList[j].AlienXPOS, alienList[j].AlienYPOS); //alien projectiles for (int a = 0; a < alienList[j].getShootX().Count; a++) { args.DrawingSession.DrawImage(Scaling.img(AlienLaser), alienList[j].getShootX()[a], alienList[j].getShootY()[a]); //alien projectile collision if (alienList[j].getShootX()[a] - (25 * scaleWidth) >= myShip.ShipXPOS && alienList[j].getShootX()[a] <= myShip.ShipXPOS + (110 * scaleWidth) && alienList[j].getShootY()[a] + (60 * scaleHeight) >= myShip.ShipYPOS && alienList[j].getShootY()[a] <= myShip.ShipYPOS + (110 * scaleHeight)) { boomX = myShip.ShipXPOS; boomY = myShip.ShipYPOS; alienList[j].removeShoot(a); lives--; if (lives == 0) { RoundEnded = true; } } } } //Display Projectiles for (int i = 0; i < myShip.getBulletX().Count; i++) { //Beam.png needs no dimension scaling args.DrawingSession.DrawImage(Scaling.img(Photon), myShip.getBulletX()[i], myShip.getBulletY()[i]); for (int h = 0; h < alienList.Count; h++) { //70 and 77 are dimensions from boom.png if (myShip.getBulletX()[i] >= alienList[h].AlienXPOS && myShip.getBulletX()[i] <= alienList[h].AlienXPOS + (70 * scaleWidth) && myShip.getBulletY()[i] >= alienList[h].AlienYPOS && myShip.getBulletY()[i] <= alienList[h].AlienYPOS + (77 * scaleHeight)) { //50 is half of boom.png width 100 and 91 is also from boom.png boomX = alienList[h].AlienXPOS; boomY = alienList[h].AlienYPOS; MyScore = MyScore + alienList[h].AlienScore; //increases score based on the alien type destroyed liveScore = liveScore + alienList[h].AlienScore; //keeps track of score for gaining lives alienList.RemoveAt(h); myShip.removeBullet(i); //If not the first time receiving a bonus life then life is incremented every 1300000 points if (liveScore >= 65000 && firstBonus == false && lives < 6) { lives++; liveScore -= 65000; } if (liveScore >= 32000 && firstBonus == true) { lives++; firstBonus = false; } if (alienList.Count == 0) { RoundEnded = true; } break; } } } //Ship/alien collision and decremention of life. Ends game when lives get to zero for (int i = 0; i < alienList.Count; i++) { if (alienList[i].AlienXPOS + (70 * scaleWidth) >= myShip.ShipXPOS && alienList[i].AlienXPOS <= myShip.ShipXPOS + (110 * scaleWidth) && alienList[i].AlienYPOS + (77 * scaleHeight) >= myShip.ShipYPOS && alienList[i].AlienYPOS <= myShip.ShipYPOS + (110 * scaleHeight)) { boomX = myShip.ShipXPOS; boomY = myShip.ShipYPOS; alienList.RemoveAt(i); lives--; if (alienList.Count == 0) { RoundEnded = true; } else if (lives == 0) { RoundEnded = true; } } } //Draws ship args.DrawingSession.DrawImage(Scaling.img(MyShip), myShip.ShipXPOS, myShip.ShipYPOS); } } //Redraws everything GameCanvas.Invalidate(); }
public static void laser(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { for (int i = 0; i < MainPage.photonXPOS.Count; ++i) { pointX = (photonX + (MainPage.photonXPOS[i] - photonX) * percent[i]); pointY = (photonY + (MainPage.photonYPOS[i] - photonY) * percent[i]); args.DrawingSession.DrawImage(Scaling.img(photon), pointX - (145 * scaleWidth), pointY - (145 * scaleHeight)); percent[i] += (0.050f * scaleHeight); //Enemies foreach (Enemy Mob in MainPage.Enemies) { Mob.Move(); } for (int h = 0; h < MainPage.enemyXPOS.Count; ++h) { if (pointX >= MainPage.enemyXPOS[h] && pointX <= MainPage.enemyXPOS[h] + (302 * scaleWidth) && pointY >= MainPage.enemyYPOS[h] && pointY <= MainPage.enemyYPOS[h] + (167 * scaleHeight)) { boomX = pointX - (151 * scaleWidth); boomY = pointY - (83 * scaleHeight); MainPage.enemyXPOS.RemoveAt(h); MainPage.enemyYPOS.RemoveAt(h); MainPage.enemySHIP.RemoveAt(h); MainPage.enemyDIR.RemoveAt(h); MainPage.photonXPOS.RemoveAt(i); MainPage.photonYPOS.RemoveAt(i); MainPage.percent.RemoveAt(i); Myscore = Myscore + 100; break; } } if (pointY < 0f) { MainPage.photonXPOS.RemoveAt(i); MainPage.photonYPOS.RemoveAt(i); MainPage.percent.RemoveAt(i); } } //for (int i = 0; i<MainPage.photonXPOS.Count; ++i) // { // pointX = (photonX + (MainPage.photonXPOS[i] - photonX) * percent[i]); // pointY = (photonY + (MainPage.photonYPOS[i] - photonY) * percent[i]); // } // for (int h = 0; h < MainPage.enemyXPOS.Count; ++h) // { // if (pointX >= MainPage.enemyXPOS[h] && pointX <= MainPage.enemyXPOS[h] + (302 * scaleWidth) && pointY >= enemyYPOS[h] && pointY <= enemyYPOS[h] + (167 * scaleHeight)) // { // boomX = pointX - (151 * scaleWidth); // boomY = pointY - (83 * scaleHeight); // MainPage.enemyXPOS.RemoveAt(h); // MainPage.enemyYPOS.RemoveAt(h); // MainPage.enemySHIP.RemoveAt(h); // MainPage.enemyDIR.RemoveAt(h); // MainPage.photonXPOS.RemoveAt(i); // MainPage.photonYPOS.RemoveAt(i); // MainPage.percent.RemoveAt(i); // Myscore = Myscore + 100; // break; // } // } }
private void GameCanvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { gsm.GSM(); args.DrawingSession.DrawImage(Scaling.img(BG)); args.DrawingSession.DrawText(countdown.ToString(), 100, 100, Windows.UI.Colors.Yellow); if (RoundEnded == true) { if (Myscore > Convert.ToInt16(STRHighScore)) { Storage.UpdateScore(); } CanvasTextLayout textLayout1 = new CanvasTextLayout(args.DrawingSession, Myscore.ToString(), new CanvasTextFormat() { FontSize = (120 * scaleHeight), WordWrapping = CanvasWordWrapping.NoWrap }, 0.0f, 0.0f); args.DrawingSession.DrawTextLayout(textLayout1, ((DesighWidth * scaleWidth) / 2) - ((float)textLayout1.DrawBounds.Width / 2), 350 * scaleHeight, Windows.UI.Colors.White); args.DrawingSession.DrawText("HighScore: " + Convert.ToInt16(STRHighScore), new Vector2(200, 200), Windows.UI.Color.FromArgb(255, 255, 255, 255)); } else { if (GameState > 0) { args.DrawingSession.DrawText("Score: " + Myscore.ToString(), (float)bounds.Width / 2, 10, Windows.UI.Color.FromArgb(255, 255, 255, 255)); if (boomX > 0 && boomY > 0 && boomCount > 0) { args.DrawingSession.DrawImage(Scaling.img(Boom), boomX, boomY); boomCount -= 1; } else { boomCount = 60; boomX = 0; boomY = 0; } //enemy for (int j = 0; j < enemyXPOS.Count; ++j) { if (enemySHIP[j] == 1) { SHIP_IMG = Enemy1; } if (enemySHIP[j] == 2) { SHIP_IMG = Enemy2; } if (enemyDIR[j] == "left") { enemyXPOS[j] -= 3; } else { enemyXPOS[j] += 3; } enemyYPOS[j] += 3; args.DrawingSession.DrawImage(Scaling.img(SHIP_IMG), enemyXPOS[j], enemyYPOS[j]); } // display foreach (Player1 ship in Players) { ship.laser(); } for (int i = 0; i < photonXPOS.Count; ++i) { pointX = (photonX + (photonXPOS[i] - photonX) * percent[i]); pointY = (photonY + (photonYPOS[i] - photonY) * percent[i]); args.DrawingSession.DrawImage(Scaling.img(photon), pointX - (145 * scaleWidth), pointY - (145 * scaleHeight)); percent[i] += (0.050f * scaleHeight); // //Enemies // foreach (Enemy Mob in Enemies) // { // Mob.Move(); // } for (int h = 0; h < enemyXPOS.Count; ++h) { if (pointX >= enemyXPOS[h] && pointX <= enemyXPOS[h] + (302 * scaleWidth) && pointY >= enemyYPOS[h] && pointY <= enemyYPOS[h] + (167 * scaleHeight)) { boomX = pointX - (151 * scaleWidth); boomY = pointY - (83 * scaleHeight); enemyXPOS.RemoveAt(h); enemyYPOS.RemoveAt(h); enemySHIP.RemoveAt(h); enemyDIR.RemoveAt(h); photonXPOS.RemoveAt(i); photonYPOS.RemoveAt(i); percent.RemoveAt(i); Myscore = Myscore + 100; break; } } if (pointY < 0f) { photonXPOS.RemoveAt(i); photonYPOS.RemoveAt(i); percent.RemoveAt(i); } } args.DrawingSession.DrawImage(Scaling.img(Myship), (float)bounds.Width / 2 - (150 * scaleWidth), (float)bounds.Height - (130 * scaleHeight)); } } GameCanvas.Invalidate(); }
// Handles all drawing of the game and its resources private void GameCanvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { // Load initial Game State GSM(); // Draw the start screen args.DrawingSession.DrawImage(Scaling.ScaleImage(BG)); args.DrawingSession.DrawText(countdown.ToString(), 100, 100, Colors.Yellow); // Check if the round has ended, otherwise contine to draw level if (RoundEnded == true) { // Set the parametres for the final score to be drawn // Use a textLayout as opposed to just hard coding it in // Used this as a reference to help with font scaling https://stackoverflow.com/questions/30696838/how-to-calculate-the-size-of-a-piece-of-text-in-win2d CanvasTextLayout textLayout1 = new CanvasTextLayout(args.DrawingSession, gameScore.ToString(), textFormat, 0.0f, 0.0f); args.DrawingSession.DrawTextLayout(textLayout1, ((DesignWidth * scaleWidth) / 2) - ((float)textLayout1.DrawBounds.Width / 2), 650 * scaleHeight, Colors.White); } else { // Only draw enemies, weapons and projectiles if the start screen has been passed if (GameState > 0) { // Draw the weapon first args.DrawingSession.DrawImage(Scaling.ScaleImage(Weapon), WeaponPosX, WeaponPosY); // If the ship gets destroyed draw the explosion image if (boomX > 0 && boomY > 0 && boomCount > 0) { args.DrawingSession.DrawImage(Scaling.ScaleImage(Boom), boomX, boomY); boomCount -= 1; } else // Otherwise don't explode { boomCount = 60; boomX = 0; boomY = 0; } // Draw the enemies for (int j = 0; j < enemyXpos.Count; j++) { if (enemyType[j] == 1) { ENEMY_IMG = MinusMonster; } if (enemyType[j] == 2) { ENEMY_IMG = PlusMonster; } // Change direction of enemy movement depending on how they spawn // Connected code can be seen in the enemyTimer function if (enemyDir[j].Equals("left")) { enemyXpos[j] -= 3; } else { enemyXpos[j] += 3; } // Move the enemies down, change value to change speed enemyYpos[j] += 3; args.DrawingSession.DrawImage(Scaling.ScaleImage(ENEMY_IMG), enemyXpos[j], enemyYpos[j]); } //Draw projectiles for (int i = 0; i < blastXPos.Count; i++) { // Linear Interpolation for moving the projectiles // Adapted from https://stackoverflow.com/questions/25276516/linear-interpolation-for-dummies // THIS CODE IS FOR MOBILE ONLY OR DEVICES WITH AN ACCELEROMETRE pointX = (blastXPos[i] + (blastXPos[i] - blastXPos[i]) * percent[i]); pointY = (blastYPos[i] + (blastYPos[i] - blastYPos[i]) * percent[i]); // THIS CODE ALLOWS FOR DESKTOPS, LAPTOPS ETC. TO SEE THE BLASTS MOVE pointX = (photonX + (blastXPos[i] - photonX) * percent[i]); pointY = (photonY + (blastYPos[i] - photonY) * percent[i]); args.DrawingSession.DrawImage(Scaling.ScaleImage(Blast), pointX - (30 * scaleWidth), pointY - (30 * scaleHeight)); // Decrease by 30 to compensate for the offset of the mouse // Increment the position of the projectile to give the appearance of movement percent[i] += (0.040f); // Check if the blast has hit an enemy (collision detection) for (int h = 0; h < enemyXpos.Count; h++) { // If the blast hits an enemy, adjusted for image size of boom.png (185 x 175) if (pointX >= enemyXpos[h] && pointX <= enemyXpos[h] + (185 * scaleWidth) && pointY >= enemyYpos[h] && pointY <= enemyYpos[h] + (175 * scaleHeight)) { boomX = pointX - ((185 / 2) * scaleWidth); boomY = pointY - ((175 / 2) * scaleWidth); // Remove the enemy image enemyXpos.RemoveAt(h); enemyYpos.RemoveAt(h); enemyType.RemoveAt(h); enemyDir.RemoveAt(h); // Remove the blast image blastXPos.RemoveAt(i); blastYPos.RemoveAt(i); blastXPosStartPos.RemoveAt(i); blastYPosStartPos.RemoveAt(i); percent.RemoveAt(i); // Increment Score gameScore = gameScore + 100; break; } } // If the projectile goes off the screen if (pointY < 0f) { // Remove any projectiles that go off the top of the screen blastXPos.RemoveAt(i); blastYPos.RemoveAt(i); blastXPosStartPos.RemoveAt(i); blastYPosStartPos.RemoveAt(i); percent.RemoveAt(i); } } } } // Redraw everything in the draw method (roughly 60fps) GameCanvas.Invalidate(); }
// Adapted from https://microsoft.github.io/Win2D/html/T_Microsoft_Graphics_Canvas_UI_Xaml_CanvasControl.htm private void GameCanvas_CreateResources(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args) { // Calls CreateResourcesAsync Task and ensures could will not execute until the task completes args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction()); }
private void mySurface_CreateResources(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args) { controlOfCanvas = (CanvasControl)sender; }