python - AI 问题(即使不应该发生碰撞)
问题描述
我基本上是在尝试制作非常基本的 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
解决方案
尽管我不完全了解您要做什么,但我想您遇到的问题是:
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_x2
值dir_y2
。因此,下一个 if 块将使用这些修改后的值来评估其条件。例如,如果dir_x2, dir_y2
在0, 5
开头,则第一个 if 块可能会将其更改为5, 0
. 但是,第三个 if 块的条件 ,dir_x2 == 5
现在已经满足了,所以它也被执行并且可能dir_x2, dir_y2
进一步变为0, -5
。我想这可能不是你想要的。
如果是这个问题,解决办法就是把最后三个if
s改成elif
s。现在每次运行这段代码时,最多会执行四个块中的一个。
推荐阅读
- android - Mockk 在 verify { ... } 块中缺少调用
- r - 使用 highcharter 在旭日形图中显示平均值而不是总和
- mysql - 如何在sql中获得一月到十二月的所有星期
- javascript - Firebase setDoc()不创建文档
- javascript - 将 DelayNode 设置为大于 1
- excel - 如何对表中的每行数据执行 Excel Web JSON 查询
- python - 我应该如何合并两个数据框,以便生成的数据框没有整列的 NaN 值?
- android - Recyclerview notifyItemRemoved 删除了错误的项目,并且在使用视图绑定时无法正常工作
- html - 在 TailwindCSS + Nuxt 项目中未显示背景图像
- powershell - PowerShell URL 从根目录重定向到子目录