面向对象的问题 [英] Object Oriented Problem

查看:77
本文介绍了面向对象的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会遗漏一些非常明显的东西,但是我一辈子都无法弄清楚是什么.我正在制作一个冒险游戏,角色在环游世界的过程中会看到不同的事物.我做到了,当他移动他的X和Y坐标时,它也会相应地发生变化(因此,如果他向北移动,Y将为1),这样就可以了.我的问题是当他进入某个区域时会出现一些东西.

使角色移动在一个类(播放器)和另一个类(地图)中都是发生的,我有以下代码行:


I''m probably missing something incredibly obvious, but I can''t for the life of me figure out what. I''m making an adventure game, where the character sees different things on his path through the world. I''m making it so, when he moves his X and Y co-ordinates change accordingly (so if he moves North, Y will be 1) That''s working fine. My problem is getting something to show up when he moves into an area.

Making the character move is all happening in one class (Player) and in another class (Map) I have this line of code:


string comment;

public void PeopleAtLoc(Player ThePlayer)
{
    if (ThePlayer.XPos == 1)
    {
        comment = "You walked into a tree";
    }
}




我的目标是,如果玩家移动到某个位置(即x = 1),那么它应该打印出那里的东西,然后让玩家继续自己的旅程.

在另一堂课中,我有这段代码:




What I''m aiming for is that if the player moves to a certain position (i.e. x=1) then it should print something that''s there, and then allow the player to continue on their journey.

In another class, I have this block of code:

public void eMove()
{
    Console.WriteLine("In which direction would you want to move?");
    Console.WriteLine("\n1) North \n 2) South \n 3) East \n 4) West");
    int nInput = int.Parse(Console.ReadLine());
    switch (nInput)
    {
        case 1:
            m_nYPos++;
            break;
        case 2:
            m_nYPos--;
            break;
        case 3:
            m_nXPos++;
            break;
        case 4:
            m_nXPos--;
            break;
    }
    Console.WriteLine("\nYour current position is X: " + XPos + " Y: " + YPos + "\n\n");
}



因此,基本上,我想问我将如何打印那段在x1处写的代码,例如,您会看到一棵树.

预先感谢.



So, basically, I''m asking how I would go about printing that piece of code that says at point x1 for example, you would see a tree.

Thanks in advance.

推荐答案

好吧,玩家对象需要能够看到地图对象(也许通过将地图作为参数传递给玩家对象).的构造函数).那时,您可以在eMove方法的结尾处​​执行此操作:

Well, the player object needs to be able to see the map object (maybe by passing the map as a parameter in the player object''s constructor). At that point you could do this at the end of the eMove method:

map.PeopleAtLoc(this);


如何使用Move方法在Map类上

因此,地图将使用Player对象(我认为该对象具有当前位置"字段?!)和一个参数来指示玩家正在向哪个方向移动

这里的另一个类可能会有用.用于处理方向的枚举和用于保持方向+在特定方向上移动的类(例如,向北移动1 \ 2 \ 3等)

How about having the Move method on the Map class

So the map would take Player object (which I''m assuming has a ''current location'' field?!), and a parameter to indicate which direction the player is moving in

Another class here would probably be useful. An enum to handle direction and a class to hold the direction + movement in certain direction (e.g move North 1 \ 2 \ 3 etc)

public enum Direction
{
    North = 1,
    South = 2 ,
    East = 3,
    West = 4
}


public class Movement
{
    public Direction Heading {get; set;}
    public int Advancement {get; set;}
}



因此,您的Map对象现在可以支持类似
的方法
map.Move(播放器,运动动作)

然后,您的地图对象会引发一个事件,例如PlayerMoved

您需要为事件定义一些参数,例如PlayerMovedEventArguments.这应该保持玩家移动到的新位置以及该位置发生的任何事情(例如走进树上)

您可以将其传递回控制类,以使用某些变量(例如"LocationInfo"
)打印出信息.



So your Map object could now support a method like

map.Move (Player player, Movement movement)

Your map object would then raise an event such as PlayerMoved

You''d need to define some arguments for the event, e.g. PlayerMovedEventArguments. This should hold the new position the player has moved to and anything that happened at that location (such as walking into a tree)

You could pass that back to the controlling class to print out the info in some variable such as ''LocationInfo''


private void Map_OnPlayerMoved(object sender, PlayerMovedEventArguments e)
{
     Console.WriteLine(e.LocationInfo);
}



HTH!



HTH!


这篇关于面向对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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