Share my xNa super Mary (1) circle my game dream, xNa super Mary

Source: Internet
Author: User
Document directory
  • Processing Acceleration

I mentioned in the previous article (circle my game dream, xNa super Mary) that I recently learned about WP development, because I need to learn xNa, the idea of trying to develop a game named "Super Mary" that had been played countless times since childhood was created.

These two days have finally completed the prototype of the game. starting from this article, I will share my development history step by step. since I have not systematically studied game development, the content mentioned in this article may not be a formal practice for game development. However, if you have never been involved in Game Development and are interested in it, I think my article will help you.

Preparations

Before you start, you need some necessary development tools: VS and xNa. The following is the development environment corresponding to this article.

  • Vs2010
  • Microsoft xNa game studio 4.0

VS is believed to be available. As for versions, the xNa versions corresponding to vs are as follows. If you are not using vs2010, you can download the corresponding xNa version.

  • XNa game studio 2.0 (vs2005)
  • XNa game studio 3.0 (vs2008)
  • XNa game studio 3.1 (vs2008)
  • XNa game studio 4.0 (vs2010)

Download the connection and you will not post it. Gu Ge will be out after a while, and I will bring it to me when installing wp7.1.

Hello Mario

After the preparation is complete, we can officially start the development process of Mary.

Open vs, create a project, and select windows game (4.0) template in C #-xNa game studio. The project name is Supermario. For example, click OK.

At this time, Vs will generate a solution named Supermario. There are two projects under the solution, one is the main project named Supermario, and the other is the resource project named supermariocontent. the supermariocontent project is designed to store game resources such as video clips, sounds, and maps. solution example:

Open game1.cs. This is our main game file. It is similar to form1.cs created by default when you create a winform project. Similarly, it is loaded by program. CS.

We can see that it has five rewrite methods, and the first three can be guessed by their names. the next two are our focus. here I will give a brief introduction, and I want you to know how a movie is formed: the constant switching of a static picture leads to visual animation. in fact, the game is the same. The difference is that a movie has already taken a picture and switched one by one. The Game Draws a static picture based on the player's control, and then keeps switching, the formation of animation means that the game has a painting step more than the movie, so playing the game requires higher computer configuration than watching the movie, because there are more things to do. the draw method in game1.cs is used for painting. update updates the elements in the game based on the player input and time flow. when the game is running, the update and draw methods will continue to run in turn, and you have not added any features to the game in real time. for example, when you press F5 to run, you will see a static blue background window, but the update and draw methods will execute 60 times per second.

Next let Mary appear on the game screen. add a folder named image to the supermariocontent project, and add this image in the folder type "add existing items". This is Mario's clip image (a little small, let's get together and use it. I can't find any better ones ). enter the following code in game1.cs:

 1         Texture2D _marioText; 2         protected override void LoadContent() 3         { 4             spriteBatch = new SpriteBatch(GraphicsDevice); 5  6             _marioText = this.Content.Load<Texture2D>(@"Image/mario"); 7         } 8          protected override void Draw(GameTime gameTime) 9         {10             GraphicsDevice.Clear(Color.CornflowerBlue);11             spriteBatch.Begin();12             spriteBatch.Draw(_marioText, new Vector2(100, 100), new Rectangle(0, 0, 16, 16), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);13             spriteBatch.End();14             base.Draw(gameTime);15         }

We load an image in loadcontent, and then draw it in the draw method. The position is the coordinate (X: 100, Y: 100 ), in xNa, the default coordinate origin is the upper left corner. when the image is drawn, whether or not the whole image is drawn, but a small block. The position of the small block in the image is (X: 0, Y: 0) with a length of 16. press F5 to run. mario is already on the game screen...

Run Mario.

How can Mario save the princess? Let's solve the problem of Mario running first. since Mario can move, it indicates that his location will change, declare a variable for his location, and update his location in the update method. the Code is as follows:

        Texture2D _marioText;        Vector2 _marioPosition = new Vector2(100, 100);        protected override void Update(GameTime gameTime)        {            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                this.Exit();            _marioPosition.X++;            base.Update(gameTime);        }        protected override void Draw(GameTime gameTime)        {            GraphicsDevice.Clear(Color.CornflowerBlue);            spriteBatch.Begin();            spriteBatch.Draw(_marioText, _marioPosition, new Rectangle(0, 0, 16, 16), Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);            spriteBatch.End();            base.Draw(gameTime);        }

Press F5 to run. How is Mario "moving? However, the current movement is a bit like a ghost, and it is automatic and out of our control. we add control to him first. Generally, the game uses the "a" "D" key to move, with a left shift and a right shift. modify the UPDATE function

 1         protected override void Update(GameTime gameTime) 2         { 3             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 4                 this.Exit(); 5             KeyboardState keyState = Keyboard.GetState(); 6             if (keyState.IsKeyDown(Keys.A)) 7             { 8                 _marioPosition.X--; 9             }10             if (keyState.IsKeyDown(Keys.D))11             {12                 _marioPosition.X++;13             }14 15             base.Update(gameTime);16         }

The code is easy to understand. I will not explain it .... now press F5 to run. Mario has become a game role that we can control. but Mario is still floating like a ghost. It's time to let his feet move. The second and third Mary pictures in the two pictures. Here we will number the positions of all the small images in the image, starting from 0, so we should use images 1 and 2 for running. First look at the code

Int _ frmstartindex = 0; int _ frmendindex = 0; int _ frmindex = 0; // The index int _ frmchangetime = 100 of the currently drawn image block; // how many milliseconds to change the image int _ frmcurrenttime = 0; // The number of milliseconds from the last time the image was changed protected override void Update (gametime) {If (gamepad. getstate (playerindex. one ). buttons. back = buttonstate. pressed) This. exit (); keyboardstate keystate = keyboard. getstate (); If (keystate. iskeydown (keys. a) {_ marioposition. X --; _ frmstartindex = 1; _ frmendindex = 2;} If (keystate. iskeydown (keys. d) {_ marioposition. X ++; _ frmstartindex = 1; _ frmendindex = 2;} If (keystate. getpressedkeys (). count () = 0) {_ frmstartindex = 0; _ frmendindex = 0;} _ frmcurrenttime + = gametime. elapsedgametime. milliseconds; If (_ frmcurrenttime> _ frmchangetime) {_ frmcurrenttime = 0; _ frmindex ++; If (_ frmindex> _ frmendindex) _ frmindex = _ frmstartindex;} base. update (gametime);} protected override void draw (gametime) {graphicsdevice. clear (color. cornflowerblue); spritebatch. begin (); spritebatch. draw (_ mariotext, _ marioposition, new rectangle (_ frmindex * 16, 0, 16, 16), color. white, 0, vector2.zero, 1.0f, spriteeffects. none, 0); spritebatch. end (); base. draw (gametime );}

Note that the second parameter of the draw function has some minor changes. In the UPDATE function, we select the area of the image block to be selected when drawing by checking the button, when Mario does not move, the index range of the image block is 0-0, and the time range is 1-2, and the interval Variable _ frmchangetime for switching the image is defined, the update function keeps accumulating. When it exceeds 100 milliseconds, you can change the next image. If there is no such interval variable, the image will be changed every time the update is executed, and Mario will go crazy. When you press F5 to run, Mario will move. I think someone will say, "Why do you only move your hands and feet?" simply because I cannot find the pictures that Mary should use when moving, you don't have to worry about the current Code. If you can find the right picture, you can let Mario run correctly.

Processing Acceleration

If you have played the Mario game, you must know that there is a process of acceleration when you move Mario, rather than moving at a fixed speed when you press the running key. In fact, this is easy to solve. We all know that acceleration is not just to increase the speed of acceleration and then motion. Here, we only need to simulate real motion through physical knowledge in the game. View code

Float _ runacceleration = 0.01f; // running acceleration float _ runresistance = 0.005f ;//... int maxspeedr = 3; // int maxspeedl =-3; // float _ speed = 0; // current speed protected override void Update (gametime) {If (gamepad. getstate (playerindex. one ). buttons. back = buttonstate. pressed) This. exit (); keyboardstate keystate = keyboard. getstate (); int frmtime = gametime. elapsedgametime. milliseconds; If (keystate. Iskeydown (Keys. a) {_ speed = _ speed <maxspeedl? Maxspeedl: _ speed-_ runacceleration * frmtime;} If (keystate. iskeydown (Keys. d) {_ speed = _ speed> maxspeedr? Maxspeedr: _ speed + _ runacceleration * frmtime;} If (keystate. getpressedkeys (). count () = 0) {If (_ speed> 0) {_ speed-= _ runresistance * frmtime; If (_ speed <0) _ speed = 0 ;} if (_ speed <0) {_ speed + = _ runresistance * frmtime; If (_ speed> 0) _ speed = 0 ;}} if (_ speed = 0) {_ frmstartindex = 0; _ frmendindex = 0;} else {_ frmstartindex = 1; _ frmendindex = 2;} This. _ marioposition. X + = _ speed; _ frmcurrenttime + = frmtime; If (_ frmcurrenttime> _ frmchangetime) {_ frmcurrenttime = 0; _ frmindex ++; If (_ frmindex> _ frmendindex) _ frmindex = _ frmstartindex;} base. update (gametime );}

The update function can now be divided into two parts. The first part processes the Mario speed based on the user's keyboard input and multiply the time by the acceleration to get the speed. When the user no longer presses any key, the processing slows down. The second part is to process the image index range based on Mario's speed. Now, when Mario's speed is not 0, the running image is displayed. Here this. _ marioposition. x + = _ speed; what is the solution? Because the calculated speed is the speed within the frame, it can be understood as the unit speed. The distance equals to the speed multiplied by the time, and the time is 1, which is equivalent to the distance equals to the speed. Finally, let's press F5 to run it. Now, Mario is feeling a little bit.
Currently, regardless of the Left or Right Shift of Mario, the body is on the right. when moving to the left, it should face to the left. This is not hard to handle. The picture already exists. If you are interested, try it on your own.

End

The first article introduces us here. Our super Mary is far from complete, and the rest will be coming soon. I hope to help you.

It is not easy to write a Bock in the snow. I will give a high praise to you .....

Source code: http://files.cnblogs.com/xxfss2/SuperMario1.rar

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.