Game Architecture

后端 未结 4 1528
抹茶落季
抹茶落季 2020-12-07 17:58

I have a question about a XNA game I\'m making, but it is also a generic question for future games. I\'m making a Pong game and I don\'t know exactly what to update where, s

4条回答
  •  太阳男子
    2020-12-07 18:42

    You could also opt to start thinking about how different components of the game need to talk to each other.

    Ball and Paddle, both are objects in the game and in this case, Renderable, Movable objects. The Paddle has the following criteria

    It can only move up and down

    1. The paddle is fixed to one side of the screen, or to the bottom
    2. The Paddle might be controlled by the user (1 vs Computer or 1 vs 1)
    3. The paddle can be rendered
    4. The paddle can only be moved to the bottom or the top of the screen, it cannot pass it's boundaries

    The ball has the following criteria

    It cannot leave the boundaries of the screen

    1. It can be rendered
    2. Depending on where it gets hit on the paddle, you can control it indirectly (Some simple physics)
    3. If it goes behind the paddle the round is finished
    4. When the game is started, the ball is generally attached to the Paddle of the person who lost.

    Identifying the common criteria you can extract an interface

    public interface IRenderableGameObject
    {
       Vector3 Position { get; set; }
       Color Color { get; set; }
       float Speed { get; set; }
       float Angle { get; set; }
    }
    

    You also have some GamePhysics

    public interface IPhysics
    {
        bool HasHitBoundaries(Window window, Ball ball);
        bool HasHit(Paddle paddle, Ball ball);
        float CalculateNewAngle(Paddle paddleThatWasHit, Ball ball);
    }
    

    Then there is some game logic

    public interface IGameLogic
    {
       bool HasLostRound(...);
       bool HasLostGame(...);
    }
    

    This is not all the logic, but it should give you an idea of what to look for, because you are building a set of Libraries and functions that you can use to determine what is going to happen and what can happen and how you need to act when those things happen.

    Also, looking at this you can refine and refactor this so that it's a better design.

    Know your domain and write your ideas down. Failing to plan is planning to fail

提交回复
热议问题