pygame游戏中的矩形区域描述类pygame.Rect

pygame游戏中的矩形区域描述类pygame.Rect

pygame教程qingyu2022-11-29 0:28:50521A+A-

  pygame游戏中的矩形区域描述类pygame.Rect

学员.png

  说明:

  在pygame游戏中,一切元素都是矩形。pygame专门提供的类pygame.Rect用于描述矩形区域。要描述一个矩形有四个元素x,y矩形原点,width,height矩形宽度和高度。

  pygame.Rect

  [

  x,y

  left,top,bottom,right

  center,centerx,centery

  size,width,height

  ]

  提示:

  pygame.Rect是一个比较特殊的类,内部只是提供一些数字计算。

  代码示例:

# 导入 pygame 模块
import pygame
# 启动 pygame 初始化
pygame.init()
# 创建特定尺寸的显示面对象
window = pygame.display.set_mode((500, 400))
# 在窗口中添加标题
pygame.display.set_caption('矩形示例')
# 将矩形初始坐标存储在两个变量中,即 x 和 y
x = 100
y = 100
#矩形宽度和高度
width = 210
height= 158
#矩形填充颜色
rect_color=(155,55,60)
run = True
while run:
    # 用黑色填充背景
    window.fill((0, 0, 0))
    # 在 x 和 y 坐标处创建矩形
    myrect =pygame.Rect(x,y,width,height)
    #将创建的矩形画出来
    #(根据(窗口), 颜色, 矩形对象)
    pygame.draw.rect(window,rect_color,myrect)
    # 将表面对象绘制到屏幕上
    pygame.display.update()
    # 迭代 pygame.event.get() 方法返回的 Event 对象列表。
    for event in pygame.event.get():
        # 如果事件类型为 QUIT,则关闭窗口和程序
        if event.type == pygame.QUIT:
            run = False
pygame.quit()

78.png

点击这里复制本文地址 欢迎来到大黄鸡源码分享网
qrcode

大黄鸡源码编程网 © All Rights Reserved.  
网站备案号:闽ICP备18012015号-4
Powered by Z-BlogPHP
联系我们| 关于我们| 广告联系| 网站管理