ユーザ用ツール

サイト用ツール


youtube:monogame-ball

画面を跳ね回るボール

Game1.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MyBallGame;

public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;
    private Ball _ball;

    const int GAME_WIDTH = 320;
    const int GAME_HEIGHT = 480;

    public Game1()
    {
        _graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;

        _graphics.PreferredBackBufferWidth = GAME_WIDTH;
        _graphics.PreferredBackBufferHeight = GAME_HEIGHT;
        _graphics.ApplyChanges();
    }

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    protected override void LoadContent()
    {
        _spriteBatch = new SpriteBatch(GraphicsDevice);

        var ballTexture = Content.Load<Texture2D>("Circle");
        var ballRadius = ballTexture.Width / 2.0f;
        var ballPosition = new Vector2(GAME_WIDTH / 2.0f - ballRadius, GAME_HEIGHT / 2.0f - ballRadius);
        var ballVelocity = new Vector2(300.0f, 300.0f);
        _ball = new Ball(ballPosition, ballRadius, ballVelocity, ballTexture);
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        _ball.Update(gameTime, GAME_WIDTH, GAME_HEIGHT);

        base.Update(gameTime);
    }

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

        _spriteBatch.Begin();
        _ball.Draw(_spriteBatch);
        _spriteBatch.End();

        base.Draw(gameTime);
    }
}

class Ball
{
    private Vector2 _position;
    private float _radius;
    private Vector2 _velocity;
    private Texture2D _texture;

    public Ball(Vector2 position, float radius, Vector2 velocity, Texture2D texture)
    {
        _position = position;
        _radius = radius;
        _velocity = velocity;
        _texture = texture;
    }

    public void Update(GameTime gameTime, int gameWidth, int gameHeight)
    {
        float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
        _position = _position + _velocity * deltaTime;

        if (_position.X <= 0.0f || _position.X + _radius * 2.0f >= gameWidth)
        {
            _velocity.X *= -1.0f;
        }

        if (_position.Y <= 0.0f || _position.Y + _radius * 2.0f >= gameHeight)
        {
            _velocity.Y *= -1.0f;
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(_texture, _position, Color.Green);
    }
}

Program.cs

using var game = new MyBallGame.Game1();
game.Run();

youtube/monogame-ball.txt · 最終更新: 2024/02/28 18:36 by freemikan

特に明示されていない限り、本Wikiの内容は次のライセンスに従います: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki