파이 게임 기반의 간단한 타일 기반 게임을 만들고 있습니다.파이 게임 타일 맵을 강조 표시합니다.
현재, 무작위로 선택된 타일이 10x10 격자로 표시됩니다. 그 부분은 완벽하게 작동하지만 강조 표시에 문제가 있습니다.
타일 위로 마우스를 가져 가면 약 절반의 불투명도로 회색 타일이로드됩니다. 로드되지만 불투명도가 올바르게 작동하지 않습니다. 타일에 마우스를 올려 놓으면 불투명도와 함께 마우스가 올바르게로드됩니다. 그러나 마우스 주위를 마우스로 움직이면 불투명하지 않고 정상적으로 변합니다.
이벤트가 발생할 때마다 강조 표시 타일이로드되기 때문에 상황이 발생한다고 생각하지만 문제를 해결하는 방법을 모르겠습니다.
타일 맵을 생성하고 그릴 때 사용한 타일 맵이라는 클래스를 사용하고 있습니다. 나는이 모든 것을 일으키는 기능이 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()
자세한 설명이 필요하면 언제든지 문의하십시오. 그냥 while running:
# Load the highlighter
tilemap.ht = pygame.image.load("highlight.png").convert(8)
tilemap.ht.set_alpha(64)
전에 라인 tilemap.ht.set_alpha(<VALUE>)
을 추가하면