如何将矩形传递给 pygame.display.update() 以更新 window 的特定区域?
How do I pass a rect into pygame.display.update() to update a specific area of the window?
在 documentation page for pygame.display.update()
上,它说您可以将一个矩形传递到方法中以更新屏幕的一部分。但是,我看到的所有示例都只是从程序中的图像或形状传递现有的矩形。我怎样才能告诉它直接更新屏幕上的区域?例如,在绘制矩形时,我可以使用 (100,200,30,40)
的 rect 参数。这将绘制一个顶部为 200、左侧为 100、宽度为 30、高度为 40 的矩形。如何将类似的参数传递给 pygame.display.update()
?我试过 pygame.display.update((100,200,30,40))
,但这会更新整个 window。
只需定义一个 rect and pass it to pygame.display.update()
以仅更新显示的这个特定区域。您还可以传递一个矩形列表。
import random
import pygame as pg
from pygame.math import Vector2
# A simple sprite, just to have something moving on the screen.
class Ball(pg.sprite.Sprite):
def __init__(self, screen_rect):
super().__init__()
radius = random.randrange(5, 31)
self.image = pg.Surface((radius*2, radius*2), pg.SRCALPHA)
pg.draw.circle(self.image, pg.Color('dodgerblue1'), (radius, radius), radius)
pg.draw.circle(self.image, pg.Color('dodgerblue3'), (radius, radius), radius-2)
self.rect = self.image.get_rect(center=screen_rect.center)
self.vel = Vector2(random.uniform(-2, 2), random.uniform(-2, 2))
self.pos = Vector2(self.rect.center)
self.screen_rect = screen_rect
self.lifetime = 350
def update(self):
self.pos += self.vel
self.rect.center = self.pos
self.lifetime -= 1
if not self.screen_rect.contains(self.rect) or self.lifetime <= 0:
self.kill()
def main():
screen = pg.display.set_mode((800, 600))
screen.fill((20, 40, 70))
pg.display.update()
screen_rect = screen.get_rect()
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
# Pass this rect to `pg.display.update` to update only this area.
update_rect = pg.Rect(50, 50, 500, 400)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
all_sprites.add(Ball(screen_rect))
all_sprites.update()
screen.fill((20, 50, 90))
all_sprites.draw(screen)
# Update only the area that we specified with the `update_rect`.
pg.display.update(update_rect)
clock.tick(60)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
在 documentation page for pygame.display.update()
上,它说您可以将一个矩形传递到方法中以更新屏幕的一部分。但是,我看到的所有示例都只是从程序中的图像或形状传递现有的矩形。我怎样才能告诉它直接更新屏幕上的区域?例如,在绘制矩形时,我可以使用 (100,200,30,40)
的 rect 参数。这将绘制一个顶部为 200、左侧为 100、宽度为 30、高度为 40 的矩形。如何将类似的参数传递给 pygame.display.update()
?我试过 pygame.display.update((100,200,30,40))
,但这会更新整个 window。
只需定义一个 rect and pass it to pygame.display.update()
以仅更新显示的这个特定区域。您还可以传递一个矩形列表。
import random
import pygame as pg
from pygame.math import Vector2
# A simple sprite, just to have something moving on the screen.
class Ball(pg.sprite.Sprite):
def __init__(self, screen_rect):
super().__init__()
radius = random.randrange(5, 31)
self.image = pg.Surface((radius*2, radius*2), pg.SRCALPHA)
pg.draw.circle(self.image, pg.Color('dodgerblue1'), (radius, radius), radius)
pg.draw.circle(self.image, pg.Color('dodgerblue3'), (radius, radius), radius-2)
self.rect = self.image.get_rect(center=screen_rect.center)
self.vel = Vector2(random.uniform(-2, 2), random.uniform(-2, 2))
self.pos = Vector2(self.rect.center)
self.screen_rect = screen_rect
self.lifetime = 350
def update(self):
self.pos += self.vel
self.rect.center = self.pos
self.lifetime -= 1
if not self.screen_rect.contains(self.rect) or self.lifetime <= 0:
self.kill()
def main():
screen = pg.display.set_mode((800, 600))
screen.fill((20, 40, 70))
pg.display.update()
screen_rect = screen.get_rect()
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
# Pass this rect to `pg.display.update` to update only this area.
update_rect = pg.Rect(50, 50, 500, 400)
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
all_sprites.add(Ball(screen_rect))
all_sprites.update()
screen.fill((20, 50, 90))
all_sprites.draw(screen)
# Update only the area that we specified with the `update_rect`.
pg.display.update(update_rect)
clock.tick(60)
if __name__ == '__main__':
pg.init()
main()
pg.quit()