I am trying to make a pong game that uses a burger as a ball and a plate as its paddle. I have everything right except that the burger goes right through the plate. Can anyone help me?
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Burger_Attack_Mitch_McCauley
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GameSprite myGameSprite;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D imageBurger;
Texture2D imagePlate;
Texture2D imageStart;
Texture2D imageEnd;
KeyboardState keyState_01;
KeyboardState keyState_01_Old;
GamePadState pad_01;
GamePadState pad_01_Old;
int intGameState;
GameSprite spriteStartScreen_01;
GameSprite spriteEndScreen_01;
GameSprite spritePlate;
GameSprite spriteBurger;
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()
{
// TODO: Add your initialization logic here
myGameSprite = new GameSprite();
spriteStartScreen_01 = new GameSprite();
spriteEndScreen_01 = new GameSprite();
spritePlate = new GameSprite(300, 425, 150, 50);
spriteBurger = new GameSprite(300, 300, 150, 100, 1, -1);
intGameState = 5;
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);
imageBurger = Content.Load<Texture2D>("Images\\DeathBurger");
imagePlate = Content.Load<Texture2D>("Images\\Plate");
imageStart = Content.Load<Texture2D>("Images\\StartScreen_01");
imageEnd = Content.Load<Texture2D>("Images\\EndScreen_01");
spriteStartScreen_01.spriteTexture = imageStart;
spriteEndScreen_01.spriteTexture = imageEnd;
spriteBurger.spriteTexture = imageBurger;
spritePlate.spriteTexture = imagePlate;
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
GetUserInput();
ChangeGameState();
switch (intGameState)
{
case 5:
break;
case 86:
break;
default:
MoveUserControlledSprites();
MoveSprites();
TestForCollisions();
break;
}
// TODO: Add your update logic here
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.CornflowerBlue);
spriteBatch.Begin();
switch (intGameState)
{
case 5:
spriteBatch.Draw(spriteStartScreen_01.spriteTexture,
spriteStartScreen_01.spritePos, Color.White);
break;
case 86:
spriteBatch.Draw(spriteEndScreen_01.spriteTexture,
spriteEndScreen_01.spritePos, Color.White);
break;
default:
spriteBatch.Draw(spriteBurger.spriteTexture,
spriteBurger.spritePos, Color.White);
spriteBatch.Draw(spritePlate.spriteTexture,
spritePlate.spritePos, Color.White);
break;
}
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
private void GetUserInput()
{
keyState_01_Old = keyState_01;
pad_01_Old = pad_01;
keyState_01 = Keyboard.GetState();
pad_01 = GamePad.GetState(PlayerIndex.One);
}
private void ChangeGameState()
{if (intGameState == 5 &&
pad_01.Buttons.B == ButtonState.Pressed &&
pad_01_Old.Buttons.B == ButtonState.Released)
{
intGameState = 10;
}
if (intGameState == 5 &&
keyState_01.IsKeyDown (Keys.Space) &&
keyState_01_Old.IsKeyUp(Keys.Space))
{
intGameState = 10;
}
if (intGameState == 86 &&
pad_01.Buttons.B == ButtonState. Pressed &&
pad_01_Old.Buttons.B == ButtonState.Released)
{
intGameState = 5;
spriteBurger.spritePos.X = 300;
spriteBurger.spritePos.Y = 0;
}
if (intGameState == 86 &&
keyState_01.IsKeyDown(Keys.Space) &&
keyState_01_Old.IsKeyUp(Keys.Space))
{
intGameState = 5;
spriteBurger.spritePos.X = 300;
spriteBurger.spritePos.Y = 0;
}
}
private void MoveSprites()
{
if (spriteBurger.spritePos.X <= 0)
{
spriteBurger.spriteSpeed.X = 2;
}
if (spriteBurger.spriteSpeed.Y <= 0)
{
spriteBurger.spriteSpeed.Y = 2;
}
if (spriteBurger.spritePos.X +
spriteBurger.spriteDims.X >=
Window.ClientBounds.Width)
{
spriteBurger.spriteSpeed.X = -2;
}
if (spriteBurger.spritePos.Y +
spriteBurger.spriteDims.Y >+
Window.ClientBounds.Height)
{
intGameState = 86;
}
spriteBurger.spritePos.X += spriteBurger.spriteSpeed.X;
spriteBurger.spritePos.Y += spriteBurger.spriteSpeed.Y;
}
private void MoveUserControlledSprites()
{
if (keyState_01.IsKeyDown(Keys.Left))
{
spritePlate.spritePos.X -= 3;
}
if (keyState_01.IsKeyDown(Keys.Right))
{
spritePlate.spritePos.X += 3;
}
spritePlate.spritePos.X += pad_01.ThumbSticks.Right.X * 5;
if (spritePlate.spritePos.X < 0)
{
spritePlate.spritePos.X = 0;
}
if (Window.ClientBounds.Width < (spritePlate.spritePos.X + spritePlate.spriteDims.X))
{
spritePlate.spritePos.X = (Window.ClientBounds.Width - spritePlate.spriteDims.X);
}
}
private void TestForCollisions()
{
Rectangle plateRect = new Rectangle(
(int)spritePlate.spritePos.X,
(int)spritePlate.spritePos.Y,
(int)spritePlate.spriteDims.X,
(int)spritePlate.spriteDims.Y);
Rectangle burgerRect = new Rectangle(
(int)spriteBurger.spritePos.X,
(int)spriteBurger.spritePos.Y,
(int)spriteBurger.spriteDims.X,
(int)spriteBurger.spriteDims.Y);
if (plateRect.Intersects(burgerRect))
{
spriteBurger.spriteSpeed.Y = -1 *
Math.Abs(spriteBurger.spriteSpeed.Y);
}
}
}
}