youtube:monogame-ball
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
youtube:monogame-ball [2024/02/28 18:26] – 削除 - 外部編集 (不明な日付) 127.0.0.1 | youtube:monogame-ball [2024/02/28 18:36] (現在) – freemikan | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== 画面を跳ね回るボール ====== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== Game1.cs ===== | ||
+ | |||
+ | <file csharp> | ||
+ | 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 = " | ||
+ | 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< | ||
+ | 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, | ||
+ | _ball = new Ball(ballPosition, | ||
+ | } | ||
+ | |||
+ | protected override void Update(GameTime gameTime) | ||
+ | { | ||
+ | if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) | ||
+ | Exit(); | ||
+ | |||
+ | _ball.Update(gameTime, | ||
+ | |||
+ | 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, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Program.cs ===== | ||
+ | |||
+ | |||
+ | <file csharp> | ||
+ | using var game = new MyBallGame.Game1(); | ||
+ | game.Run(); | ||
+ | </ | ||
+ | |||