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

查看:2036
本文介绍了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.


  • 创建所需大小的画布

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

  • 定义矩形

  • 将画布上的表面在正确位置处涂满(重叠)

  • 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

语法: canvas.blit(surface,surfacerect)

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

推荐答案

将其实际放置可能会有所帮助,尽管尽可能简单-> 正在绘画

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:


  • 创建所需大小的画布

这是我们的窗口,由<$ c $创建c> 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值
  • Define a Rect value of the surface

使用诸如 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.


  • 将画布上的表面在正确的位置变亮(重叠)

现在,所有重要的位。我们需要获取此表面(背景)并将其绘制到窗口上。为此,我们将调用 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天全站免登陆