Hela programmet - Pong
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont arial;
Vector2 poängPosition = new Vector2(350, 50);
int vänsterPoäng = 0;
int högerPoäng = 0;
Vector2 bollPosition;
Vector2 bollHastighet;
Rectangle bollHitbox;
Texture2D bollBild;
Rectangle vänsterPaddle;
Rectangle högerPaddle;
Texture2D paddleBild;
KeyboardState tangentBord = Keyboard.GetState();
Random slump = new Random();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
bollBild = Content.Load<Texture2D>("boll");
bollHitbox = new Rectangle(0, 0, bollBild.Width, bollBild.Height);
NyBoll();
paddleBild = Content.Load<Texture2D>("paddle");
vänsterPaddle = new Rectangle(50, 250, paddleBild.Width, paddleBild.Height);
högerPaddle = new Rectangle(750 - paddleBild.Width, 250, paddleBild.Width, paddleBild.Height);
arial = Content.Load<SpriteFont>("arial");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Uppdatera tangentbord
tangentBord = Keyboard.GetState();
// Flytta bollen
bollPosition += bollHastighet;
KollaKollisioner();
FlyttaPaddles();
KollaPoäng();
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.DrawString(arial, $"{vänsterPoäng} - {högerPoäng}", poängPosition, Color.White);
spriteBatch.Draw(bollBild, bollPosition, Color.White);
spriteBatch.Draw(paddleBild, vänsterPaddle, Color.White);
spriteBatch.Draw(paddleBild, högerPaddle, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
/// <summary>
/// Placerar bollen i mitten av spelplanen med en slumpad hastighet
/// </summary>
void NyBoll()
{
bollPosition.X = 400 - (bollBild.Width / 2);
bollPosition.Y = 240 - (bollBild.Height / 2);
bollHastighet.X = slump.Next(5, 10);
bollHastighet.Y = slump.Next(5, 10);
}
/// <summary>
/// Ser till så att bollen studsar mot paddles och väggarna
/// </summary>
void KollaKollisioner()
{
bollHitbox.X = (int)bollPosition.X;
bollHitbox.Y = (int)bollPosition.Y;
if (bollHitbox.Intersects(vänsterPaddle) == true || bollHitbox.Intersects(högerPaddle) == true)
{
bollHastighet.X *= -1;
}
if (bollPosition.Y < 0 || bollPosition.Y + bollBild.Height > 480)
{
bollHastighet.Y *= -1;
}
}
/// <summary>
/// Hanterar förflyttningen av spelarnas paddles
/// </summary>
void FlyttaPaddles()
{
if (tangentBord.IsKeyDown(Keys.W) == true)
{
vänsterPaddle.Y -= 5;
}
if (tangentBord.IsKeyDown(Keys.S) == true)
{
vänsterPaddle.Y += 5;
}
if (tangentBord.IsKeyDown(Keys.Up) == true)
{
högerPaddle.Y -= 5;
}
if (tangentBord.IsKeyDown(Keys.Down) == true)
{
högerPaddle.Y += 5;
}
}
/// <summary>
/// Ger poäng om bollen kommit i mål och startar då om bollen i mitten av planen
/// </summary>
void KollaPoäng()
{
if (bollPosition.X < 0)
{
högerPoäng++;
NyBoll();
}
else if (bollPosition.X > 800)
{
vänsterPoäng++;
NyBoll();
}
}