与 blitting 有点混淆(Pygame) [英] A bit confused with blitting (Pygame)

查看:87
本文介绍了与 blitting 有点混淆(Pygame)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习一些 pygame(对整体编程来说还很陌生),我对它的工作原理有一些非常基本的问题.

我还没有找到可以解释何时需要 blit 或不需要在屏幕上包含某个表面的地方.比如画圆的时候:

circle = pygame.draw.circle(screen, (0, 0, 0), (100, 100), 15, 1)

我不需要做screen.blit(circle),但是在显示文本时:

text = font.render("TEXT", 1, (10, 10, 10))textpos = text.get_rect()textpos.centerx = screen.get_rect().centerxscreen.blit(文本,textpos)

如果我不 blit,文本就不会出现.

老实说,除了将所需的表面粘贴"到屏幕上之外,我真的不知道 blitting 应该做什么.我希望我已经足够清楚了.

解决方案

简答

<块引用>

我还没有找到可以解释何时需要 blit 或不需要在屏幕上包含某个表面的地方.

每个操作的行为都不同,您需要阅读您正在使用的函数的文档.

长答案

什么是 Blitting?

首先,您需要了解 blitting 的作用.您的屏幕只是一组像素,而 blitting 是将一组像素完整复制到另一组像素上.例如,您可以拥有一个带有从硬盘驱动器加载的图像的表面,并且可以通过多次将该表面在 screen 表面上的 blitting 多次显示在屏幕上的不同位置.

所以,你经常有这样的代码......

my_image = load_my_image()screen.blit(my_image,位置)screen.blit(my_image, another_position)

在两行代码中,我们通过blitting"将大量像素从源表面 (my_image) 复制到屏幕上.

pygame.draw.* 函数如何 blit?

从技术上讲,可以编写 pygame.draw.* 方法来做类似的事情.所以,而不是你的例子......

pygame.draw.circle(screen, COLOR, POS, RADIUS, WIDTH)

...他们可以让你这样做...

circle_surface = pygame.draw.circle(COLOR, RADIUS, WIDTH)screen.blit(circle_surface,POS)

如果是这种情况,您会得到相同的结果.但是,在内部,pygame.draw.circle() 方法直接操作您传递给它的表面,而不是创建新的表面.这可能被选为做事的方式,因为与创建新表面相比,它们可以让它运行得更快或内存更少.

那我该怎么办?

因此,对于何时进行 blit"和何时不进行"的问题,基本上,您需要阅读文档以了解该函数的实际作用.

这是 pygame.draw.circle() 文档:

<块引用>

pygame.draw.circle():

围绕一个点画一个圆

circle(Surface, color, pos, radius, width=0) -> Rect

在 Surface 上绘制一个圆形.pos 参数是圆心,radius 是大小.width 参数是绘制外边缘的厚度.如果宽度为零,则圆将被填充.

请注意,它说在表面上绘制形状",因此它已经为您完成了像素更改.此外,它不返回一个表面(它返回一个 Rect,但它只是告诉您像素更改完成的位置).

现在让我们看看 pygame.font.Font.render() 文档:

<块引用>

在新 Surface 上绘制文本

render(text, antialias, color, background=None) -> Surface

这会创建一个新的 Surface,上面渲染了指定的文本.Pygame 没有提供直接在现有 Surface 上绘制文本的方法:相反,您必须使用 Font.render() 创建文本的图像(Surface),然后将该图像 blit 到另一个 Surface 上....

如您所见,它特别说明文本是在新表面上绘制的,该表面是创建并返回给您的.这个表面不是你的屏幕表面(它不可能,你甚至没有告诉 render() 函数你的屏幕表面是什么).这是一个很好的迹象,表明您实际上需要将这个表面 blit 到屏幕上.

I've just started learning some pygame (quite new to programming overall), and I have some very basic questions about how it works.

I haven't found a place yet that explains when I need to blit or not to include a certain surface on the screen. For example, when drawing a circle:

circle = pygame.draw.circle(screen, (0, 0, 0), (100, 100), 15, 1)

I don't need to do screen.blit(circle), but when displaying text:

text = font.render("TEXT", 1, (10, 10, 10)) 
textpos = text.get_rect()
textpos.centerx = screen.get_rect().centerx
screen.blit(text, textpos)

If I don't blit, the text won't appear.

To be honest, I really don't know what blitting is supposed to do, apart from "pasting" the desired surface onto the screen. I hope I have been clear enough.

解决方案

The short answer

I haven't found a place yet that explains when I need to blit or not to include a certain surface on the screen.

Each operation will behave differently, and you'll need to read the documentation for the function you're working with.

The long answer

What Is Blitting?

First, you need to realize what blitting is doing. Your screen is just a collection of pixels, and blitting is doing a complete copy of one set of pixels onto another. For example, you can have a surface with an image that you loaded from the hard drive, and can display it multiple times on the screen in different positions by blitting that surface on top of the screen surface multiple times.

So, you often have code like this...

my_image = load_my_image()
screen.blit(my_image, position)
screen.blit(my_image, another_position)

In two lines of code, we copied a ton of pixels from the source surface (my_image) onto the screen by "blitting".

How do the pygame.draw.* functions blit?

Technically, the pygame.draw.* methods could have been written to do something similar. So, instead of your example...

pygame.draw.circle(screen, COLOR, POS, RADIUS, WIDTH)

...they COULD have had you do this...

circle_surface = pygame.draw.circle(COLOR, RADIUS, WIDTH)
screen.blit(circle_surface, POS)

If this were the case, you would get the same result. Internally, though, the pygame.draw.circle() method directly manipulates the surface you pass to it rather than create a new surface. This might have been chosen as the way to do things because they could have it run faster or with less memory than creating a new surface.

So which do I do?

So, to your question of "when to blit" and "when not to", basically, you need to read the documentation to see what the function actually does.

Here is the pygame.draw.circle() docs:

pygame.draw.circle():

draw a circle around a point

circle(Surface, color, pos, radius, width=0) -> Rect

Draws a circular shape on the Surface. The pos argument is the center of the circle, and radius is the size. The width argument is the thickness to draw the outer edge. If width is zero then the circle will be filled.

Note that it says that "draws a shape on the surface", so it has already done the pixel changes for you. Also, it doesn't return a surface (it returns a Rect, but that just tells you where the pixel changes were done).

Now let's look at the pygame.font.Font.render() documentation:

draw text on a new Surface

render(text, antialias, color, background=None) -> Surface

This creates a new Surface with the specified text rendered on it. Pygame provides no way to directly draw text on an existing Surface: instead you must use Font.render() to create an image (Surface) of the text, then blit this image onto another Surface. ...

As you can see, it specifically says that the text is drawn on a NEW Surface, which is created and returned to you. This surface is NOT your screen's surface (it can't be, you didn't even tell the render() function what your screen's surface is). That's a pretty good indication that you will need to actually blit this surface to the screen.

这篇关于与 blitting 有点混淆(Pygame)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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