的多个实例pygame.event.get()

2024-09-27 09:28:01 发布

您现在位置:Python中文网/ 问答频道 /正文

有两个for event in pygame.event.get():实例。在第二个里面不行。我想调用函数两次是行不通的。这里该怎么办?顺便说一下,这是一个在棋盘游戏中把一块棋子从正方形移到另一块正方形的函数。在

def movement_one(blit1,charac1,screen,squareblitter,boardcoord,placecheck_True):
    global mouse1, mouse1_des
    run1=True
    while run1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse1 = pygame.mouse.get_pos()
                for i in range(80):
                    if squareblitter[i].collidepoint(mouse1):
                        collided = i
                        break
                print "wow"
                print i
                print collided in placecheck_True
                if collided in placecheck_True:
                    #checks if the square is occupied or not
                    print "wiw"
                    for event in pygame.event.get():
                        print "wtf"
                        if event.type == pygame.QUIT:
                            exit()
                        elif event.type == pygame.MOUSEBUTTONDOWN:
                            mouse1_des = pygame.mouse.get_pos()
                            print "omg"
                            squareblitter[i]
                            pygame.display.update()
                            for i in range(80):
                                if squareblitter[i].collidepoint(mouse1_des):
                                    screen.blit(placecheck_True[collided], squareblitter[i])
                                    placecheck_True.update[i]=placecheck_True[collided]
                                    pygame.display.update()
                                    run1=False
                                    break
                else:
                    break

Tags: ineventtrueforgetiftypepygame
1条回答
网友
1楼 · 发布于 2024-09-27 09:28:01

在处理前一个调用的事件时再次调用pygame.event.get()通常不会起作用。从您的代码中不清楚您要实现什么。但是,我建议您在内部for event循环中存在冲突时,将处理pygame.MOUSEBUTTONDOWN事件的代码移动到外部循环(并完全删除内部for event循环)。在

我的意思是(当然是未经测试的):

def movement_one(blit1, charac1, screen, squareblitter, boardcoord, placecheck_True):
    global mouse1, mouse1_des
    run1 = True
    while run1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse1 = pygame.mouse.get_pos()
                for i in range(80):
                    if squareblitter[i].collidepoint(mouse1):
                        collided = i
                        break
                print "wow"
                print i
                print collided in placecheck_True
                if collided in placecheck_True:
                    mouse1_des = pygame.mouse.get_pos()
                    print "omg"
                    squareblitter[i]
                    pygame.display.update()
                    for i in range(80):
                        if squareblitter[i].collidepoint(mouse1_des):
                            screen.blit(placecheck_True[collided], squareblitter[i])
                            placecheck_True.update[i] = placecheck_True[collided]
                            pygame.display.update()
                            run1 = False  # not sure you should do this...
                            break

相关问题 更多 >

    热门问题