如何使用C#在MonoGame中移动我的精灵

本文关键字:移动 我的 精灵 MonoGame 何使用 | 更新日期: 2023-09-27 17:58:14

首先,我对编程还很陌生,大约2个月后,我涉猎了控制台应用程序、WinForms,并且仍然使用丑陋的控制台来更好地处理算法。但现在,我想开始深入研究游戏编程,因为这就是我想学习编程的原因。我偶然发现了MonoGame,虽然这比说Unity更难,但在仅仅使用代码创建了一些东西后,我获得了巨大的成就感。我已经制作了《太空入侵者》和《乒乓球》,但与精灵动画无关,使用精灵表和移动玩家。所以两天前,我启动了一个平台生成器,划分了我的精灵表,制作了一些动画,现在是时候移动我的玩家了,我完全迷失了方向。我试着阅读一些关于矢量的教程,但对我的情况没有帮助。也许你能对此事有所了解。

所以,事不宜迟,下面是代码:

Game.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace MafiaJohnny
{
public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    private JohnnyPlayer johnnyPlayer;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }
    protected override void Initialize()
    {
        base.Initialize();
    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        Texture2D texture = Content.Load<Texture2D>("JohnnyDone");
        johnnyPlayer = new JohnnyPlayer(texture, 2, 4);
    }
    protected override void UnloadContent()
    {
    }
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();
        johnnyPlayer.Update(gameTime);
        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.White);
        johnnyPlayer.Draw(spriteBatch, new Vector2(200, 200));
        base.Draw(gameTime);
    }
}
}

JohnnyPlayer.cs

using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
namespace MafiaJohnny
{
class JohnnyPlayer
{
    public Texture2D Texture { get; set; }
    public int Rows { get; set; }
    public int Columns { get; set; }
    private int currentFrame;
    private int totalFrames;
    //Slow down frame animation
    private int timeSinceLastFrame = 0;
    private int millisecondsPerFrame = 400;
    public JohnnyPlayer(Texture2D texture, int rows, int columns)
    {
        Texture = texture;
        Rows = rows;
        Columns = columns;
        currentFrame = 0;
        totalFrames = Rows * Columns;
    }
    public void Update (GameTime gameTime)
    {
        timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
        if (timeSinceLastFrame > millisecondsPerFrame)
        {
            timeSinceLastFrame -= millisecondsPerFrame;
            KeyboardState keystate = Keyboard.GetState();
            //Idle animation
            if (keystate.GetPressedKeys().Length == 0)
            currentFrame++;
            timeSinceLastFrame = 0;
            if (currentFrame == 2)
                currentFrame = 0;
            //Walking Animation
            if (keystate.IsKeyDown(Keys.Left))
            {
            }
        }
    }
    public void Draw (SpriteBatch spriteBatch, Vector2 location)
    {
        int width = Texture.Width/Columns;
        int height = Texture.Height / Rows;
        int row = (int) ((float) currentFrame/Columns);
        int column = currentFrame % Columns;
        Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
        Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);
        spriteBatch.Begin();
        spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
        spriteBatch.End();
    }
}
}

这是我的密码,给我找个答案小黄人!谢谢就是我的意思:)

如何使用C#在MonoGame中移动我的精灵

您只需要更改"位置",使精灵向左/向右/向上/向下移动。此外,我建议将此代码从JohnnyPlayer移到另一个"控制器"类。

此处:http://www.gamefromscratch.com/post/2015/06/15/MonoGame-Tutorial-Creating-an-Application.aspx

他们制作一个精灵,并将其从左向右移动。在您的情况下,精灵上的纹理会随着时间(动画)而变化,但移动仍然相同。