pygame 中的矩形

Rectangles in pygame

你能在 pygame 中更改矩形一侧的长度吗?

例如:

img = pygame.image.load('image.gif')
rect = img.get_rect()

在该示例中,我加载了一张图片并在其周围创建了一个矩形。考虑到这一点,例如,我可以在不改变矩形的任何其他边的情况下改变矩形底部的长度吗?

看这里:http://www.pygame.org/docs/ref/rect.html

rect 只是这些东西的数据结构,您可以设置 rect 的以下任何属性

x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h

只需在 python 中创建一个新的 rect shell 并玩一玩,您可以自己创建 Rects

import pygame
rect = pygame.Rect((10,10),(20,20))
print rect.height
>>20
rect.height = 30
print rect.height
>>30

编辑: 这不会影响您加载的图像的外观,要绘制到屏幕上,您需要一个表面和一个矩形,如果您想修改图像,则需要更改表面。或者将通过绘图加载的图像拉伸到增加的矩形将起作用,请查看 Surface.blit、http://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit

的解释

您可以使用 PyGames Rect objects存储直角坐标

正如@Tehsmeely 提到的,有许多所谓的虚拟属性可用于移动或对齐 Rect 对象 在你的游戏中:

当您想将图像从文件加载到您的游戏中时,使用 .blit() 方法 pygame.image.load() function returns a new Surface object which is like your main screen surface. You can blit(即复制)这个新表面对象到您想要的任何其他表面:

  • .blit()的第一个参数是源表面

  • 第二个参数可以是一对表示源表面左上角的坐标
    a Pygame Rect 对象,矩形的左上角将用作 blit 的位置
    目标矩形的大小不影响blit

这意味着您可以更改 rect:

底部的长度
rect.width= 100

但这不会影响您 img 的大小。

希望对您有所帮助:)