Creating A C# & XNA 'Monster Dash' Like Ga

2019-07-16 11:31发布

I decided that my last year C# project would be a 'Monster Dash' like game, I would develop it using C# and XNA, and it would be targeted for PCs, I am using Microsoft Visual Studio 2010, and XNA Game Studio 4.0. The game would be a 2D game, not 3D.

Thinking on the development process, a few problems rise up:

First, I need to create a 'sort-of-platforms' which the player will run on and will have holes and gaps between them (which will appear randomly) and I have no idea how to do that! The only close thing I've found that might explain that is an sample code called 'Platformer' from microsoft, which I did not understand (it would be nice of someone could explain the method they use there, or a better / more simple way).

Second, I would need the player to jump (in order to avoid the gaps and holes and continue running) and I have no idea how to approach the physics part of that feature, I would be really happy if someone could suggest a way to handle that, or point to a useful piece of code.

Thanks ahead, iLyrical.

2条回答
叼着烟拽天下
2楼-- · 2019-07-16 11:47

The platformer example on the Microsoft website is a good place to start.

I made something similar a while back somewhat based on that example. Its a tiled based approach where you have a 2 dimensional list of "Tiles" (A list of lists of tiles List>) You set a static size for the tiles (say 16x16 pixels), then it's just a matter of looping through the list to draw/do collision detection, etc. Just something to think about, if I think about it I can post some code later if you wanted, it's at home.

A simple way I usually approach movement physics like that is to use 3 Vector2 objects, one for position, one for velocity and one for acceleration. If you're familiar with simple physics, position = velocity * deltaTime, velocity = acceleration * deltaTime. When a player jumps you just increase the players acceleration, then in the update loop, calculate the position:

// On Jump
player.acceleration += someConstant

// On Update
this.velocity += this.acceleration * deltaTime
this.position += this.velocity * deltaTime

Again, I'll post some real code later.

Hope this helps get you started

Edit:

Here's some movement code

public override void Update(GameTime gameTime)
    {
        float deltaTime = ((float)gameTime.ElapsedGameTime.Milliseconds) / 1000f;
        currentState = Keyboard.GetState();

        if (canMove)
        {

            // Input
            if (currentState.IsKeyDown(Keys.Left))
                Acceleration.X -= 1000;
            if (currentState.IsKeyDown(Keys.Right))
                Acceleration.X += 1000;
            if (!airbourne && currentState.IsKeyDown(Keys.Space) && previousState.IsKeyUp(Keys.Space))
            {
                Acceleration.Y -= 25000;
                airbourne = true;
            }

            // Friction in X to limit sliding
            if (Velocity.X > 0)
            {
                Velocity.X -= X_FRICTION;
                if (Velocity.X < 0)
                    Velocity.X = 0;
            }
            else
            {
                Velocity.X += X_FRICTION;
                if (Velocity.X > 0)
                    Velocity.X = 0;
            }

            // Gravity
            Acceleration.Y += 500;
        }

        Velocity += Acceleration * deltaTime;
        if (Velocity.X > 0)
            Velocity.X += speedMod;
        else if (Velocity.X < 0)
            Velocity.X -= speedMod;

        // Move and check collisions in X
        Position.X += Velocity.X * deltaTime;
        if (game.checkCollisions(boundingBox()))
        {
            Position.X -= Velocity.X * deltaTime;
            Velocity.X = 0;
        }

        // Move and check collisions in Y
        Position.Y += Velocity.Y * deltaTime;
        movingUp = Velocity.Y < 0;
        if (game.checkCollisions(boundingBox()))
        {
            // If moving downwards, player not airbourne
            if (Velocity.Y >= 0)
            {
                airbourne = false;
                Position.Y = game.getCollisionDistance(boundingBox()) - 32;
            }
            else
            {
                Position.Y = game.getCollisionDistance(boundingBox()) + 32;
            }

            Velocity.Y = 0;
        }
        else
            airbourne = true;

        movingUp = false;

        // Reset acceleration
        Acceleration = Vector2.Zero;
        previousState = currentState;

    }
查看更多
孤傲高冷的网名
3楼-- · 2019-07-16 11:59

Make no mistake, just because it is 2D, a platformer is not an easy task. So i would suggest you use any third party library help you can get, using XNA and C# is already a good starting point. Next thing is, and this is where your questions come in, how to solve the physics? You could write it on your own, which is a nice learning experience but if time is crucial it can be difficult and frustrating. So i would suggest using a third party lib for that, like Farseer. Maybe this little tutorial comes in handy aswell.

But again, this is definitely not an easy task. Depending on your Skill, which i of course don't know, i would suggest an easier game, if it must be a game at all. Games are propably the most difficult programming task you can imagine. Dealing with multiple subsystems simultaneously (Graphics, AI, Sound, Input, Physics); making them work together is already a huge task, but having the content (Sprites, Wavs, Music, Menu gfx, etc.) is another beast on its own.

A final advise is, if you don't understand the Platformer code; read it again, and again; if you don't understand certain parts read about them until you understand them. If you have problems with c# first learn it and never stop to learn it. Also read as many tutorials and code about games you can find. It is important to see how and why are other people solving problems the way they do.

查看更多
登录 后发表回答