C#控制台游戏碰撞检测 [英] C# Console Game Collision Detection

查看:115
本文介绍了C#控制台游戏碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经完成了我的编程语言的开发,并且我已经离开"了一段时间,只是为了让我的大脑休息一下.我考虑过制作一款基于控制台的游戏,发现除了一件事情外,这确实很容易.我陷入了碰撞检测的困境.这是我的代码:

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:

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!

推荐答案

我想地图是二维数组或类似的东西?如果真是这样,那就很容易解决,只需检查一下您要移动的位置是否已被占用,例如EG.添加此以移动英雄:

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:

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



用于检测项目的方法可以相同,只是将"更改为(ItemSymbol)",依此类推,当您初次接触它时不要太费力.

-杰基



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


这篇关于C#控制台游戏碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆