Files
pong/Pong/Game1.cs

140 lines
6.1 KiB
C#
Raw Permalink Normal View History

2022-08-13 21:13:58 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
//open content editor with mgcb-editor-windows
namespace Pong {
public class Game1 : Game {
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private Random random;
2022-08-14 17:18:58 +02:00
private KeyboardState kState;
2022-08-13 21:13:58 +02:00
public Texture2D ballTexture;
public Texture2D playerOneTexture;
public Texture2D playerTwoTexture;
public Rectangle playerOne;
public Rectangle playerTwo;
Vector2 ballPosition;
Vector2 playerOnePosition;
Vector2 playerTwoPosition;
Vector2 ballDir;
float ballSpeed;
2022-08-14 17:18:58 +02:00
float playerSpeed;
2022-08-13 21:13:58 +02:00
public Game1() {
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
Window.AllowUserResizing = true;
}
protected override void Initialize() {
// TODO: Add your initialization logic here
Window.Title = "Pong";
_graphics.PreferredBackBufferHeight = 720;
_graphics.PreferredBackBufferWidth = 1280;
_graphics.ApplyChanges();
random = new Random();
playerOneTexture = new Texture2D(_graphics.GraphicsDevice, 20, 125);
playerTwoTexture = new Texture2D(_graphics.GraphicsDevice, 20, 125);
2022-08-14 17:18:58 +02:00
//filling the player rectangles with color data
2022-08-13 21:13:58 +02:00
Color[] data = new Color[playerOneTexture.Width*playerOneTexture.Height];
for (int i = 0; i < data.Length; ++i) data[i] = Color.White;
playerOneTexture.SetData(data);
playerTwoTexture.SetData(data);
ballPosition = new Vector2(_graphics.PreferredBackBufferWidth / 2, _graphics.PreferredBackBufferHeight / 2);
2022-08-14 17:18:58 +02:00
ballDir = new Vector2(random.Next(-5, 5), random.Next(-7, 7));
ballDir.X = ballDir.X < 0 ? -5 : 5;
2022-08-13 21:13:58 +02:00
ballSpeed = 100.0f;
2022-08-14 17:18:58 +02:00
playerSpeed = 500f;
2022-08-13 21:13:58 +02:00
2022-08-14 17:18:58 +02:00
//players starting position
2022-08-13 21:13:58 +02:00
playerOnePosition = new Vector2(25, _graphics.PreferredBackBufferHeight / 2 - playerOneTexture.Height / 2);
playerTwoPosition = new Vector2(_graphics.PreferredBackBufferWidth - 50, _graphics.PreferredBackBufferHeight / 2 - playerTwoTexture.Height / 2);
base.Initialize();
}
protected override void LoadContent() {
_spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
ballTexture = Content.Load<Texture2D>("Textures/ball");
}
protected override void Update(GameTime gameTime) {
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
2022-08-14 17:18:58 +02:00
kState = Keyboard.GetState();
2022-08-13 21:13:58 +02:00
2022-08-14 17:18:58 +02:00
//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
2022-08-13 21:13:58 +02:00
if (kState.IsKeyDown(Keys.Up)) {
2022-08-14 17:18:58 +02:00
playerTwoPosition.Y -= playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (playerTwoPosition.Y < 0) {
playerTwoPosition.Y = 0;
}
2022-08-13 21:13:58 +02:00
}
if (kState.IsKeyDown(Keys.Down)) {
2022-08-14 17:18:58 +02:00
playerTwoPosition.Y += playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (playerTwoPosition.Y + playerTwoTexture.Height > _graphics.PreferredBackBufferHeight) {
playerTwoPosition.Y = _graphics.PreferredBackBufferHeight - playerTwoTexture.Height;
}
2022-08-13 21:13:58 +02:00
}
ballPosition.X += ballDir.X * ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
ballPosition.Y += ballDir.Y * ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
//Bouncing
if (ballPosition.X > _graphics.PreferredBackBufferWidth - ballTexture.Width / 2) {
ballDir.X = -ballDir.X; //_graphics.PreferredBackBufferWidth - ballTexture.Width / 2;
} else if (ballPosition.X < ballTexture.Width / 2) {
ballDir.X = -ballDir.X; //ballPosition.X = ballTexture.Width / 2;
}
if (ballPosition.Y > _graphics.PreferredBackBufferHeight - ballTexture.Height / 2) {
ballDir.Y = -ballDir.Y; //ballPosition.Y = _graphics.PreferredBackBufferHeight - ballTexture.Height / 2;
} else if (ballPosition.Y < ballTexture.Height / 2) {
ballDir.Y = -ballDir.Y; //ballPosition.Y = ballTexture.Height / 2;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
_spriteBatch.Begin();
_spriteBatch.Draw(ballTexture, ballPosition, null, Color.White, 0f, new Vector2(ballTexture.Width / 2, ballTexture.Height / 2), Vector2.One, SpriteEffects.None, 0f);
_spriteBatch.Draw(playerOneTexture, new Rectangle((int)playerOnePosition.X, (int)playerOnePosition.Y, playerOneTexture.Width, playerOneTexture.Height), Color.White);
_spriteBatch.Draw(playerTwoTexture, new Rectangle((int)playerTwoPosition.X, (int)playerTwoPosition.Y, playerTwoTexture.Width, playerTwoTexture.Height), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}