Click here to Skip to main content
15,889,879 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning about collision detection using rectangles.I created a simple project in which we control the player with arrow keys and if he touches the ball,a new ball will appear.However,a new ball does not appear when I touch the player with the ball.The code:

Main game:
C#
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 WindowsGame4
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public static Texture2D player, ball;
        public static GraphicsDeviceManager graphics;
        public static SpriteBatch spriteBatch;
        public static KeyboardState state;
        public static SpriteFont font;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        /// <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();
            new Ball();
            new Player();
 
        }

        /// <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);
            player = Content.Load<Texture2D>("player");
            ball = Content.Load<Texture2D>("ball");
            font = Content.Load<SpriteFont>("SpriteFont1");

            // 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();
            state = Keyboard.GetState();
            Player.Update();
            // TODO: Add your update logic here
            new NewCol();
            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);
            Ball.Draw();
            Player.Draw();
            NewCol.Collision();
            
            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}


Ball class:
C#
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 WindowsGame4
{
    class Ball
    {
        private static int width = 500;
        private static int height = 300;
        public static Vector2 ballloc = new Vector2(width, height);
        public Ball()
        { }
        public static void Draw()
        {
            Game1.spriteBatch.Begin();
            Game1.spriteBatch.Draw(Game1.ball, ballloc, Color.White);
            Game1.spriteBatch.DrawString(Game1.font, "ball width=" + width + "ball height=" + height, new Vector2(0, 0), Color.Black);
            Game1.spriteBatch.End();
        }
    }
}

Player class:
C#
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 WindowsGame4
{
    class Player
    {
        private static int width = 0;
        private static int height = 0;
        public static Vector2 playerloc = new Vector2(width, height);
        public Player()
        { }
        public static void Draw()
        { Vector2 playerloc = new Vector2(width, height);
            Game1.spriteBatch.Begin();
            Game1.spriteBatch.Draw(Game1.player, playerloc, Color.White);
            Game1.spriteBatch.DrawString(Game1.font, "player width=" + width + "player height=" + height, new Vector2(50, 50), Color.Black);
            Game1.spriteBatch.End();
        }
        public static void Update()
        { Vector2 playerloc = new Vector2(width, height);
            if (Game1.state.IsKeyDown(Keys.Up))
            { height = height - 1; }
            if (Game1.state.IsKeyDown(Keys.Down))
            { height = height + 1; }
            if (Game1.state.IsKeyDown(Keys.Left))
            { width = width - 1; }
            if (Game1.state.IsKeyDown(Keys.Right))
            { width = width + 1; }
        }
    }
}

Collision Detection class:
C#
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 WindowsGame4
{
    class NewCol
    {
        static Rectangle playerrect = new Rectangle((int)Player.playerloc.X, (int)Player.playerloc.Y, Game1.player.Width, Game1.player.Height);
        static Rectangle ballrect = new Rectangle((int)Ball.ballloc.X, (int)Ball.ballloc.Y, Game1.ball.Width, Game1.ball.Height);
        public NewCol()
        { }
        public static void Collision()
        {
            if (playerrect.Intersects(ballrect))
            {
                Game1.spriteBatch.Begin();
                Game1.spriteBatch.Draw(Game1.ball, new Vector2(100, 100), Color.White);
                Game1.spriteBatch.End();
            }
        }
    }
    
}


Please tell me what to do and correct the code.
It's very important for me.
Posted
Comments
George Jonsson 16-Sep-14 6:33am    
Does this code even compile?
swapnil999 16-Sep-14 6:47am    
It does.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900