简体   繁体   English

用python更新表面的一部分或透明表面

[英]Updating part of a surface in python, or transparent surfaces

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. 我有一个用python编写的应用程序,它基本上是一个蚀刻草图,您可以使用WASD和箭头键来移动像素,并留下痕迹。 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? 或者,除了文字以外,我是否可以使表面完全透明,以便您可以看到下面的绘图表面?

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. 不幸的是,使用Rigo的方案,字体将继续在其自身之上呈现,这将导致两个以上像素数变化而变得混乱。

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()

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. 我建议让Sprite来管理。

What you need is pygame.font module 您需要的是pygame.font模块

#define a font surface 
spamSurface = pygame.font.SysFont('Arial', 20)

#then, in your infinite cycle...   
eggsPixels = spamSurface.render(str(pixelsOnScreen), True, (255, 255, 255))
hamDisplay.blit(eggsPixels, (10, 10))

Where spamSurface is a new font surface, eggsPixels is the value that spamSurface will render (display/show) and hamDisplay is your main surface display. spamSurface是新的字体表面, eggsPixelsspamSurface将呈现(显示/显示)的值,而hamDisplay是您的主要表面显示。

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

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