Pygame - 表面
使用 Pygame 时,表面通常用于表示对象的外观及其在屏幕上的位置。我们在 Pygame 中创建的所有对象、文本、图像都是使用表面创建的。
创建曲面
在 pygame 中创建曲面非常容易。我们只需要将高度和宽度和一个元组传递给pygame.Surface()方法。我们可以根据需要使用各种方法来格式化我们的表面。例如,我们可以使用pygame.draw()绘制形状,我们可以使用surface.fill()方法在表面上进行填充。现在,执行这些功能。让我们讨论语法和参数。
Syntax: pygame.surface()
It takes 4 arguments a tuple of width and height, flags, depth, mask.
pygame.draw():
它用于绘制对象、形状。
Syntax: Surface.rect(surface, color, rect)
使用 pygame.draw.rect() 方法绘制矩形的代码如下:
Python
# Importing the library
import pygame
import time
# Initializing Pygame
pygame.init()
# Creating the surface
sample_surface = pygame.display.set_mode((400,300))
# Choosing red color for the rectangle
color = (255,255,0)
# Drawing Rectangle
pygame.draw.rect(sample_surface, color,
pygame.Rect(30, 30, 60, 60))
# The pygame.display.filp() method is used
# to update content on the display screen
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# Creating the surface
sample_surface = pygame.display.set_mode((400,300))
# Choosing yellow color to fill
color = (255,255,0)
# filling color to the surface
sample_surface.fill(color)
# updating the display
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display
# surface
display_surface.blit(image,(100,100))
# updating the display
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the first image surface
image1 = pygame.image.load('gfg_logo.png')
# Creating the second image surface
image2 = pygame.image.load('gfg_logo.png')
# putting our first image surface on
# display surface
display_surface.blit(image1,(0,0))
# putting our second image surface on
# display surface
display_surface.blit(image1,(300,300))
# updating the display
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# Creating the surface
sample_surface = pygame.display.set_mode((400,300))
# changing the pixel format of an image
pygame.Surface.convert(sample_surface)
# updating the display
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# Creating the surface
sample_surface = pygame.display.set_mode((400,300))
# changing the pixel format
# of an image including per pixel alphas
pygame.Surface.convert_alpha(sample_surface)
# updating the display
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# Creating the surface
sample_surface = pygame.display.set_mode((400,300))
# creating a copy of sample_surface
# and naming it as copied_surface
copied_surface=pygame.Surface.copy(sample_surface)
# updating the display
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display surface
# making the white colored part
# of the surface as transparent
pygame.Surface.set_colorkey (image, [255,255,255])
display_surface.blit(image,(100,100))
# updating the display
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display surface
# making the white colored part of the surface
# as transparent
pygame.Surface.set_colorkey(image, [255, 255, 255])
# printing the colorkey value for the surface
print(pygame.Surface.get_colorkey(image))
display_surface.blit(image, (100, 100))
# updating the display
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display surface
# making the alpha value of surface as 100
pygame.Surface.set_alpha(image, 100)
display_surface.blit(image,(100,100))
# updating the display
pygame.display.flip()
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display surface
# making alpha value of image surface to 100
pygame.Surface.set_alpha(image, 100)
# printing the alpha value of the surface
print(pygame.Surface.get_alpha(image))
display_surface.blit(image,(100,100))
# updating the display
pygame.display.flip()
输出:
surface_name.fill():
pygame.Surface.fill:用于在表面填充颜色。
Syntax:pygame.Surface.fill(color, rect=None, special_flags=0)
使用surface_name.fill()方法在surface中填充颜色的代码是:
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# Creating the surface
sample_surface = pygame.display.set_mode((400,300))
# Choosing yellow color to fill
color = (255,255,0)
# filling color to the surface
sample_surface.fill(color)
# updating the display
pygame.display.flip()
输出:
在表面加载图像
虽然我们可以在表面上绘制形状和填充颜色,但我们仍然需要在表面上有一张图片。我们可以使用 pygame.image.load() 方法轻松制作这样的表面。此方法将图像路径相对或绝对作为输入。
Syntax: pygame.image.load(img)
代码:
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display
# surface
display_surface.blit(image,(100,100))
# updating the display
pygame.display.flip()
输出:
添加块传输
对于要显示的表面,它需要在屏幕上进行 blitted。 Blitting 可以被认为是将一个表面的像素复制到另一个表面。我们可以使用 Surface.blit() 方法进行 blit,该方法将需要 blit 的表面作为第一个参数,将坐标元组作为第二个坐标。
Syntax: pygame.Surface.blit(source, dest, area=None, special_flags=0)
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the first image surface
image1 = pygame.image.load('gfg_logo.png')
# Creating the second image surface
image2 = pygame.image.load('gfg_logo.png')
# putting our first image surface on
# display surface
display_surface.blit(image1,(0,0))
# putting our second image surface on
# display surface
display_surface.blit(image1,(300,300))
# updating the display
pygame.display.flip()
输出:
现在我们已经讨论了一些表面函数,如 .blit()、.fill() 等。让我们讨论一些更重要的 pygame 屏幕函数。
- pygame.Surface.convert:它制作像素格式改变的表面副本。新的像素格式可以根据现有的表面或深度来确定,可以使用标志和掩码参数。
Syntax: pygame.Surface.convert(Surface=None)
代码:
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# Creating the surface
sample_surface = pygame.display.set_mode((400,300))
# changing the pixel format of an image
pygame.Surface.convert(sample_surface)
# updating the display
pygame.display.flip()
输出:
- pygame.Surface.convert_alpha:它使用所需的像素格式创建表面的新副本。新表面将采用适合于使用每像素 alpha 快速位块传输到给定格式的格式。如果没有给出表面,新的表面将被优化以用于当前显示的 blitting。
Syntax: pygame.Surface.convert_alpha(Surface)
代码:
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# Creating the surface
sample_surface = pygame.display.set_mode((400,300))
# changing the pixel format
# of an image including per pixel alphas
pygame.Surface.convert_alpha(sample_surface)
# updating the display
pygame.display.flip()
输出:
- pygame.Surface.copy:它创建表面的新副本。复制的表面将具有与原始表面相同的像素格式、调色板、透明度设置和类。
Syntax: pygame.Surface.copy()
代码:
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# Creating the surface
sample_surface = pygame.display.set_mode((400,300))
# creating a copy of sample_surface
# and naming it as copied_surface
copied_surface=pygame.Surface.copy(sample_surface)
# updating the display
pygame.display.flip()
输出:
- pygame.Surface.set_colorkey:设置表面的当前颜色键。将此表面块传输到目标上时,与颜色键具有相同颜色的任何像素都将是透明的。
Syntax: set_colorkey(Color, flags=0)
代码:
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display surface
# making the white colored part
# of the surface as transparent
pygame.Surface.set_colorkey (image, [255,255,255])
display_surface.blit(image,(100,100))
# updating the display
pygame.display.flip()
输出:
上述代码的输出将是黑色表面上的 geeksforgeeks 标志,白色像素变为透明。
- pygame.Surface.get_colorkey:它返回 Surface 的当前颜色键值。如果未设置颜色键,则返回 None。
Syntax: get_colorkey()
代码:
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display surface
# making the white colored part of the surface
# as transparent
pygame.Surface.set_colorkey(image, [255, 255, 255])
# printing the colorkey value for the surface
print(pygame.Surface.get_colorkey(image))
display_surface.blit(image, (100, 100))
# updating the display
pygame.display.flip()
输出:
上面代码的输出将是一个窗口,显示在 get_colorkey 示例中看到的各种表面,并且还将打印 colorkey 值。
- pygame.Surface.set_alpha:为整个表面图像设置的 alpha 值。传递 0 表示不可见,传递 255 表示完全不透明。
Syntax: set_alpha(value, flags=0) or set_alpha(None)
代码:
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display surface
# making the alpha value of surface as 100
pygame.Surface.set_alpha(image, 100)
display_surface.blit(image,(100,100))
# updating the display
pygame.display.flip()
输出:
上面代码的输出将是 geeksforgeeks 标志,它会略微透明,因为我们已将其 alpha 值更改为 100。
- pygame.Surface.get_alpha:它返回表面的当前 alpha 值。
Syntax: get_alpha()
代码:
Python
# Importing the library
import pygame
# Initializing Pygame
pygame.init()
# creating the display surface
display_surface = pygame.display.set_mode((500, 500 ))
# Creating the image surface
image = pygame.image.load('gfg_logo.png')
# putting our image surface on display surface
# making alpha value of image surface to 100
pygame.Surface.set_alpha(image, 100)
# printing the alpha value of the surface
print(pygame.Surface.get_alpha(image))
display_surface.blit(image,(100,100))
# updating the display
pygame.display.flip()
输出:
上面代码的输出将是一个窗口,显示在 set_alpha 示例中看到的各种表面,并且还将打印 alpha 值。