pygame.rect中 Rect类 属性示意

部分pygame.rect源代码如下

class Rect(object):
    x: int
    y: int
    top: int
    left: int
    bottom: int
    right: int
    topleft: Tuple[int, int]
    bottomleft: Tuple[int, int]
    topright: Tuple[int, int]
    bottomright: Tuple[int, int]
    midtop: Tuple[int, int]
    midleft: Tuple[int, int]
    midbottom: Tuple[int, int]
    midright: Tuple[int, int]
    center: Tuple[int, int]
    centerx: int
    centery: int
    size: Tuple[int, int]
    width: int
    height: int
    w: int
    h: int
    __hash__: None  # type: ignore

示意图
在这里插入图片描述

  • 除 x,y wight,height以外, 实际英文单词含义为 top, left bottom ,right 四条边的坐标(取不为0的坐标)
  • x,y : 矩形左上A点坐标, 与left ,top 相同
  • bottom,right : 右下B点坐标
  • 其余属性均为交点,或中点的x,y坐标组成的元组,望图思意即可
  • 增大图形时考虑 wight,height(h,w属性与其相同)

测试代码

import pygame

def test():
    """ 测试pygame.rect中Rect类的属性 """

    # 初始化pygame,设置background 
    pygame.init()
    screen = pygame.display.set_mode([1200, 600])
    pygame.display.set_caption("backgroundd")
    pygame.display.update()

    # 获取分辨率,可以不加
    pygame.display.list_modes() 

    # 获取 surface 的外接矩形
    screen_rect = screen.get_rect()

    # 创建 Rect 实例 Rect(left, top, width, height)
    rect = pygame.Rect(20, 20, 100, 300)
    rect_color = [250, 250, 250]

    # rect.x = 0
    # rect.y = 0 
    # rect.bottom = 600
    # rect.right = 1200
    rect.h = 600

    while True:
        # background 填充颜色
        screen.fill([0, 0, 0])

        # 画出外接矩形
        pygame.draw.rect(screen, rect_color, rect)

        # 更新整个 surface 对象
        pygame.display.flip()

test()

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值