根据产生的那一侧移动敌人的精灵。 C#单机游戏

时间:2019-09-14 14:11:44

标签: c# monogame

所以我有一个代码,其中精灵在窗口外生成,我希望它沿特定的直线移动,具体取决于它生成的位置,例如,如果精灵在顶部生成,它会在底部出现,反之亦然然后向左生成,它将向右移动,反之亦然。

Game1.cs代码:

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Player player;
    List<Enemy> enemies = new List<Enemy>();


    public Vector2 ecords
    {
        get { return ecords; }
        set { ecords = value; }
    }



    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {


        base.Initialize();
    }


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        //player
        Texture2D playertexture = Content.Load<Texture2D>("Player");
        player = new Player(new Vector2(350, 175), playertexture);

        //enemy
        Texture2D enemytexture = Content.Load<Texture2D>("Enemy");
        Random random = new Random();
        var width = GraphicsDevice.Viewport.Width;
        var height = GraphicsDevice.Viewport.Height;

        for (int i = 0; i < 20; i++)
        {
            enemies.Add(new Enemy(new Vector2(random.Next(width, width + 100), random.Next(0, height)), enemytexture));
            enemies.Add(new Enemy(new Vector2(random.Next(0 - 100, 0), random.Next(0, height)), enemytexture));
            enemies.Add(new Enemy(new Vector2(random.Next(0, width), random.Next(-100, 0)), enemytexture));
            enemies.Add(new Enemy(new Vector2(random.Next(0, width), random.Next(height, height + 100)), enemytexture));
        }





    }

    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();
        foreach (Enemy enemy in enemies)
        {
            enemy.Update(gameTime);
        }
        player.Update(gameTime);



        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        foreach (Enemy enemy in enemies)
        {
            enemy.Draw(spriteBatch);
        }
        player.Draw(spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}

Enemy.cs

class Enemy
{
    Texture2D enemytexture;
    Vector2 enemyposition;


    public Enemy(Vector2 enemyposition, Texture2D enemytexture)
    {
        this.enemyposition = enemyposition;
        this.enemytexture = enemytexture;
    }
    public void Update(GameTime gameTime)
    {

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(enemytexture, enemyposition, Color.White);
    }
}

1 个答案:

答案 0 :(得分:1)

一种选择是将velocity(或您可以将其命名为movement / direction)添加到Enemy,并通过参数传递给构造函数,就像您的现有的enemyposition。然后通过在Update的{​​{1}}方法中添加速度来更新当前位置:

Enemy

您可以使用单位矢量(可选乘以速度)作为速度值:

public void Update(GameTime gameTime)
{
    this.enemyposition += this.velocity;
}