如何提高敌人的AI?

时间:2018-07-24 13:17:26

标签: python object logic artificial-intelligence pyglet

到目前为止,这是我的代码:

from JMSSGraphics import *
import math
import random

class Zombie:
    # attributes:
    # x
    # y
    # infected
    # image

    # we need to define a special function
    # that lets us 'construct' an object from this class
    # this special function is called a constructor

    def __init__(self):
        # creating my attributes
        # and assigning them to initial values
        self.x = 0
        self.y = 0
        self.img = None
        self.speed = 0
        self.rotation = 0
        # Must use self so that variables
        # do not lose values after function returns

class Player:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.img = None
        self.speed = 0
        self.rotation = 0
        self.fireSpeed = 0

class Bullet:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.speed = 0
        self.img = None
        self.rotation = 0
        self.locationVector = []
    def __del__(self):
        pass

class Wall:
    def __init__(self):
        self.x1 = 0
        self.y1 = 0
        self.x2 = 0
        self.y2 = 0

jmss = Graphics(width = 800, height = 600, title = "city", fps = 120)

#Zombie ratio
zombieHeightFactor = 1.205
zombieWidth = 50

zombieHeight = zombieWidth * zombieHeightFactor
#Player ratio
playerHeightFactor = 0.66
playerWidth = 50

playerHeight = playerWidth * playerHeightFactor
#Bullet ratio
bulletHeightFactor = 0.28
bulletWidth = 35

bulletHeight = bulletWidth * bulletHeightFactor

zombiesList = []
n = 0
while n < 7:
    newZombie = Zombie()
    newZombie.img = jmss.loadImage("zombieImage.png")
    newZombie.x = random.randint(10,790)
    newZombie.y = random.randint(10,590)
    newZombie.speed = random.uniform(1,3)
    print(newZombie.speed)
    zombiesList.append(newZombie)
    n+=1

#Creating player object
player = Player()
player.img = jmss.loadImage("PlayerSprite.png")
player.x = 400
player.y = 300
player.speed = 10
player.fireSpeed = 20

bulletList = []

cooldown = 0


@jmss.mainloop
def Game():
    global cooldown
####################PLAYER LOOK###################################
    mouseX = jmss.mouseCoordinate()[0]
    mouseY = jmss.mouseCoordinate()[1]
    if mouseX-player.x > 0:
        angle = 360 - math.degrees(math.atan((mouseY-player.y)/(mouseX-player.x))) #Calculates angle between player and mouse in degrees
        player.rotation = angle
    if mouseX - player.x < 0:
        angle = 360 - math.degrees(math.atan((mouseY-player.y)/(mouseX-player.x))) #Calculates angle between player and mouse in degrees
        player.rotation = angle + 180 

####################PLAYER MOVEMENT#################################
    jmss.clear(1,1,1,1)
    if jmss.isKeyDown(KEY_W):
        player.y += player.speed
    if jmss.isKeyDown(KEY_A):
        player.x -= player.speed
    if jmss.isKeyDown(KEY_D):
        player.x += player.speed
    if jmss.isKeyDown(KEY_S):
        player.y -= player.speed

    if player.x > 800: ##ADDING BORDERS 
        player.x = 800
    if player.x < 0:
        player.x = 0
    if player.y > 600:
        player.y = 600
    if player.y < 0:
        player.y = 0
    jmss.drawImage(player.img,player.x,player.y,width = playerWidth,height = playerHeight,rotation = player.rotation)
####################PLAYER SHOOT####################################
    if jmss.isKeyDown(KEY_SPACE) and cooldown > player.fireSpeed:
        cooldown = 0
        bullet = Bullet()
        bullet.img = jmss.loadImage("bullet.png")
        bullet.x = player.x
        bullet.y = player.y
        bullet.speed = 20
        bullet.locationx = mouseX
        bullet.locationy = mouseY
        bullet.rotation = player.rotation
        bulletList.append(bullet)


    n = 0
    while n < len(bulletList):
        bullet = bulletList[n]

        bullet.locationVector = [math.cos(math.radians(bullet.rotation)),math.sin(math.radians(bullet.rotation))]

        bullet.x += bullet.locationVector[0]*bullet.speed
        bullet.y += -bullet.locationVector[1]*bullet.speed


        jmss.drawImage(bullet.img,bullet.x,bullet.y,width = bulletWidth,height = bulletHeight,rotation = bullet.rotation)
        if bullet.x > 800:
            del bulletList[n]
        elif bullet.y > 600:
            del bulletList[n]


        n += 1
    cooldown += 1

############################ZOMBIE AI#########################################
    n = 0
    while n < len(zombiesList):
        currentZombie = zombiesList[n]

        if player.x-currentZombie.x > 0:
            angle = 360 - math.degrees(math.atan((player.y-currentZombie.y)/(player.x-currentZombie.x))) #Calculates angle between player and mouse in degrees
            currentZombie.rotation = angle
        if player.x - currentZombie.x < 0:
            angle = 360 - math.degrees(math.atan((player.y-currentZombie.y)/(player.x-currentZombie.x))) #Calculates angle between player and mouse in degrees
            currentZombie.rotation = angle + 180

        if currentZombie.x < player.x:
            currentZombie.x += currentZombie.speed
        if currentZombie.x > player.x:
            currentZombie.x -= currentZombie.speed
        if currentZombie.y < player.y:
            currentZombie.y += currentZombie.speed
        if currentZombie.y > player.y:
            currentZombie.y -= currentZombie.speed

        jmss.drawImage(currentZombie.img,currentZombie.x,currentZombie.y,zombieWidth,zombieHeight,currentZombie.rotation)
        currentZombie.speed += 0.001
        n += 1

######################POWER UP################################################
        spawnChance = random.randint(0,10000)

        if spawnChance == 5000:
            print("SPAWN POWERUP")


##########################CREATING ENVIRONMENT###############################

jmss.run()

我想要做的是改进僵尸AI,以减少乏味和可预测性。任何帮助将不胜感激:)

到目前为止,我在AI上所做的只是根据敌人物体的x和y坐标是否小于或大于玩家的坐标来调整。它只能在带有x或y的二维级别上工作,而不能同时使用这两个级别。

1 个答案:

答案 0 :(得分:0)

OP中的僵尸AI用作跟踪控制器。这意味着,僵尸正在追随人类玩家。可以通过在不同的抽象级别上进行跟踪来改进此想法。在源代码中,跟踪仅适用于欧式空间,即玩家与僵尸之间的2d坐标差异。一个更抽象的度量是与自然语言有关的一切。例如,人类玩家可以处于称为“攻击性”的模式,该模式等于两点之间的快速移动,而僵尸也可以以攻击性行为对这种模式做出反应。分类动作的第二个机会是定义几何区域。人类玩家可以是区域A,B或C,而僵尸可以制定规则,仅在玩家处于同一区域时攻击该玩家。这将使休闲游戏玩家的行为难以预测。

要创建更复杂的角色,可以使用专用的任务模型。一种从头开始创建这种模型的简单方法是一种称为思维导图的创造力技术。在论文的中心写下了问题,然后程序员确定了AI可能采取的措施。这些动作被排序为子动作(称为层)。如果思维导图发展成为一个庞大的网络,那么该是时候以可执行源代码实现系统了。可以使用版本控制系统手动完成。

实现复杂的人工智能系统始终与自然语言有关。这个想法不是像diff=pos.x-pos.y那样在数学上描述问题,而是在语言层面上描述问题。僵尸游戏的领域必须用故事,思维导图,角色,动作和概念图来描述。这样就形成了对该问题的抽象视图。如果不清楚,思维图是什么样的,那么鼓舞人心的来源就是来自相同背景,电影和著名著作的其他游戏。对源代码进行编程本身就是在计算机游戏的媒介上讲述故事。