Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Grid Based Games

Grid Based Games

Scheduled Pinned Locked Moved C#
csshelptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Midnight Ahri
    wrote on last edited by
    #1

    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?

    P E 2 Replies Last reply
    0
    • M Midnight Ahri

      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?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • P Pete OHanlon

        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

        M Offline
        M Offline
        Midnight Ahri
        wrote on last edited by
        #3

        Thank you very much for the help. I will try to understand it as I never use get set or that kind of class.

        1 Reply Last reply
        0
        • M Midnight Ahri

          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?

          E Offline
          E Offline
          Emmanuel Medina
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups