更新 python 中的表面的一部分,或透明表面 [英] Updating part of a surface in python, or transparent surfaces

查看:40
本文介绍了更新 python 中的表面的一部分,或透明表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 python 编写的应用程序,它基本上是一个 etch-a-sketch,你用 WASD 和箭头键移动像素,它会留下痕迹.但是,我想为屏幕上的像素数量添加一个计数器.如何在不更新整个表面和像素绘图的情况下更新计数器?

I have an application written in python that's basically an etch-a-sketch, you move pixels around with WASD and arrow keys and it leaves a trail. However, I want to add a counter for the amount of pixels on the screen. How do I have the counter update without updating the entire surface and pwning the pixel drawings?

或者,我可以制作一个除文本外完全透明的表面,以便您可以看到下面的绘图表面吗?

Alternatively, can I make a surface that's completely transparent except for the text so you can see the drawing surface underneath?

推荐答案

要解决此问题,您需要为 Etch-a-Sketch 像素设置一个单独的表面,以便在刷新时不会损坏它们屏幕.不幸的是,在 Rigo 的方案中,字体将继续在其自身顶部呈现,这会因超过两个像素数的变化而变得混乱.

To solve this problem, you want to have a separate surface for your Etch-a-Sketch pixels, so that they do not get clobbered when you go to refresh the screen. Unfortunately, with Rigo's scheme, the font will continue to render on top of itself, which will get messy for more than two pixel count changes.

所以,这里有一些示例渲染代码:

So, here's some sample rendering code:

# Fill background
screen.fill((0xcc, 0xcc, 0xcc))
# Blit Etch-a-Sketch surface (with the drawing)
# etch_surf should be the same size as the screen
screen.blit(etch_surf, (0, 0))
# Render the pixel count
arial = pygame.font.SysFont('Arial', 20)
counter_surf = arial.render(str(pixel_count), True, (0, 0, 0))
screen.blit(counter_surf, (16, 16))
# Refresh entire screen
pygame.display.update()

现在,诚然,更新整个屏幕的效率相当低.为此,您有两个选择:仅在绘图更改时刷新屏幕或跟踪绘图更改的位置并刷新单个位置(请参阅 更新文档).如果你选择第二个选项,你将不得不刷新文本和它之前的位置;我建议有一个 Sprite 管理这个.

Now, admittedly, updating the entire screen is rather inefficient. For this, you have two options: only refresh the screen when the drawing changes or track the location of drawing changes and refresh individual locations (see the update documentation). If you choose the second option, you will have to refresh the text and where it was previously; I would recommend having a Sprite manage this.

这篇关于更新 python 中的表面的一部分,或透明表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆