AI问题(即使不应该发生冲突)

时间:2019-06-16 03:07:03

标签: python pygame

我基本上是在尝试使用随机方向移动的基本AI。即使我创建了If语句来防止这种情况,也会由于某些原因将其移入其内部。它只会在x坐标上发生,如果我更改顺序,它将在y坐标上发生。在这段代码中,我更改了顺序,它显示了。

我看不到问题,需要快速帮助。 发生的情况的示例(列表显示):

玩家1胜出(蓝色)

  

[[205,300],[210,300],[215,300],[220,300],[225,300],[225,305],[225,310],[225,315 ],[225、320],[225、325],[225、320]]

dir_x = -5
dir_y = 0
dir_x2 = 5
dir_y2 = 0

x = 600
y = 300
x2 = 200
y2 = 300


cool = True
list1 = []
list2 = []

player1_score = 0
player2_score = 0

speedP1,speedPN1 = 5,-5
speedP2, speedPN2 = 5, -5

while cool == True: 

        screen.fill(black)
        possible_cor = ([5,0],[-5,0],[0,5],[0,-5])
        choices = random.choice(possible_cor)
        if len(list2) != 0:
            if len(list2)%5 == 0:  
                if dir_y2 == 5:
                    possible_cor = ([5,0],[-5,0],[0,5])
                    choices = random.choice(possible_cor)
                    dir_x2 = choices[0]
                    dir_y2 = choices[1]

                if dir_y2 == -5:

                    possible_cor = ([5,0],[-5,0],[0,-5])
                    choices = random.choice(possible_cor)
                    dir_x2 = choices[0]
                    dir_y2 = choices[1]
                if dir_x2 == 5:

                    possible_cor = ([5,0],[0,5],[0,-5])
                    choices = random.choice(possible_cor)
                    dir_x2 = choices[0]
                    dir_y2 = choices[1]

                if dir_x2 == -5:

                    possible_cor = ([-5,0],[0,5],[0,-5])
                    choices = random.choice(possible_cor) 
                    dir_x2 = choices[0]
                    dir_y2 = choices[1]






        x += dir_x
        y += dir_y

        x2 += dir_x2
        y2 += dir_y2

        message_display(str(score1),20,1.9,12,text_objects2)
        message_display(str(score2),20,2.1,12,text_objects2)
        message_display((":"),20,2,12,text_objects2)

        lightbike(blue,x,y)
        lightbike(green,x2,y2)


        if speedP1 == 5 or speedPN1 == -5:
            list1.append([x,y])
        if speedP2 == 5 or speedPN2 == -5:
            list2.append([x2,y2])




        for event in pygame.event.get():
            print (event)
            #Exit 
            if event.type ==  QUIT:
                pygame.quit()
                sys.exit()
            if (event.type == KEYDOWN):

                #Player1

                if (event.key == 276): #LEFT
                    if dir_x == 0:
                        dir_x = speedPN1
                    if dir_y != 0:
                        dir_y = 0
                if (event.key == 275): #RIGHT
                    if dir_x == 0:
                        dir_x = speedP1
                    if dir_y != 0:
                        dir_y = 0
                if (event.key == 273): #UP
                    if dir_y == 0:
                        dir_y  = speedPN1
                    if dir_x != 0:
                        dir_x = 0
                if (event.key == 274): #DOWN
                    if dir_y == 0:
                        dir_y = speedP1
                    if dir_x != 0:
                        dir_x = 0

        #Boundaries
        if x + 2 > w or x - 2 < 0 or y + 2 > h or y - 2 < 0:
            print ("Player 2 Wins(Green)")
            return AI_GAME(total_score,score2+1,score1)
        if y2 + 2 > h or y2 - 2 < 0 or x2 + 2 > w or x2 - 2 < 0:
            print ("Player 1 Wins(Blue)")
            return AI_GAME(total_score,score2,score1+1)

        for i,j in zip(list1,list2):
            lightbike(blue,i[0],i[1])
            lightbike(green,j[0],j[1])
            if list1.count(i) > 1:
                print ("Player 2 Wins(Green)") 
                return AI_GAME(total_score,score2+1,score1)
            if list2.count(i) > 0:
                player2_score += 1
                if player1_score != 0:
                    print ("Player 2 Wins(Green)") 
                    return AI_GAME(total_score,score2+1,score1)
            if list1.count(j) > 0:
                player1_score += 1
                if player2_score != 0:
                    print ("Player 1 Wins(Blue)")
                    return AI_GAME(total_score,score2,score1+1)
            if list2.count(j) > 1:
                print ("Player 1 Wins(Blue)")
                print (list2)
                pygame.quit()
                sys.exit()







        if len(list1) > 500:
            del list1[0:1]
        if len(list2) > 500:
            del list2[0:1]



        pygame.display.update()
        fpsClock.tick(30)

        if score1 ==3 or score2 == 3:
            cool = False

2 个答案:

答案 0 :(得分:0)

我假设您要验证位置(x, y)的玩家是否与位置(x2, y2)的玩家冲突。
接下来的(w, h) (w2, h2)是播放器的宽度和高度。

使用pygame.Rect对象和.colliderect()验证玩家是否会在该位置发生碰撞。
如果他们不冲突,请修改他们的位置:

rect1 = pygame.Rect(x + dir_x, y + dir_y, w, h)
rect2 = pygame.Rect(x2 + dir_x2, y2 + dir_y2, w2, h2)

if not rect1.colliderect(rect2):

    x += dir_x
    y += dir_y

    x2 += dir_x2
    y2 += dir_y2

答案 1 :(得分:0)

尽管我不太了解您要做什么,但是我想您遇到的问题是:

                if dir_y2 == 5:
                    possible_cor = ([5,0],[-5,0],[0,5])
                    choices = random.choice(possible_cor)
                    dir_x2 = choices[0]
                    dir_y2 = choices[1]

                if dir_y2 == -5:

                    possible_cor = ([5,0],[-5,0],[0,-5])
                    choices = random.choice(possible_cor)
                    dir_x2 = choices[0]
                    dir_y2 = choices[1]
                if dir_x2 == 5:

                    possible_cor = ([5,0],[0,5],[0,-5])
                    choices = random.choice(possible_cor)
                    dir_x2 = choices[0]
                    dir_y2 = choices[1]

                if dir_x2 == -5:

                    possible_cor = ([-5,0],[0,5],[0,-5])
                    choices = random.choice(possible_cor) 
                    dir_x2 = choices[0]
                    dir_y2 = choices[1]

我发现您可能没有考虑到某些问题。每个if块都会更改dir_x2dir_y2的值。因此,下一个if块将使用那些修改后的值来评估其条件。例如,如果dir_x2, dir_y2在开头是0, 5,则第一个if块可能会将其更改为5, 0。但是,第三个if块dir_x2 == 5的条件现在已满足,因此它也被执行,可能会将dir_x2, dir_y2进一步更改为0, -5。我认为这可能不是您想要的。

如果这是问题所在,解决方案是将最后三个if更改为elif。现在,每次运行这段代码,最多将执行四个块之一。