pygame 中的surface.blit() 函数是什么?它有什么作用?它是如何工作的? [英] What is the surface.blit() function in pygame? What does it do? How does it work?

查看:62
本文介绍了pygame 中的surface.blit() 函数是什么?它有什么作用?它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

  • 创建所需大小的画布
  • 创建一个包含要显示的对象的较小尺寸的表面.
  • 定义表面的 Rect 值.
  • Blit(重叠)画布上矩形位置的表面

语法:canvas.blit(surface,surfacerect)

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

推荐答案

把它放在实际中可能会有所帮助,尽管尽可能简单 -> blitting 正在绘制

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

完成您提到的每个步骤:

Going through each of the steps you have mentioned:

  • 创建所需大小的画布

这是我们的窗口,由 screen = pygame.display.set_mode((width,height)) 创建.其中 screen 是画布名称.最终,一切都需要绘制到这个画布上,以便我们可以看到它.

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

  • 创建一个包含要显示的对象的较小尺寸的表面

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

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.

  • 定义表面的 Rect 值

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

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.

  • Blit(重叠)画布上矩形位置的表面

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

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

一个简单的例子:

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

这篇关于pygame 中的surface.blit() 函数是什么?它有什么作用?它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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