Pygame .Rect不会“碰撞"到Pygame .Rect.用鼠标 [英] Pygame .Rect won't "collide" with mouse

查看:259
本文介绍了Pygame .Rect不会“碰撞"到Pygame .Rect.用鼠标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的游戏,并带有一个小圈的系统".我希望能够单击每个系统,以便以后在游戏中可以使用它做更多的工作,但是我很难仅识别一次单击.我将随机生成的坐标传递给字典,然后应使用鼠标位置检查每个矩形的碰撞,但由于某些原因,该碰撞不再起作用.感谢您的帮助.

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

推荐答案

我不清楚SysCoords是什么SysList/SysX/Y.它可以容纳SysCoords中项目的宽度,高度吗?如果是这样,则已经在Rect()

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()

是您在Rect s中的dict.

代码如下:

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 

这篇关于Pygame .Rect不会“碰撞"到Pygame .Rect.用鼠标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆