Python pygame 中的surface.blit() 函数是什么?它有什么作用?它是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37800894/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:57:04  来源:igfitidea点击:

What is the surface.blit() function in pygame? What does it do? How does it work?

pythoncanvaspygameblit

提问by Lavina Khushlani

I am a beginner in Python and I am not clear about the function surface.blit(). What does it do? How does it works?

我是 Python 初学者,对函数不是很清楚surface.blit()。它有什么作用?它是如何工作的?

I have come across the following points as to how to create it.

关于如何创建它,我遇到了以下几点。

  • Create a canvas of desired size
  • Create a surface of smaller size containing the object to be displayed.
  • Define the Rect value of the surface.
  • Blit (overlap) the surface on the canvas at the rect position
  • 创建所需大小的画布
  • 创建一个包含要显示的对象的较小尺寸的表面。
  • 定义表面的 Rect 值。
  • Blit(重叠)画布上矩形位置的表面

Syntax: canvas.blit(surface, surfacerect)

句法: canvas.blit(surface, surfacerect)

Why is only rect used? Can it be any other shape?

为什么只使用 rect?可以是其他形状吗?

回答by DCA-

Putting this in real terms may help, although as simply put as possible -> blitting is drawing

尽管尽可能简单地将其放在实际中可能会有所帮助 - > blitting 正在绘制

Going through each of the steps you have mentioned:

完成您提到的每个步骤:

  • Create a canvas of a desired size
  • 创建所需大小的画布

This is our window, created by screen = pygame.display.set_mode((width,height)). Where screenis the canvas name. Eventually everything will need to be drawn onto this canvas so that we can see it.

这是我们的窗口,由screen = pygame.display.set_mode((width,height)). screen画布名称在哪里。最终,一切都需要绘制到这个画布上,这样我们才能看到它。

  • Create a surface of smaller size containing the object to be displayed
  • 创建一个包含要显示的对象的较小尺寸的表面

This is a surface that we will populate with objects such as images. It does not need to be smaller than the window size and it can be moved around freely.

这是一个我们将用图像等对象填充的表面。它不需要小于窗口大小,可以自由移动。

  • Define a Rect value of the surface
  • 定义表面的 Rect 值

When you create a surface using something like background = pygame.Surface((width,height))you specify it's size. The images or drawn items on the surface can be any shape or size but they must all be contained within the bounds set by this width and height.

当您使用诸如background = pygame.Surface((width,height))指定其大小之类的东西创建曲面时。表面上的图像或绘制的项目可以是任何形状或大小,但它们必须全部包含在此宽度和高度设置的边界内。

  • Blit (overlap) the surface on the canvas at the rect position
  • Blit(重叠)画布上矩形位置的表面

Now the all important bit. We need to get this surface (background) and draw it onto the window. To do this we will call screen.blit(background,(x,y))where (x,y) is the position inside the window where we want the top left of the surface to be. This function says take the background surface and draw it onto the screen and position it at (x,y).

现在最重要的一点。我们需要获取这个表面(背景)并将其绘制到窗口上。为此,我们将调用screen.blit(background,(x,y))where (x,y) 是窗口内我们希望表面左上角所在的位置。该函数表示取背景表面并将其绘制到屏幕上并将其定位在 (x,y) 处。

A simple example:

一个简单的例子:

import pygame

pygame.init()

#### Create a canvas on which to display everything ####
window = (400,400)
screen = pygame.display.set_mode(window)
#### Create a canvas on which to display everything ####

#### Create a surface with the same size as the window ####
background = pygame.Surface(window)
#### Create a surface with the same size as the window ####

#### Populate the surface with objects to be displayed ####
pygame.draw.rect(background,(0,255,255),(20,20,40,40))
pygame.draw.rect(background,(255,0,255),(120,120,50,50))
#### Populate the surface with objects to be displayed ####

#### Blit the surface onto the canvas ####
screen.blit(background,(0,0))
#### Blit the surface onto the canvas ####

#### Update the the display and wait ####
pygame.display.flip()
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
#### Update the the display and wait ####

pygame.quit()