private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
private Rectangle _viewPortRectangle; // private Texture2D _background; // protected override void LoadContent() { <... skip ...> // _viewPortRectangle = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); _background = Content.Load<Texture2D>( @"background" ); <... skip ...> } * This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- protected override void Draw (GameTime gameTime)
- {
- GraphicsDevice.Clear (Color.CornflowerBlue);
- spriteBatch.Begin (SpriteBlendMode.AlphaBlend);
- // Draw the background
- spriteBatch.Draw (_background, _viewPortRectangle, Color.White);
- spriteBatch.End ();
- base .Draw (gameTime);
- }
* This source code was highlighted with Source Code Highlighter .
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework. Graphics ;
- namespace Arkanoid
- {
- public class GameObject
- {
- public Texture2D Sprite { get ; set ; } // Sprite
- public Vector2 Position; // Position
- public Vector2 Velocity; // speed
- public int Width { get { return Sprite.Width; }} // Width
- public int Height { get { return Sprite.Height; }} // Height
- public bool IsAlive { get ; set ; } // object is alive
- public Rectangle Bounds // Object Borders
- {
- get
- {
- return new Rectangle (( int ) Position.X, ( int ) Position.Y, Width, Height);
- }
- }
- // Expand motion on the horizontal axis
- public void ReflectHorizontal ()
- {
- Velocity.Y = -Velocity.Y;
- }
- // Spreading on the vertical axis
- public void ReflectVertical ()
- {
- Velocity.X = -Velocity.X;
- }
- public GameObject (Texture2D sprite)
- {
- Sprite = sprite;
- IsAlive = true ;
- Position = Vector2.Zero;
- Velocity = Vector2.Zero;
- }
- }
- }
* This source code was highlighted with Source Code Highlighter .
- private GameObject _paddle; // racket
- protected override void LoadContent ()
- {
- <... skip ...>
- // Create a racket, the starting position in the middle of the playing field, above the bottom edge
- _paddle = new GameObject (Content.Load <Texture2D> ( @ "paddle" ));
- _paddle.Position = new Vector2 ((_ viewPortRectangle.Width - _paddle.Width) / 2,
- _viewPortRectangle.Height - _paddle.Height - 20);
- <... skip ...>
- }
* This source code was highlighted with Source Code Highlighter .
- protected override void Draw (GameTime gameTime)
- {
- <... skip ...>
- spriteBatch.Draw (_paddle.Sprite, _paddle.Position, Color.White);
- <... skip ...>
- }
* This source code was highlighted with Source Code Highlighter .
- protected override void Update (GameTime gameTime)
- {
- <... skip ...>
- KeyboardState keyboardState = Keyboard.GetState ();
- // Move the racket to the right
- if (keyboardState.IsKeyDown (Keys.Right))
- _paddle.Position.X + = 6f;
- // Move the racket to the left
- if (keyboardState.IsKeyDown (Keys.Left))
- _paddle.Position.X - = 6f;
- // Restrict the movement of the racket to the playing field
- _paddle.Position.X = MathHelper.Clamp (_paddle.Position.X, 0, _viewPortRectangle.Width - _paddle.Width);
- <... skip ...>
- }
* This source code was highlighted with Source Code Highlighter .
- private int _brickPaneWidth = 10; // How many Cypriots draw in width
- private int _brickPaneHeight = 5; // How many Cypriots draw in height
- private Texture2D _brickSprite; // Sprite bricks
- private GameObject [,] _bricks; // Array of bricks
* This source code was highlighted with Source Code Highlighter .
- protected override void LoadContent ()
- {
- <... skip ...>
- // Create an array of bricks
- _brickSprite = Content.Load <Texture2D> ( @ "brick" );
- _bricks = new GameObject [_brickPaneWidth, _brickPaneHeight];
- for ( int i = 0; i <_brickPaneWidth; i ++)
- {
- for ( int j = 0; j <_brickPaneHeight; j ++)
- {
- _bricks [i, j] = new GameObject (_brickSprite)
- {
- Position = new Vector2 (i * 55 + 120, j * 25 + 100)
- };
- }
- }
- <... skip ...>
- }
* This source code was highlighted with Source Code Highlighter .
- protected override void Draw (GameTime gameTime)
- {
- <... skip ...>
- // Draw Bricks
- foreach ( var brick in _bricks)
- if (brick.IsAlive)
- spriteBatch.Draw (brick.Sprite, brick.Position, Color.White);
- <... skip ...>
- }
* This source code was highlighted with Source Code Highlighter .
- private GameObject _ball; // Ball
- protected override void LoadContent ()
- {
- <... skip ...>
- // Making the ball, the starting position in the middle of the racket,
- // starting direction - right, up
- _ball = new GameObject (Content.Load <Texture2D> ( @ "ball" ));
- _ball.Position = new Vector2 ((_ viewPortRectangle.Width - _ball.Width) / 2,
- _viewPortRectangle.Height - _paddle.Height - _ball.Height - 20);
- _ball.Velocity = new Vector2 (3, -3);
- <... skip ...>
- }
* This source code was highlighted with Source Code Highlighter .
- private void UpdateBall ()
- {
- _ball.Position + = _ball.Velocity;
- }
- protected override void Update (GameTime gameTime)
- {
- <... skip ...>
- // Move the ball
- UpdateBall ();
- <... skip ...>
- }
* This source code was highlighted with Source Code Highlighter .
- protected override void Draw (GameTime gameTime)
- {
- <... skip ...>
- // Draw a ball
- spriteBatch.Draw (_ball.Sprite, _ball.Position, Color.White);
- <... skip ...>
- }
* This source code was highlighted with Source Code Highlighter .
- // Determination of the side of the collision and reflection of the ball's direction of flight
- public void Collide (GameObject gameObject, Rectangle rect2)
- {
- // Object collided from above or below, we reflect the direction of flight horizontally
- if (rect2.Left <= gameObject.Bounds.Center.X && gameObject.Bounds.Center.X <= rect2.Right)
- gameObject.ReflectHorizontal ();
- // Object collided to the left or to the right, reflect the direction of the flight vertically
- else if (rect2.Top <= gameObject.Bounds.Center.Y && gameObject.Bounds.Center.Y <= rect2.Bottom)
- gameObject.ReflectVertical ();
- }
* This source code was highlighted with Source Code Highlighter .
- private void UpdateBall ()
- {
- // The future position of the ball is needed to prevent the ball from sticking on the object's surface
- Rectangle nextRect = new Rectangle (( int ) (_ ball.Position.X + _ball.Velocity.X),
- ( int ) (_ ball.Position.Y + _ball.Velocity.Y),
- _ball.Width, _ball.Height);
- // Facing the top edge of the playing field
- if (nextRect.Y <= 0)
- _ball.ReflectHorizontal ();
- // When pushing the ball against the bottom edge of the playing field, the ball "dies"
- if (nextRect.Y> = _viewPortRectangle.Height - nextRect.Height)
- {
- _ball.IsAlive = false ;
- }
- // Collision of the ball with the left or right edge of the playing field
- if ((nextRect.X> = _viewPortRectangle.Width - nextRect.Width) || nextRect.X <= 0)
- {
- _ball.ReflectVertical ();
- }
- // Collision of the ball with the racket
- if (nextRect.Intersects (_paddle.Bounds))
- Collide (_ball, _paddle.Bounds);
- // Collision of a ball with bricks
- foreach ( var brick in _bricks)
- {
- if (nextRect.Intersects (brick.Bounds) && brick.IsAlive)
- {
- brick.IsAlive = false ;
- Collide (_ball, brick.Bounds);
- }
- }
- _ball.Position + = _ball.Velocity;
- }
Source: https://habr.com/ru/post/77864/
All Articles