I have created a game where the player moves a character around the screen but if a ball hits them then they lose the game however I'm stuck with the part where the ball collides with the player as it simply doesnt work.

Here is my code:

// Imported libaries
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 WindowsGame5
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D man;
        Vector2 man_position = Vector2.Zero;

        Texture2D ball;
        Vector2 ball_position = Vector2.Zero;

        Texture2D game_over;
        Vector2 game_over_position = Vector2.Zero;

        Boolean moving_up, moving_left;

        bool end;

        // Called when class is referanced
        public Game1()
        {
            // Initializing graphics device manager
            graphics = new GraphicsDeviceManager(this);
            // Stuff in content folder
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            // Happen when program is run
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Loads stuf in to memory
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Loads the sprite into memory
            man = Content.Load<Texture2D>("man");

            // Centers the man
            man_position = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) - man.Width / 2, (graphics.GraphicsDevice.Viewport.Height / 2) - man.Height / 2);

            ball = Content.Load<Texture2D>("ball");
            ball_position = new Vector2((graphics.GraphicsDevice.Viewport.Width - 300) - ball.Width / 2, (graphics.GraphicsDevice.Viewport.Height / 2) - ball.Height / 2);

            game_over = Content.Load<Texture2D>("game_over");
            game_over_position = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) - game_over.Width / 2, (graphics.GraphicsDevice.Viewport.Height / 2) - game_over.Height / 2);
            moving_left = true;
            moving_up = true;

        }

        protected override void UnloadContent()
        {
            // Takes stuff out of memory

        }

        protected override void Update(GameTime gameTime)
            // Called 60 times a second(60 fps)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && man_position.X >= 0)
            {
                man_position.X -= 4;
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && man_position.X <= graphics.GraphicsDevice.Viewport.Width - 100 )
            {
                man_position.X += 4;
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up) && man_position.Y >= 0)
            {
                man_position.Y -= 4;
            }

            if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down) && man_position.Y <= graphics.GraphicsDevice.Viewport.Height - man.Height)
            {
                man_position.Y += 4;
            }

            if (moving_up)
            {
                ball_position.Y -= 5;
            }

            if (moving_left)
            {
                ball_position.X -= 5;
            }

            if (!moving_up)
            {
                ball_position.Y += 5;
            }

            if (!moving_left)
            {
                ball_position.X += 5;
            }

            if (ball_position.X <= 0 && moving_left)
            {
                moving_left = false;
            }

            if (ball_position.Y <= 0 && moving_up)
            {
                moving_up = false;
            }

            if (ball_position.X >= graphics.GraphicsDevice.Viewport.Width - ball.Width  && !moving_left)
            {
                moving_left = true;
            }

            if (ball_position.Y >= graphics.GraphicsDevice.Viewport.Height - ball.Height  && !moving_up)
            {
                moving_up = true;
            }



            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            // Tells it what to draw to the screen
            // Colour of background.
            GraphicsDevice.Clear(Color.LightBlue);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            if (DetectCollisionBetweenSprites())
            {
                spriteBatch.Draw(game_over, game_over_position, Color.White);
                ball_position = new Vector2(0, 0);
                man_position = new Vector2(0, 0);
            }
            else
            {
                spriteBatch.Draw(man, man_position, Color.White);
                spriteBatch.Draw(ball, ball_position, Color.White);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }

        // For detection
        public Boolean DetectCollisionBetweenSprites()
        {
            if (ball_position.Y >= man_position.Y && (ball_position.Y <= (man_position.Y - man.Height / 2)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

However the bit that detects the sprites touching is:

 // For detection
        public Boolean DetectCollisionBetweenSprites()
        {
            if (ball_position.Y >= man_position.Y && (ball_position.Y <= (man_position.Y - man.Height / 2)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

I don't really have any idea how to do this.

mat

Recommended Answers

All 2 Replies

Well, the ball position Y can't be both greater than man position Y and less than man position Y, which is what your if statement requires. I think you are trying to check if the ball is in the mans 'space', so you'd want to do something like

if (ball_position.Y >= man_position.Y - man.Height / 2 &&
    ball_position.Y <- man_position.Y + man.Height / 2) {
        return true;
} else {
        return false;
}

This requires the Y coordinate of the ball be within 1/2 the mans height to be considered a collision.

Just what I was looking for, thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.