Previously, Flyer01 said that using Silverlight to develop games can be completed through code and Blend development tools. The Blend view development environment is similar to that of Flash, but it is more emphasized that it is a combination of programs, I never know how to clearly define the animation axis, and Silverlight cannot be developed without the development tool like Flash. I plan to let my friends know through this simple example, how to Develop a Silverlight game using only code.
1. An environment is required. In this article, you can find the answer: Silverlight C # Game Development: Silverlight Development Environment
2. Create a Silverlight project and then write the code :)
First, we need a window to present the entire game content, so I need to add the following code in XAML:
The above indicates that a canvas is added to a space of X and the background color is blue. for more information, see Silverlight C # Game Development: About the genie for Silverlight container.
Now, you can add a class in the project. If you like to add code files directly, it is good. I am a lazy and automatic.
Write the following code. It indicates a cloud control inherited from the Canvas. Add an Image to this control. The Image cannot be inherited and cannot be used as a base class. Therefore, we use a Canvas as a container.
But where are our resources? You need to prepare a set of images and add them to the project. A Src directory is added here, as shown in: (the file can be downloaded from the attachment below)
Add the following code to the MainPage class:
Run the command to see the effect:
We can see a cloud on the page. This is the first Image displayed in the form of pure code. In this structure, the ClassCloud class is a control inherited from the Canvas, which contains an Image control, although it is difficult to say, It is not complicated. I believe it should be easy to understand, but a piece of cloud is meaningless. Our ultimate goal is to let it move, but how can we do it? First, we can control its coordinates:
The XY above can directly control its own coordinates, so we don't need to do experiments here ...... Clouds must be a bunch of clouds, and there will be many clouds. In this case, another container is required to load these clouds. To make the structure clear, let's set a ClassCloudGroup to manage these clouds. At the same time, this is also a control container:
The code above indicates creating a container and adding a specified number of clouds in the MainPage constructor and then adding a window. The running effect is as follows:
Now there are a lot of clouds, which are randomly distributed on the blue background. OK, so the next step is to let them move up. Because the game is set to fly down, the clouds should move up, therefore, we add the following code to the cloud group:
The running effect is as follows:
For better looks, I made some special processing. For details, see the source code: Click here to download
Some interesting corrections have been made on the code, which may be inconsistent with the above Code, but there are detailed comments, and I believe they should be helpful to everyone.
This is the most attractive part of the game. You can create a world through operations. This is a simple background. Next time, let's fall together with the pilot :)