2015-12-26 76 views
1

我剛剛開始我的第一個PyGame項目,我無法找到如何正確使用蒙版實體應用於移動的Surface。使用PyGame在移動表面上分配靜態蒙版

我做了一個特定的類動畫面

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) 

這個類的重點是檢查面超過給定區域,並向左滑動,以便能夠讀取整個文本中呈現它。

在我的主循環,我只是它的blit到屏幕這樣

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())。我在網上看到一些關於Mask的例子,但是當面具是靜止的並且表面在移動時,我沒有找到使用它的方法。

編輯:我嘗試使用blit函數中的區域選項,但有一些奇怪的東西,面積和表面移動不同步。

編輯2:最後,這裏是區域選項的良好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())) 
+0

如果你有'self.pos'('pygame.Rect'實例),那麼你就不需要'self.x'和'self.y'因爲你有'self.pos.x','self.pos.y','self.pos.width','self.pos.right'(而不是'self.pos [0] + self.pos [2] ''''''self.pos.center'等。您可以使用'get_rect(x = self.x,y = self.y)'代替'get_rect()。move(self.x,self.y) ' – furas

+0

您可以使用'self.pos.x + = self.speed'來代替'self.pos = self.pos.move(self.speed,0)' – furas

+0

@furas感謝您的意見,我會澄清我的班級有你的建議。 – mickaelb91

回答

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() 

編輯:我與areablit製成的例子。我只有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() 
+0

我接受你的答案,即使我最終使用了區域選項。瞭解幾種做某事的方法總是很好的。對於這個領域你是對的,我只需要用'label.x'替換'label.pos.x'來使blit成爲靜態的,就像一個魅力一樣。再次感謝您;) – mickaelb91

+0

我也用'area'做了例子 - 因爲我以前從未使用'area'。 – furas

+0

是的,謝謝,如果你想看到我的「粗糙」代碼,它在Github上;)https://github.com/jbltx/PyAudioTFT – mickaelb91