Python:如何使绘制的元素在 pygame 中对齐网格 [英] Python: How to make drawn elements snap to grid in pygame

查看:65
本文介绍了Python:如何使绘制的元素在 pygame 中对齐网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个 Pathfinding 项目,但在创建工作 GUI 时卡住了.我正在使用 pygame 并且已经创建了一个网格和一个功能,当您按下(或持续按下)鼠标按钮时,它会绘制立方体.但是,这些立方体只会随您单击的位置移动,而不会捕捉到网格.我想过以某种方式使用模数,但我似乎无法让它工作.请找到下面附加的代码.Cube 类是我用于在屏幕上绘制方块的类.此外,drawgrid() 函数是我设置网格的方式.我很想在这方面得到一些帮助,因为我已经被困在这个障碍上三天了.

I am experimenting with a Pathfinding project and got stuck while creating a working GUI. I'm using pygame and already created a grid and a feature, which draws cubes when you press (or keep pressing) the mouse-button. However, these cubes just go wherever you click, and do not snap to the grid. I thought about using modulo somehow but I cannot seem to get it to work. Please find the code attached below. The Cube class is what I use for the squares drawn on the screen. Moreover, the drawgrid() function is how I set up my grid. I'd love some help on this, as I've been stuck on this roadblock for three days now.

class Cube:
    def update(self):
        self.cx, self.cy = pygame.mouse.get_pos()
        self.square = pygame.Rect(self.cx, self.cy, 20, 20)

    def draw(self):
        click = pygame.mouse.get_pressed()
        if click[0]:  # evaluate left button
            pygame.draw.rect(screen, (255, 255, 255), self.square)

其他drawgrid()函数:

def drawgrid(w, rows, surface):
    sizebtwn = w // rows  # Distance between Lines
    x = 0
    y = 0
    for i in range(rows):
        x = x + sizebtwn
        y = y + sizebtwn
        pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, w))
        pygame.draw.line(surface, (255, 255, 255), (0, y), (w, y))

推荐答案

您必须将位置与网格大小对齐.使用楼层除法运算符 (//) 将坐标除以单元格的大小并计算网格中的积分索引:

You have to align the position to the grids size. Use the floor division operator (//) to divide the coordinates by the size of a cell and compute the integral index in the grid:

x, y = pygame.mouse.get_pos()

ix = x // sizebtwn
iy = y // sizebtwn

将结果乘以单元格的大小来计算坐标:

Multiply the result by the size of a cell to compute the coordinate:

self.cx, self.cy = ix * sizebtwn, iy * sizebtwn


最小示例:


Minimal example:

import pygame
pygame.init()

screen = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()

def drawgrid(w, rows, surface):
    sizebtwn = w // rows 
    for i in range(0, w, sizebtwn):
        x, y = i, i
        pygame.draw.line(surface, (255, 255, 255), (x, 0), (x, w))
        pygame.draw.line(surface, (255, 255, 255), (0, y), (w, y))

class Cube:
    def update(self, sizebtwn):
        x, y = pygame.mouse.get_pos()
        ix = x // sizebtwn
        iy = y // sizebtwn
        self.cx, self.cy = ix * sizebtwn, iy * sizebtwn
        self.square = pygame.Rect(self.cx, self.cy, sizebtwn, sizebtwn)
    def draw(self, surface):
        click = pygame.mouse.get_pressed()
        if click[0]:
            pygame.draw.rect(surface, (255, 255, 255), self.square)

cube = Cube()

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    cube.update(screen.get_width() // 10)

    screen.fill(0)
    drawgrid(screen.get_width(), 10, screen)
    cube.draw(screen)
    pygame.display.flip()

这篇关于Python:如何使绘制的元素在 pygame 中对齐网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆