C # development Wpf/silverlight animation and games series Tutorials (Game Course): (22)

Source: Internet
Author: User
Tags silverlight

C # development Wpf/silverlight animation and Game Tutorials (Game Course): (22) Refactoring – Let code fly with wings free

In the previous section, I made a major change in the game map pattern, this means that the project has been massively reset in real development, and although it seems to be on the surface, just one allmove () method has been transformed to achieve a perfect transition, thanks to the relatively highly scalable platform built up in the first 20 sections. However, as the development continues, I slowly feel a little uneasy, because the code on the increasingly loose and the structure is gradually sparse as the Windows system disk fragmentation is increasing, the future maintenance of the cumbersome and bitter is vividly remembered. Code sent me a distress signal, with what to save you-my code? It's time for the sword to –-my first intimate reconstruction.

I'll refactor the code in the previous section in a few minutes:

One, unified code format, so that the code to achieve the readability of the most:

For example, I create a map-surface code in the previous section:

private void InitMapSurface() {
 MapSurface.Width = 1750;
 MapSurface.Height = 1440;
 MapSurface.Source = BitmapFrame.Create((new Uri(@"Map\0\0.jpg", UriKind.Relative)));
 Carrier.Children.Add(MapSurface);
 Canvas.SetLeft(MapSurface, -320);
 Canvas.SetTop(MapSurface, -200);
 MapSurface.SetValue(Canvas.ZIndexProperty, -1);
}

First, we look at the first 3 lines in the Initmap () method, all of which are assigned with the beginning of the Mapsurface, and then the penultimate 2 and 3 lines are canvas.set ... () mode to set the properties of the mapsurface; what's more, the last line can be written as Canvas.setzindex (...) of the form, but also with its first two lines, but the author rushed to write the project, all kinds of writing out. Although this is required to achieve functionality, but only 10 lines of code readability has reached a "shadowy" point, have you ever thought of such a similar code once more, besides you, who can maintain? God knows.

In fact, this code is very simple to change, but call the unified writing format, thus improving the readability of the code. Here's how I operate:

1, analysis, Canvas.setleft, Canvas.settop and canvas.zindexproperty These 3 things are set is the upper left corner of the surface layer picture from the game window x distance, y distance and its depth in the game window (level). Also, their assignment ranges are integers (positive, negative, 0).

2. Think, WPF and Silverlight belong to different forms of application, a desktop, a browser, but they can use the same XAML for the presentation layer design, is the MS deliberately closer to the distance between the two? This is impossible to verify, the future can answer us, but from the similarities between the two to remind me of the Web page.

3, compare, the Web Page object's style (CSS style sheet) properties of 3 properties and the above 3 property names and the role of the astonishing similarity: left, top, z-index, but they must be used in Position:absolute mode, but this is what we wanted, The interior layout of the canvas canvas is also based on the absolute location of the point, both of which seem like a thing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.