Python 与 blitting 有点混淆(Pygame)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17454257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A bit confused with blitting (Pygame)
提问by Pabce
I've just started learning some pygame (quite new to programming overall), and I have some very basic questions about how it works.
我刚刚开始学习一些 pygame(对整体编程来说很新),我有一些关于它如何工作的非常基本的问题。
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:
我还没有找到一个地方来解释我何时需要 blit 或不包括在屏幕上的某个表面。比如画圆的时候:
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:
我不需要做screen.blit(circle)
,但是在显示文本时:
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.
如果我不 blit,文本将不会出现。
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.
老实说,除了将所需的表面“粘贴”到屏幕上之外,我真的不知道 blitting 应该做什么。我希望我已经足够清楚了。
采纳答案by Mark Hildreth
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.
我还没有找到一个地方来解释我何时需要 blit 或不包括在屏幕上的某个表面。
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?
什么是 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.
首先,您需要了解 blitting 的作用。您的屏幕只是一组像素,而 blitting 是将一组像素完整复制到另一组像素上。例如,您可以拥有一个带有从硬盘驱动器加载的图像的表面,并且可以通过将该screen
表面多次块传输到表面的顶部,在屏幕上的不同位置多次显示它。
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".
在两行代码中,我们通过“blitting”将大量像素从源表面 (my_image) 复制到屏幕上。
How do the pygame.draw.* functions blit?
pygame.draw.* 函数如何 blit?
Technically, the pygame.draw.* methods could have been written to do something similar. So, instead of your example...
从技术上讲,可以编写 pygame.draw.* 方法来做类似的事情。所以,而不是你的例子......
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.
如果是这种情况,您将得到相同的结果。但是,在内部,该pygame.draw.circle()
方法直接操作您传递给它的表面,而不是创建一个新的表面。这可能被选择作为做事的方式,因为与创建新表面相比,它们可以让它运行得更快或内存更少。
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.
因此,对于“何时进行 blit”和“何时不进行”的问题,基本上,您需要阅读文档以了解该函数的实际作用。
Here is the pygame.draw.circle()docs:
这是pygame.draw.circle()文档:
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.
pygame.draw.circle():
围绕一个点画一个圆圈
圆(表面,颜色,位置,半径,宽度 = 0)-> 矩形
在 Surface 上绘制圆形。pos 参数是圆心,radius 是大小。width 参数是绘制外边缘的厚度。如果宽度为零,则圆将被填充。
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).
请注意,它说“在表面上绘制形状”,因此它已经为您完成了像素更改。此外,它不返回一个表面(它返回一个 Rect,但它只是告诉您像素更改完成的位置)。
Now let's look at the pygame.font.Font.render() documentation:
现在让我们看看pygame.font.Font.render() 文档:
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. ...
在新 Surface 上绘制文本
渲染(文本,抗锯齿,颜色,背景=无)-> 表面
这将创建一个新的 Surface,其上呈现了指定的文本。Pygame 没有提供直接在现有 Surface 上绘制文本的方法:相反,您必须使用 Font.render() 创建文本的图像(Surface),然后将该图像 blit 到另一个 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.
如您所见,它特别说明文本是在一个 NEW Surface 上绘制的,该 Surface 是创建并返回给您的。这个表面不是你的屏幕表面(它不可能,你甚至没有告诉render()
函数你的屏幕表面是什么)。这是一个很好的迹象,表明您需要实际将该表面 blit 到屏幕上。
回答by Serial
Blit means 'BL'ock 'I'mage 'T'ranfser
Blit 的意思是 'BL'ock 'I'mage 'T'ranfser
When you are displaying things on the screen you will, in some way, use screen
because that's where you are putting it.
当你在屏幕上显示东西时,你会以某种方式使用,screen
因为那是你放置它的地方。
When you do:
当你这样做时:
pygame.draw.circle(screen, (0, 0, 0), (100, 100), 15, 1)
you are still using screen but you are just not blitting because pygame is drawing it for you.
你仍在使用 screen 但你只是没有 blitting 因为 pygame 正在为你绘制它。
And when you use text, pygame renders it into an image then you have to blit it.
当您使用文本时,pygame 会将其渲染为图像,然后您必须对其进行 blit。
So basically you blit images, but you can also have pygame draw them for you. But remember when you blit an image, say over a background, you need to loop it back and fourth; so that it blits the background, then the image, then the background etc...
所以基本上你是 blit 图像,但你也可以让 pygame 为你绘制它们。但是请记住,当您对图像进行 blit 时,例如在背景上,您需要将其循环回四次;以便它闪烁背景,然后是图像,然后是背景等......
You dont need to know much more than that, but you can read all about it here Pygame Blit
你不需要知道更多,但你可以在这里阅读所有相关内容Pygame Blit
I hope this helped. Good Luck!
我希望这有帮助。祝你好运!
回答by Bartlomiej Lewandowski
Imagine that you are a painter:
假设你是一名画家:
You have a canvas, and a brush.
你有一个画布和一个画笔。
Let's say that your main screen surface will be your canvas, and all the other surfaces, are "in your head" - you know how to draw them already.
假设您的主屏幕表面将是您的画布,而所有其他表面都“在您的脑海中”——您已经知道如何绘制它们了。
When you call blit, you paint on topof the surface, covering any pixels that were overlapped. That is why you need to repaint the whole screen black so that you won't have any smudges on the painting while moving an object.
当你调用 blit 时,你在表面上绘画,覆盖任何重叠的像素。这就是为什么您需要将整个屏幕重新绘制为黑色,以便在移动对象时不会在绘画上留下任何污迹。
As Mark already said, you can draw a circle with a function, or first blit it to a new surface, and blit that on the screen surface.
正如 Mark 已经说过的,你可以用一个函数画一个圆,或者先把它 blit 到一个新的表面,然后 blit 在屏幕表面上。
If you have a more complicated surface - curves, text etc. you wouldn't need to have a surface for that, so you don't have to do any expensive calculations, just drawing. The setback is that your program takes up more memory, so you have to choose between those 2.
如果您有更复杂的曲面 - 曲线、文本等,则不需要为此创建曲面,因此您无需进行任何昂贵的计算,只需绘制即可。挫折是你的程序占用了更多的内存,所以你必须在这两个之间进行选择。