【问题标题】:Highlighting tile on Pygame tilemap在 Pygame 瓷砖地图上突出显示瓷砖
【发布时间】:2017-03-26 09:42:01
【问题描述】:

我正在使用 Pygame 制作一个简单的基于图块的游戏。

目前,它显示一个 10x10 的随机选择图块网格。那部分工作得很好,但我在突出显示时遇到了问题。

当您将鼠标悬停在一个图块上时,它应该加载一个大约一半不透明度的灰色图块。它加载,但不透明度无法正常工作。如果将鼠标放在瓷砖上,它会正确加载不透明度和所有内容。但是如果你在瓷砖周围移动鼠标,它就会变成正常的,没有不透明度。

我认为它正在发生,因为每次发生事件时它都会加载突出显示的磁贴,但我不知道如何修复它。

我正在使用一个名为 Tilemap 的类来生成和绘制 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()

如果您需要更多解释,请随时询问!

【问题讨论】:

    标签: python pygame tile


    【解决方案1】:

    如果您在 while running: 之前添加行 tilemap.ht.set_alpha(&lt;VALUE&gt;)

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

    图块将是透明的。该值介于 0 和 255(含)之间。我还将convert_alpha() 更改为convert(8),所以它是8 位的。

    来源:

    how to use pygame set_alpha() on a picture

    附加说明:您可能希望跟踪突出显示哪个方块,以便在选择新图块之前不会再次绘制。此时,当鼠标移动时,它将继续在选定的图块上绘制正方形,使其变得不那么透明。

    例子:

    初始选择与继续选择

    【讨论】:

      【解决方案2】:

      每次在 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,但如果您有问题,请告诉我,我会更新答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-18
        • 1970-01-01
        • 2018-05-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多