diff --git a/client/Ball.cs b/client/Ball.cs index 2ed1c43..7742225 100644 --- a/client/Ball.cs +++ b/client/Ball.cs @@ -13,7 +13,7 @@ namespace client internal class Ball { public Vector2 Position = new Vector2(0, 0); - public Vector2 DefaultPosition = new Vector2(0,0); + public Vector2 DefaultPosition = new Vector2(0, 0); public int Radius = 5; public bool CanMove = false; @@ -31,16 +31,16 @@ namespace client public Ball(Vector2 position, int radius, float speed) { - this.Position = position; - this.DefaultPosition = position; - this.Radius = radius; - this.Speed = speed; + Position = position; + DefaultPosition = position; + Radius = radius; + Speed = speed; } // Default Position public Ball GetBackToDefaultPos() { - this.Position = this.DefaultPosition; + Position = DefaultPosition; return this; } @@ -49,66 +49,66 @@ namespace client { spriteBatch.DrawCircle( - this.Position, - this.Radius, + Position, + Radius, 360, Color.White, - this.Radius - ) ; + Radius + ); return this; } // Start | Stop ball moving public Ball StartMoving() { - this.MoveX = random.Next((int)-this.Speed, (int)this.Speed); - this.MoveY = random.Next((int)-this.Speed, (int)this.Speed); - this.CanMove = true; + MoveX = random.Next((int)-Speed, (int)Speed); + MoveY = random.Next((int)-Speed, (int)Speed); + CanMove = true; return this; } public Ball Move(GameTime gameTime, float screenHeight, float screenWidth, Player p1, Player p2) { - if (this.CanMove) + if (CanMove) { // X - if (this.Position.X < 0) + if (Position.X < 0) { - this.MoveX -= this.Speed; + MoveX -= Speed; } else { - this.MoveX += this.Speed; + MoveX += Speed; } // Y - if (this.Position.Y < 0) + if (Position.Y < 0) { - this.MoveY -= this.Speed; + MoveY -= Speed; } else { - this.MoveY += this.Speed; + MoveY += Speed; } - this.Position.X += this.MoveX; - this.Position.Y += this.MoveY; + Position.X += MoveX; + Position.Y += MoveY; // Collisions - if (this.Position.Y < this.Radius*2 && this.MoveY < 0) + if (Position.Y < Radius * 2 && MoveY < 0) { - this.MoveY = Math.Abs(this.MoveY); + MoveY = Math.Abs(MoveY); } - if (this.Position.Y > screenHeight - this.Radius*2 && this.MoveY > 0) + if (Position.Y > screenHeight - Radius * 2 && MoveY > 0) { - this.MoveY = -this.MoveY; + MoveY = -MoveY; } if ( - (this.Position.X < p1.Position.X + p1.width && this.Position.X > p1.Position.X && this.Position.Y > p1.Position.Y && this.Position.Y < p1.Position.Y + p1.height) || - (this.Position.X+ this.Radius*2 > p2.Position.X && this.Position.X + this.Radius*2 < p2.Position.X + p2.width && this.Position.Y > p2.Position.Y && this.Position.Y < p2.Position.Y + p2.height) + Position.X < p1.Position.X + p1.width && Position.X > p1.Position.X && Position.Y > p1.Position.Y && Position.Y < p1.Position.Y + p1.height || + Position.X + Radius * 2 > p2.Position.X && Position.X + Radius * 2 < p2.Position.X + p2.width && Position.Y > p2.Position.Y && Position.Y < p2.Position.Y + p2.height ) { - this.MoveX = -this.MoveX; + MoveX = -MoveX; } } @@ -118,7 +118,7 @@ namespace client public Ball StopMoving() { - this.CanMove = false; + CanMove = false; return this; } } diff --git a/client/Content/Content.mgcb b/client/Content/Content.mgcb index dd1814b..879e5f3 100644 --- a/client/Content/Content.mgcb +++ b/client/Content/Content.mgcb @@ -25,3 +25,10 @@ /processorParam:TextureFormat=Color /build:ball.png +#begin defaultFont.spritefont +/importer:FontDescriptionImporter +/processor:FontDescriptionProcessor +/processorParam:PremultiplyAlpha=True +/processorParam:TextureFormat=Compressed +/build:defaultFont.spritefont;scoreFont.spritefont + diff --git a/client/Content/defaultFont.spritefont b/client/Content/defaultFont.spritefont new file mode 100644 index 0000000..d4f3991 --- /dev/null +++ b/client/Content/defaultFont.spritefont @@ -0,0 +1,60 @@ + + + + + + + Arial + + + 38 + + + 0 + + + true + + + + + + + + + + + + ~ + + + + diff --git a/client/Game1.cs b/client/Game1.cs index 3d31fd6..15e8ac5 100644 --- a/client/Game1.cs +++ b/client/Game1.cs @@ -12,6 +12,9 @@ namespace client private KillZone killZone1; private KillZone killZone2; + private UI UI; + private SpriteFont spriteFont; + private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; @@ -24,6 +27,12 @@ namespace client protected override void Initialize() { + // Window + Window.Title = "Pong"; + + // UI + UI = new UI(); + // PLAYERS pl1 = new Player( new Vector2(15, _graphics.PreferredBackBufferHeight / 2 - 50), @@ -31,7 +40,7 @@ namespace client 100, 100f ).SetControllable(true); - + pl2 = new Player( new Vector2(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50), 15, @@ -70,6 +79,7 @@ namespace client protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); + spriteFont = Content.Load("scoreFont"); } protected override void Update(GameTime gameTime) @@ -89,6 +99,8 @@ namespace client GraphicsDevice.Clear(Color.Black); _spriteBatch.Begin(); + UI.Draw(_spriteBatch, _graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight, spriteFont, 0, 0); + pl1.DrawPlayer(_spriteBatch, GraphicsDevice); pl2.DrawPlayer(_spriteBatch, GraphicsDevice); diff --git a/client/KillZone.cs b/client/KillZone.cs index 4fa61c0..807047f 100644 --- a/client/KillZone.cs +++ b/client/KillZone.cs @@ -27,34 +27,35 @@ namespace client public void Draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice) { - Texture2D texture = new Texture2D(graphicsDevice, this.Width, this.Height); - Color[] data = new Color[this.Width * this.Height]; + Texture2D texture = new Texture2D(graphicsDevice, Width, Height); + Color[] data = new Color[Width * Height]; for (int i = 0; i < data.Length; ++i) { if (Debug) { data[i] = Color.Red; - } else + } + else { data[i] = Color.Transparent; } } texture.SetData(data); - spriteBatch.Draw(texture, this.Position, Color.White); + spriteBatch.Draw(texture, Position, Color.White); } public void Collisions(Ball ball) { - if (this.isLeft) + if (isLeft) { - if (ball.Position.X < this.Position.X + this.Width && ball.CanMove) + if (ball.Position.X < Position.X + Width && ball.CanMove) { ball.StopMoving(); } } else { - if (ball.Position.X > this.Position.X && ball.CanMove) + if (ball.Position.X > Position.X && ball.CanMove) { ball.StopMoving(); } diff --git a/client/Player.cs b/client/Player.cs index 543d9fc..62ba395 100644 --- a/client/Player.cs +++ b/client/Player.cs @@ -22,17 +22,17 @@ namespace client // Constructors public Player() { - this.Position = new Vector2(0, 0); - this.DefaultPosition = new Vector2(0, 0); - this.width = 20; - this.height = 100; - this.speed = 100f; + Position = new Vector2(0, 0); + DefaultPosition = new Vector2(0, 0); + width = 20; + height = 100; + speed = 100f; } public Player(Vector2 position, int width, int height, float speed) { - this.Position = position; - this.DefaultPosition = position; + Position = position; + DefaultPosition = position; this.width = width; this.height = height; this.speed = speed; @@ -41,73 +41,73 @@ namespace client // Update the player's position public Player setPos(Vector2 pos) { - this.Position = pos; + Position = pos; return this; } // Set | Get default player position public Player setDefaultPos(Vector2 pos) { - this.DefaultPosition = pos; + DefaultPosition = pos; return this; } public Vector2 getDefaultPos() { - return this.DefaultPosition; + return DefaultPosition; } public Player BackToDefaultPos() { - this.Position = this.DefaultPosition; + Position = DefaultPosition; return this; } // Set controllable by input public Player SetControllable(bool state) { - this.canMove = state; + canMove = state; return this; } // Draw player on screen public Player DrawPlayer(SpriteBatch spriteBatch, GraphicsDevice _graphicsDevice) { - Texture2D texture = new Texture2D(_graphicsDevice, this.width, this.height); - Color[] data = new Color[this.width * this.height]; + Texture2D texture = new Texture2D(_graphicsDevice, width, height); + Color[] data = new Color[width * height]; for (int i = 0; i < data.Length; ++i) { data[i] = Color.White; } texture.SetData(data); - spriteBatch.Draw(texture, this.Position, Color.White); + spriteBatch.Draw(texture, Position, Color.White); return this; } // Inputs controls public Player InputsControls(GameTime gameTime, float screenHeight) { - if (this.canMove) + if (canMove) { if (Keyboard.GetState().IsKeyDown(Keys.Up)) { - Vector2 asked = new Vector2(this.Position.X, this.Position.Y - this.speed * (float)gameTime.ElapsedGameTime.TotalSeconds); + Vector2 asked = new Vector2(Position.X, Position.Y - speed * (float)gameTime.ElapsedGameTime.TotalSeconds); if (asked.Y <= 0) { asked.Y = 0f; } - this.Position = asked; + Position = asked; } if (Keyboard.GetState().IsKeyDown(Keys.Down)) { - Vector2 asked = new Vector2(this.Position.X, this.Position.Y + this.speed * (float)gameTime.ElapsedGameTime.TotalSeconds); - if (asked.Y >= screenHeight - this.height) + Vector2 asked = new Vector2(Position.X, Position.Y + speed * (float)gameTime.ElapsedGameTime.TotalSeconds); + if (asked.Y >= screenHeight - height) { - asked.Y = screenHeight - this.height; + asked.Y = screenHeight - height; } - this.Position = asked; + Position = asked; } } return this; diff --git a/client/UI.cs b/client/UI.cs new file mode 100644 index 0000000..49023e8 --- /dev/null +++ b/client/UI.cs @@ -0,0 +1,41 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace client +{ + internal class UI + { + public UI() + { + + } + + public void Draw(SpriteBatch spriteBatch, float screenWidth, float screenHeight, SpriteFont spriteFont, int score1, int score2) + { + // Lines + int divider = 21; + for(int i = 0; i < screenHeight / divider; i++) + { + if (i % 2 == 0) + { + spriteBatch.DrawLine( + new Vector2(screenWidth / 2, screenHeight / divider * i), + new Vector2(screenWidth / 2, screenHeight / divider * (i+1)), + Color.White + ); + } + } + + // Scores + int spacing = 25; + spriteBatch.DrawString(spriteFont, $"{score1}", new Vector2(screenWidth / 2 - spriteFont.MeasureString($"{score1}").X - spacing, 5), Color.LightGray); + spriteBatch.DrawString(spriteFont, $"{score2}", new Vector2(screenWidth / 2 + spacing, 5), Color.LightGray); + } + } +} diff --git a/client/client.csproj b/client/client.csproj index a642392..32e5c5c 100644 --- a/client/client.csproj +++ b/client/client.csproj @@ -22,6 +22,8 @@ + +