检查鼠标指针是否触摸使用循环创建的任何矩形
我目前正在创建一个扫雷游戏,而我的代码看起来像这样(用于创建矩形)。我已经创建了每个矩形及其坐标的列表,但是我现在需要一件事。我需要知道鼠标指针是否单击矩形(以及哪个矩形 - 每个矩形都有不同的信息)。当它单击一个时,它将检查那个是否有炸弹。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想知道行和列,则只需要测试鼠标是否在矩形中,然后使用
//
(落地式)计算行和列。在列表中查找 rect 对象,您可以使用
pygame.Rect.Collidelist
:另请参见我如何检测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:If you want to find the Rect object in the list, you can use
pygame.Rect.collidelist
:See also How do I detect collision in pygame? and Pygame mouse clicking detection.
我通常会这样这样做:
示例:
I usually do it like this:
Example: