Windows Phone 上的XNA游戏编程学习笔记(1)--编写第一个手机小游戏

XNA(维基百科介绍)是微软开发的专门的游戏开发工具,用于开发Xbox游戏,也是windows phone 上的专业游戏开发平台。下面就是我编写的第一个XNA游戏,共享一下,纯属学习,大神神马的就别看了。
(1)下载并安装windows phone 的SDK(包含XNA framework )
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27153
(2)打开visual studio2010,文件|新建|项目…并选择XNA Game Studio 中的Windows Studio Game(4.0)。
Windows Phone 上的XNA游戏编程学习笔记(1)--编写第一个手机小游戏
(3)建好工程之后,由于XNA是基于位图的程序,需要向工程中添加一些必要的文件,如使用的字体、以及使用到的图片。Windows Phone 上的XNA游戏编程学习笔记(1)--编写第一个手机小游戏
先添加一个新的字体segoe(位图版的字体是有版权的,方便起见还是用系统自带的吧)。
Windows Phone 上的XNA游戏编程学习笔记(1)--编写第一个手机小游戏
并且添加位图football.png到工程中。(先下载,再Add| Existing Item..)
Windows Phone 上的XNA游戏编程学习笔记(1)--编写第一个手机小游戏
(4)之后,就可以向Game1.cs中添加代码。
将其中的Game1 类换成如下代码:
1 public class Game1 : Microsoft.Xna.Framework.Game 2 { 3 GraphicsDeviceManager graphics; 4 SpriteBatch spriteBatch; 5 const float DECELERATION = 1000; // pixels per second squared 6 Texture2D texture; 7 RenderTarget2D tinyTexture; 8 Vector2 position = Vector2.Zero; 9 Vector2 velocity; 10 SpriteFont segoe14; 11 StringBuilder text = new StringBuilder(); 12 Viewport viewport; 13 public Game1() 14 { 15 graphics = new GraphicsDeviceManager(this); 16 Content.RootDirectory = "Content"; 17 // Frame rate is 30 fps by default for Windows Phone. 18 TargetElapsedTime = TimeSpan.FromTicks(333333); 19 TouchPanel.EnabledGestures = GestureType.Flick; 20 } 21 22 /// 23 /// Allows the game to perform any initialization it needs to before starting to run. 24 /// This is where it can query for any required services and load any non-graphic 25 /// related content. Calling base.Initialize will enumerate through any components 26 /// and initialize them as well. 27 /// 28 protected override void Initialize() 29 { 30 // TODO: Add your initialization logic here 31 32 base.Initialize(); 33 } 34 35 /// 36 /// LoadContent will be called _disibledevent=> 37 /// all of your content. 38 /// 39 protected override void LoadContent() 40 { 41 // Create a new SpriteBatch, which can be used to draw textures. 42 spriteBatch = new SpriteBatch(GraphicsDevice); 43 44 tinyTexture = new RenderTarget2D(this.GraphicsDevice, 1, 1); 45 46 this.GraphicsDevice.SetRenderTarget(tinyTexture); 47 this.GraphicsDevice.Clear(Color.White); 48 this.GraphicsDevice.SetRenderTarget(null); 49 50 texture = this.Content.Load("football"); 51 segoe14 = this.Content.Load("Segoe"); 52 } 53 54 /// 55 /// UnloadContent will be called _disibledevent=> 56 /// all content. 57 /// 58 protected override void UnloadContent() 59 { 60 // TODO: Unload any non ContentManager content here 61 } 62 63 /// 64 /// Allows the game to run logic such as updating the world, 65 /// checking for collisions, gathering input, and playing audio. 66 /// 67 /// Provides a snapshot of timing values.
68 protected override void Update(GameTime gameTime) 69 { 70 // Allows the game to exit 71 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); 72 // Set velocity from Flick gesture 73 while (TouchPanel.IsGestureAvailable) 74 { 75 GestureSample gesture = TouchPanel.ReadGesture(); 76 if (gesture.GestureType == GestureType.Flick) 77 velocity += gesture.Delta; 78 } 79 // Use velocity to adjust position and decelerate 80 if (velocity != Vector2.Zero) 81 { 82 float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds; 83 position += velocity * elapsedSeconds; 84 viewport = this.GraphicsDevice.Viewport; 85 while (true) 86 { 87 if (position.X < 0) 88 { 89 position.X = -position.X; 90 velocity.X = -(float)0.8 * velocity.X; 91 continue; 92 } 93 if (position.Y < 0) 94 { 95 position.Y = -position.Y; 96 velocity.Y = -(float)0.8 * velocity.Y; 97 continue; 98 } 99 if (position.X > viewport.Width - texture.Width) 100 { 101 position.X = 2 * (viewport.Width - texture.Width) - position.X; 102 velocity.X = -(float)0.8 * velocity.X; 103 continue; 104 } 105 if (position.Y > viewport.Height - texture.Height) 106 { 107 position.Y = 2 * (viewport.Height - texture.Height) - position.Y; 108 velocity.Y = -(float)0.8 * velocity.Y; 109 continue; 110 } 111 break; 112 } 113 float newMagnitude = velocity.Length() - DECELERATION * elapsedSeconds; 114 velocity.Normalize(); velocity *= Math.Max(0, newMagnitude); 115 } 116 // Display current position and velocity 117 text.Remove(0, text.Length); 118 text.AppendFormat("Position: {0} Velocity: {1}", position, velocity); 119 base.Update(gameTime); 120 } 121 122 /// 123 /// This is called when the game should draw itself. 124 /// 125 /// Provides a snapshot of timing values.
126 protected override void Draw(GameTime gameTime) 127 { 128 GraphicsDevice.Clear(Color.CornflowerBlue); 129 spriteBatch.Begin(); 130 spriteBatch.Draw(tinyTexture, new Rectangle(0, 0, 800, 480), Color.White); 131 spriteBatch.Draw(texture, position, Color.White); 132 spriteBatch.DrawString(segoe14, text, Vector2.Zero, Color.Blue); 133 spriteBatch.End(); 134 base.Draw(gameTime); 135 } 136 }
(5)编译运行,在屏幕中出现一个足球,你可以通过鼠标模拟手指轻划屏幕,足球就会在屏幕中弹来弹去,直至速度为0;
Windows Phone 上的XNA游戏编程学习笔记(1)--编写第一个手机小游戏


Tags: 

延伸阅读

最新评论

发表评论