你能找出我的代码中的一个错误,并解释为什么当我使用int((mouse_x + scroll_x) // (tw / MAX_COL)), int((mouse_y // th / MAX_ROW))并将鼠标悬停在某个磁贴上时,每次我zoom 窗口时,它总是打印不正确的磁贴编号吗?例如,当引用平铺(19,0)时,打印的数字不正确,它显示(21,0),并且偏移量随着更大的窗口比例而增加或减小.它也只有在我首先滚动我的窗口时才会发生.我的代码是:

import pygame as pg
import json

SCREEN_WIDTH = 1920
SCREEN_HEIGHT = 1080
ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT
WIDTH = 640
HEIGHT = 360
TILE_SIZE = 32
MAX_COL = 20
MAX_ROW = 10

pg.init()

# 16:9 R
screen = pg.display.set_mode((WIDTH, HEIGHT), pg.RESIZABLE) # 0, 0
s = pg.Surface([WIDTH, HEIGHT])

pg.display.set_caption("Level editor")

scroll_x = 0

move_front = False
move_back = False

img = pg.image.load("rock.png") # .get_rect().colliderect() -> bool

world_data = []
for col in range(MAX_COL):
    world_data.append([-1 for i in range(MAX_ROW)])

def draw_world():
    for x, col in enumerate(world_data):
        for y, row in enumerate(col):
            if row >= 0:
                s.blit(img, (x * TILE_SIZE - scroll_x, y * TILE_SIZE))
    
    with open("data.json", "w") as file:
        json.dump(world_data, file)


def draw_grid():

    s.fill((255, 255, 255))

    for v in range(MAX_COL + 1):
        # vertical / col
        pg.draw.line(s, (0, 0, 0), (TILE_SIZE * v - scroll_x, 0), (TILE_SIZE * v - scroll_x, HEIGHT))
        # horizontal / row
    for h in range(MAX_ROW + 1):
        pg.draw.line(s, (0, 0, 0), (0 - scroll_x, TILE_SIZE * h), (MAX_COL * TILE_SIZE - scroll_x, TILE_SIZE * h))

def draw_bg():
    global tw, th
    sw = screen.get_width()
    sh = screen.get_height()
    tw = sw
    th = tw / ASPECT
    if th > sh:
        th = sh
        tw = th * ASPECT
    pad_x = sw / 2 - tw / 2
    pad_y = sh / 2 - th / 2

    f = pg.transform.scale(s, (tw ,th))
    screen.blit(f, (pad_x, pad_y))

while True:

    draw_grid()
    draw_world()
    draw_bg()

    if move_front: # move_right
        scroll_x += 1
    elif move_back:
        scroll_x -= 1

    mouse_x, mouse_y = pg.mouse.get_pos()

    grid_x = screen.get_width() / 2 - tw / 2
    grid_y = screen.get_height() / 2 - th / 2
    mouse_x -= grid_x
    mouse_y -= grid_y

    print(int((mouse_x + scroll_x) // (tw / MAX_COL)), int((mouse_y // th / MAX_ROW)))

    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_d:
                move_front = True
            elif event.key == pg.K_a:
                move_back = True
        if event.type == pg.KEYUP:
            if event.key == pg.K_d:
                move_front = False
            elif event.key == pg.K_a:
                move_back = False
        if event.type == pg.MOUSEBUTTONDOWN:
            if mouse_x >= 0 and mouse_y >= 0:
                if mouse_x < tw and mouse_y < th:
                    pass

    pg.display.flip()

enter image description here enter image description here enter image description here

推荐答案

问题是,在计算列时,您会按比例zoom scroll_x.但是,网格在zoom 之前会滚动,因此绘制网格时使用的平铺大小必须用于计算滚动偏移:

print(int((mouse_x + scroll_x) // (tw / MAX_COL)), int((mouse_y // th / MAX_ROW)))

print(int(scroll_x / TILE_SIZE + mouse_x / (tw / MAX_COL)), int((mouse_y // (th / MAX_ROW))))

Python相关问答推荐

根据网格和相机参数渲染深度

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

优化pytorch函数以消除for循环

Mistral模型为不同的输入文本生成相同的嵌入

log 1 p numpy的意外行为

在np数组上实现无重叠的二维滑动窗口

如何设置视频语言时上传到YouTube与Python API客户端

计算每个IP的平均值

连接一个rabrame和另一个1d rabrame不是问题,但当使用[...]'运算符会产生不同的结果

如何在Python中使用Pandas将R s Tukey s HSD表转换为相关矩阵''

如何防止Pandas将索引标为周期?

如何从pandas DataFrame中获取. groupby()和. agg()之后的子列?

如何在GEKKO中使用复共轭物

用由数据帧的相应元素形成的列表的函数来替换列的行中的值

时长超过24小时如何从Excel导入时长数据

FileNotFoundError:[WinError 2]系统找不到指定的文件:在os.listdir中查找扩展名

在聚合中使用python-polars时如何计算模式

我怎样才能让深度测试在OpenGL中使用Python和PyGame呢?

函数()参数';代码';必须是代码而不是字符串

try 在单个WITH_COLUMNS_SEQ操作中链接表达式时,使用Polars数据帧时出现ComputeError