gpt4 book ai didi

python - 如何在 PyGame 中绘制到离屏显示

转载 作者:行者123 更新时间:2023-12-01 05:25:22 25 4
gpt4 key购买 nike

我正在使用 PyGame 进行图形测试来模拟展开的龙曲线。我已经制作了一个成功的版本,可以在所有点相互旋转时跟踪它们,但显然,经过几次迭代后,速度开始大幅减慢。为了加快速度,我想简单地将绘制的片段存储到图像变量中,并不断将屏幕的片段保存到变量中并绘制那些移动的片段,而不是跟踪很多点。我该如何执行以下任一操作?

  • 绘制到屏幕外图像变量,然后将其绘制到屏幕上的正确位置
  • 将可见显示的一部分保存到图像变量中

我尝试阅读一些 PyGame 文档,但没有成功。

谢谢!

最佳答案

解决方案是创建一个额外的表面对象,然后向其绘图。然后可以将该表面对象绘制到显示器的表面对象上,如下所示。

有关 PyGame Surface 对象的更多信息可以找到 here

import pygame, sys

SCREEN_SIZE = (600, 400)
BG_COLOR = (0, 0, 0)
LINE_COLOR = (0, 255, 0)
pygame.init()
clock = pygame.time.Clock() # to keep the framerate down

image1 = pygame.Surface((50, 50))
image2 = pygame.Surface((50, 50))
image1.set_colorkey((0, 0, 0)) # The default background color is black
image2.set_colorkey((0, 0, 0)) # and I want drawings with transparency

screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
screen.fill(BG_COLOR)

# Draw to two different images off-screen
pygame.draw.line(image1, LINE_COLOR, (0, 0), (49, 49))
pygame.draw.line(image2, LINE_COLOR, (49, 0), (0, 49))

# Optimize the images after they're drawn
image1.convert()
image2.convert()

# Get the area in the middle of the visible screen where our images would fit
draw_area = image1.get_rect().move(SCREEN_SIZE[0] / 2 - 25,
SCREEN_SIZE[1] / 2 - 25)

# Draw our two off-screen images to the visible screen
screen.blit(image1, draw_area)
screen.blit(image2, draw_area)

# Display changes to the visible screen
pygame.display.flip()

# Keep the window from closing as soon as it's finished drawing
# Close the window gracefully upon hitting the close button
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
clock.tick(30)

关于python - 如何在 PyGame 中绘制到离屏显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21441217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com