Grid Based Games
-
I understand how to create animation like pacman, but to move the pacman, I can only think of using location. Problem is, how could I know there is obstacle or not?
-
I understand how to create animation like pacman, but to move the pacman, I can only think of using location. Problem is, how could I know there is obstacle or not?
Ultimately, the object will have a coordinate. This could be used to perform a rudimentary collision detection. The process could be as simple as this:
public class BaseGameObject
{
public int X { get; set; }
public int Y { get; set; }
}public class GameObjects : BaseGameObject
{
public List<BaseGameObject> GameObjects { get; set; }public BaseGameObject HitTest(int x, int y)
{
foreach (BaseGameObject gameObject in GameObjects)
{
if (gameObject.X == x && gameObject.Y == y)
{
return gameObject;
}
}
return null;
}
}I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
Ultimately, the object will have a coordinate. This could be used to perform a rudimentary collision detection. The process could be as simple as this:
public class BaseGameObject
{
public int X { get; set; }
public int Y { get; set; }
}public class GameObjects : BaseGameObject
{
public List<BaseGameObject> GameObjects { get; set; }public BaseGameObject HitTest(int x, int y)
{
foreach (BaseGameObject gameObject in GameObjects)
{
if (gameObject.X == x && gameObject.Y == y)
{
return gameObject;
}
}
return null;
}
}I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierThank you very much for the help. I will try to understand it as I never use get set or that kind of class.
-
I understand how to create animation like pacman, but to move the pacman, I can only think of using location. Problem is, how could I know there is obstacle or not?
What you are looking for, as the above poster pointed out, is Collision Detection, you should be able to find plenty of sources on it, I will list a few: Collision Detection[^] on wikipedia (theory) Testing for Collisions[^] on MSDN, this is for (the now unsupported) XNA Framework Pacman in C# (map & collision detection)[^] A question here in CodeProject C# Game Programming for Teens[^] In this book you will follow the chapters to make a 2D Dungeon Crawler. Chapter 4 deals with collision detection EDIT: If you are using sprites (which I guess is the case) you have to check if the object's rectangle intersects with any other object's rectangle, take into account that the rectangle also counts the transparent pixels of your sprite. You could do your own way of checking for an intersection between two rectangles, but you can use the IntersectsWith[^] method. If your object is non-player controlled, it most likely has a velocity, so you can predict the collision by calculating the coordinates for the next iteration and checking for intersections with those coordinates. Example: Player controlled object:
public class PlayerObject
{
public Point Location {get; set;}
public Size ObjectSize (get; set;}
public Rectangle Bounds {get; set;}public PlayerObject(Point loc, Size objSize)
{
Location = loc;
ObjectSize = objSize;
Bounds = new Rectangle(Location.X, Location.Y, Location.X + Size.Width, Location.Y + Size.Height);
}//Collision detection