繁体   English   中英

如何在PyGame中绘制为屏幕外显示

[英]How to draw to an off-screen display in PyGame

我正在使用PyGame进行图形测试,以模拟正在展开的Dragon Curve。 我已经制作了一个成功的版本,该版本可以跟踪所有点彼此之间的旋转,但是很明显,经过几次迭代后,它开始显着降低速度。 为了加快速度,我想简单地将绘制的段存储到图像变量中,并将屏幕的一部分连续保存到变量中,然后绘制移动的那些段而不是跟踪很多点。 我该怎么做以下之一?

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

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

谢谢!

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

有关PyGame Surface对象的更多信息,请参见此处

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM