+ Game UI

This commit is contained in:
2022-10-20 09:48:48 +02:00
parent 1d93d70024
commit 711d0d4e36
8 changed files with 183 additions and 60 deletions
+30 -30
View File
@@ -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;
}
}
+7
View File
@@ -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
+60
View File
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
<Asset Type="Graphics:FontDescription">
<!--
Modify this string to change the font that will be imported.
-->
<FontName>Arial</FontName>
<!--
Size is a float value, measured in points. Modify this value to change
the size of the font.
-->
<Size>38</Size>
<!--
Spacing is a float value, measured in pixels. Modify this value to change
the amount of spacing in between characters.
-->
<Spacing>0</Spacing>
<!--
UseKerning controls the layout of the font. If this value is true, kerning information
will be used when placing characters.
-->
<UseKerning>true</UseKerning>
<!--
Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
and "Bold, Italic", and are case sensitive.
-->
<Style>Bold</Style>
<!--
If you uncomment this line, the default character will be substituted if you draw
or measure text that contains characters which were not included in the font.
-->
<!-- <DefaultCharacter>*</DefaultCharacter> -->
<!--
CharacterRegions control what letters are available in the font. Every
character from Start to End will be built and made available for drawing. The
default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
character set. The characters are ordered according to the Unicode standard.
See the documentation for more information.
-->
<CharacterRegions>
<CharacterRegion>
<Start>&#32;</Start>
<End>&#126;</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
+13 -1
View File
@@ -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<SpriteFont>("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);
+8 -7
View File
@@ -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();
}
+22 -22
View File
@@ -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;
+41
View File
@@ -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);
}
}
}
+2
View File
@@ -22,6 +22,8 @@
<PackageReference Include="MonoGame.Extended" Version="3.8.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="MonoGame.UI.Forms" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="Restore">
<Message Text="Restoring dotnet tools" Importance="High" />