Pygame库中Surface 对象介绍

关于Python中pygame游戏模块的安装使用可见 https://blog.csdn.net/cnds123/article/details/119514520

Pygame库中Surface 对象

在Pygame库中,Surface 对象是一个非常核心的概念,它代表了一个二维的矩形画布,可以在上面绘制图像、图形、文本等内容。每个 Surface 都有自己的像素格式和尺寸,它们可以被用来表示游戏中的任何可见元素,比如背景、角色、物品等。

创建Surface对象

你可以通过多种方式创建一个 Surface 对象:

通过 pygame.display.set_mode() 创建主显示 Surface,这通常是游戏窗口的画布。screen = pygame.display.set_mode((width, height))通过 pygame.Surface() 直接创建一个新的 Surface 对象。new_surface = pygame.Surface((width, height))通过加载一个图像文件创建 Surface 对象。image_surface = pygame.image.load('image.png')

Surface对象的属性和方法

Surface 对象有很多属性和方法,以下是一些常用的:

get_size(): 返回 Surface 对象的尺寸,即宽和高。get_width(): 返回 Surface 对象的宽度。get_height(): 返回 Surface 对象的高度。fill(): 使用单一颜色填充整个 Surface。blit(): 将一个 Surface 对象绘制到另一个上面,这是图像合成的基本操作。convert(): 改变 Surface 的像素格式,以更好地匹配显示设备,这可以提高绘制速度。set_alpha(): 设置 Surface 的透明度。get_rect(): 获取一个矩形对象,其大小与 Surface 相同,常用于处理位置和碰撞检测。

绘制和更新Surface

在Pygame中,当你对 Surface 进行绘制操作后,这些操作不会立即反映到屏幕上。你需要调用 pygame.display.flip() 或 pygame.display.update() 来更新显示的内容。

pygame.display.flip(): 更新整个屏幕的内容。pygame.display.update(): 可以更新屏幕的一个区域或多个区域。

Pygame 中 Surface 对象的基本使用方法如下:

1、创建 Surface 对象

要在 Pygame 中创建一个 Surface 对象,你可以使用 pygame.Surface() 函数,并传入一个元组,指定你希望创建的 Surface 的宽度和高度:

import pygame

# 初始化pygame pygame.init()

# 创建一个Surface对象 surface_width = 800 surface_height = 600 my_surface = pygame.Surface((surface_width, surface_height))  

2、绘制 Surface 对象

你可以使用各种 Pygame 的绘图函数来在 Surface 上绘制图形。例如,你可以在一个 Surface 上绘制一个矩形:

# 设置颜色 color = (255, 0, 0) # RGB颜色,这里是红色

# 绘制一个矩形 # pygame.draw.rect(Surface, color, Rect, width=0) pygame.draw.rect(my_surface, color, (10, 10, 50, 50))  

3、显示 Surface 对象

创建和绘制 Surface 之后,你需要将它绘制到主显示 Surface 上,这通常是你通过 pygame.display.set_mode() 获取的 Surface。你可以使用 blit 方法来完成这个操作:

# 创建主显示Surface screen = pygame.display.set_mode((800, 600))

# 把我们创建的Surface对象绘制到主显示Surface上 screen.blit(my_surface, (0, 0))

# 更新整个待显示的 Surface 对象到屏幕上 pygame.display.flip()

4、事件循环

在 Pygame 中,你需要设置一个事件循环来保持游戏的运行,并在适当的时候处理事件(如按键和退出):

# 游戏主循环 running = True while running:     for event in pygame.event.get():         if event.type == pygame.QUIT:             running = False

    # 其他游戏逻辑和渲染

    # 更新显示     pygame.display.flip()

# 退出pygame pygame.quit()

使用Python的pygame库画花

例1、效果图:

源码如下:

import pygame

import sys

# 初始化pygame

pygame.init()

# 设置窗口尺寸

width, height = 400, 400

screen = pygame.display.set_mode((width, height))

pygame.display.set_caption('画花')

# 定义颜色

WHITE = (255, 255, 255)

RED = (255, 0, 0)

YELLOW = (255, 255, 0)

GREEN = (0, 255, 0)

BROWN = (139, 69, 19)

# 绘制背景

screen.fill(WHITE)

# 画花

pygame.draw.circle(screen, RED, (200, 200), 30) # 花心

pygame.draw.circle(screen, YELLOW, (170, 170), 30) # 花瓣1

pygame.draw.circle(screen, YELLOW, (230, 170), 30) # 花瓣2

pygame.draw.circle(screen, YELLOW, (170, 230), 30) # 花瓣3

pygame.draw.circle(screen, YELLOW, (230, 230), 30) # 花瓣4

# 画枝条

pygame.draw.rect(screen, BROWN, (195, 250, 10, 100)) # 枝条

# 画叶子

pygame.draw.polygon(screen, GREEN, [(100, 200), (150, 280), (195, 300)]) # 左叶子

pygame.draw.polygon(screen, GREEN, [(280, 200), (250, 280), (205, 300)]) # 右叶子

# 刷新屏幕

pygame.display.flip()

# 保持窗口打开

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit()

例2、效果图:

源码如下:

import pygame

import sys

# 初始化 Pygame

pygame.init()

# 设置屏幕大小

screen = pygame.display.set_mode((600, 400))

# 设置标题

pygame.display.set_caption("花")

# 定义樱花的颜色

red = (255, 0, 0)

Green = (0, 255, 0)

# 定义枝条的颜色

brown = (139, 69, 19)

def draw_cherry_blossom(x, y):

# 绘制樱花的五个瓣

pygame.draw.ellipse(screen, red, (x, y, 10, 20))

pygame.draw.ellipse(screen, red, (x+10, y-10, 20, 10))

pygame.draw.ellipse(screen, red, (x+20, y, 10, 20))

pygame.draw.ellipse(screen, red, (x+5, y+10, 20, 10))

pygame.draw.ellipse(screen, red, (x-10, y+10, 20, 10))

# 绘制花的中心

pygame.draw.circle(screen, (255, 255, 255), (x+15, y+15), 5)

# 绘制枝条

pygame.draw.line(screen, Green, (x+15, y+1), (x+15, y+35), 2)

pygame.draw.line(screen, brown, (x+10, y+35), (x+20, y+35), 4)

# 游戏主循环

running = True

while running:

# 处理事件

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

# 填充背景

screen.fill((255, 255, 255))

# 绘制花

draw_cherry_blossom(150, 200)

draw_cherry_blossom(250, 250)

draw_cherry_blossom(300, 180)

draw_cherry_blossom(320, 280)

draw_cherry_blossom(400, 200)

# 更新屏幕

pygame.display.flip()

# 退出 Pygame

pygame.quit()

sys.exit()

说明:pygame.draw.line(screen, Green, (100,200),(220,320),2)的意思:

pygame.draw.line: 这是pygame库中的一个函数,用于绘制线段。

screen: 这是一个pygame的Surface 对象,通常是你想要绘制线的屏幕或窗口。

Green: 这是一个颜色元组,表示线的颜色。在这个例子中,线将是绿色的。

(100,200): 这是线段起始点的坐标。

(220,320): 这是线段终点的坐标。

2: 这是线段的宽度。

 

文章来源

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: