{
/// <summary>
/// Alan Carstairs
/// 26/9/14
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
int DisplayWidth;
int DisplayHeight;
const float speed = 2.4f; //speed of image
int PlayerScore = 0; //
//structure for 2D graphics
struct graphic2D
{
public Texture2D image;
public Rectangle rect;
}
//structure for moving 2D graphics
struct sprite2D
{
public Texture2D image; //Texture to hold the image
public Vector3 position; //Position on screen
public Rectangle rect; //Size & position
public Vector2 origin; //Center point
public float size; //Size ratio
public float rotation;//Amount of rotation
public Vector3 velocity; //Sprite speed and direction
public float thrust; //Amount of thrust applied
public BoundingSphere bsphere;//Bounding sphere
//public int spriteNum = 1;
//public int maxSpriteNum;
//public bool alive;
//public Enemy(Texture2D Texture, Vector3 position, Vector3 velocity,
}
sprite2D Sprite, Sprite2;
graphic2D BackGround;
Boolean GameOver;
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
DisplayWidth = graphics.GraphicsDevice.Viewport.Width;
DisplayHeight = graphics.GraphicsDevice.Viewport.Height;
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);
//Load BackGround
BackGround.image = Content.Load<Texture2D>("fullmoon");
// work out the ratio for image depending on screen size
float ratio = ((float)DisplayWidth / BackGround.image.Width);
BackGround.rect.Width = DisplayWidth;
BackGround.rect.Height = (int)(BackGround.image.Height * ratio);
BackGround.rect.X = 0;
BackGround.rect.Y = (DisplayHeight - BackGround.rect.Height) / 2;
//Sprite
Sprite.image = Content.Load<Texture2D>("raptor");
//Sprite2
Sprite2.image = Content.Load<Texture2D>("raptor2");
reset();
// TODO: use this.Content to load your game content here
}
void reset()
{
//sprite
// Set initial position
Sprite.position = new Vector3(DisplayWidth / 2, DisplayHeight / 2, 0);
Sprite.size = 0.2f; // set size of sprite
Sprite.origin.X = Sprite.image.Width / 2;
Sprite.origin.Y = Sprite.image.Height / 2;
Sprite.rect.Width = (int)(Sprite.image.Width * Sprite.size);
Sprite.rect.Height = (int)(Sprite.image.Height * Sprite.size);
Sprite.rotation = 0;//speed of rotation
//sprite2
// Set initial position
Sprite2.position = new Vector3(DisplayWidth / 2, DisplayHeight / 4, 0);
Sprite2.size = 0.2f; // set size of sprite
Sprite2.origin.X = Sprite2.image.Width / 2;
Sprite2.origin.Y = Sprite2.image.Height / 2;
Sprite2.rect.Width = (int)(Sprite2.image.Width * Sprite2.size);
Sprite2.rect.Height = (int)(Sprite2.image.Height * Sprite2.size);
Sprite2.rotation = 0;//speed of rotation
GameOver = false;
}
/// <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)
{
GamePadState pad_P1 = GamePad.GetState(PlayerIndex.One);//reads gamepad 1
KeyboardState keys = Keyboard.GetState(); // reads keyboard
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (!GameOver)
{
//collision detection
if (Sprite.bsphere.Intersects(Sprite2.bsphere))
{
Sprite.image = null;
PlayerScore++;// increase Points
}
//Display score in top lefthand side of the screen
this.Window.Title = "Score: " + PlayerScore.ToString();
//sprite
Sprite.thrust = (speed * (pad_P1.Triggers.Right + 2));// calculate the amount of thrust based on speen and right trigger
Sprite.velocity.X = pad_P1.ThumbSticks.Right.X;
Sprite.velocity.Y = -pad_P1.ThumbSticks.Right.Y;
Sprite.velocity *= Sprite.thrust;//multi velocity from the thumb stick
Sprite.position += Sprite.velocity; //add current velocity to pos of thr
// check if ball hits any of the four sides and bounce it off them
if ((Sprite.position.X + Sprite.rect.Width / 2) >= DisplayWidth) //RHS
Sprite.position.X = DisplayWidth - (Sprite.rect.Width / 2);
if ((Sprite.position.X - Sprite.rect.Width / 2) <= 0)//LHS
Sprite.position.X = (Sprite.rect.Width / 2);
if ((Sprite.position.Y + Sprite.rect.Height / 2) >= DisplayHeight) //bottom
Sprite.position.Y = DisplayHeight - (Sprite.rect.Height / 2);
if ((Sprite.position.Y - Sprite.rect.Height / 2) <= 0) //Top
Sprite.position.Y = (Sprite.rect.Height / 2);
// set position of Sprite into the rectangle from the position property
Sprite.rect.X = (int)Sprite.position.X;
Sprite.rect.Y = (int)Sprite.position.Y;
//set bounding sphere
Sprite.bsphere = new BoundingSphere(Sprite.position, Sprite.rect.Width / 2);
//sprite2
Sprite2.thrust = speed * (pad_P1.Triggers.Left + 2);// calculate the amount of thrust based on speen and right trigger
Sprite2.velocity.X = pad_P1.ThumbSticks.Left.X;
Sprite2.velocity.Y = -pad_P1.ThumbSticks.Left.Y;
Sprite2.velocity *= Sprite2.thrust;//multi velocity from the thumb stick
Sprite2.position += Sprite2.velocity; //add current velocity to pos of thr
// check if ball hits any of the four sides and bounce it off them
if ((Sprite2.position.X + Sprite2.rect.Width / 2) >= DisplayWidth) //RHS
Sprite2.position.X = DisplayWidth - (Sprite2.rect.Width / 2);
if ((Sprite2.position.X - Sprite2.rect.Width / 2) <= 0)//LHS
Sprite2.position.X = (Sprite2.rect.Width / 2);
if ((Sprite2.position.Y + Sprite2.rect.Height / 2) >= DisplayHeight) //bottom
Sprite2.position.Y = DisplayHeight - (Sprite2.rect.Height / 2);
if ((Sprite2.position.Y - Sprite2.rect.Height / 2) <= 0) //Top
Sprite2.position.Y = (Sprite2.rect.Height / 2);
// set position of Sprite into the rectangle from the position property
Sprite2.rect.X = (int)Sprite2.position.X;
Sprite2.rect.Y = (int)Sprite2.position.Y;
//set bounding sphere
Sprite2.bsphere = new BoundingSphere(Sprite2.position, Sprite2.rect.Width / 2);
// user presses X game is over
if (pad_P1.Buttons.X == ButtonState.Pressed) GameOver = true;
}
else
{
if (pad_P1.Buttons.Start == ButtonState.Pressed || keys.IsKeyDown(Keys.Enter))
reset();
}
// 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);
// TODO: Add your drawing code here
//Draw ship
spriteBatch.Begin();
spriteBatch.Draw(BackGround.image, BackGround.rect, Color.White);
//sprite
spriteBatch.Draw(Sprite.image, Sprite.rect, null, Color.DarkSeaGreen, Sprite.rotation, Sprite.origin, SpriteEffects.None, 0);
//sprite2
spriteBatch.Draw(Sprite2.image, Sprite2.rect, null, Color.DarkSlateBlue, Sprite2.rotation, Sprite2.origin, SpriteEffects.None, 0);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
You'll need to add a bool which tracks if the secondary sprite (Sprite2?) is visible.
Something like:
Bool isVisible=true; //start out so it IS visible
then, in your rendering code, change the third render call to:
if(isVisible)
{spriteBatch.Draw(Sprite2.image, Sprite2.rect, null, Color.DarkSlateBlue, Sprite2.rotation, Sprite2.origin, SpriteEffects.None, 0);}
then it's simply a matter of setting isVisible to false when you detect a collision!
"May the mercy of His Divine Shadow fall upon you." - Stanley H. Tweedle, Security Guard class IV, The League of 20,000 planets
Thanks you helped alot, but when the main sprite hovers over that area the points go up even when the image is gone
Hello Cerberus666!
First of all, stick with either 2D or 3D, do not mix them unless you have reason to. I am guessing you are doing a 2D game, so change all Vector3 to Vector2. Do not use volume checks (Bounding Sphere) in 2D worlds. Stick with rectangles, triangles and circles.
The 2DTexture is essentially a rectangle, so you can use it just as any other.
YourTexture.Contains(SomePoint)
//Or
YourTexture.Intersect(OtherRectangle)
Here are some additional ideas:
-I strongly recommend you to rename your Sprite class, the name is very confusing for someone reading your code.
-Do not show the score in the window title. Use a SpriteFont instead.
-You can use a list to keep all the gameĀ“s entities in and simply iterate trough the list every game update.
Good luck!