《pygame游戏开发实战指南》第四节 认识Recct对象

在前面的文章中,我们已经多次接触到了Rect对象。当Surface对象调用get_rect()函数后就会返回一个Rect对象。本节将详细介绍有关Rect对象的知识点。

项目代码下载地址: https://github.com/la-vie-est-belle/pygame_codes


4.1 什么是Rect以及如何创建Rect对象

在pygame中,Rect对象用来存储和控制被绘制对象所在的矩形区域。在上一节的示例代码3-1中,我们加载了小恐龙图片,并通过get_rect()函数获得了一个Rect对象,然后通过该Rect对象更新了小恐龙所在的矩形区域。

dino = pygame.image.load('dino_start.png').convert_alpha()
dino_rect = dino.get_rect()
dino_rect.topleft = (80, 450)

除了get_rect函数,我们还可以直接通过Rect类来实例化一个Rect对象,只需要传入左上角xy坐标和宽高值,请看示例代码4-1。

pygame.Rect(left, top, width, height)
import sys
import pygame


pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')

rect_area = pygame.Rect(0, 0, 100, 100)                 # 1
rect_area.topleft = (10, 10)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, (0, 255, 0), rect_area)    # 2

    pygame.display.flip()

运行结果如下:
示例代码4-1运行结果

代码解释如下:
#1 通过Rect类实例化了一个坐标为(0, 0),宽高都为100像素的Rect对象,保存到rect_area变量中。然后将该矩形区域的左上角坐标设为了(0, 0)

#2 调用pygame.draw模块中的rect()函数在屏幕上绘制一个绿色矩形,传入rect_area设置该绘制对象所在的矩形区域。

注:笔者会在下一结详细讲解如何在屏幕上绘制各种几何图形。


4.2 Rect对象的常用属性和函数

首先来看下Rect对象的属性。

  • x: 左上角x坐标。
  • y: 左上角y坐标。
  • w: 宽度。
  • h: 高度。
  • size: 尺寸,是一个元组(width, height)。
  • top: 左上角y坐标,等同于y。
  • left: 左上角x坐标,等同于x。
  • bottom: 右下角y坐标。
  • right: 右下角x坐标。
  • centerx: 中心点x坐标。
  • centery: 中心点y坐标。
  • center: 中心点(x, y)坐标。
  • topleft: 左上角(x, y)坐标。
  • topright: 右上角(x, y)坐标。
  • bottomleft: 左下角(x, y)坐标。
  • bottomright: 右下角(x, y)坐标。
  • midtop: 上边中心点(x, y)坐标。
  • midleft: 左边中心点(x, y)坐标
  • midright: 右边中心点(x, y)坐标。

在下面的示例代码4-2中我们使用了一些属性。

import sys
import pygame


pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')

rect_area = pygame.Rect(0, 0, 100, 100)
rect_area.topleft = (10, 10)							# 1
print(rect_area.topleft)

rect_area.width = 200                                   # 2
rect_area.height = 300
print(rect_area.width)
print(rect_area.height)

rect_area.center = (int(screen.get_width()/2), int(screen.get_height()/2))  # 3
print(rect_area.center)
print(rect_area.topleft)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, (0, 255, 0), rect_area)

    pygame.display.flip()

运行结果如下:
示例代码4-2运行结果

代码解释如下:
#1Rect对象的左上角坐标设置为(10, 10)并打印出来。

#2 设置Rect对象的宽高,并打印出来。

#3 设置Rect对象的中心点坐标为(int(screen.get_width()/2), int(screen.get_height()/2))屏幕中心。

从以上代码示例中我们知道,既可以获取对象属性又可以直接给对象属性赋值。当修改其中一个属性时,其他属性也可能会跟着改变。比如给center属性赋值后,Rect对象的topleft属性返回了(150, 100)这个坐标。

注:所有属性都只接收整数或者整数元组。


下面是Rect对象的一些常用函数。

  • copy(): 复制一个Rect对象。
  • move(x, y): 移动矩形区域,这里的x和y是指x和y方向上移动的偏移量。
  • move_ip(x, y): 同上,只不过该函数是对原Rect对象直接进行操作,返回值为None。而move(x, y)函数会返回一个变换后的Rect对象。
  • inflate(x, y): 缩放矩形区域,这里的x和y是指x和y方向上的缩放量。如果传入的值是正的,就表示放大矩形区域,反之则缩小。
  • inflate_ip(x, y): 同上,只不过该函数是对原Rect对象直接进行操作,返回值为None。而inflate(x, y)函数会返回一个变换后的Rect对象。
  • scale_by(x, y): 按倍数缩放矩形区域,这里的x和y是指x和y方向上的缩放倍数。如果传入0-1之间的值,就会按照指定倍数缩小;如果传入大于1的值,则会按照指定倍数放大。
  • scale_by_ip(x, y): 同上,只不过该函数是对原Rect对象直接进行操作,返回值为None。而scale_by(x, y)函数会返回一个变换后的Rect对象。
  • contains(Rect): 判断传入的Rect对象所表示的矩形区域是否被包含在调用者Rect对象所表示的矩形区域中。如果是包含的,则返回True,否则返回False。
  • collidepoint(x, y): 判断坐标(x, y)是否在调用者Rect对象所表示的矩形区域中。是的话返回True,否则返回False。
  • colliderect(Rect): 判断传入的Rect对象所表示的矩形区域和调用者Rect对象所表示的矩形区域是否有重合。有的话返回True,否则返回False。
  • collidelist(list): 传入一个Rect列表,返回列表中第一个跟调用者矩形区域有重合的Rect对象。没有一个是重合的话,返回-1。
  • collidelistall(list): 传入一个Rect列表,返回列表中跟调用者矩形区域有重合的Rect对象索引所组成的列表。没有一个是重合的话,返回空列表。

在示例代码4-3中我们使用了其中一些函数。

import sys
import pygame


pygame.init()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption('pygame demo')

rect_area = pygame.Rect(0, 0, 100, 100)
rect_area_copy1 = rect_area.copy()                      # 1
rect_area_copy1.topleft = (50, 50)                  
rect_area_copy2 = rect_area.copy()
rect_area_copy2.topleft = (10, 60)

rect_area = rect_area.move(10, 10)                      # 2
rect_area.scale_by_ip(0.5, 0.5)                         # 3

print(rect_area.contains(rect_area_copy1))              # 4
print(rect_area.colliderect(rect_area_copy1))           # 5
print(rect_area.collidelistall([rect_area_copy1, rect_area_copy2]))  # 6

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, (0, 255, 0), rect_area)
    pygame.draw.rect(screen, (0, 0, 255), rect_area_copy1)
    pygame.draw.rect(screen, (255, 0, 0), rect_area_copy2)
    pygame.display.flip()

运行结果如下:
示例代码4-3运行结果

代码解释如下:
#1 调用copy()函数复制出了两个Rect对象。

#2 调用move()函数移动了rect_area,该函数会返回一个变换后的Rect对象,我们继续将其保存在rect_area变量中。

#3 调用scale_by_ip()函数传入0.5,将宽和高都变成原来的一半。由于该函数是在原Rect对象上操作的,所以没有返回值。

#4 调用contains()函数判断rect_area的矩形区域是否包含rect_area_copy1的矩形区域。运行后会返回False

#5 调用colliderect()函数判断rect_area的矩形区域和rect_area_copy1的矩形区域是否有重合。运行后会返回True

#6 调用collidelistall()函数判断rect_area的矩形区域和列表[rect_area_copy1, rect_area_copy2]中的矩形区域是否有重合,有重合的话记录索引,最后会返回一个索引列表。运行后会返回[0, 1]


4.3 小结

在本节我们了解了Rect对象的常见方法以及它常见的属性和函数。学完之后,我们就可以更好的在界面上操作绘制的图片了。

如果你喜欢笔者的这部专栏,可以给笔者一些打赏,或者通过购买一些项目来支持作者,非常感谢!

微信支付   支付宝   微店
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

la_vie_est_belle

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值