Player movement
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -10,6 +10,7 @@ namespace Pong {
|
|||||||
private GraphicsDeviceManager _graphics;
|
private GraphicsDeviceManager _graphics;
|
||||||
private SpriteBatch _spriteBatch;
|
private SpriteBatch _spriteBatch;
|
||||||
private Random random;
|
private Random random;
|
||||||
|
private KeyboardState kState;
|
||||||
|
|
||||||
public Texture2D ballTexture;
|
public Texture2D ballTexture;
|
||||||
public Texture2D playerOneTexture;
|
public Texture2D playerOneTexture;
|
||||||
@@ -23,6 +24,7 @@ namespace Pong {
|
|||||||
Vector2 ballDir;
|
Vector2 ballDir;
|
||||||
|
|
||||||
float ballSpeed;
|
float ballSpeed;
|
||||||
|
float playerSpeed;
|
||||||
|
|
||||||
public Game1() {
|
public Game1() {
|
||||||
_graphics = new GraphicsDeviceManager(this);
|
_graphics = new GraphicsDeviceManager(this);
|
||||||
@@ -43,15 +45,19 @@ namespace Pong {
|
|||||||
playerOneTexture = new Texture2D(_graphics.GraphicsDevice, 20, 125);
|
playerOneTexture = new Texture2D(_graphics.GraphicsDevice, 20, 125);
|
||||||
playerTwoTexture = new Texture2D(_graphics.GraphicsDevice, 20, 125);
|
playerTwoTexture = new Texture2D(_graphics.GraphicsDevice, 20, 125);
|
||||||
|
|
||||||
|
//filling the player rectangles with color data
|
||||||
Color[] data = new Color[playerOneTexture.Width*playerOneTexture.Height];
|
Color[] data = new Color[playerOneTexture.Width*playerOneTexture.Height];
|
||||||
for (int i = 0; i < data.Length; ++i) data[i] = Color.White;
|
for (int i = 0; i < data.Length; ++i) data[i] = Color.White;
|
||||||
playerOneTexture.SetData(data);
|
playerOneTexture.SetData(data);
|
||||||
playerTwoTexture.SetData(data);
|
playerTwoTexture.SetData(data);
|
||||||
|
|
||||||
ballPosition = new Vector2(_graphics.PreferredBackBufferWidth / 2, _graphics.PreferredBackBufferHeight / 2);
|
ballPosition = new Vector2(_graphics.PreferredBackBufferWidth / 2, _graphics.PreferredBackBufferHeight / 2);
|
||||||
ballDir = new Vector2(random.Next(-7, 7), random.Next(-7, 7));
|
ballDir = new Vector2(random.Next(-5, 5), random.Next(-7, 7));
|
||||||
|
ballDir.X = ballDir.X < 0 ? -5 : 5;
|
||||||
ballSpeed = 100.0f;
|
ballSpeed = 100.0f;
|
||||||
|
playerSpeed = 500f;
|
||||||
|
|
||||||
|
//players starting position
|
||||||
playerOnePosition = new Vector2(25, _graphics.PreferredBackBufferHeight / 2 - playerOneTexture.Height / 2);
|
playerOnePosition = new Vector2(25, _graphics.PreferredBackBufferHeight / 2 - playerOneTexture.Height / 2);
|
||||||
playerTwoPosition = new Vector2(_graphics.PreferredBackBufferWidth - 50, _graphics.PreferredBackBufferHeight / 2 - playerTwoTexture.Height / 2);
|
playerTwoPosition = new Vector2(_graphics.PreferredBackBufferWidth - 50, _graphics.PreferredBackBufferHeight / 2 - playerTwoTexture.Height / 2);
|
||||||
|
|
||||||
@@ -69,20 +75,35 @@ namespace Pong {
|
|||||||
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
||||||
Exit();
|
Exit();
|
||||||
|
|
||||||
/*KeyboardState kState = Keyboard.GetState();
|
kState = Keyboard.GetState();
|
||||||
|
|
||||||
|
//Player one movement
|
||||||
|
if (kState.IsKeyDown(Keys.W)) {
|
||||||
|
playerOnePosition.Y -= playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||||
|
if(playerOnePosition.Y < 0) {
|
||||||
|
playerOnePosition.Y = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (kState.IsKeyDown(Keys.S)) {
|
||||||
|
playerOnePosition.Y += playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||||
|
if (playerOnePosition.Y + playerOneTexture.Height > _graphics.PreferredBackBufferHeight) {
|
||||||
|
playerOnePosition.Y = _graphics.PreferredBackBufferHeight - playerOneTexture.Height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Player two movement
|
||||||
if (kState.IsKeyDown(Keys.Up)) {
|
if (kState.IsKeyDown(Keys.Up)) {
|
||||||
ballPosition.Y -= ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
playerTwoPosition.Y -= playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||||
|
if (playerTwoPosition.Y < 0) {
|
||||||
|
playerTwoPosition.Y = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (kState.IsKeyDown(Keys.Down)) {
|
if (kState.IsKeyDown(Keys.Down)) {
|
||||||
ballPosition.Y += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
playerTwoPosition.Y += playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||||
|
if (playerTwoPosition.Y + playerTwoTexture.Height > _graphics.PreferredBackBufferHeight) {
|
||||||
|
playerTwoPosition.Y = _graphics.PreferredBackBufferHeight - playerTwoTexture.Height;
|
||||||
}
|
}
|
||||||
if (kState.IsKeyDown(Keys.Left)) {
|
|
||||||
ballPosition.X -= ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
||||||
}
|
}
|
||||||
if (kState.IsKeyDown(Keys.Right)) {
|
|
||||||
ballPosition.X += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
ballPosition.X += ballDir.X * ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
ballPosition.X += ballDir.X * ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||||
ballPosition.Y += ballDir.Y * ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
ballPosition.Y += ballDir.Y * ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user