找回密码
 立即注册
搜索
热搜: 活动 交友
查看: 94|回复: 0

【抛砖引玉】关于太空大逃亡AI思路

[复制链接]

7

主题

19

回帖

451

积分

高级程序员

积分
451
发表于 前天 12:13 | 显示全部楼层 |阅读模式
在刚刚过去的719RT年度派对上,我们的活动任务是现场为RT课堂经典小游戏“太空大逃亡”写一个ai,作为主持人我现场没有参与编程,但在前期准备我也有试着写过一个,采取的是纯人工,下面分享一下思路:

        首先我把决策分为两类,宏观和局部——宏观是指从整个屏幕来看哪里的陨石密度最低或者用其他方法来量化“最好生存”,以此作为大的前进方向;局部则是指当附近存在威胁较大的陨石就需要优先避让。总结而言就是,先处理近在眼前的危险,在周围环境压力相对较低时尝试向更为安全的区域移动。
  1. #判断当前box相对方位并在满足距离要求时记录is_near和b_density
  2.             if 0 <= myBox.rect.top - b.rect.bottom <= 25:
  3.     b_density[0] += b.rect.height * b.rect.width
  4.             if 0 <= b.rect.top - myBox.rect.bottom <= 25:
  5.     b_density[1] += b.rect.height * b.rect.width
  6.             if 0 <= myBox.rect.left - b.rect.right <= 20:
  7.     b_density[2] += b.rect.height * b.rect.width
  8.             if 0 <= b.rect.left - myBox.rect.right <= 20:
  9.     b_density[3] += b.rect.height * b.rect.width
  10.             if (0 <= b.rect.top - myBox.rect.bottom <= 30) and\
  11.                     (b.rect.right+1 >= myBox.rect.left) and \
  12.                     (myBox.rect.right+1 >= b.rect.left): #在飞船下方
  13.     is_near[0] = 1
  14.             if (0 <= myBox.rect.top - b.rect.bottom <= 30) and\
  15.                      (b.rect.right+1 >= myBox.rect.left) and\
  16.                      (myBox.rect.right+1 >= b.rect.left): #在飞船上方
  17.     is_near[1] = 1
  18.             if (0 <= b.rect.left - myBox.rect.right <= 30) and\
  19.                     (b.rect.bottom+1 >= myBox.rect.top) and\
  20.                     (myBox.rect.bottom+1 >= b.rect.top): #在飞船右方
  21.     is_near[2] = 1
  22.             if (0 <= myBox.rect.left - b.rect.right <= 30) and\
  23.                     (b.rect.bottom+1 >= myBox.rect.top) and\
  24.                     (myBox.rect.bottom+1 >= b.rect.top): #在飞船左方
  25.     is_near[3] = 1
复制代码
以上是非常简单的代码,统计以飞船为中心将屏幕分割为四个区域的陨石多少来判断那个方向更安全(宏观),然后检测上下左右方向是否有威胁大即距离近的陨石(局部)
接下来则是具体决策飞船速度,大体分为几类:①四面被包围,此时需要尽量在原地抽搐等待陨石自己撞开,或者优先向相对更空的方向跑,两种我都有尝试,效果好像差不多?可能是我程序本身就太简陋的原因②三面被包围/相邻两个方向有陨石/一个方向有陨石,这三种情况都是朝空处跑,三面的就往空的那面,相邻两面的就往反方向的角跑,一面的就往反方向跑③相对的两面有陨石,尝试从侧向逃离④周围没有陨石,听从宏观决策往安全区块跑
以上几种情况如果遇到多个速度方向都可以走的情况,我就会选择与宏观决策方位更一致的。

  1. #当靠近边界且有被附近box向边界挤压时,把边界方向视为有box进行脱离
  2.         if is_near[0] == 1 and myBox.rect.y <= 5:
  3.             is_near[1] = 1
  4.         if is_near[1] == 1 and myBox.rect.y + myBox.rect.height - SCREEN_SIZE[1] >= 5:
  5.             is_near[0] = 1
  6.         if is_near[2] == 1 and myBox.rect.x <= 5:
  7.             is_near[3] = 1
  8.         if is_near[3] == 1 and myBox.rect.x + myBox.rect.width - SCREEN_SIZE[0] >= 5:
  9.             is_near[2] = 1
  10.         #四周均无靠近的box,向周围box数最少的方向前进
  11.         if sum(is_near) == 0:
  12.             if b_density[0] > b_density[1]:
  13.                 myBox.speed[1] = 1
  14.             else:
  15.                 myBox.speed[1] = -1
  16.             if b_density[2] > b_density[3]:
  17.                 myBox.speed[0] = 1
  18.             else:
  19.                 myBox.speed[0] = -1
  20.         #四个方向都有box,通过不断反向试图停在原地
  21.         elif sum(is_near) == 4:
  22.             #myBox.speed[0] = -myBox.speed[0]
  23.             #myBox.speed[1] = -myBox.speed[1]
  24.             if b_density[0] > b_density[1]:
  25.                 myBox.speed[1] = 1
  26.             else:
  27.                 myBox.speed[1] = -1
  28.             if b_density[2] > b_density[3]:
  29.                 myBox.speed[0] = 1
  30.             else:
  31.                 myBox.speed[0] = -1
  32.         #三个方向有box,向空位走
  33.         elif sum(is_near) == 3:
  34.             if is_near[0] == 0:
  35.                 myBox.speed[1] = 1
  36.                 #myBox.speed[0] = -myBox.speed[0]
  37.                 if b_density[2] > b_density[3]:
  38.                     myBox.speed[0] = 1
  39.                 else:
  40.                     myBox.speed[0] = -1
  41.             elif is_near[1] == 0:
  42.                 myBox.speed[1] = -1
  43.                 #myBox.speed[0] = -myBox.speed[0]
  44.                 if b_density[2] > b_density[3]:
  45.                     myBox.speed[0] = 1
  46.                 else:
  47.                     myBox.speed[0] = -1
  48.             elif is_near[2] == 0:
  49.                 myBox.speed[0] = 1
  50.                 #myBox.speed[1] = -myBox.speed[1]
  51.                 if b_density[0] > b_density[1]:
  52.                     myBox.speed[1] = 1
  53.                 else:
  54.                     myBox.speed[1] = -1
  55.             elif is_near[3] == 0:
  56.                 myBox.speed[0] = -1
  57.                 #myBox.speed[1] = -myBox.speed[1]
  58.                 if b_density[0] > b_density[1]:
  59.                     myBox.speed[1] = 1
  60.                 else:
  61.                     myBox.speed[1] = -1
  62.         else:
  63.             #两个对向有box,在此方向不断反向试图静止,另一方向选择box数更少的区域
  64.             if is_near[0] == is_near[1] == 1:
  65.                 if b_density[2] > b_density[3]:
  66.                     myBox.speed[0] = 1
  67.                 else:
  68.                     myBox.speed[0] = -1
  69.                 myBox.speed[1] = -myBox.speed[1]
  70.             elif is_near[2] == is_near[3] == 1:
  71.                 if b_density[0] > b_density[1]:
  72.                     myBox.speed[1] = 1
  73.                 else:
  74.                     myBox.speed[1] = -1
  75.                 myBox.speed[0] = -myBox.speed[0]
  76.             #相邻方向或只有一个方向有box,直接对应反向
  77.             else:
  78.                 if is_near[0] == 1:
  79.                     myBox.speed[1] = -1
  80.                 if is_near[1] == 1:
  81.                     myBox.speed[1] = 1
  82.                 if is_near[2] == 1:
  83.                     myBox.speed[0] = -1
  84.                 if is_near[3] == 1:
  85.                     myBox.speed[0] = 1
复制代码
同样是实现难度很低的代码,主要还是脑子里把规则想好。

我个人也有问deepseek要过代码,但并未拿回来仔细改,只是尝试运行了一下,就不专门贴出来了,本次活动中有其他同学与ai的合作更加紧密,这方面我还是期待他们的分享吧。
【是ai就撑过30秒】 https://www.bilibili.com/video/B ... 768b04148b197d89075这是以上程序的运行结果,反正比我人力强点hhhhhh
希望能继续和大家讨论这个ai如何继续进化,大佬们有想法还请在评论区指点指点
也希望能炸出来有参加活动的同学分享你们的程序!

评分

参与人数 1金钱 +20 收起 理由
littleblackLB + 20 赞一个!

查看全部评分

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|RealDevClub ( 沪ICP备2024093864号-1 )

GMT+8, 7-24-2025 04:01 , Processed in 0.062419 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表