Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How to add collision detection to an object in the game.

$
0
0
Ok I am trying to add collison detection to an object in my game. real basic stuff. just started programming. this is my code.

 using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        

        Texture2D stickFigure;
        Vector2 texturePosition = new Vector2(0, 0);
        Texture2D Chess;
        Vector2 chessPosition = new Vector2(100, 100);
        


        KeyboardState keyboardState;

        float speed = 10;

        

        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

            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);

            stickFigure = Content.Load <Texture2D>("StickFigure");
            Chess = Content.Load <Texture2D>("chess");


            

            // 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();

            keyboardState = Keyboard.GetState();
            
            if (keyboardState.IsKeyDown(Keys.Up))
            texturePosition.Y -= speed;
            if (keyboardState.IsKeyDown(Keys.Down))
                texturePosition.Y += speed;
            if (keyboardState.IsKeyDown(Keys.Left))
                texturePosition.X -= speed;
            if (keyboardState.IsKeyDown(Keys.Right))
                texturePosition.X += speed;

            if (texturePosition.X > window.ClientBounds.Width - 75)
            {
                texturePosition.X = window.ClientBounds.Width - 75;  
            }
            if (texturePosition.Y > window.ClientBounds.Height-stickFigure.Height)
            {
                texturePosition.Y = window.ClientBounds.Height-stickFigure.Height;
            }
            if (texturePosition.X <0)
            {
                texturePosition.X = 0;
            }
            if (texturePosition.Y <0)
            {
                texturePosition.Y = 0;
            }

            if (chessPosition.Y > window.ClientBounds.Height - 2)
                chessPosition.Y = window.ClientBounds.Height -2;
            



           


            // 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);

            spriteBatch.Begin();
            spriteBatch.Draw(stickFigure, texturePosition, Color.White);
            spriteBatch.End();

            spriteBatch.Begin();
            spriteBatch.Draw(Chess, chessPosition, Color.White);
            spriteBatch.End();
            

            base.Draw(gameTime);
        }
    }
}



I want the stickFigure to be able to hit the Chess and go back to 0,0 on the screen. I have tried looking for this. I am new to this if anyone can help.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>