XNA Level Select Menu

Posted by user29901 on Game Development See other posts from Game Development or by user29901
Published on 2013-06-23T20:55:32Z Indexed on 2013/06/24 16:39 UTC
Read the original article Hit count: 568

Filed under:
|

I'll try to explain this the best I can. I'm trying to create a level select menu for a game I'm making. The menu is basically a group of blocks numbered 1-16, similar to something like the Angry Birds menu. What I've done is created a cursor, basically just an outline to surround a block, that the user can move to select what level they want to play. What I want it do is move from block to block instead of simply moving around on the X and Y axes as it does now.

So my question is, how can I get the cursor (highLight in the below code) to move from block to block(destinationRectangle1 etc. in the code)?

        /// Field for the "cursor"

        Vector2 highLightPos = new Vector2(400, 200);

        ///This is the Update code

        KeyboardState keyBoardState = Keyboard.GetState();
            if (keyBoardState.IsKeyDown(Keys.Up))
                highLightPos.Y--;                       
            if (keyBoardState.IsKeyDown(Keys.Down))
                highLightPos.Y++;
            if (keyBoardState.IsKeyDown(Keys.Right))
                highLightPos.X++;
            if (keyBoardState.IsKeyDown(Keys.Left))
                highLightPos.X--;

        /// This is the draw code

        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        Rectangle screenRectangle = new Rectangle(0, 0, 1280, 720);            

        Rectangle destinationRectangle1 = new Rectangle(400, 200, 64, 64);
        Rectangle frameRectangle1 = new Rectangle(0, 0, 64, 64);

        Rectangle destinationRectangle2 = new Rectangle(500, 200, 64, 64);
        Rectangle frameRectangle2 = new Rectangle(64, 0, 64, 64);

        Rectangle destinationRectangle3 = new Rectangle(600, 200, 64, 64);
        Rectangle frameRectangle3 = new Rectangle(128, 0, 64, 64);

        Rectangle destinationRectangle4 = new Rectangle(700, 200, 64, 64);
        Rectangle frameRectangle4 = new Rectangle(192, 0, 64, 64);

        Rectangle destinationRectangle5 = new Rectangle(800, 200, 64, 64);
        Rectangle frameRectangle5 = new Rectangle(256, 0, 64, 64);

        Rectangle destinationRectangle6 = new Rectangle(400, 300, 64, 64);
        Rectangle frameRectangle6 = new Rectangle(320, 0, 64, 64);

        Rectangle destinationRectangle7 = new Rectangle(500, 300, 64, 64);
        Rectangle frameRectangle7 = new Rectangle(384, 0, 64, 64);

        Rectangle destinationRectangle8 = new Rectangle(600, 300, 64, 64);
        Rectangle frameRectangle8 = new Rectangle(448, 0, 64, 64);

        Rectangle destinationRectangle9 = new Rectangle(700, 300, 64, 64);
        Rectangle frameRectangle9 = new Rectangle(0, 64, 64, 64);

        Rectangle destinationRectangle10 = new Rectangle(800, 300, 64, 64);
        Rectangle frameRectangle10 = new Rectangle(64, 64, 64, 64);

        Rectangle destinationRectangle11 = new Rectangle(400, 400, 64, 64);
        Rectangle frameRectangle11 = new Rectangle(128, 64, 64, 64);

        Rectangle destinationRectangle12 = new Rectangle(500, 400, 64, 64);
        Rectangle frameRectangle12 = new Rectangle(192, 64, 64, 64);

        Rectangle destinationRectangle13 = new Rectangle(600, 400, 64, 64);
        Rectangle frameRectangle13 = new Rectangle(256, 64, 64, 64);

        Rectangle destinationRectangle14 = new Rectangle(700, 400, 64, 64);
        Rectangle frameRectangle14 = new Rectangle(320, 64, 64, 64);

        Rectangle destinationRectangle15 = new Rectangle(800, 400, 64, 64);
        Rectangle frameRectangle15 = new Rectangle(384, 64, 64, 64);

        Rectangle destinationRectangle16 = new Rectangle(600, 500, 64, 64);
        Rectangle frameRectangle16 = new Rectangle(448, 64, 64, 64);

        spriteBatch.Begin();
        spriteBatch.Draw(forestBG, screenRectangle, Color.White);
        spriteBatch.Draw(highLight, highLightPos, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle1, frameRectangle1, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle2, frameRectangle2, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle3, frameRectangle3, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle4, frameRectangle4, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle5, frameRectangle5, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle6, frameRectangle6, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle7, frameRectangle7, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle8, frameRectangle8, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle9, frameRectangle9, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle10, frameRectangle10, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle11, frameRectangle11, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle12, frameRectangle12, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle13, frameRectangle13, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle14, frameRectangle14, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle15, frameRectangle15, Color.White);
        spriteBatch.Draw(levelSelectTiles, destinationRectangle16, frameRectangle16, Color.White);
        spriteBatch.End();

PS, I'm aware that this code is probably inefficient, cumbersome or that there's a better way to draw parts of a tile sheet. Any suggestions would be appreciated.

© Game Development or respective owner

Related posts about XNA

Related posts about menu