Files
pong/Pong/Game1.cs
2022-08-13 21:13:58 +02:00

119 lines
5.1 KiB
C#

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;
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;
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);
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);
ballDir = new Vector2(random.Next(-7, 7), random.Next(-7, 7));
ballSpeed = 100.0f;
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();
/*KeyboardState kState = Keyboard.GetState();
if (kState.IsKeyDown(Keys.Up)) {
ballPosition.Y -= ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (kState.IsKeyDown(Keys.Down)) {
ballPosition.Y += ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
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.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);
}
}
}