What do “.subsurface()”, “.convert_alpha()”, and “

2019-02-21 03:11发布

问题:

I'm trying to learn Pygame, and the tutorial that I am following has a section explaining how to animate sprites. It gives me a sprite sheet that has 8 images measuring 128x128 each, while the entire sheet measures 1024x128.

Then it presents the following code:

#! /usr/bin/env_python
import pygame, sys
from pygame.local import *

pygame.init()

ZONE = pygame.display.set_mode((400,300))

pygame.display.set_caption("Game Zone")

RED = (255,0,0)
clock = pygame.time.Clock()

counter = 0
sprites = []

sheet = pygame.image.load("spritesheet.gif").convert_alpha()
width = sheet.get_width()

for i in range(int(width/128)):
    sprites.append(sheet.subsurface(i*128,0,128,128)) 

while True:
    pygame.display.update()

    ZONE.fill(RED)
    ZONE.blit(sprites[counter],(10,10))

    counter = (counter + 1) % 8

    clock.tick(16) 

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit() 

The tutorial is very vague about what those lines do, so I wonder:

  1. What does sheet.subsurface() do? And what do those four parameters stand for? (I believe that the third and fourth are referring to the individual images' width and height.)
  2. What does .convert_alpha() do? The tutorial says it "preserves transparency," but I found it strange since I already used images with transparent backgrounds before and none of those needed such conversion.
  3. What does % do? I already know that / stands for division, but the tutorial never explained %.

回答1:

subsurface gets you a surface that represents a rectangular section of a larger surface. In this case you have one big surface with lots of sprites on it, and subsurface is used to extract the pieces from that surface. You could also create new surfaces and use blit to copy the pixels, but it's a bit easier to use subsurface and it doesn't need to copy the pixel data.

https://www.pygame.org/docs/ref/surface.html#pygame.Surface.subsurface

Suggested search: pygame subsurface

convert and convert_alpha are both used to convert surfaces to the same pixel format as used by the screen. This ensures that you won't lose performance because of conversions when you're blitting them to the screen. convert throws away any alpha channel, whereas convert_alpha keeps it. The comment that you see refers to the choice to use convert_alpha instead of convert, rather than a choice to use convert_alpha instead of nothing.

https://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert

Suggested search: pygame convert_alpha

The '%' operator isn't a Pygame feature, it's just Python's "modulo/remainder" operator. In this case it's used to make the counter variable loop repeatedly through the values 0 through to 7 and back to 0 again.

https://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations

Suggested search: python percent sign



回答2:

Let's talk about subsurface(). Assume you have 1,600 images you want to load into the program. There are two ways to do that. (Well, more than two, but I'm making a point here.) First, you could create 1,600 files, load each one into a surface in turn, and start the program. Alternately, you could place them in one file, load that one file into a single surface, and use subsurface(). In this case, spritesheet.gif is 128 pixels high, and contains a new image every 128 pixels.

The two ways basically do the same thing, but one may be more convenient than the other. In particular, opening and reading a file has a small performance cost, and if you need to do this 1,600 times in a row, that cost could be significant.

My understanding of a child surface is that it's basically a Pygame Surface, but defined in terms of a parent surface; if you changed the parent Surface, any child surfaces would be changed in the same way. However, in all other ways, it can be treated as a regular surface.