reffec.com

Working with 2D Characters in Java Compose QR Code 2d barcode in Java Working with 2D Characters

Working with 2D Characters using none tocreate none on asp.net web,windows application QR Code Safty For each Use none for none r Control created, the IDE is going to show the image with a transparent background in the preview, as shown in the following screenshot for the RedAlien:. What just happened We created a none for none new User Control for each of the aliens, the tent, and the ship. These are the characters of our raster game. Now, the project has a new XAML and a Class for each potential sprite:.

BlueAlien GreenAlien RedAlien Ship Tent Now we have everything we need to begin moving these characters. Time for action displaying images and sprites First, we ar none none e going to display the raster images and give them life by transforming them into our first sprites. We are going to add some code, which we will improve later:. 1. 2..

Stay in the none for none SilverlightInvaders2D project. Open the XAML code for MainPage.xaml and replace the existing code with the following.

(You will see a button and the aliens aligned in five rows in the preview, as shown in the screenshot after the code.). <UserCont rol x:Class="SilverlightInvaders2D.MainPage" xmlns="http://schemas.microsoft.

com/winfx/2006/xaml [ 5 ]. 2 /presenta none none tion" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="1366" Height="768" xmlns:SilverlightInvaders2D= "clr-namespace:SilverlightInvaders2D"> <Canvas x:Name="LayoutRoot" Background="White"> <!-- A button to start moving the sprites --> <Button x:Name="btnStartGame" Content="Start the game!" Canvas.

Left="200" Canvas.Top="20" Width="200" Height="30" Click="btnStartGame_Click"> </Button> <!-- Code to show the User Controls (the sprites) --> <SilverlightInvaders2D:GreenAlien x:Name="ucGreenAlien1" Canvas.Left="50" Canvas.

Top="10"/> <SilverlightInvaders2D:BlueAlien x:Name="ucBlueAlien1" Canvas.Left="50" Canvas.Top="70"/> <SilverlightInvaders2D:BlueAlien x:Name="ucBlueAlien2" Canvas.

Left="50" Canvas.Top="130"/> <SilverlightInvaders2D:RedAlien x:Name="ucRedAlien1" Canvas.Left="50" Canvas.

Top="190"/> <SilverlightInvaders2D:RedAlien x:Name="ucRedAlien2" Canvas.Left="50" Canvas.Top="250"/> </Canvas> </UserControl>.

[ 5 ]. Working with 2D Characters Open MainPag e.xaml.cs and add the following lines to define the private variables in public partial class MainPage : UserControl:.

// Holds the none none green alien"s location in the Canvas private Point _GreenAlien1Location = new Point(0, 0); // Holds the speed for X and Y movement private Point _GreenAlien1Speed = new Point(250, 0); // The upper left corner for the animation private Point _UpperLeftCorner = new Point(0, 0); // The bottom right corner for the animation private Point _BottomRightCorner = new Point(0, 0); // The aliens" row height private double _RowHeight = 60; // Holds the time when the method finished rendering a frame private DateTime _LastTick;. Add the foll owing lines of code to program the event handler that will render each frame and update the sprite"s position:. private void none for none RenderFrame(object sender, EventArgs e) { // Hold the elapsed time after the last call to this method TimeSpan ElapsedTime = (DateTime.Now - _LastTick); // Update the X-coordinate according to the elapsed time and the speed _GreenAlien1Location.X += _GreenAlien1Speed.

X * (double)ElapsedTime.TotalSeconds; // Update the Y-coordinate according to the elapsed time and the speed _GreenAlien1Location.Y += _GreenAlien1Speed.

Y * (double)ElapsedTime.TotalSeconds; if (_GreenAlien1Location.X > _BottomRightCorner.

X) { // Right bound reached, invert direction _GreenAlien1Speed.X *= -1; _GreenAlien1Location.X = _BottomRightCorner.

X; // Advance one row _GreenAlien1Location.Y += _RowHeight; } else if (_GreenAlien1Location.X < _UpperLeftCorner.

X) { // Left bound reached, invert direction [ 60 ]. 2 _GreenAli en1Speed.X *= -1; _GreenAlien1Location.X = _UpperLeftCorner.

X; // Advance one row _GreenAlien1Location.Y += _RowHeight; } // Set the new location for the sprite ucGreenAlien1.SetValue(Canvas.

LeftProperty, _GreenAlien1Location.X); ucGreenAlien1.SetValue(Canvas.

TopProperty, _GreenAlien1Location. Y); // Save the current time _LastTick = DateTime.Now; }.

Finally, add none for none the following lines of code to program the event handler for the button"s Click event. (This code will start animating the sprite.).

private void btnStartGame_Click(object sender, RoutedEventArgs e) { // Hide the button btnStartGame.Visibility = Visibility.Collapsed; // Store the green alien"s current location _GreenAlien1Location = new Point((double)ucGreenAlien1.

GetValue(Canvas.LeftProperty), (double)ucGreenAlien1.GetValue(Canvas.

TopProperty)); // Define the upper left corner and bottom right corner for the animation _UpperLeftCorner = new Point(_GreenAlien1Location.X, _GreenAlien1Location.Y); _BottomRightCorner = new Point(LayoutRoot.

ActualWidth ucGreenAlien1.imgSprite01.ActualWidth, LayoutRoot.

ActualHeight ucGreenAlien1.imgSprite01.ActualHeight); // Save the current time _LastTick = DateTime.

Now; // Add an EventHandler CompositionTarget.Rendering += RenderFrame; }. [ 61 ]. Working with 2D Characters Build and ru none for none n the solution. Click on the button and the green alien (the sprite) will move from the left to right, down a row, right to left, down a row, left to right, and so on as shown in the following screenshot:.
Copyright © reffec.com . All rights reserved.