检查鼠标指针是否触摸使用循环创建的任何矩形

发布于 2025-02-04 10:10:21 字数 2494 浏览 2 评论 0原文

我目前正在创建一个扫雷游戏,而我的代码看起来像这样(用于创建矩形)。我已经创建了每个矩形及其坐标的列表,但是我现在需要一件事。我需要知道鼠标指针是否单击矩形(以及哪个矩形 - 每个矩形都有不同的信息)。当它单击一个时,它将检查那个是否有炸弹。

for i in range(1,boardSquaresY):
    global rectList
    for x in range(1,boardSquaresX):
        pygame.draw.rect(display,gray,pygame.Rect(squareX*x,squareY,tileSize,tileSize))
        rectList.append(Rect(squareX*x,squareY,tileSize,tileSize))
        print(rectList)
        for stroke in range(4):
            pygame.draw.rect(display, (0,0,0), pygame.Rect(squareX*x-2, squareY-2, tileSize + 2, tileSize + 2), 1)
            pygame.draw.rect(display, (220, 220, 220), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 2, tileSize + 2), 1)
            pygame.draw.rect(display, (150, 150, 150), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 0.5, tileSize + 1), 1)
    squareY += 50

下面的完整代码(当前) - 到目前为止仅完成UI。

import pygame
from pygame.locals import *
#Beginner has a 9x9 board and 10 mines
rectList = []
def Main():
    pygame.init()

    display = pygame.display.set_mode((850,600),0,32)

    white = (255,255,255)
    blue = (0,0,255)
    gray = (220,220,220)
    display.fill(white)

    squareX = 50
    squareY = 50
    tileSize = 50
    boardSquaresX = 10
    boardSquaresY = 10
    mouse = pygame.mouse.get_pos()
    print(mouse)

    for i in range(1,boardSquaresY):
        global rectList
        for x in range(1,boardSquaresX):
            pygame.draw.rect(display,gray,pygame.Rect(squareX*x,squareY,tileSize,tileSize))
            rectList.append(Rect(squareX*x,squareY,tileSize,tileSize))
            print(rectList)
            for stroke in range(4):
                pygame.draw.rect(display, (0,0,0), pygame.Rect(squareX*x-2, squareY-2, tileSize + 2, tileSize + 2), 1)
                pygame.draw.rect(display, (220, 220, 220), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 2, tileSize + 2), 1)
                pygame.draw.rect(display, (150, 150, 150), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 0.5, tileSize + 1), 1)
        squareY += 50
    while True:
        mouse = pygame.mouse.get_pos()
        if mouse[0]
        if squareX * x + tileSize > mouse[0] > squareX * x - tileSize and squareY + tileSize > mouse[1] > squareY - tileSize:
            print(mouse)
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.update()
Main()
rectList = []

I'm currently creating a minesweeper game and my code looks something like this (for creating the rectangles). I've already created a list of every rectangle and their coordinates but there's one thing I now need. I need to know if the mouse pointer clicks on a rectangle (and which rectangle - each will have different information). When it clicks on one, it will check if that one has a bomb.

for i in range(1,boardSquaresY):
    global rectList
    for x in range(1,boardSquaresX):
        pygame.draw.rect(display,gray,pygame.Rect(squareX*x,squareY,tileSize,tileSize))
        rectList.append(Rect(squareX*x,squareY,tileSize,tileSize))
        print(rectList)
        for stroke in range(4):
            pygame.draw.rect(display, (0,0,0), pygame.Rect(squareX*x-2, squareY-2, tileSize + 2, tileSize + 2), 1)
            pygame.draw.rect(display, (220, 220, 220), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 2, tileSize + 2), 1)
            pygame.draw.rect(display, (150, 150, 150), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 0.5, tileSize + 1), 1)
    squareY += 50

Full code below (currently) - only done UI so far.

import pygame
from pygame.locals import *
#Beginner has a 9x9 board and 10 mines
rectList = []
def Main():
    pygame.init()

    display = pygame.display.set_mode((850,600),0,32)

    white = (255,255,255)
    blue = (0,0,255)
    gray = (220,220,220)
    display.fill(white)

    squareX = 50
    squareY = 50
    tileSize = 50
    boardSquaresX = 10
    boardSquaresY = 10
    mouse = pygame.mouse.get_pos()
    print(mouse)

    for i in range(1,boardSquaresY):
        global rectList
        for x in range(1,boardSquaresX):
            pygame.draw.rect(display,gray,pygame.Rect(squareX*x,squareY,tileSize,tileSize))
            rectList.append(Rect(squareX*x,squareY,tileSize,tileSize))
            print(rectList)
            for stroke in range(4):
                pygame.draw.rect(display, (0,0,0), pygame.Rect(squareX*x-2, squareY-2, tileSize + 2, tileSize + 2), 1)
                pygame.draw.rect(display, (220, 220, 220), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 2, tileSize + 2), 1)
                pygame.draw.rect(display, (150, 150, 150), pygame.Rect(squareX * x - 4, squareY - 4, tileSize + 0.5, tileSize + 1), 1)
        squareY += 50
    while True:
        mouse = pygame.mouse.get_pos()
        if mouse[0]
        if squareX * x + tileSize > mouse[0] > squareX * x - tileSize and squareY + tileSize > mouse[1] > squareY - tileSize:
            print(mouse)
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
        pygame.display.update()
Main()
rectList = []

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那些过往 2025-02-11 10:10:21

如果您只想知道行和列,则只需要测试鼠标是否在矩形中,然后使用//(落地式)计算行和列

mouse = pygame.mouse.get_pos()
gridRect = pygame.Rect(0, 0, boardSquaresX*tileSize, boardSquaresY*tileSize)
if gridRect.collidePoint(mouse):
    row = mouse[0] // tileSize
    col = mouse[1] // tileSize

。在列表中查找 rect 对象,您可以使用 pygame.Rect.Collidelist

mouse = pygame.mouse.get_pos()
muoseRect = pygame.Rect(mouse[0], mouse[1], 1, 1)
index = muoseRect.collidelist(rectList):
if index >= 0:
    rect = rectList[index]
    row = rect.x // tileSize
    col = rect.y // tileSize

另请参见我如何检测Pygame中的碰撞? pygame鼠标单击检测

If you only want to know the row and column, you just need to test if the mouse is in the rectangle and calculate the row and column using the // (floor division) operator:

mouse = pygame.mouse.get_pos()
gridRect = pygame.Rect(0, 0, boardSquaresX*tileSize, boardSquaresY*tileSize)
if gridRect.collidePoint(mouse):
    row = mouse[0] // tileSize
    col = mouse[1] // tileSize

If you want to find the Rect object in the list, you can use pygame.Rect.collidelist:

mouse = pygame.mouse.get_pos()
muoseRect = pygame.Rect(mouse[0], mouse[1], 1, 1)
index = muoseRect.collidelist(rectList):
if index >= 0:
    rect = rectList[index]
    row = rect.x // tileSize
    col = rect.y // tileSize

See also How do I detect collision in pygame? and Pygame mouse clicking detection.

冰火雁神 2025-02-11 10:10:21

我通常会这样这样做:

grid = []
for y in range(5):
    grid.append([])
    for x in range(5):
        grid[y].append(0)

rectangle_size = 100

while True:
    mouse_pos = pygame.mouse.get_pos()
    for y in range(len(grid)):
        for x in range(len(grid[y])):
            if y * rectangle_size < mouse_pos[0] < y * rectangle_size + 50:
                if x * rectangle_size < mouse_pos[1] < x * rectangle_size + 50:
                    print(f"{y}, {x}")

示例:

import pygame
import sys
import random


pygame.init()

pygame.display.set_caption("hub")
screen = pygame.display.set_mode((500, 500))

grid = []
for y in range(5):
    grid.append([])
    for x in range(5):
        grid[y].append((random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))

rect_size = 100

while True:
    pygame.time.Clock().tick(30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((0, 0, 0))

    mouse_pos = pygame.mouse.get_pos()
    for y in range(len(grid)):
        for x in range(len(grid[y])):

            pygame.draw.rect(screen, grid[y][x], (y * rect_size, x * rect_size, rect_size, rect_size))

            if y * rect_size < mouse_pos[0] < y * rect_size + 50:
                if x * rect_size < mouse_pos[1] < x * rect_size + 50:
                    print(f"{y}, {x}")
    pygame.display.update()

I usually do it like this:

grid = []
for y in range(5):
    grid.append([])
    for x in range(5):
        grid[y].append(0)

rectangle_size = 100

while True:
    mouse_pos = pygame.mouse.get_pos()
    for y in range(len(grid)):
        for x in range(len(grid[y])):
            if y * rectangle_size < mouse_pos[0] < y * rectangle_size + 50:
                if x * rectangle_size < mouse_pos[1] < x * rectangle_size + 50:
                    print(f"{y}, {x}")

Example:

import pygame
import sys
import random


pygame.init()

pygame.display.set_caption("hub")
screen = pygame.display.set_mode((500, 500))

grid = []
for y in range(5):
    grid.append([])
    for x in range(5):
        grid[y].append((random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))

rect_size = 100

while True:
    pygame.time.Clock().tick(30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill((0, 0, 0))

    mouse_pos = pygame.mouse.get_pos()
    for y in range(len(grid)):
        for x in range(len(grid[y])):

            pygame.draw.rect(screen, grid[y][x], (y * rect_size, x * rect_size, rect_size, rect_size))

            if y * rect_size < mouse_pos[0] < y * rect_size + 50:
                if x * rect_size < mouse_pos[1] < x * rect_size + 50:
                    print(f"{y}, {x}")
    pygame.display.update()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文