Pygame基础5-Surface & Rect

简介: Pygame基础5-Surface & Rect

5-Surface & Rect

Pygame显示处理图像的基础分别是Surface和Rect(Rectangle)。

精灵(Spirite)类将两者封装在一起,免去了手动处理surface和reactangle的麻烦。如果你对surface和rect的原理感兴趣,可以继续看下去。

Surface

Surface 原理

Surfaces:一个包含图像的层(A layer that contains visuals)。Pygame有两种Surface。


  • Display Surfaces:最终显示的层。可以认为是屏幕。
  • Regular Surfaces:可以放置在Display Surfaces上的层。可以认为是图片。

我们基本上只会用下面这个函数创建Display Surface

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

display.set_mode文档[1]

Regular Surfaces 可以通过pygame.Surface((width, height))创建:

#Regular Surface
second_surface = pygame.Surface((100, 200))
second_surface.fill((0, 255, 0))


或者通过pygame.image.load()创建:

kitten = pygame.image.load("kitten.jpg")

Regular Surfaces 需要放置在Display Surfaces上, 可以通过 screen.blit(surface,(x,y) )实现放置。

screen.blit(second_surface, (200, 200))


PyGame的坐标系以显示器的左上角为原点,向右为x轴正方向,向下为y轴正方向。

Surface 案例

下面我们创建一个绿色长方形,并在屏幕上显示:

import sys
import pygame

pygame.init()
clock = pygame.time.Clock()

width, height = 800, 600
#  Display surface
screen = pygame.display.set_mode((width, height))

# Regular Surface
second_surface = pygame.Surface((100, 200))
second_surface.fill((0, 255, 0))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    screen.fill((255, 255,255))  # 将screen填充为白色
    screen.blit(second_surface, (200, 200))  # 将second_surface放置在screen上
    pygame.display.flip() # 更新屏幕画面
    clock.tick(60)  # 控制游戏速率,每秒60帧

Rect

Rect原理

Rectangle:矩形 我们想要操作Surface时,需要使用Rect(矩形)。 Rect是一个矩形区域,可以借助Rect控制Surface的位置。


surface.get_rect()会返回一个和surface形状一样的矩形,默认位置在(0,0)(即屏幕的左上角)。然后screen.blit(kitten, kitten_rect) 会使用rect的左上角坐标放置surface。

kitten = pygame.image.load("kitten.jpg")
kitten_rect = kitten.get_rect(topleft=(100,100)) # 返回一个和kitten大小一样的矩形,topleft:  矩形左上角在屏幕上的位置
...
while True:
    ...
     screen.blit(kitten, kitten_rect) # 将ketten放在 rect位置。
    kitten_rect.x += 1 # 修改rect的位置 向右偏移一个像素。

下面我们通过Rect来移动Surface。

Rect案例

放置两个surface,并通过改变Rect移动其中的一个surface。

import sys
import pygame

pygame.init()
clock = pygame.time.Clock()

width, height = 800, 600
#  Display surface
screen = pygame.display.set_mode((width, height))

# Regular Surface
second_surface = pygame.Surface((100, 200))
second_surface.fill((0, 255, 0))

# img : also a surface
kitten = pygame.image.load("kitten.jpg")
kitten = pygame.transform.scale(kitten, (400, 400))

kitten_rect = kitten.get_rect(topleft=(100,100)) # 返回一个和kitten大小一样的矩形,topleft:  矩形左上角在屏幕上的位置


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    screen.fill((255, 255,255))  # 将screen填充为白色
    screen.blit(second_surface, (200, 200))  # 将second_surface放置在screen上
    
    screen.blit(kitten, kitten_rect)
    kitten_rect.x += 1

    pygame.display.flip() # 更新屏幕画面
    clock.tick(60)  # 控制游戏速率,每秒60帧

[1] display.set_mode文档: https://www.pygame.org/docs/ref/display.html#pygame.display.set_mode

相关文章
|
1月前
|
存储 人工智能 算法
使用 Python 和 Pygame 制作游戏:第九章到第十章
使用 Python 和 Pygame 制作游戏:第九章到第十章
62 0
使用 Python 和 Pygame 制作游戏:第九章到第十章
|
1月前
|
Python
Python使用pygame播放MP3
Python使用pygame播放MP3
45 0
|
1月前
|
定位技术 Python
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
【python】pygame实现植物大战僵尸小游戏(附源码 有注释)
800 1
|
6天前
|
开发框架 Python
Python的`pygame`库用于2D游戏开发,涵盖图形、音频和输入处理。
【6月更文挑战第21天】Python的`pygame`库用于2D游戏开发,涵盖图形、音频和输入处理。要开始,先通过`pip install pygame`安装。基本流程包括:初始化窗口、处理事件循环、添加游戏元素(如玩家和敌人)、响应用户输入、更新游戏状态及结束条件。随着项目发展,可逐步增加复杂性。
17 1
|
13天前
|
Python
【Python的魅力】:利用Pygame实现游戏坦克大战——含完整源码
【Python的魅力】:利用Pygame实现游戏坦克大战——含完整源码
|
15天前
|
Linux 开发工具 开发者
Pygame是一个免费且开源的Python库
【6月更文挑战第12天】Pygame是一个免费且开源的Python库
24 3
|
1月前
|
存储 定位技术 数据库
【python毕业设计】python基于Pygame的吃金币游戏设计与实现(源码+毕业论文)【独一无二】
【python毕业设计】python基于Pygame的吃金币游戏设计与实现(源码+毕业论文)【独一无二】

热门文章

最新文章