2017-03-26 170 views
1

我正在用Pygame製作一個簡單的基於瓷磚的遊戲。突出顯示Pygame瓷磚地圖上的瓷磚

目前,它顯示一個隨機選擇的瓷磚10x10網格。這部分工作完美,但我突出顯示問題。

當您將鼠標懸停在某個圖塊上時,它應該以大約一半的不透明度加載一塊灰色圖塊。它加載,但不透明度無法正常工作。如果您將鼠標放在一個圖塊上,則它會正確加載不透明度和全部圖片。但是,如果您將鼠標移動到磁貼上,它就會正常傳送,而不會透明。

我認爲這是因爲它每次發生事件時都會加載突出顯示塊,但我不知道如何解決該問題。

我正在使用一個名爲Tilemap的類來生成和繪製地圖。我認爲這是造成這一切的draw()函數。

import pygame 
import sys 
import random 
from pygame.locals import * 

running = True 

class Tilemap: 
    tilemap = [] 
    ht = None # ht = highlight texture 

    def __init__(self, height, width, tilesize, textures): 
     self.height = height # How many tiles high it is 
     self.width = width # How many tiles wide it is 
     self.tilesize = tilesize # How many pixels each tile is 
     self.textures = textures # The textures 
     self.size = (self.width*self.tilesize,self.height*self.tilesize) 

    def generate_random(self): 
     # Generate a random tilemap 
     self.tilemap = [[random.randint(0, len(
      self.textures)-1) for e in range(self.width)] for e in range(
       self.height)] 

    def draw(self, display, mouse=None): 
     mouse = mouse 
     # Draw the map 
     for row in range(self.width): 
      for column in range(self.height): 



       texture = self.textures[self.tilemap[row][column]] 
       # Highlight a tile (this is where the problem is) 
       if self.ht != None: 
        if mouse[0] >= (column*self.tilesize) and mouse[0] <= (
         column*self.tilesize)+self.tilesize: 
         if mouse[1] >= (row*self.tilesize) and mouse[1] <= (
          row*self.tilesize)+self.tilesize: 
          texture = self.ht 

       display.blit(texture, 
          (column*self.tilesize, row*self.tilesize)) 




tilemap = Tilemap(10,10,40, 
        # Load the textures 
        {0: pygame.image.load("tile1.png"), 
        1: pygame.image.load("tile2.png") 
        } 
       ) 

tilemap.generate_random() # Generate a random tilemap 

pygame.init() 
DISPLAYSURF = pygame.display.set_mode((tilemap.size)) 
# Load the highlighter 
tilemap.ht = pygame.image.load("highlight.png").convert_alpha() 

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

     # Draw the tilemap 
     tilemap.draw(DISPLAYSURF, pygame.mouse.get_pos()) 

    pygame.display.update() 

如果您需要更多解釋請隨時詢問!

回答

2

如果你只是while running:

# Load the highlighter 
tilemap.ht = pygame.image.load("highlight.png").convert(8) 
tilemap.ht.set_alpha(64) 

之前添加行tilemap.ht.set_alpha(<VALUE>)瓷磚將是透明的。該值介於0和255之間(包括)。我也將convert_alpha()更改爲convert(8),所以它是8位的。

來源:

how to use pygame set_alpha() on a picture

附加說明:您可能要實現它的廣場被突出顯示,這樣,直到選擇了新的瓷磚它不會再次繪製跟蹤。此時,當鼠標移動時,它將繼續在所選瓷磚上畫正方形,導致它變得不透明。

實施例:

enter image description here

初始選擇與繼續選擇

1

每次繪製到在Pygame的一個表面,所述前一幀的數據仍然存在。 由於您繪製的是alpha,所以之前的部分透明的繪圖仍處於相同的位置,並且您在每個框架的相同位置繪製了部分透明的圖塊。這導致該方塊越來越接近最終顏色。你通常會做的是擦掉你正在拍攝的區域,然後重新繪製。在這種情況下,你希望你的display.blit之前做到這一點:

display.fill((0,0,0), (0, 0, column*self.tilesize, row*self.tilesize)) 
display.blit(texture, (column*self.tilesize, row*self.tilesize)) 

這將工作,只要你的背景是黑色的。如果最終獲得背景圖像,則只需將背景圖像切片粘貼到那裏。

此代碼未經測試,因爲我沒有您的示例PNG,但如果您有問題,請讓我知道,我會更新答案。