Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System;
- using System.Collections.Generic;
- namespace PingPong
- {
- public class AnimatedSprite : Sprite
- {
- public int Rows, Columns;
- protected int _currentFrame, _totalFrames;
- protected int _width, _height;
- int timeSinceLastFrame = 0;
- int milisecondsPerFrame;
- protected Texture2D explosionTexture;
- protected static readonly TimeSpan explosionInterval = TimeSpan.FromMilliseconds(900);
- protected TimeSpan waitingTime;
- protected Rectangle explosionRectangle;
- protected bool isExplosionVisible = false;
- public AnimatedSprite(Texture2D texture, int rows, int columns, int missingFrames, int msecondsPerFrame, Texture2D explosion)
- : base(texture)
- {
- milisecondsPerFrame = msecondsPerFrame;
- Rows = rows;
- Columns = columns;
- _currentFrame = 0;
- _totalFrames = Rows * Columns - missingFrames;
- _width = texture.Width / Columns;
- _height = texture.Height / Rows;
- explosionTexture = explosion;
- base._textureWidth = _width;
- base._textureHeight = _height;
- explosionRectangle = new Rectangle(0, 0, _width, _height);
- }
- public override void Update(GameTime gameTime, List<Sprite> sprites)
- {
- if (isExplosionVisible)
- {
- waitingTime += gameTime.ElapsedGameTime;
- if (waitingTime > explosionInterval)
- {
- isExplosionVisible = false;
- waitingTime = new TimeSpan();
- }
- }
- timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
- if (timeSinceLastFrame > milisecondsPerFrame)
- {
- timeSinceLastFrame -= milisecondsPerFrame;
- _currentFrame++;
- if (_currentFrame == _totalFrames)
- _currentFrame = 0;
- }
- }
- public override void Draw(SpriteBatch spriteBatch)
- {
- if (isExplosionVisible)
- {
- int row2 = (int)((float)_currentFrame / (float)5);
- int column2 = _currentFrame % 5;
- Rectangle sourceRectangle2 = new Rectangle(_width * column2, _height * row2, _width, _height);
- var lastRect = new Rectangle((int)base.Position.X, (int)base.Position.Y, _width, _height);
- //if (explosionRectangle == Rectangle.Empty)
- // explosionRectangle = new Rectangle((int)base.Position.X, (int)base.Position.Y, _width, _height);
- spriteBatch.Draw(explosionTexture, explosionRectangle, sourceRectangle2, Color.White);
- }
- else
- {
- int row = (int)((float)_currentFrame / (float)Columns);
- int column = _currentFrame % Columns;
- Rectangle sourceRectangle = new Rectangle(_width * column, _height * row, _width, _height);
- Rectangle destinationRectangle = new Rectangle((int)base.Position.X, (int)base.Position.Y, _width, _height);
- spriteBatch.Draw(base._texture, destinationRectangle, sourceRectangle, Color.White);
- }
- }
- }
- public class Ball : AnimatedSprite
- {
- private Vector2? _startPosition = null;
- private float? _startSpeed;
- private bool _isPlaying;
- public Score Score;
- public int explosionRows = 5;
- public int explosionColums = 5;
- public Ball(Texture2D texture, int rows, int columns, Texture2D explosion)
- : base(texture, rows, columns, 9, 16, explosion)
- {
- Speed = 3f;
- }
- public override void Update(GameTime gameTime, List<Sprite> sprites)
- {
- if (!isExplosionVisible)
- {
- if (_startPosition == null)
- {
- _startPosition = Position;
- _startSpeed = Speed;
- Restart();
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Space))
- _isPlaying = true;
- if (!_isPlaying)
- return;
- foreach (var sprite in sprites)
- {
- if (sprite == this)
- continue;
- if (this.Velocity.X > 0 && this.IsTouchingLeft(sprite))
- {
- this.Velocity.X = -this.Velocity.X;
- Speed++;
- sprite.Shining = true;
- }
- if (this.Velocity.X < 0 && this.IsTouchingRight(sprite))
- {
- this.Velocity.X = -this.Velocity.X;
- Speed++;
- sprite.Shining = true;
- }
- if (this.Velocity.Y > 0 && this.IsTouchingTop(sprite))
- this.Velocity.Y = -this.Velocity.Y;
- if (this.Velocity.Y < 0 && this.IsTouchingBottom(sprite))
- this.Velocity.Y = -this.Velocity.Y;
- }
- if (Position.Y <= 0 || Position.Y + base._textureHeight >= Game1.ScreenHeight)
- Velocity.Y = -Velocity.Y;
- if (Position.X <= 0)
- {
- Kaboom();
- base.explosionRectangle.Location = new Point((int)Position.X,(int)Position.Y);
- Score.Score2++;
- Restart();
- }
- if (Position.X + base._textureWidth >= Game1.ScreenWidth)
- {
- Kaboom();
- base.explosionRectangle.Location = new Point((int)Position.X,(int)Position.Y);
- Score.Score1++;
- Restart();
- }
- Position += Velocity * Speed;
- }
- base.Update(gameTime, sprites);
- }
- void Kaboom()
- {
- base.isExplosionVisible = true;
- }
- public void Restart()
- {
- var direction = Game1.Random.Next(0, 4);
- switch (direction)
- {
- case 0:
- Velocity = new Vector2(1, 1);
- break;
- case 1:
- Velocity = new Vector2(1, -1);
- break;
- case 2:
- Velocity = new Vector2(-1, -1);
- break;
- case 3:
- Velocity = new Vector2(-1, 1);
- break;
- }
- Position = (Vector2)_startPosition;
- Speed = (float)_startSpeed;
- _isPlaying = false;
- }
- }
- public class Bat : Sprite
- {
- int timeSinceLastFrame = 0;
- int shiningMiliseconds = 300;
- public Bat(Texture2D texture)
- : base(texture)
- {
- Speed = 5f;
- }
- public override void Update(GameTime gameTime, List<Sprite> sprites)
- {
- if (Keyboard.GetState().IsKeyDown(Input.Up))
- Velocity.Y = -Speed;
- else if (Keyboard.GetState().IsKeyDown(Input.Down))
- Velocity.Y = Speed;
- Position += Velocity;
- Position.Y = MathHelper.Clamp(Position.Y, 0, Game1.ScreenHeight - _texture.Height);
- Velocity = Vector2.Zero;
- if (Shining)
- {
- ShineBat(gameTime);
- }
- }
- void ShineBat(GameTime gameTime)
- {
- base.Color = Color.Yellow;
- timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
- if (timeSinceLastFrame > shiningMiliseconds)
- {
- timeSinceLastFrame -= shiningMiliseconds;
- base.Color = Color.White;
- base.Shining = false;
- }
- }
- }
- public class Input
- {
- public Keys Up;
- public Keys Down;
- }
- public class Score
- {
- public int Score1;
- public int Score2;
- private SpriteFont _font;
- public Score(SpriteFont font)
- {
- _font = font;
- }
- public void Draw(SpriteBatch spriteBatch)
- {
- spriteBatch.DrawString(_font, Score1.ToString(), new Vector2(320, 70), Color.White);
- spriteBatch.DrawString(_font, Score2.ToString(), new Vector2(460, 70), Color.White);
- }
- }
- public class Sprite
- {
- protected Texture2D _texture;
- public Vector2 Position;
- public Vector2 Velocity;
- public float Speed;
- public Input Input;
- int offset = 10;
- protected int _textureWidth, _textureHeight;
- public bool Shining = false;
- public Color Color = Color.White;
- public Rectangle Rectangle
- {
- get
- {
- return new Rectangle((int)Position.X, (int)Position.Y, _textureWidth, _textureHeight);
- }
- }
- public Sprite(Texture2D texture)
- {
- _texture = texture;
- _textureWidth = texture.Width;
- _textureHeight = texture.Height;
- }
- public virtual void Update(GameTime gameTime, List<Sprite> sprites)
- {
- }
- public virtual void Draw(SpriteBatch spriteBatch)
- {
- spriteBatch.Draw(_texture, Position, Color);
- }
- //kolizja
- protected bool IsTouchingLeft(Sprite sprite)
- {
- return this.Rectangle.Right + this.Velocity.X - offset > sprite.Rectangle.Left &&
- this.Rectangle.Left < sprite.Rectangle.Left &&
- this.Rectangle.Bottom > sprite.Rectangle.Top &&
- this.Rectangle.Top < sprite.Rectangle.Bottom;
- }
- protected bool IsTouchingRight(Sprite sprite)
- {
- return this.Rectangle.Left + this.Velocity.X + offset < sprite.Rectangle.Right &&
- this.Rectangle.Right > sprite.Rectangle.Right &&
- this.Rectangle.Bottom > sprite.Rectangle.Top &&
- this.Rectangle.Top < sprite.Rectangle.Bottom;
- }
- protected bool IsTouchingTop(Sprite sprite)
- {
- return this.Rectangle.Bottom + this.Velocity.Y - offset > sprite.Rectangle.Top &&
- this.Rectangle.Top < sprite.Rectangle.Top &&
- this.Rectangle.Right > sprite.Rectangle.Left &&
- this.Rectangle.Left < sprite.Rectangle.Right;
- }
- protected bool IsTouchingBottom(Sprite sprite)
- {
- return this.Rectangle.Top + this.Velocity.Y + offset < sprite.Rectangle.Bottom &&
- this.Rectangle.Bottom > sprite.Rectangle.Bottom &&
- this.Rectangle.Right > sprite.Rectangle.Left &&
- this.Rectangle.Left < sprite.Rectangle.Right;
- }
- }
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- public static int ScreenWidth;
- public static int ScreenHeight;
- public static Random Random;
- private Score _score;
- private List<Sprite> _sprites;
- Texture2D bcgTexture;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize()
- {
- // TODO: Add your initialization logic here
- ScreenWidth = graphics.PreferredBackBufferWidth;
- ScreenHeight = graphics.PreferredBackBufferHeight;
- Random = new Random();
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- bcgTexture = Content.Load<Texture2D>("pongBackground");
- var batTexture = Content.Load<Texture2D>("paddle1a");
- var batTexture2 = Content.Load<Texture2D>("paddle2a");
- var explosionTexture = Content.Load<Texture2D>("explosion64"); //5,5 row
- var ballAnimTexture = Content.Load<Texture2D>("ball-anim");
- _score = new Score(Content.Load<SpriteFont>("font"));
- _sprites = new List<Sprite>()
- {
- new Bat(batTexture)
- {
- Position = new Vector2(20, (ScreenHeight / 2) - (batTexture.Height / 2)),
- Input = new Input()
- {
- Up = Keys.Q,
- Down = Keys.A,
- }
- },
- new Bat(batTexture2)
- {
- Position = new Vector2(ScreenWidth - 20 - batTexture.Width, (ScreenHeight / 2) - (batTexture.Height / 2)),
- Input = new Input()
- {
- Up = Keys.P,
- Down = Keys.L,
- }
- },
- new Ball(ballAnimTexture,5,16,explosionTexture)
- {
- Position = new Vector2((ScreenWidth / 2) - (32), (ScreenHeight / 2) - (32)),
- Score = _score,
- }
- };
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- foreach (var sprite in _sprites)
- {
- sprite.Update(gameTime, _sprites);
- }
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- // TODO: Add your drawing code here
- spriteBatch.Begin();
- spriteBatch.Draw(bcgTexture, GraphicsDevice.Viewport.Bounds, Color.White);
- foreach (var sprite in _sprites)
- sprite.Draw(spriteBatch);
- _score.Draw(spriteBatch);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement