【问题标题】:How do you create rect variables in pygame without drawing them?如何在 pygame 中创建 rect 变量而不绘制它们?
【发布时间】:2018-04-21 08:56:08
【问题描述】:

我想要做的是将一个矩形分配给一个变量而不在屏幕上绘制它。我想知道如何做到这一点。这是我当前将 20x20 白色矩形分配给“myRect”的代码:

import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((50, 50))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    myRect  = pygame.draw.rect(screen, (255, 255, 255), (0, 0, 20, 20))

【问题讨论】:

标签: python python-3.x pygame


【解决方案1】:

你使用 Pygame.Rect:

myRect = pygame.Rect(20,20,100,200)    # 20 left, 20 top, 100 width, 200 height

见:https://www.pygame.org/docs/ref/rect.html

pygame.Rect
用于存储直角坐标的 pygame 对象
矩形(左、上、宽、高)-> 矩形
Rect((left, top), (width, height)) -> Rect
矩形(对象)-> 矩形

如果你也想给它加上颜色,你可以扩展 Rect 类。

【讨论】:

  • 啊,谢谢。我以为它会像那样简单
猜你喜欢
  • 2015-01-06
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多