简体   繁体   English

如何将一个单位表示为pharo中的一个点?

[英]How to represent a unit as a point in pharo?

I am trying to program a game in Pharo. 我正在尝试在Pharo中编写游戏。 I have just started to code in Pharo, so I am trying to get used to it. 我刚刚开始在Pharo中进行编码,因此我正在努力适应它。
The game should include a game map and each character(or thing) is represented by a symbol. 游戏应包括游戏地图,并且每个角色(或事物)均由符号表示。
The very first thing I did was to create a GameMapClass which contains a GameMap object. 我做的第一件事是创建一个包含GameMap对象的GameMapClass。
Then I drew the map, which was easy. 然后我绘制了地图,这很容易。
The next thing to do is to put units. 接下来要做的是放置单元。 I have a Unit class(and some other classes that inherit the Unit class). 我有一个Unit类(以及其他一些继承Unit类的类)。 I also have a unitAt method in my GameMapClass which is so far like this: 我的GameMapClass中也有一个unitAt方法,到目前为止,它是这样的:

unitAt: aPoint |thePoint| ^thePoint := map at: aPoint ifAbsent: [self error: 'The space is empty'].

I don't know how to proceed in this method. 我不知道如何进行这种方法。 The point should also returns the unit if it is not empty. 如果该点不为空,则该点也应返回该单位。 How do I return for multiple times? 我如何多次返回? Each point is aPointin my program. 每个点都是我程序中的一个要点。 I also need to use dictionary to represent "things" with symbols and I have no idea about where to put this dictionary and how to access to it. 我还需要使用词典来用符号表示“事物”,而且我不知道该词典放在何处以及如何访问它。

Here's a way to do it: 这是一种方法:

unitAt: aPoint
    | theUnit |
    theUnit := map 
        at: aPoint
        ifAbsent: [ self error: 'The space is empty' ].
    ^ { theUnit. aPoint }

Please consider @Uko's comment though: you pass in the point in this manner: 但是请考虑@Uko的评论:您以这种方式通过了这一点:

 unitAndPoint := myObject unitAt: myPoint.

If you look at this carefully you'll see that the variable unitAndPoint contains the same point object myPoint that you are passing as an argument. 如果仔细查看,您会发现变量unitAndPoint包含与您作为参数传递的点对象myPoint相同的对象。 That means you already know the point and there is no reason for you to return it from unitAt: . 这意味着您已经知道要点, 没有理由unitAt:返回它。 Here's how I think the solution should (probably) look like: 我认为解决方案应该(可能)是这样的:

unitAt: aPoint
    ^ map 
        at: aPoint
        ifAbsent: [ self error: 'The space is empty' ]

Called like this: 这样称呼:

myPoint := 1@2.
unitOfPoint := myObject unitAt: myPoint.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM