C# Grid System Help

Started by
2 comments, last by Jettoz 17 years, 5 months ago
Hello fellow programmers, I have had a heck of a time doing Collision with object in XNA so I'm going to make a grid system for movement and checking for moveable and non-moveable spots. I have a map that is 640x480 with 32x32 Tiles and 20x15 Tile Display. My Picture (Yes the graphics suck but I'm only trying to aim at a grid system before I spent time on them. ^_^) http://img223.imageshack.us/img223/492/map1th7.png Now the graphic part really isn't important since the grid system would work fine without the background map it just gives you something to look at. For moving my Box (will act as the main sprite for now) would I set a timer so it moves by 32 X and 32 Y without moving super fast? I'm not sure how to make a grid system, something like in Diablo 1. Would I use an Array for this? With Map1X coords and Map1Y coords then have the main sprite move like ++Map1X; NOTE: Only helpful advice and information, please don't post maybe this is too hard for me, ect...
____________________VB/C++/C# Programmer
Advertisement
I just made a Map Array to match my Map1 picture.

// Map        int[,] MapOne = new int[15, 20]{            {1,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {1,1,1,1,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {1,1,1,1,1,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {1,1,1,1,1,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {1,1,1,1,1,1,0,0,0,2,2,2,1,0,0,0,0,0,0,0},            {1,1,1,1,1,1,0,0,0,2,2,2,1,1,0,0,0,0,0,0},            {1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,0,0,0},            {0,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1},            {0,0,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1},            {0,0,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1},            {0,0,0,0,0,1,1,1,1,2,2,2,1,1,1,1,1,1,0,0},            {0,0,0,0,0,0,0,0,0,2,2,2,0,1,1,1,0,0,0,0},            {0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},        };


Now I need to make some movement going with this map.

0 = Grass (Moveable)
1 = Water (Non-Moveable)
2 = Rock/Ground (Moveable)

^_^
____________________VB/C++/C# Programmer
In pretty much any game you want a render/update loop whereby you're updating repeatedly. In that case you should store the pixel position of your object (as a Point probably), and yes, each frame (or timestep), move the object by one pixel. You will probably then need to check for collisions against up to four tiles (think about moving diagonally).
Alright, I got the Array Map up, and got movement.

#region Using Statementsusing System;using System.Collections.Generic;using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;using Microsoft.Xna.Framework.Storage;#endregionnamespace Game{    /// <summary>    /// This is the main type for your game    /// </summary>    public class Game1 : Microsoft.Xna.Framework.Game    {        GraphicsDeviceManager graphics;        ContentManager content;        public Game1()        {            graphics = new GraphicsDeviceManager(this);            content = new ContentManager(Services);            graphics.PreferredBackBufferHeight = 480;            graphics.PreferredBackBufferWidth = 640;            BlockPosition = new Vector2(320, 448);        }        // Setup        Texture2D Map1;        Texture2D Block;        Vector2 BlockPosition = Vector2.Zero;        // SpriteBatch        SpriteBatch spriteBatch;        // Map        const int Map1Height = 15;        const int Map1Width = 20;        int[,] MapOne = new int[Map1Height, Map1Width]{            {1,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {1,1,1,1,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {1,1,1,1,1,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {1,1,1,1,1,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {1,1,1,1,1,1,0,0,0,2,2,2,1,0,0,0,0,0,0,0},            {1,1,1,1,1,1,0,0,0,2,2,2,1,1,0,0,0,0,0,0},            {1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,0,0,0},            {0,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1},            {0,0,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1},            {0,0,1,1,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,1},            {0,0,0,0,0,1,1,1,1,2,2,2,1,1,1,1,1,1,0,0},            {0,0,0,0,0,0,0,0,0,2,2,2,0,1,1,1,0,0,0,0},            {0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},            {0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,0,0,0,0},        };        // Map1 X, Y        int Map1X = 0;        int Map1Y = 0;        /// <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>        /// Load your graphics content.  If loadAllContent is true, you should        /// load content from both ResourceManagementMode pools.  Otherwise, just        /// load ResourceManagementMode.Manual content.        /// </summary>        /// <param name="loadAllContent">Which type of content to load.</param>        protected override void LoadGraphicsContent(bool loadAllContent)        {            if (loadAllContent)            {                // TODO: Load any ResourceManagementMode.Automatic content                Map1 = content.Load<Texture2D>("gfx/maps/Map1");                Block = content.Load<Texture2D>("gfx/sprites/Block");                spriteBatch = new SpriteBatch(graphics.GraphicsDevice);            }            // TODO: Load any ResourceManagementMode.Manual content        }        /// <summary>        /// Unload your graphics content.  If unloadAllContent is true, you should        /// unload content from both ResourceManagementMode pools.  Otherwise, just        /// unload ResourceManagementMode.Manual content.  Manual content will get        /// Disposed by the GraphicsDevice during a Reset.        /// </summary>        /// <param name="unloadAllContent">Which type of content to unload.</param>        protected override void UnloadGraphicsContent(bool unloadAllContent)        {            if (unloadAllContent == true)            {                content.Unload();            }        }        /// <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 default game to exit on Xbox 360 and Windows            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                this.Exit();            // TODO: Add your update logic here            Input();            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)        {            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);            // TODO: Add your drawing code here            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);            spriteBatch.Draw(Map1, new Rectangle(0, 0, 640, 480), Color.White);            spriteBatch.Draw(Block, BlockPosition, Color.White);            spriteBatch.End();            base.Draw(gameTime);        }        // Movement        public void Input()        {            // Setup            BlockPosition.Y = Map1Y;            BlockPosition.X = Map1X;            // Up            if (Keyboard.GetState().IsKeyDown(Keys.Up))            {                Map1Y--;                BlockPosition.Y = Map1Y;            }            // Down            if (Keyboard.GetState().IsKeyDown(Keys.Down))            {                Map1Y++;                BlockPosition.Y = Map1Y;            }            // Left            if (Keyboard.GetState().IsKeyDown(Keys.Left))            {                Map1X--;                BlockPosition.X = Map1X;            }            // Right            if (Keyboard.GetState().IsKeyDown(Keys.Right))            {                Map1X++;                BlockPosition.X = Map1X;            }        }    }}


If anyone can help me understand how to do grid movement with my Array Map? I havn't drawn it to the screen yet either because I'm not sure how to go about making that work without a Texture2D.
____________________VB/C++/C# Programmer

This topic is closed to new replies.

Advertisement