前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pygame基础5-Surface & Rect

Pygame基础5-Surface & Rect

作者头像
一只大鸽子
发布2024-04-11 13:00:21
570
发布2024-04-11 13:00:21
举报

5-Surface & Rect

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

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

Surface

Surface 原理

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

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

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

代码语言:javascript
复制
screen = pygame.display.set_mode((width, height))

display.set_mode文档[1]

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

代码语言:javascript
复制
# Regular Surface
second_surface = pygame.Surface((100, 200))
second_surface.fill((0, 255, 0))

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

代码语言:javascript
复制
kitten = pygame.image.load("kitten.jpg")

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

代码语言:javascript
复制
screen.blit(second_surface, (200, 200))

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

Surface 案例

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

代码语言:javascript
复制
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的位置。

Rect

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

代码语言:javascript
复制
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。

通过Rect移动Surface

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2024-03-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 一只大鸽子 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 5-Surface & Rect
  • Surface
    • Surface 原理
      • Surface 案例
      • Rect
        • Rect原理
          • Rect案例
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档