我在pygame中用python(2.7)编写了一个简单的游戏.在这个游戏中,我必须存储2D坐标.这些项目的数量将从0开始,每步增加2.它们将增加到6000左右.在每个步骤中,我必须检查它们中是否有9个特定坐标.我试图将它们简单地存储在列表中(x,y),但在这样的列表中搜索效率不高.
如何存储这些坐标,以便在它们之间进行搜索更有效?
我在每一步中都想做的事情:
# Assuming:
myList = []
co1 = (12.3,20.2) # and so on..
valuesToCheck = [co1,co2,co3,co4,co5,co6,co7,co8,co9]
# In each step:
# Adding 2 coordinates
myList.append((x1,y1))
myList.append((x2,y2))
# Searching 9 specific coordinates among all
for coordinate in valuesToCheck:
if coordinate in myList:
print "Hit!"
break
# Note that the valuesToCheck will change in each step.
del valuesToCheck[0]
valuesToCheck.append(co10)
Run Code Online (Sandbox Code Playgroud)
坐标是浮点数,它们的最高值是有限的.它们从(0.0,0.0)开始到(1200.0,700.0).
我搜索了这个,但存储的值是字符串或常数.
我正在使用sf::Texture另一个库中的类()。我想获得在该类中声明类型别名的功能,例如:
class Texture
{
public:
using ResourceId = TextureId;
//...
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以在我的代码中做到这一点:
enum class TextureId
{
texture1,
texture2 //..etc
}
template<class ResourceType>
class ResourceContainer
{
public:
ResourceContainer();
private:
ResourceType* resource_;
typename ResourceType::ResourceId id; // <--- this will have TextureId type
// when we create this object
// with <sf::Texture>
}
Run Code Online (Sandbox Code Playgroud)
但是,正如我提到的,Texture该类来自另一个库,因此我无法编辑其声明。
我试图在代码中将using sf::Texture::ResourceId = TextureId;其声明为,但它不起作用(无法解析符号ResourceId)。
那么,有没有一种替代方法可以在不添加ResourceId第二个模板参数的情况下获得相同的功能?
(使用C ++ 17)