python - Pygame鼠标点击更新?

标签 python pygame

我希望当我点击其中一个可能的方 block 时打印一个 X。 我列出了井字游戏网格的所有坐标。我还添加了每个矩形坐标的中心点。如果我在显示 X 按钮的方 block 之一的区域内单击,我将尝试做到这一点。最终目标是使双击结果产生一个已提交的结果。


import matplotlib.pyplot as plt
import pygame
import sys
import pygame

pygame.font.init()

size = 320, 240
black = 0, 0, 0
white = 255,255,255
red = 255, 0, 0

x1y1 = [(100, 0), (100, 300)]
x2y2 = [(200, 0), (200, 300)]
x3y3 = [(0, 100), (300, 100)]
x4y4 = [(0, 200), (300, 200)]

def centroid(coord1, coord2): 
    xx = 50
    yy = 50
    coords = []
    for a in range(0,3): 
        for b in range(0,3):
            if a + 1 == int(coord1) and b + 1 == int(coord2):
                coords += tuple([xx + a*100, yy + b*100])
                return tuple(coords)

def fourCorners(a,b,length,width): 
    center = (a, b)
    corner3 = (int(a + length/2), int(b + width/2))
    corner2 = (int(a + length/2), int(b - width/2))
    corner4 = (int(a - length/2), int(b + width/2))
    corner1 = (int(a - length/2), int(b - width/2))
    return [corner1 ,corner2 ,corner3 ,corner4]

def withinRect(a,b,corners):
    if len(corners) != 4: 
        print('Pass a list parameter of length 4.')
    elif int(corners[0][0]) >= int(a) >= int(corners[1][0]) and int(corners[0][1]) >= int(b) >= int(corners[1][1]): 
        return True    


screen = pygame.display.set_mode((300,300))

screen.fill(white)
pygame.draw.line(screen, (0, 0, 255), x1y1[0], x1y1[1], 3)
pygame.draw.line(screen, (0, 0, 255), x2y2[0], x2y2[1], 3)
pygame.draw.line(screen, (0, 0, 255), x3y3[0], x3y3[1], 3)
pygame.draw.line(screen, (0, 0, 255), x4y4[0], x4y4[1], 3)

while True:
    ev = pygame.event.get()
    for event in ev:
    # handle get_pressed
        if event.type == pygame.MOUSEBUTTONUP:
            pos = pygame.mouse.get_pos()      
            x, y = event.pos
            v = fourCorners(centroid(1,1)[0],centroid(1,1)[1],100,100)
            button = pygame.Rect(v[1][0], v[1][1] ,100,100)
            if (withinRect(x,y,v) == True and button.collidepoint(event.pos)):
                print('X')
                pygame.display.flip()
            else: 
                break
    pygame.display.flip()

最佳答案

通过2个嵌套loos遍历所有9个字段并定义一个pygame.Rect对于相应的字段:

for a in range(3):
    for b in range(3):
        button = pygame.Rect(a*100, b*100, 100, 100)

如果您愿意,您可以定义边距,以将区域限制在字段的中心:

margin = 4
button = pygame.Rect(a*100+margin, b*100+margin, 100-2*margin, 100-2*margin)

使用colliderect()检查点击是否在区域内:

if button.collidepoint(pos):
    # [...]

从 .bottomleft.topright 画一条线,从 .topleft.bottomright,形成一个十字:

pygame.draw.line(screen, (0, 0, 255), button.bottomleft, button.topright, 3)
pygame.draw.line(screen, (0, 0, 255), button.topleft, button.bottomright, 3)

ev = pygame.event.get()
for event in ev:

    if event.type == pygame.MOUSEBUTTONUP:
        pos = pygame.mouse.get_pos()
        margin = 4
        for a in range(3):
            for b in range(3):
                button = pygame.Rect(a*100+margin, b*100+margin, 100-2*margin, 100-2*margin)
                if button.collidepoint(pos):
                    pygame.draw.line(screen, (0, 0, 255), button.bottomleft, button.topright, 3)
                    pygame.draw.line(screen, (0, 0, 255), button.topleft, button.bottomright, 3)

关于python - Pygame鼠标点击更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55466991/

相关文章:

python - 如何在 Python heredocs 中插入变量?

python - 为什么将 multiprocessing 与 pandas apply 一起使用会导致如此显着的加速?

python - 如何从尽可能快运行的 CherryPy BackgroundTask 返回数据

python - 当仅修改一个成员时,如何更新 Pygame 矩形的所有成员变量?

python - pygame.错误 : no video mode has been set

python - 找不到在 pygame 中制作多行的方法

python - 使用 multiprocess.Process 创建进程以并行操作多个生成器,无需先创建列表

python - 获取调用事件的对象

python - 尝试在 pygame 中制作一个切换类型按钮。有问题

python - 带有 Python 绑定(bind)的 C lib,两者都想要渲染