C# 我怎样才能在里面画一个精灵';她是自己的班级吗?
我正试着和一个朋友做一个游戏,我很困惑如何画精灵。我可以在下面的Game1类中绘制代码C# 我怎样才能在里面画一个精灵';她是自己的班级吗?,c#,graphics,xna,sprite,C#,Graphics,Xna,Sprite,我正试着和一个朋友做一个游戏,我很困惑如何画精灵。我可以在下面的Game1类中绘制代码 namespace SweetGeorgiaBrown { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { enum GameStates { TitleScre
namespace SweetGeorgiaBrown
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
enum GameStates { TitleScreen, Overworld, Menu, Battle, GameOver };
GameStates gameState = GameStates.Battle;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D dinoTex;
Texture2D alienTex;
Texture2D robotTex;
Texture2D eroboTex;
SpriteFont basic;
private Vector2 textLocation = new Vector2(20, 20);
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
public class Health
{
public static int dinoHP = 300;
public static int alienHP = 200;
public static int robotHP = 100;
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
dinoTex = Content.Load<Texture2D>(@"Sprites\dino\dinoFrontOvwld");
alienTex = Content.Load<Texture2D>(@"Sprites\alien\alienFrontOvwld");
robotTex = Content.Load<Texture2D>(@"Sprites\robot\robotFrontOvwld");
eroboTex = Content.Load<Texture2D>(@"Sprites\robo1\robo1OvwldFront");
basic = Content.Load<SpriteFont>(@"Fonts\Basic");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
switch (gameState)
{
case GameStates.Battle:
break;
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
// TODO: Add your drawing code here
if ((gameState == GameStates.Battle))
{
spriteBatch.DrawString(
basic,
"ASDFASDFADFASDFA ", textLocation, Color.Black);
spriteBatch.Draw(
dinoTex, textLocation, Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
乔治亚·布朗
{
///
///这是游戏的主要类型
///
公共类游戏1:Microsoft.Xna.Framework.Game
{
枚举游戏状态{标题屏幕、超世界、菜单、战斗、游戏结束};
GameStates gameState=游戏状态。战斗;
图形管理器图形;
SpriteBatch SpriteBatch;
纹理2D恐龙;
纹理2d-alienTex;
质感2d-robotTex;
纹理2d-eroboTex;
SpriteFont basic;
私有向量2 textLocation=新向量2(20,20);
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
公共卫生
{
公共静态int dinoHP=300;
公共静态int alienHP=200;
公共静态int robotHP=100;
}
///
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
///
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
}
///
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
///
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
dinoTex=Content.Load(@“Sprites\dino\dinoFrontOvwld”);
alienTex=Content.Load(@“Sprites\alien\alienFrontOvwld”);
robotTex=Content.Load(@“Sprites\robot\robotFrontOvwld”);
eroboTex=Content.Load(@“Sprites\robo1\robo1OvwldFront”);
basic=Content.Load(@“Fonts\basic”);
//TODO:使用此.Content在此处加载游戏内容
}
///
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
///
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
///
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
///
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//TODO:在此处添加更新逻辑
开关(游戏状态)
{
案例游戏状态。战斗:
打破
}
更新(游戏时间);
}
///
///这就是所谓的比赛应该平局的时候。
///
///提供计时值的快照。
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();
//TODO:在此处添加图形代码
如果((游戏状态==游戏状态.Battle))
{
spriteBatch.抽绳(
基本的,
“ASDFASDFADFASDFA”,文本位置,颜色。黑色);
spriteBatch.画图(
dinoTex,文本位置,颜色。白色);
}
spriteBatch.End();
基础。抽签(游戏时间);
}
}
}
在我的Game1代码中,我可以成功地在屏幕上绘制精灵和文本。然而,我想在另一节课上画一个精灵,但我完全不知道该放什么
在另一节课上,我希望我能做一些简单的事情
Draw(//这里是精灵所在位置的x和y坐标,纹理的名称),就是这样,但似乎不是这样。有人知道我该怎么做吗?它确实是这样工作的。将特定于sprite的代码提取到一个类中,该类的结构类似于:
public Sprite()
{
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch spriteBatch)
{
}
然后在game1类中,您可以调用
Sprite.Update(gameTime);
及
这应该能帮助你开始。您是否自己尝试过一些方法,如果是的话,发布它们,我们可以看到它们哪里出了问题,或者您只是希望得到一个可以复制和粘贴的快速答案?我删除了大部分方法,因为它们不起作用,但我尝试了Game1.Draw(spritename)之类的方法,或者我尝试编写自己的绘图代码时尝试了更长的时间,但是我没有更新,所以也没用。谢谢你的时间和帮助。我现在还不知道我应该做什么。我创建了一个名为SpriteDraw的类,在其中我有公共SpriteDraw(Vector2位置,Texture2D纹理){}公共无效更新(游戏时间游戏时间){}公共无效绘制(SpriteBatch SpriteBatch){}我不知道在游戏1中我应该把SpiteDraw.Draw(spriteBatch)放在哪里;还有SpriteDraw.Update(游戏时间),因为它每次都给我一个错误。我试着通过输入inspriteDraw=newspritedraw(newvector2(0,30),newtexture2d“@/Sprites/dino/dinobackOvwld.png”)将其放入LoadContent;但这似乎不起作用,尽管我不知道如何让它显示精灵。我是不是通过另一个不是SpriteDraw的类调用它?在LoadContent中,我将spriteDraw=newspritedraw(textLocation,robotTex);我将spriteDraw.Draw(spriteBatch)放在“if((gameState=GameStates.Battle))”代码中,并将更新代码放在“case GameStates.Battle:”从GameObject派生您的精灵”下面
Sprite.Draw(spriteBatch);