1 Replies - 3946 Views - Last Post: 27 October 2007 - 08:48 PM Rate Topic: -----

#1 pspfreak101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 28-July 07

Tile collision

Posted 13 October 2007 - 09:15 AM

I'm making a little pacman game I have my tile map finished but just need help on setting my collision on tile 2 so pacman can only move on tile 0


heres my code:

namespace Packman
{
	/// <summary>
	/// This is the main type for your game
	/// </summary>
	public class Game1 : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		ContentManager content;
		bool canWalk;

		public Game1()
		{
			graphics = new GraphicsDeviceManager(this);
			content = new ContentManager(Services);
			graphics.PreferredBackBufferHeight = 630;
			graphics.PreferredBackBufferWidth = 630;
			graphics.ApplyChanges();
		}


		/// <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();
		}
		//2D textures
		Texture2D pacman;
		Vector2 pacmanPosition = new Vector2(0, 270);
		SpriteBatch spriteBatch;
		/// <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>


		// An array of "Texture2D" objects to hold our Tile Set
		Texture2D[] t2dTiles = new Texture2D[3];
		const int iMapWidth = 21;
		const int iMapHeight = 21;
		// Our simple integer-array based map
		int[,] iMap = new int[iMapHeight, iMapWidth] {
							 { 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0}, 
							 { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0},
							 { 0, 2, 0, 2, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 0, 2, 0},
							 { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0},
							 { 0, 2, 0, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 0, 2, 0},
							 { 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0},
							 { 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0},
							 { 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0},
							 { 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2},
							 { 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0},
							 { 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2},
							 { 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0},
							 { 0, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 0},
							 { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0},
							 { 0, 2, 0, 2, 2, 0, 2, 2, 2, 0, 2, 0, 2, 2, 2, 0, 2, 2, 0, 2, 0},
							 { 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0},
							 { 0, 2, 2, 0, 2, 0, 2, 0, 2, 2, 2, 2, 2, 0, 2, 0, 2, 0, 2, 2, 0},
							 { 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0},
							 { 0, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 0},
							 { 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0},
							 { 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0},
							
		};
		// Variable we will need for Keyboard Input
		KeyboardState ksKeyboardState;
		//Map coordinates for upper left corner
		int iMapX = 0;
		int iMapY = 0;
		// How far from the Upper Left corner of the display do we want our map to start
		int iMapDisplayOffsetX = 0;
		int iMapDisplayOffsetY = 0;
		// How many tiles should we display at a time
		int iMapDisplayWidth = 21;
		int iMapDisplayHeight = 21;
		// The size of an individual tile in pixels
		int iTileWidth = 30;
		int iTileHeight = 30;
	   
		protected override void LoadGraphicsContent(bool loadAllContent)
		{
			if (loadAllContent)
			{
				// TODO: Load any ResourceManagementMode.Automatic content
				t2dTiles[0] = content.Load<Texture2D>("blank");
				t2dTiles[1] = content.Load<Texture2D>("grass");
				t2dTiles[2] = content.Load<Texture2D>("block1");
				pacman = content.Load<Texture2D>("pacman");
				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)
			{
				// TODO: Unload any ResourceManagementMode.Automatic content
				content.Unload();
			}

			// TODO: Unload any ResourceManagementMode.Manual content
		}


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

			KeyboardState newState = Keyboard.GetState();

			// Allows the default game to exit on Xbox 360 and Windows.
			if (newState.IsKeyDown(Keys.Escape))
			{
				this.Exit();
			}

			// Move the sprite around.
			UpdateSprite(gameTime);
			Collision();
			base.Update(gameTime);
		}
		void UpdateSprite(GameTime gameTime)
		{
			KeyboardState newState = Keyboard.GetState();

			if (newState.IsKeyDown(Keys.Down) && canWalk == true)
			{
				pacmanPosition.Y = pacmanPosition.Y + 3;
			}

			if (newState.IsKeyDown(Keys.Up) && canWalk == true)
			{
				pacmanPosition.Y = pacmanPosition.Y - 3;
			}

			if (newState.IsKeyDown(Keys.Left) && canWalk == true)
			{
				pacmanPosition.X = pacmanPosition.X - 3;
			}

			if (newState.IsKeyDown(Keys.Right) && canWalk == true)
			{
				pacmanPosition.X = pacmanPosition.X + 3;
			}
			
			if (pacmanPosition.X < 0) { pacmanPosition.X = 0; }
			if (pacmanPosition.X > 600) { pacmanPosition.X = 600; }
			if (pacmanPosition.Y < 0) { pacmanPosition.Y = 0; }
			if (pacmanPosition.Y > 600) { pacmanPosition.Y = 600; }

		}
		public void Collision()
		{
			//Collision here
			if (pacmanPosition.X == t2dTiles[2].Width ) { canWalk = false; } else { canWalk = true; }
			if (pacmanPosition.Y == t2dTiles[2].Height ) { canWalk = false; } else { canWalk = true; }
			
		}
		
		/// <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.Black);
			spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
			// Draw the map
			for (int y = 0; y < iMapDisplayHeight; y++)
			{
				for (int x = 0; x < iMapDisplayWidth; x++)
				{
					spriteBatch.Draw(t2dTiles[iMap[y + iMapY, x + iMapX]],
									 new Rectangle((x * iTileWidth) + iMapDisplayOffsetX,
									 y * iTileHeight + iMapDisplayOffsetY, iTileWidth, iTileHeight),
									 Color.White);
				}
			}
			spriteBatch.Draw(pacman, pacmanPosition, Color.White);
			spriteBatch.End();
			base.Draw(gameTime);

		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Tile collision

#2 awesty  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 27-October 07

Re: Tile collision

Posted 27 October 2007 - 08:48 PM

If pacman is moving up check if the tile above him is '2', and if it is he can stop moving or change direction. Just do the same for all the other sides and directions as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1