如何使矩形完全覆盖pygame中的瓷砖

时间:2018-12-20 10:37:42

标签: python-3.x pygame

我正在为我制作的瓷砖游戏创建地图编辑器。我正在尝试在地图编辑器中的某个图块周围创建一个透明矩形,以实现某种效果,尽管该图覆盖了大多数图块,而不是全部。因此,我将如何使pygame光标停留在透明矩形的中间,并使其覆盖整个图块本身。我试过将mouse_x和mouse_y变量更改为仅鼠标移动,但它位于光标的边缘,并且不粘在图块上。因此,如何使光标位于矩形的中间,然后使矩形停留在大部分矩形所在的图块中,但要填充整个矩形而不是大多数矩形。这是我的地图编辑器代码

import pygame, math
from scripts.Textures import *
from scripts.Dexti_GUI import *

pygame.init()

screen = pygame.display.set_mode((600,600), pygame.RESIZABLE)

n = []
next = False

tile_data = []

# Get Map Dimensions
MapWidth = 1
MapHeight = 1

camera_x, camera_y = 0,0
mouse_x, mouse_y = 0,0

# CREATE THE MAP WITH USERS DIMENSIONS
for x in range(0, MapWidth * 32, 32):
    for y in range(0, MapHeight * 32, 32):
        tile_data.append([x, y, "1"])

selector = pygame.Surface((32, 32), pygame.HWSURFACE|pygame.SRCALPHA)
selector.fill(Color.WithAlpha(100, Color.RoyalBlue)) #CREATES A SEE THROUGH BLUE RECTANGLE THE SIZE OF A TILE


while True:
    screen.fill(Color.Black)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.VIDEORESIZE:
            screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
            width = event.w
            height = event.h
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_1:
                n.append("1")
            elif event.key == pygame.K_2:
                n.append("2")
            elif event.key == pygame.K_3:
                n.append("3")
            elif event.key == pygame.K_4:
                n.append("4")
            elif event.key == pygame.K_5:
                n.append("5")
            elif event.key == pygame.K_6:
                n.append("6")
            elif event.key == pygame.K_7:
                n.append("7")
            elif event.key == pygame.K_8:
                n.append("8")
            elif event.key == pygame.K_9:
                n.append("9")
            elif event.key == pygame.K_0:
                n.append("0")
            elif event.key == pygame.K_BACKSPACE:
                try:
                    del n[-1]
                except:
                    pass
            elif event.key == pygame.K_RETURN:
                next = True

    move = pygame.key.get_pressed()
    if move[pygame.K_w]:
        camera_y += 1
    elif move[pygame.K_s]:
        camera_y -= 1
    elif move[pygame.K_a]:
        camera_x += 1
    elif move[pygame.K_d]:
        camera_x -= 1

    #Display Map
    for tile in tile_data:
        try:
            screen.blit(Tiles.Texture_Tags[tile[2]], (tile[0] + camera_x, tile[1] + camera_y))
        except:
            print("Error creating Default Map! Be sure that a textures.py file exists in scripts")

    #Display cursor
    mouse = pygame.mouse.get_pos()
    mouse_x = math.floor(mouse[0] / 32) * 32
    mouse_y = math.floor(mouse[1] / 32) * 32
    screen.blit(selector, (mouse_x,mouse_y))

    pygame.display.flip()

纹理文件:

import pygame

class Tiles:

    TileSize = 32

    Blocked = []

    Blocked_Types = [] # "Add Tile Number to block that tile

    def Load_texture(self, file, size):
        bitmap = pygame.image.load(file)
        bitmap = pygame.transform.scale(bitmap, (size, size))
        surface = pygame.Surface((size, size), pygame.HWSURFACE|pygame.SRCALPHA)
        surface.blit(bitmap, (0,0))
        return surface

    Grass = Load_texture(Load_texture, "Tiles\\Grass.png", 32)

    Texture_Tags = {"1": Grass} #Add texture tag here

Dexti_GUI模块只是具有颜色和功能的模块,用于创建文本和按钮。

A sinle tile with cursor tile covering it 这就是我想要我的光标的方式,因此当光标经过一个图块时它会完全覆盖它 My tiled map editor with one tile and then the cursor tile hanging off that tile 这是我的地图编辑器,光标位于图块上方,但光标图块没有覆盖整个图块

1 个答案:

答案 0 :(得分:1)

blit 的第二个参数可以是一个 pygame.Rect 对象。使用 get_rect() 获取 Surface 的边界矩形并通过关键字参数设置中心:

screen.blit(my_surface, my_surface.get_rect(center = (x, y)))