0

私は単純なゲームと小さな円の「システム」に取り組んでいます。各システムをクリックしてゲームの後半でさらに多くのことができるようにしたいのですが、1 回のクリックしか認識できません。ランダムに生成された座標をディクショナリに渡します。次に、各四角形の衝突をマウスの位置でチェックする必要がありますが、何らかの理由で機能しなくなりました。どんな助けでも大歓迎です。

これは、より関連性の高いコードの一部です。

    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
4

2 に答える 2

2

SysList / SysX/Y、SysCoords とは何かについては明確ではありません。SysCoords のアイテムの幅、高さを保持していますか? もしそうなら、それはすでに入っていますRect()

以下systemsはあなたdictRectです。

コードは次のとおりです。

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 
于 2013-09-01T04:40:02.217 に答える
0

Pygamerectオブジェクトには、座標を取り、衝突に基づいてブール値を返すcollidepoint組み込みメソッドがあります。次のように、マウス座標を関数に入力できます。

MousePos=mouse.get_pos()
if mySurface.get_rect().collidepoint(MousePos):
    doSomething()
于 2013-09-01T06:40:39.597 に答える