【问题标题】:How to make a rectangle completely cover a tile in pygame [duplicate]如何在pygame中使矩形完全覆盖瓷砖[重复]
【发布时间】:2019-05-20 20:59:05
【问题描述】:

我正在为正在制作的瓷砖游戏创建地图编辑器。我正在尝试在地图编辑器中的磁贴周围创建一个透明矩形,尽管它覆盖了大部分磁贴而不是全部。那么我如何让 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 模块只是一个带有颜色和功能的模块,用于创建文本和按钮。

这就是我希望我的光标的样子,所以当光标越过一个瓷砖时,它会完全覆盖它 这是我的地图编辑器,光标在图块上,但光标图块没有覆盖整个图块

【问题讨论】:

  • 我不太明白你的问题;也许您应该将问题中的代码更改为可自行运行。当用户应该能够移动鼠标时,让 pygame 光标停留在透明矩形的中间是什么意思? Tiles.Texture_Tags 是什么?
  • 好的,我会处理代码
  • 您的图块和光标矩形似乎都是 32 像素宽。这应该“覆盖”瓷砖,但如果你想要一些备用像素,也许你想在“选择器”光标大小(比如 36 而不是 32)中添加一些像素,并且在规范化你的 mouse_ 之后?坐标,减去“2”使其居中?从我可以从这段代码中读到你正在创建一个单一的瓷砖? -
  • 是的,它会在地图中创建一个图块。我这样做只是为了测试光标。是为了覆盖整个瓷砖,但由于某种原因,瓷砖只覆盖了大部分瓷砖,然后光标瓷砖挂在瓷砖上。我想知道如何在不覆盖其他图块的情况下将光标图块完全覆盖在地图图块上
  • 所以我已经玩了一点,它工作得很好,直到我开始移动,所以我认为相机正在添加或减去鼠标,导致光标瓷砖不能正确落在瓷砖上。我只是不确定这是怎么发生的

标签: python python-3.x pygame


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多