Pygame有两种表示矩形区域的方法(就像有两种表示颜色的方法)。
第一种是四个整数的元组:
左上角的X坐标。
左上角的Y坐标。
矩形的宽度(以像素为单位)。
然后是矩形的高度(以像素为单位)。
第二种方式是
作为pygame.Rect 对象,我们将简称为Rect对象。例如,下面的代码创建一个Rect对象,其左上角位于(10,20),宽200像素,高300像素:
>>> import pygame
>>> spamRect = pygame.Rect(10, 20, 200, 300)
>>> spamRect == (10, 20, 200, 300)
True
关于这一点的一个方便之处是Rect对象自动计算矩形的其他特征的坐标。例如,如果您需要知道我们存储在spamRect 变量中的pygame.Rect对象右边缘的X坐标,您只需访问Rect对象的right 属性:
>>> spamRect.right
210
Rect对象的Pygame代码自动计算,如果左边缘位于X坐标10并且矩形宽度为200像素,则右边缘必须位于X坐标210.如果重新分配右边属性,则所有其他边缘属性会自动重新计算:
>>> spamRect.right = 350
>>> spamRect.left
150
这是pygame.Rect对象提供的所有属性的列表(在我们的示例中,Rect对象存储在名为spamRect的变量中的变量):
属性名称 | 描述 |
---|---|
myRect.left | The int value of the X-coordinate of the left side of the rectangle.(矩形左侧X坐标的int值) |
myRect.right | The int value of the X-coordinate of the right side of the rectangle.(矩形右侧X坐标的int值) |
myRect.top | The int value of the Y-coordinate of the top side of the rectangle.(矩形顶边的Y坐标的int值) |
myRect.bottom | The int value of the Y-coordinate of the bottom side.(底边的Y坐标的int值) |
myRect.centerx | The int value of the X-coordinate of the center of the rectangle.(矩形中心的X坐标的int值) |
myRect.centery | The int value of the Y-coordinate of the center of the rectangle.(矩形中心的Y坐标的int值) |
myRect.width | The int value of the width of the rectangle.(矩形宽度的int值) |
myRect.height | The int value of the height of the rectangle.(矩形高度的int值) |
myRect.size | A tuple of two ints: (width, height)(两个整数的元组:(宽度,高度)) |
myRect.topleft | A tuple of two ints: (left, top)(两个整数的元组:(左,上)) |
myRect.topright | A tuple of two ints: (right, top)(两个整数的元组:(右,顶部)) |
myRect.bottomleft | A tuple of two ints: (left, bottom)(两个整数的元组:(左,下)) |
myRect.bottomright | A tuple of two ints: (right, bottom)(两个整数的元组:(右,底)) |
myRect.midleft | A tuple of two ints: (left, centery)(两个整数的元组:(左,中心)) |
myRect.midright | A tuple of two ints: (right, centery)(两个整数的元组:(右,居中)) |
myRect.midtop | A tuple of two ints: (centerx, top)(两个整数的元组:( centerx,top)) |
myRect.midbottom | A tuple of two ints: (centerx, bottom)(两个整数的元组:( centerx,bottom)) |
网友评论