python - 使用 Pygame 加载更新的图像
问题描述
我正在使用 pygame 制作一个基本的 Mandelbrot 缩放程序,每次用户放大时使用枕头生成图像。更新此图像时,pygame 窗口仅加载原始图像而不是更新的图像。
我目前的解决方案是在每次缩放时杀死 pygame 窗口并重新初始化它,这会为每次缩放增加相当多的时间。这是我当前的代码
def pg_window(width, height):
pg.init()
fill_color = 255, 255, 255
window = pg.display.set_mode((width, height))
global set_image
set_img = pg.image.load('mandelbrot.png')
# The original load of the image
zoom_x = int(width * .15)
zoom_y = int(height * .15)
while True:
window.fill(fill_color)
... extra code ommitted ...
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
if event.type == pg.MOUSEBUTTONDOWN:
zoom(*zoom_rect.center, width, height, zoom_x, zoom_y)
# The zoom function call modifies the image directly.
set_image = pg.image.load('mandelbrot.png')
# changing the set_image variable just loads the original image, not the updated one
# My current solution involves calling pg.quit() here
# and recalling the pg_window function to reinitialize
window.fill(fill_color)
window.blit(set_img, (0, 0))
pg.display.flip()
无论如何都可以在不重置窗口的情况下加载更新的图像?
解决方案
blit
到窗口的图像的名称是set_img
而不是set_image
。
所以你必须设置set_img
:
while True:
window.fill(fill_color)
# [...]
for event in pg.event.get():
# [...]
if event.type == pg.MOUSEBUTTONDOWN:
# [...]
set_img = pg.image.load('mandelbrot.png') # <--- set_img
window.fill(fill_color)
window.blit(set_img, (0, 0))
pg.display.flip()
推荐阅读
- julia - Julia JuMP 中的绑定 2 范数
- android - Android ML Kit Translator.getClient() 未解析参考
- fortran - Lid.exe 中 0x001D6653 处未处理的异常:0xc0000094:整数除以零
- python - 没有模块名称“face_recognition”窗口
- sql - T-SQL 传递给 LEFT 或 SUBSTRING 函数的长度参数无效
- c++ - 是否可以使用变换反馈来检索由 OpenGL 顶点几何着色器生成的三角形?
- django - 在Django中将变量从API传递到模板
- python - Python MQTT 订阅响应/确认以使用 json 发布
- r - 如何在ggplot中放置和包装图例
- python - 我正在为 Discord 机器人编写一个空白数据库,在正确的时间调用函数存在问题