1

昨日、最初の PyGame プロジェクトを開始したばかりですが、Mask エンティティを適切に使用して移動する Surface に適用する方法が見つかりません。

アニメーション化されたサーフェス用の特定のクラスを作成しました

class LabelObject():
    def __init__(self, surface, x, y, endX, speed, idleTime):
        self.surface = surface
        self.x = x
        self.y = y
        self.endX = endX
        self.speed = speed
        self.idleTime = idleTime
        self.alpha = 255
        self.timer_start = 0
        self.timer_end = 0
        self.pos = self.surface.get_rect(x=self.x,y=self.y)

    def move(self):
        self.timer_start += 1
        if self.timer_start > self.idleTime:
            if self.pos.right > self.endX:
                self.pos.x += self.speed
            elif self.pos.x < self.x:
                self.timer_end += 1
                if self.timer_end > self.idleTime:
                    self.alpha -= 25
                if self.timer_end > self.idleTime + 11:
                    self.pos = self.surface.get_rect(x=self.x, y=self.y)
                    self.timer_start = 0
                    self.timer_end = 0
                    self.alpha = 255
        self.surface.set_alpha(self.alpha)

このクラスのポイントは、サーフェスが特定の領域を超えているかどうかを確認し、左にスライドして、その中にレンダリングされたテキストを完全に読み取れるようにすることです。

メインループでは、このように画面にブリットできます

label = LabelObject(textSurface,20,10,100,-0.5,30)
while not over:
    for event in pygame.event.get():
         if event.type == pygame.QUIT
             over = True
    screen.fill((0,0,0))
    label.move()
    screen.blit(label.surface, label.pos)
    pygame.display.update()

これは問題なく動作しますが、移動する必要のないマスクを適用する必要があります。この例では、マスクの Rect は になります(20, 10, 100-20, label.surface.get_height())。Web でマスクに関する例をいくつか見ましたが、マスクが静的で表面が動いている場合に使用する方法が見つかりませんでした。

編集: blit 関数で area オプションを使用してみましたが、奇妙な点があり、領域とサーフェスの動きが同期していません。

EDIT2:最後に、area オプションを使用した優れた blit 関数を次に示します。静的な位置でブリットを作成し、アニメーションの位置で領域を作成するだけです:

self.screen.blit(label.surface, (label.x,label.y), area=pygame.Rect(label.x-label.pos.x, 0, label.endX-label.x, label.surface.get_height()))
4

1 に答える 1

0

subsurface完全な画像の目に見える部分を切り取るために、完全に機能する例を実行しようとしています。Rect (sub_image_rect) は常に同じです - 同じ場所に描画します。オフセットを変更して、画像の別の部分を切り取るだけです。

私はテキストとフォントを使用して画像を生成します。誰もが独自の画像なしで実行できます。

import pygame

# --- constants ---

BLACK = (  0,  0,  0)
RED   = (255,  0,  0)

# --- classes ---

class Label():

    def __init__(self, text, x, y, width):

        font = pygame.font.SysFont(None, 35)

        # generate full image
        self.image = font.render(text, True, RED)
        self.rect = self.image.get_rect(x=x, y=y)

        self.width = width
        self.speed = 1

        # offset of visible part
        self.offset_x = 0
        self.max_offset_x = self.rect.width - self.width

        # visible part of image
        self.sub_image = self.image.subsurface((self.offset_x,0), (self.width, self.rect.height))
        self.sub_image_rect = self.sub_image.get_rect(x=x, y=y)


    def move(self):

        # change offset
        self.offset_x += self.speed

        # change move direction
        if self.offset_x < 0:
            self.offset_x = 0
            self.speed = -self.speed
        elif self.offset_x > self.max_offset_x:
            self.offset_x = self.max_offset_x
            self.speed = -self.speed

        print self.offset_x, self.max_offset_x
        # visible part of image
        self.sub_image = self.image.subsurface((self.offset_x, 0),(self.width, self.rect.height))

    def draw(self, surface):
        surface.blit(self.sub_image, self.sub_image_rect)

# --- main ---

pygame.init()
screen = pygame.display.set_mode((800,600))

label = Label("Hello World of Python and Pygame!", 100, 100, 200)

# --- mainloop ---

fps = pygame.time.Clock()
running = True

while running:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key ==  pygame.K_ESCAPE:
                running = False

    # --- updates ---

    label.move()

    # --- draws ---

    screen.fill(BLACK)

    label.draw(screen)

    fps.tick(25)
    pygame.display.flip()

# --- the end ---

pygame.quit()

編集:area in で例を作成しましたblit。3行だけ変更しました-# changedコードで確認してください

import pygame

# --- constants ---

BLACK = (  0,  0,  0)
RED   = (255,  0,  0)

# --- classes ---

class Label():

    def __init__(self, text, x, y, width):

        font = pygame.font.SysFont(None, 35)

        # generate full image
        self.image = font.render(text, True, RED)
        self.rect = self.image.get_rect(x=x, y=y)

        self.width = width
        self.speed = 1

        # offset of visible part
        self.offset_x = 0
        self.max_offset_x = self.rect.width - self.width

        # visible part of image as 'rect' - 'width' is always the same
        self.sub_image_rect = self.image.get_rect(x=0, width=self.width) # changed

    def move(self):

        # change offset
        self.offset_x += self.speed

        # change move direction
        if self.offset_x < 0:
            self.offset_x = 0
            self.speed = -self.speed
        elif self.offset_x > self.max_offset_x:
            self.offset_x = self.max_offset_x
            self.speed = -self.speed

        # visible part of image - change only `x` 
        self.sub_image_rect.x = self.offset_x  # changed

    def draw(self, surface):
        surface.blit(self.image, self.rect, area=self.sub_image_rect)  # changed

# --- main ---

pygame.init()
screen = pygame.display.set_mode((800,600))

label = Label("Hello World of Python and Pygame!", 100, 100, 200)

# --- mainloop ---

fps = pygame.time.Clock()
running = True

while running:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key ==  pygame.K_ESCAPE:
                running = False

    # --- updates ---

    label.move()

    # --- draws ---

    screen.fill(BLACK)

    label.draw(screen)

    fps.tick(25)
    pygame.display.flip()

# --- the end ---

pygame.quit()
于 2015-12-26T15:59:39.063 に答える