Click here to Skip to main content
15,564,467 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
hey guys,

i have finished making my programming language and i have 'moved on' from making them for a while, just to give my brain a break. I thought about making a game, a console based game and found that it is really easy, except for one thing. I am stuck on collision detection. This is my code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Movement_Engine
{
    class Program
    {
        const ConsoleColor hero_color = ConsoleColor.Black;

        public static Coordinate Hero { get; set; }

        static void Main(string[] args)
        {
            InitGame();

            ConsoleKeyInfo keyInfo;
            while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Escape)
            {
                switch (keyInfo.Key)
                {
                    case ConsoleKey.UpArrow:
                        MoveHero(0, -1);
                        break;

                    case ConsoleKey.RightArrow:
                        MoveHero(1, 0);
                        break;

                    case ConsoleKey.DownArrow:
                        MoveHero(0, 1);
                        break;

                    case ConsoleKey.LeftArrow:
                        MoveHero(-1, 0);
                        break;
                }
            }
        }

        static void MoveHero(int x, int y)
        {
            Coordinate newHero = new Coordinate()
            {
                x = Hero.x + x,
                y = Hero.y + y
            };

            if (CanMove(newHero))
            {
                RemoveHero();

                Console.BackgroundColor = hero_color;
                Console.SetCursorPosition(newHero.x, newHero.y);
                Console.Write("@");

                Hero = newHero;
            }
        }

        static void RemoveHero()
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.SetCursorPosition(Hero.x, Hero.y);
            Console.Write(" ");
        }

        static bool CanMove(Coordinate c)
        {
            if (c.x < 0 || c.x >= Console.WindowWidth)
                return false;

            if (c.y < 0 || c.y >= Console.WindowHeight)
                return false;

            return true;
        }

        static void InitGame()
        {
            SetBackgroundColor();

            Hero = new Coordinate()
            {
                x = 0,
                y = 0
            };

            MoveHero(0,0);
        }

        static void SetBackgroundColor()
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.Clear();
        }
    }

    class Coordinate
    {
        public int x { get; set; }
        public int y { get; set; }
    }
}


as you can see, my character is the '@' symbol and you walk around. You cannot go outside of the window either. What i want is to have a box around the character, out of '#' for simplicity, and you cannot go outside of that box. That would be collision detection, right?

if it isn't collision detection, can you please give me an example collision detection system? Could you also copy and paste this code and just add to it the collision detection/box so that i understand what you have done.

Thanks!
Posted

1 solution

i guess the map is in a 2D array or something similar? if that is the case it is really easy to work around, just make a check whether the spot you move to is occupied, EG. add this to move hero:

C#
if (somearray[someintx + hero.x, someinty + hero.y] == " ")
{
     //TODO: Add movement code here
}


for detecting items you can pick up do just the same but change " " to "(ItemSymbol)" and so on, not too hard when you first get the hand around it.

- Jackie
 
Share this answer
 
Comments
Member 8378691 1-Jul-12 21:36pm    
thank you for your solution. I cannot press accept because it is not fully complete, but i have tagged you at the bottom of my response

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900