+ Funcdations of Player class

This commit is contained in:
2022-10-17 19:13:52 +02:00
parent 2ca103563f
commit f8c64522b9
2 changed files with 147 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace client
{
public class Game1 : Game
{
private Player pl1;
private Player pl2;
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
// PLAYERS
pl1 = new Player(
new Vector2(15, _graphics.PreferredBackBufferHeight / 2 - 50),
15,
100,
100f
);
pl2 = new Player(
new Vector2(_graphics.PreferredBackBufferWidth - 30, _graphics.PreferredBackBufferHeight / 2 - 50),
15,
100,
100f
);
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void Update(GameTime gameTime)
{
/*if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();*/
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
_spriteBatch.Begin();
pl1.DrawPlayer(_spriteBatch, GraphicsDevice);
pl2.DrawPlayer(_spriteBatch, GraphicsDevice);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
+80
View File
@@ -0,0 +1,80 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
namespace client
{
internal class Player
{
private Vector2 Position { get; set; }
private Vector2 DefaultPosition { get; set; }
public int width;
public int height;
public float speed;
public bool canMove = false;
// Constructors
public Player()
{
this.Position = new Vector2(0, 0);
this.DefaultPosition = new Vector2(0, 0);
this.width = 20;
this.height = 100;
this.speed = 100f;
}
public Player(Vector2 position, int width, int height, float speed)
{
this.Position = position;
this.DefaultPosition = position;
this.width = width;
this.height = height;
this.speed = speed;
}
// Update the player's position
public Player setPos(Vector2 pos)
{
this.Position = pos;
return this;
}
// Set | Get default player position
public Player setDefaultPos(Vector2 pos)
{
this.DefaultPosition = pos;
return this;
}
public Vector2 getDefaultPos()
{
return this.DefaultPosition;
}
public Player BackToDefaultPos()
{
this.Position = this.DefaultPosition;
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];
for (int i = 0; i < data.Length; ++i)
{
data[i] = Color.White;
}
texture.SetData(data);
spriteBatch.Draw(texture, this.Position, Color.White);
return this;
}
}
}