Pygame .Rect won't “collide” with mouse

杀马特。学长 韩版系。学妹 提交于 2019-12-13 04:31:17

问题


I am working on a simple game and with small circle "systems". I would like to be able to click each system so that I can do more with it later in game but I am having difficulty recognizing only a single click. I pass the randomly generated coords to a dictionary and then the collision for each rect should be checked with the mouse position but for some reason that is not working anymore. Any help is appreciated.

Here is some of the more relevent code.

    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

回答1:


I'm not clear on What SysList / SysX/Y, SysCoords are. Does it hold width,height of the items in SysCoords? If so, that's already in Rect()

below systems is your dict of Rects.

Here's the code:

def check_collisions(pos):
    # iterate dict, check for collisions in systems
    for k,v in systems.items():
        if v.collidepoint(pos):
            print("clicked system:", k)
            return

    print("clicked something else")

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

    elif event.type == MOUSEBUTTONDOWN:
        if event.button == 1:
            check_collisions(event.pos)

    # render 



回答2:


Pygame rect objects have a collidepoint inbuilt method which takes a coordinate and returns a boolean based on collision. You can input the mouses coordinate into function like so:

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


来源:https://stackoverflow.com/questions/18554629/pygame-rect-wont-collide-with-mouse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!