【问题标题】:python zelle graphics checkMouse()?python zelle图形checkMouse()?
【发布时间】:2016-01-05 10:46:57
【问题描述】:
yes_box = Rectangle(Point(200, 150),Point(350,50))
yes_box.setOutline('blue')
yes_box.setWidth(1)
yes_box.draw(graphics_win)









def mouse_check(arg1): 
     ??????

嘿,有一个简单的问题,可能很明显,但我真的很难过。所以我正在编写一个程序(游戏),需要您在 yes_box 的边界内单击(如上所示)。我需要编写一个函数来检查鼠标点击是否在矩形的边界内,如果是则返回'y',如果不是则返回'n'。

我知道您需要使用 win.getMouse() 和 win.checkMouse() 函数,但我不确定如何让 python 确定该单击是否在矩形对象的边界内?任何帮助将不胜感激!

【问题讨论】:

  • 不知道框架,但你不能得到鼠标位置并将它与你用于矩形的坐标进行比较吗?
  • 看,我不知道该怎么做。
  • 那么实际的问题是“如何在这个图形框架中获取鼠标指针的位置”?

标签: python zelle-graphics


【解决方案1】:

您只需要获取win.getMouse() 返回的点并确保 x 和 y 值在界限内。我在下面的inside 函数中执行此操作,然后使用此布尔值在窗口上显示“y”或“n”

from graphics import *      

def inside(test_Point, P1, P2):
    ''' 
    determines if the test_Point is inside
    the rectangle with P1 and P2 at opposite corners

    assumes P1 is upper left and P2 is lower right
    '''
    tX = test_Point.getX()
    tY = test_Point.getY()

    # first the x value must be in bounds
    t1 = (P1.getX() <= tX) and (tX <= P2.getX())

    if not t1:
        return False
    else:
        return (P2.getY() <= tY) and (tY <= P1.getY())


win = GraphWin("Box", 600, 600)

yes_box = Rectangle(Point(200, 150), Point(350, 50))
yes_box.setOutline('blue')
yes_box.setWidth(1)
yes_box.draw(win)

# where was the mouse clicked?
t = win.getMouse()

# is that inside the box?
if inside(t, yes_box.getP1(), yes_box.getP2()):
    text = 'y'
else:
    text = 'n'

# draw the 'y' or 'n' on the screen
testText = Text(Point(200,300), text)
testText.draw(win)

exitText = Text(Point(200,350), 'Click anywhere to quit')
exitText.draw(win)

win.getMouse()
win.close()

【讨论】:

    【解决方案2】:

    你可以使用这个功能:

    p1 = rectangle.getP1()
    rx1 = p1.getX()
    ry1 = p1.getY()
    
    p2 = rectangle.getP2()
    rx2 = p2.getX()
    ry2 = p2.getY()
    
    x1 = point.getX()
    y1 = point.getY()
    
    
    if x1>=rx1 and y1<=ry1 and x1<=rx2 and y1>= ry2:
    
        return y
    else: 
        return n 
    

    where point 应该是用户点击的点。并且 rectangle 是您想知道用户是否单击的矩形。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多