手把手教用XNA开发winphone7游戏(三)
XNA Game Studio 游戏循环
在这部分中您将重点两剩余部分的游戏 — — 重写Update 和 Draw 功能。有些大大可能看过相关微软的训练包,我这里主要是帮一些初学者。希望各位大大包含,毕竟文章发出来还是有工作量的。大家觉得有用就好,要是没有耽误时间给大家道个歉。(感谢http://winphone.us/)
1. 打开 BackgroundScreen.cs文件。
2. 重写基类Update 方法如下:
(Code Snippet – Game Development with XNA – Background Screen Update method)
C#
public override void Update(GameTime gameTime, bool otherScreenHasFocus,
bool coveredByOtherScreen)
{
base.Update(gameTime, otherScreenHasFocus, false);
}
3. 重写基类方法绘制。 绘图方法将绘制图形设备上使用 Microsoft.Xna.Framewok.Graphics 命名空间中的 SpriteBatch 类。一组sprites被绘制的时候使用同样的设置。改变 Draw 方法来匹配下面的代码段:
(Code Snippet – Game Development with XNA – Background Screen Draw method)
C#
public override void Draw(GameTime gameTime)
{
SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
// Make the menu slide into place during transitions, using a
// power curve to make things look more interesting (this makes
// the movement slow down as it nears the end).
float transitionOffset = (float)Math.Pow(TransitionPosition, 2);
spriteBatch.Begin();
// Draw Background
spriteBatch.Draw(background, new Vector2(0, 0),
new Color(255, 255, 255, TransitionAlpha));
// Draw Title
spriteBatch.Draw(title, new Vector2(60, 55),
new Color(255, 255, 255, TransitionAlpha));
spriteBatch.End();
}
4. 按 F5 编译并运行该应用程序。
图1
修改了updatae和Draw后的运行效果
5. 停止调试 (SHIFT + F5),并返回到编辑应用程序。
6. 将一个附加类添加到应用程序,并将其名称设置为 GameplayScreen。
Note: 要创建一个新的类,在解决方案资源管理器中右键单击 AlienGame 项目并选择Add | Class.
7. 添加以下使用申明到新类:
(Code Snippet – Game Development with XNA – Gameplay Screen using statements )
C#
using AlienGameSample;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using System.IO.IsolatedStorage;
using System.IO;
8. 从 GameScreen 派生:
C#
class GameplayScreen : GameScreen
{
}
9. 添加以下类变量 (将在比赛中使用它们)。 后面我们使用这些变量,处理游戏逻辑、 用户输入和绘图:
(Code Snippet – Game Development with XNA – Gameplay Screen variables)
C#
//
// Game Play Members
//
Rectangle worldBounds;
bool gameOver;
int baseLevelKillCount;
int levelKillCount;
float alienSpawnTimer;
float alienSpawnRate;
float alienMaxAccuracy;
float alienSpeedMin;
float alienSpeedMax;
int alienScore;
int nextLife;
int hitStreak;
int highScore;
Random random;
//
// Rendering Members
//
Texture2D cloud1Texture;
Texture2D cloud2Texture;
Texture2D sunTexture;
Texture2D moonTexture;
Texture2D groundTexture;
Texture2D tankTexture;
Texture2D alienTexture;
Texture2D badguy_blue;
Texture2D badguy_red;
Texture2D badguy_green;
Texture2D badguy_orange;
Texture2D mountainsTexture;
Texture2D hillsTexture;
Texture2D bulletTexture;
Texture2D laserTexture;
SpriteFont scoreFont;
SpriteFont menuFont;
Vector2 cloud1Position;
Vector2 cloud2Position;
Vector2 sunPosition;
// Level changes, nighttime transitions, etc
float transitionFactor; // 0.0f == day, 1.0f == night
float transitionRate; // > 0.0f == day to night
ParticleSystem particles;
//
// Audio Members
//
SoundEffect alienFired;
SoundEffect alienDied;
SoundEffect playerFired;
SoundEffect playerDied;
//Screen dimension consts
const float screenHeight = 800.0f;
const float screenWidth = 480.0f;
const int leftOffset = 25;
const int topOffset = 50;
const int bottomOffset = 20;
10. 游戏类构造函数定义 (在游戏屏幕和其他屏幕在游戏中的) 之间的屏幕转换的速度和大小—— 在处理游戏的所有操作的地方。 添加此类构造函数,如下所示::
(Code Snippet – Game Development with XNA – Gameplay Screen Constructor)
C#
public GameplayScreen()
{
random = new Random();
worldBounds = new Rectangle(0, 0, (int)screenWidth, (int)screenHeight);
gameOver = true;
TransitionOnTime = TimeSpan.FromSeconds(0.0);
TransitionOffTime = TimeSpan.FromSeconds(0.0);
}
11. 现在让我们来创建内容的加载和卸载功能。 重写基类的 LoadContent 和 UnloadContent 的方法。
添加 LoadContent 代码段::
(Code Snippet – Game Development with XNA – Gameplay Screen LoadContent method)
C#
public override void LoadContent()
{
cloud1Texture = ScreenManager.Game.Content.Load<Texture2D>("cloud1");
cloud2Texture = ScreenManager.Game.Content.Load<Texture2D>("cloud2");
sunTexture = ScreenManager.Game.Content.Load<Texture2D>("sun");
moonTexture = ScreenManager.Game.Content.Load<Texture2D>("moon");
groundTexture = ScreenManager.Game.Content.Load<Texture2D>("ground");
tankTexture = ScreenManager.Game.Content.Load<Texture2D>("tank");
mountainsTexture = ScreenManager.Game.Content.Load<Texture2D>("mountains_blurred");
hillsTexture = ScreenManager.Game.Content.Load<Texture2D>("hills");
alienTexture = ScreenManager.Game.Content.Load<Texture2D>("alien1");
badguy_blue = ScreenManager.Game.Content.Load<Texture2D>("badguy_blue");
badguy_red = ScreenManager.Game.Content.Load<Texture2D>("badguy_red");
badguy_green = ScreenManager.Game.Content.Load<Texture2D>("badguy_green");
badguy_orange = ScreenManager.Game.Content.Load<Texture2D>("badguy_orange");
bulletTexture = ScreenManager.Game.Content.Load<Texture2D>("bullet");
laserTexture = ScreenManager.Game.Content.Load<Texture2D>("laser");
alienFired = ScreenManager.Game.Content.Load<SoundEffect>("Tank_Fire");
alienDied = ScreenManager.Game.Content.Load<SoundEffect>("Alien_Hit");
playerFired = ScreenManager.Game.Content.Load<SoundEffect>("Tank_Fire");
playerDied = ScreenManager.Game.Content.Load<SoundEffect>("Player_Hit");
scoreFont = ScreenManager.Game.Content.Load<SpriteFont>("ScoreFont");
menuFont = ScreenManager.Game.Content.Load<SpriteFont>("MenuFont");
cloud1Position = new Vector2(224 - cloud1Texture.Width, 32);
cloud2Position = new Vector2(64, 80);
sunPosition = new Vector2(16, 16);
particles = new ParticleSystem(ScreenManager.Game.Content, ScreenManager.SpriteBatch);
base.LoadContent();
}
12. 添加UnloadContent代码段:
(Code Snippet – Game Development with XNA – Gameplay Screen Unload method)
C#
public override void UnloadContent()
{
particles = null;
base.UnloadContent();
}
13. 重写基类Update功能:
Note: 我们将在做游戏逻辑的时候再来修改他。
(Code Snippet – Game Development with XNA – Gameplay Screen Update method)
C#
/// <summary>
/// Runs one frame of update for the game.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime,
bool otherScreenHasFocus, bool coveredByOtherScreen)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
}
14. 重写基类绘图功能,当下的“游戏世界”是每秒30次。
(Code Snippet – Game Development with XNA – Gameplay Screen Draw region)
C#
/// <summary>
/// Draw the game world, effects, and HUD
/// </summary>
/// <param name="gameTime">The elapsed time since last Draw</param>
public override void Draw(GameTime gameTime)
{
float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
ScreenManager.SpriteBatch.Begin();
ScreenManager.SpriteBatch.End();
}
Note: The GameTime could be used to calculate the drawing locations of various game items.
15. 打开 MainMenuScreen.cs,找到 StartGameMenuEntrySelected 方法,现在是空的,我们将以下代码添加进去。 这段代码的作用是当用户点击“START GAME”按钮时,将 GameplayScreen 添加到ScreenManager:
(Code Snippet – Game Development with XNA – MainMenu Screen – GameMenuEntrySelected handler)
C#
void StartGameMenuEntrySelected(object sender, EventArgs e)
{
ScreenManager.AddScreen(new GameplayScreen());
}
16. 编译并运行该应用程序。 单击"开始游戏"菜单项,可以看到主菜单从屏幕的下方滚动上来。
图2
运行效果
Note: 现在游戏的场景你还看不到,不过不要紧,明天我们就开始了,加油!!
17. 停止调试并回到应用程序编辑状态。
在个章节,你创建了新的主游戏类,并重写了游戏基类的功能。
转载于:https://www.cnblogs.com/zouyuntao/archive/2010/11/10/1874267.html
手把手教用XNA开发winphone7游戏(三)相关推荐
- 手把手教用XNA开发winphone7游戏(四)
XNA Game Studio 游戏输入 昨天的双11让人感触颇多,幸好有http://www.appfsoft.com和博客园还有技术陪伴我,还有很多园子里的朋友,大家一定不要放弃自己的梦想. 在这 ...
- 手把手教用XNA开发winphone7游戏(五)大结局
Alien Game逻辑 在这最有一个部分你将创建game-specific logic和 helper方法和类.胜利就在眼前,你的第一个winphone7程序就要出现了,加油加油!!(感谢http: ...
- 手把手教用XNA开发winphone7游戏(二)
相关下载地址:/Files/zouyuntao/Assets.rar XNA Framework游戏资源 这个环节我们将利用XNA将提供的大量的声音.图片和声音各种资源管理起来,使游戏开发过程更加容易 ...
- Android 开发之手把手教你写 ButterKnife 框架(三)
系列文章目录导读: Android开发之手把手教你写ButterKnife框架(一) Android开发之手把手教你写ButterKnife框架(二) Android开发之手把手教你写ButterKn ...
- python手机版做小游戏代码大全-Python大牛手把手教你做一个小游戏,萌新福利!...
原标题:Python大牛手把手教你做一个小游戏,萌新福利! 引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规 ...
- Unity 之 手把手教你实现自己Unity2D游戏寻路逻辑 【文末源码】
Unity 之 手把手教你实现自己Unity2D游戏寻路逻辑 [文末源码] 前言 一,效果展示 二,场景搭建 三,代码逻辑 四,完善场景 五,使用小结 前言 还在看别人的寻路逻辑?保姆级教程,一步步教 ...
- 教你如何开发VR游戏系列教程一:前言
VR现在发展很快,也被炒的很热.因此,做VR应用开发(主要是游戏,也包含全景播放器等)的同学越来越多.AR学院(www.arvrschool.com)就准备了这么一份教程,给大家提供一些帮助和参考. ...
- 手把手教你python自动化办公(三)---PPT批量修改
手把手教你python自动化办公(三)---PPT批量修改 PPT批量修改 场景模拟:当公司让你制作10000个不同数据但背景相同的PPT时,你是干上三天,还是小手一挥,十秒搞定? 1.设计你想要的P ...
- python如何编游戏_手把手教你用python写游戏
引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规的项目开发流程,手把手教大家写个python小游戏,项目来自 ...
最新文章
- 跟我学交换机配置(四)
- iOS - UIStoryboard
- python 3.9 新特性 简介
- 2019年Vue学习路线图
- javaweb学习总结(二十六)——jsp简单标签标签库开发(二)
- Python+Selenium WebDriver API:浏览器及元素的常用函数及变量整理总结
- 【ZOJ 2974】Just Pour the Water(矩阵快速幂)
- 不让登陆_百万伙伴争代言 不让梦想咕咕叫 中国太保寿险公益活动提前117天汇聚300万颗爱心...
- python 排列组合算法_排 列 组 合 公 式 及 排 列 组 合 算 法
- 经典手眼标定算法之Tsai-Lenz的OpenCV实现
- keli 软件支持包下载
- 二分查找算法java实现
- heic格式转化jpg方法
- NB-IoT 基于蜂窝的窄带物联网
- java_opts 与catalina_opts区别_CATALINA_OPTS和 JAVA_OPTS区别
- SharePoint - 如何查询SharePoint ID?
- 所以,网络工程师能从事什么工作?
- iPhone铃声制作软件:iRingg for Mac
- hive的一些常用命令
- SpringCloud的各种超时时间配置效果