Here is the code for my whole game
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.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace WindowsPhoneGame1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture1;
Texture2D texture2;
Vector2 spritePosition1;
Vector2 spritePosition2;
Vector2 spriteSpeed1 = new Vector2(50.0f, 50.0f);
Vector2 spriteSpeed2 = new Vector2(100.0f, 100.0f);
int sprite1Height;
int sprite1Width;
int sprite2Height;
int sprite2Width;
SoundEffect soundEffect;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
}
/// <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
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);
texture1 = Content.Load<Texture2D>("SmootassEgg");
texture2 = Content.Load<Texture2D>("SmootassEgg");
soundEffect = Content.Load<SoundEffect>("Clack");
spritePosition1.X = 0;
spritePosition1.Y = 0;
spritePosition2.X = graphics.GraphicsDevice.Viewport.Width - texture1.Width;
spritePosition2.Y = graphics.GraphicsDevice.Viewport.Height - texture1.Height;
sprite1Height = texture1.Bounds.Height;
sprite1Width = texture1.Bounds.Width;
sprite2Height = texture2.Bounds.Height;
sprite2Width = texture2.Bounds.Width;
}
/// <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)
{
// Allow the game to exit.
if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
ButtonState.Pressed)
this.Exit();
// Move the sprite around.
UpdateSprite(gameTime, ref spritePosition1, ref spriteSpeed1);
UpdateSprite(gameTime, ref spritePosition2, ref spriteSpeed2);
CheckForCollision();
base.Update(gameTime);
}
void UpdateSprite(GameTime gameTime, ref Vector2 spritePosition, ref Vector2 spriteSpeed)
{
// Move the sprite by speed, scaled by elapsed time.
spritePosition +=
(spriteSpeed/2) * (float)gameTime.ElapsedGameTime.TotalSeconds;
int MaxX =
graphics.GraphicsDevice.Viewport.Width - texture1.Width;
int MinX = 0;
int MaxY =
graphics.GraphicsDevice.Viewport.Height - texture1.Height;
int MinY = 0;
// Check for bounce.
if (spritePosition.X > MaxX)
{
spriteSpeed.X *= -1;
spritePosition.X = MaxX;
}
else if (spritePosition.X < MinX)
{
spriteSpeed.X *= -1;
spritePosition.X = MinX;
}
if (spritePosition.Y > MaxY)
{
spriteSpeed.Y *= -1;
spritePosition.Y = MaxY;
}
else if (spritePosition.Y < MinY)
{
spriteSpeed.Y *= -1;
spritePosition.Y = MinY;
}
}
void CheckForCollision()
{
BoundingBox bb1 = new BoundingBox(new Vector3(spritePosition1.X - (sprite1Width / 2), spritePosition1.Y - (sprite1Height / 2), 0), new Vector3(spritePosition1.X + (sprite1Width / 2), spritePosition1.Y + (sprite1Height / 2), 0));
BoundingBox bb2 = new BoundingBox(new Vector3(spritePosition2.X - (sprite2Width / 2), spritePosition2.Y - (sprite2Height / 2), 0), new Vector3(spritePosition2.X + (sprite2Width / 2), spritePosition2.Y + (sprite2Height / 2), 0));
if (bb1.Intersects(bb2))
{
soundEffect.Play();
}
}
/// <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.DarkBlue);
graphics.GraphicsDevice.Clear(Color.DarkBlue);
// Draw the sprite.
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(texture1, spritePosition1, Color.White);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(texture2, spritePosition2, Color.White);
spriteBatch.End();
base.Draw(gameTime);
base.Draw(gameTime);
}
}
}
Here is the code that I think I need to do something with to accomplish my goal, just not sure what exactly.
void CheckForCollision()
{
BoundingBox bb1 = new BoundingBox(new Vector3(spritePosition1.X - (sprite1Width / 2), spritePosition1.Y - (sprite1Height / 2), 0), new Vector3(spritePosition1.X + (sprite1Width / 2), spritePosition1.Y + (sprite1Height / 2), 0));
BoundingBox bb2 = new BoundingBox(new Vector3(spritePosition2.X - (sprite2Width / 2), spritePosition2.Y - (sprite2Height / 2), 0), new Vector3(spritePosition2.X + (sprite2Width / 2), spritePosition2.Y + (sprite2Height / 2), 0));
if (bb1.Intersects(bb2))
{
soundEffect.Play();
}
}
I have not really tried anything to the code in this section as I am pretty green to programming and really do not even know how to go about it. I did change
if (bb1.Intersects(bb2))
and removed Intersects to try and find something that would work for my desire, but nothing seemed to fit. Not sure where to go with this one...
As far as the background image is concerned, I feel like it has to be something to do with these lines of code
GraphicsDevice.Clear(Color.DarkBlue);
graphics.GraphicsDevice.Clear(Color.DarkBlue);
Initially they were CornFlowerBlue and when I change the first one nothing happens, but when I change the second one, my background image is the color of the second one. So for example I can put
GraphicsDevice.Clear(Color.Gold);
graphics.GraphicsDevice.Clear(Color.DarkBlue);
and my background will still be DarkBlue. I have tried to put my images name in place of the color and did not work. I also tried to do the same thing but in "" and still nothing. I am sure this is rather easy to accomplish but again am new and not sure where to go. Thank you to any and everyone who responds.

New Topic/Question
Reply


MultiQuote


|