Pygame .Rect不会与鼠标“碰撞”

时间:2013-09-01 02:22:23

标签: mouseevent clickonce pygame python-3.2

我正在制作一个简单的游戏和小圈子“系统”。我希望能够点击每个系统,这样我以后可以在游戏中做更多的事情,但我很难识别只需点击一下。我将随机生成的coords传递给字典,然后应该用鼠标位置检查每个rect的碰撞,但由于某种原因不再有效。任何帮助表示赞赏。

这是一些更相关的代码。

    for i in range(NumSystems):

    SysSize = random.randint(3,SystemSize)
    SysY = random.randint(SystemSize*2,GVPHEIGHT-SystemSize*2)
    SysX = random.randint(OverLayWidth+SystemSize*2,WINWIDTH-SystemSize*2)



    SysList[str('SysNum')+str(i)] = ((SysSize,(SysX,SysY)))
    SysCoords[str('SysNum')+str(i)] = pygame.draw.circle(DISPLAYSURF, WHITE, (SysX,SysY), SysSize, 0)

    pygame.display.update()

    #time.sleep(.25)


#Code above is putting the random Coords into a dictionary.
while True:
    MousePos=mouse.get_pos()
    for event in pygame.event.get():
        if event.type == QUIT:
           pygame.QUIT()
           sys.exit()
        elif event.type == KEYDOWN:
            # Handle key presses

            if event.key == K_RETURN:
                #Restarts the map
                main()
        elif event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                SysClicky(MousePos)
                if SysClicked == True:
                    print('Clicked System')
                elif SysClicked == False:
                    print('Something Else Clicked')






def SysClicky(MousePos):

for i in range(NumSystems):
    print('Made to the SysClicky bit')
    if SysCoords['SysNum'+str(i)].collidepoint(MousePos):
        SysClicked = True
        print(SysClicked)
        return SysClicked
    else:

        SysClicked = False
        return SysClicked

2 个答案:

答案 0 :(得分:2)

我不清楚什么SysList / SysX / Y,SysCoords。它是否保持SysCoords中物品的宽度,高度?如果是,那已经在Rect()

systems以下

dict的{​​{1}}。

以下是代码:

Rect

答案 1 :(得分:0)

Pygame rect对象有一个collidepoint内置方法,它接受一个坐标并根据碰撞返回一个布尔值。您可以将鼠标坐标输入到函数中,如下所示:

MousePos=mouse.get_pos()
if mySurface.get_rect().collidepoint(MousePos):
    doSomething()