Pygame Rect对象的.width和.w属性有什么区别?

时间:2015-06-24 11:47:13

标签: pygame rect

官方pygame documentation声明有几个虚拟属性可用于移动和对齐pygame.Rect个实例:

Virtual Attributes of a PyGame Rect Object

到目前为止我分别使用myRect.w myRect.h来确定pygame.Rect对象的宽度或高度。但是为了完成此图形,我遇到了 .width.height属性

有趣的是,这两个属性似乎为我们提供了相同的日期,如下面的代码清单所示:

>>> myRect = pygame.Rect((10, 20), (200,100)) #create a new Rect instance
>>> myRect.w
200
>>> myRect.width
200
>>> myRect.size
(200, 100)

这两个属性对之间的区别是什么?

1 个答案:

答案 0 :(得分:1)

没有区别。您可以查看Rect类的source

static PyGetSetDef rect_getsets[] = {
    { "x", (getter)rect_getleft, (setter)rect_setleft, NULL, NULL },
    { "y", (getter)rect_gettop, (setter)rect_settop, NULL, NULL },
    { "w", (getter)rect_getwidth, (setter)rect_setwidth, NULL, NULL },
    { "h", (getter)rect_getheight, (setter)rect_setheight, NULL, NULL },
    { "width", (getter)rect_getwidth, (setter)rect_setwidth, NULL, NULL },
    { "height", (getter)rect_getheight, (setter)rect_setheight, NULL, NULL },
    { "top", (getter)rect_gettop, (setter)rect_settop, NULL, NULL },
    { "left", (getter)rect_getleft, (setter)rect_setleft, NULL, NULL },
    { "bottom", (getter)rect_getbottom, (setter)rect_setbottom, NULL, NULL },
    { "right", (getter)rect_getright, (setter)rect_setright, NULL, NULL },
    { "centerx", (getter)rect_getcenterx, (setter)rect_setcenterx, NULL, NULL },
    { "centery", (getter)rect_getcentery, (setter)rect_setcentery, NULL, NULL },
    { "topleft", (getter)rect_gettopleft, (setter)rect_settopleft, NULL, NULL },
    { "topright", (getter)rect_gettopright, (setter)rect_settopright, NULL,
     NULL },
    { "bottomleft", (getter)rect_getbottomleft, (setter)rect_setbottomleft,
      NULL, NULL },
    { "bottomright", (getter)rect_getbottomright, (setter)rect_setbottomright,
      NULL, NULL },
    { "midtop", (getter)rect_getmidtop, (setter)rect_setmidtop, NULL, NULL },
    { "midleft", (getter)rect_getmidleft, (setter)rect_setmidleft, NULL, NULL },
    { "midbottom", (getter)rect_getmidbottom, (setter)rect_setmidbottom, NULL,
      NULL },
    { "midright", (getter)rect_getmidright, (setter)rect_setmidright, NULL,
      NULL },
    { "size", (getter)rect_getsize, (setter)rect_setsize, NULL, NULL },
    { "center", (getter)rect_getcenter, (setter)rect_setcenter, NULL, NULL },

    { "__safe_for_unpickling__", (getter)rect_getsafepickle, NULL, NULL, NULL },
    { NULL, 0, NULL, NULL, NULL }  /* Sentinel */
};

你可以看到,例如wwidth都致电rect_getwidth

/*width*/
static PyObject*
rect_getwidth (PyRectObject *self, void *closure)
{
    return PyInt_FromLong (self->r.w);
}

我仍然建议您使用width / height代替w / h以提高可读性。