Hey thanks everyone... I'm still having difficulty. I"m understanding the theory behind it no problem. I just don't know how to update it... or Don't understand why it's not working. :/ I'm going to post my entire code everything works and I'm just kinda tired after trying to quite some time... Anyone have any ideas? Here is my code.
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 Breakout
{
/// <summary>
/// This is the main type for your game
/// </summary>
public partial class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D paddle;
Vector2 paddlePosition = Vector2.Zero;
Texture2D ball1;
Vector2 ball1Position = Vector2.Zero;
Texture2D ball2;
Vector2 ball2Position = Vector2.Zero;
Boolean movingUp, movingLeft, movingUp2,movingLeft2;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
movingUp = true;
movingLeft = true;
movingUp2 = true;
movingLeft2 = true;
spriteBatch = new SpriteBatch(GraphicsDevice);
paddle = Content.Load<Texture2D>("Paddle");
paddlePosition= new Vector2((graphics.GraphicsDevice.Viewport.Width/2)-(paddle.Width/2), (graphics.GraphicsDevice.Viewport.Height-80));
ball1 = Content.Load<Texture2D>("ball");
ball1Position=new Vector2((graphics.GraphicsDevice.Viewport.Width/2)-(ball1.Width/2), graphics.GraphicsDevice.Viewport.Height-115);
ball2 = Content.Load<Texture2D>("ball");
ball2Position = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) - ((ball2.Width / 2)+250), graphics.GraphicsDevice.Viewport.Height - 115);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
PaddleMover();
Ball1mover();
Ball2mover();
BalltoBallCollision();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(paddle, paddlePosition, Color.White);
spriteBatch.Draw(ball1, ball1Position, Color.White);
spriteBatch.Draw(ball2, ball2Position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Here is where all of the important stuff happends (movement etc) it's in the other partial class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 Breakout
{
public partial class Game1
{
public void PaddleMover()
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && paddlePosition.X >= 0)
{
paddlePosition.X -= 15;
}
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && paddlePosition.X <= (graphics.GraphicsDevice.Viewport.Width - 150))
{
paddlePosition.X += 15;
}
}
public void Ball1mover()
{
if (movingUp)
{
ball1Position.Y -= 3;
}
else
{
ball1Position.Y += 3;
}
if (movingLeft)
{
ball1Position.X -= 3;
}
else
{
ball1Position.X += 3;
}
if (ball1Position.X <= 0 && movingLeft) movingLeft = false;
if (ball1Position.Y <= 0 && movingUp) movingUp = false;
if (ball1Position.X >= (graphics.GraphicsDevice.Viewport.Width - ball1.Width) && !movingLeft) movingLeft = true;
if (DetectPaddleBallCollision())
{
movingUp = true;
}
}
public void Ball2mover()
{
if (movingUp2)
{
ball2Position.Y -= 3;
}
else
{
ball2Position.Y += 3;
}
if (movingLeft2)
{
ball2Position.X -= 3;
}
else
{
ball2Position.X += 3;
}
if (ball2Position.X <= 0 && movingLeft2) movingLeft2 = false;
if (ball2Position.Y <= 0 && movingUp2) movingUp2 = false;
if (ball2Position.X >= (graphics.GraphicsDevice.Viewport.Width - ball1.Width) && !movingLeft2) movingLeft2 = true;
if (DetectPaddleBallCollision2())
{
movingUp2 = true;
}
}
public Boolean DetectPaddleBallCollision()
{
if ((ball1Position.Y + ball1.Height) >= paddlePosition.Y &&
(ball1Position.Y + ball1.Height) < (paddlePosition.Y + 4) &&
(ball1Position.X + ball1.Width) > paddlePosition.X &&
ball1Position.X < (paddlePosition.X + paddle.Width))
{
return true;
}
else
{
return false;
}
}
public Boolean DetectPaddleBallCollision2()
{
if ((ball2Position.Y + ball2.Height) >= paddlePosition.Y &&
(ball2Position.Y + ball2.Height) < (paddlePosition.Y + 4) &&
(ball2Position.X + ball2.Width) > paddlePosition.X &&
ball2Position.X < (paddlePosition.X + paddle.Width))
{
return true;
}
else
{
return false;
}
}
public void BalltoBallCollision()
{
//Controlls ball 1 movement
if (ball1Position.Y == ball2Position.Y && ball1Position.X == ball2Position.X)
{
ball2Position.Y = -ball2Position.Y;
ball1Position.Y = -ball1Position.Y;
}
}
}
}
Any help would be appreciated.... I obviousl have no idea what I"m doing.