

#import the pygame module, and the sys module

#for exiting the window we create

import pygame, sys

import random

#import some useful constants

from pygame.locals import *



pygame.init()



#constants eventually representing some

#tile porperty, e.g. walkable, non-walkable, etc.

WALKABLE = 1

NONWALKABLE = 0

ISFOOD = 2



FPS=20





#set up game dimensions

TILESIZE = 40

MAPWIDTH = 10

MAPHEIGHT = 10



#setup objects for graphics

agents=[]

N_AGENTS = 10

foods=[]

N_FOOD = 100



tilemap = [ [WALKABLE for w in range(MAPWIDTH)] for h in range(MAPHEIGHT)]



def isWalkable(x,y):

    if (x>=0 and x < MAPWIDTH and y>=0 and y<MAPHEIGHT):

        if tilemap[x][y] != NONWALKABLE:

            return True

    else:

        return False

def occupyTile(x,y):

    tilemap[x][y] = NONWALKABLE

def setTileFree(x,y):

    tilemap[x][y] = WALKABLE







def setFood(x,y):

    tilemap[x][y] = ISFOOD

def isFood(x,y):

    if tilemap[x][y] == ISFOOD:

        return True

    else:

        return False

def eatFood(x,y):

    for i in foods:

        if i.x == ag.x and i.y == ag.y:

            foods.remove(i)







class agent:

    def __init__(self,x,y):

        self.x = x

        self.y = y

        occupyTile(x,y)

        self.energy = 1000
        self.sensor1 =0
        self.sensor2 =0
        self.sensor3 =0

    '''def __del__(self):
        agents.remove(self)
        print('An agent has fallen!')
'''
    #Array of possible actions

#    ACTIONS = ['L','R','U','D','W'] #Left, Right,Up, Down or Wait

    def action(self):

        x=self.x

        y=self.y

        self.allowedActions = []

        if isWalkable(x-1,y):

            self.allowedActions.append(0) #Left

        if isWalkable(x+1,y):

            self.allowedActions.append(1) #Reft

        if isWalkable(x,y-1):

            self.allowedActions.append(2) #Up

        if isWalkable(x+1,y+1):

            self.allowedActions.append(3) #Down

        self.allowedActions.append(4) #Wait



        #pick randomly one out of the remaining allowed actions

        self.choice = random.choice(self.allowedActions)

        #print choice

        choice =self.choice



        setTileFree(x,y)



        if choice == 0:

            x-=1

        if choice == 1:

            x+=1

        if choice == 2:

            y-=1

        if choice == 3:

            y+=1
        if choice ==4:
            self.energy+=9
        self.energy -=12


        if isFood(x,y):
            eatFood(x,y)
            self.energy +=5



        occupyTile(x,y)

        self.y=y

        self.x=x

        if self.energy <= 0:
            setTileFree(x,y)
            agents.remove(self)
            print('An agent has fallen!')



    def coord(self):

        return (self.x,self.y)



class food:

    def __init__(self,x,y):

        self.x = x

        self.y = y

        setFood(x,y)


'''
    def coord(self):

         return (self.x,self.y)



    def eat(self):

        x=self.x

        y=self.y

        if isFood(x,y):

            eatFood(x,y)

'''







fpsClock  =pygame.time.Clock()



#some colours

BLACK = (0,0,0)

WHITE = (255,255,255)

GREEN = (0,255,0)

BLUE = (0,0,255)





#the images

AGENT  = pygame.image.load('agent.bmp')

FOOD = pygame.image.load('food.bmp')





#set up the display

DISPLAYSURF=pygame.display.set_mode((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE))

pygame.display.set_caption('tilemap')





for i in range(N_AGENTS):

    ag = agent(0,i)

    agents.append(ag)



for i in range(N_FOOD):

    x = random.choice(range(MAPWIDTH))

    y = random.choice(range(MAPHEIGHT))

    if not isFood(x,y):

        fd = food(x,y)

        foods.append(fd)





#loop forever

while True:

    #get all the user events

    for event in pygame.event.get():

        #if the user wants to quit

        if event.type == QUIT:

            pygame.quit()

            sys.exit()





    DISPLAYSURF.fill(BLACK)



    for ag in agents:

        ag.action()

        #display the agent

        DISPLAYSURF.blit(AGENT, (ag.x*TILESIZE, ag.y*TILESIZE))



    #for fd in foods:

        # display the food

        #DISPLAYSURF.blit(FOOD, (fd.x*TILESIZE, fd.y*TILESIZE))









    for row in range(MAPHEIGHT):

        for column in range(MAPWIDTH):

            #add a white square (drawing surface, colour, coords)

            #the last parameter sets the border

            pygame.draw.rect(DISPLAYSURF, (255,255,255), (column*TILESIZE, row*TILESIZE, TILESIZE,TILESIZE), 1)
            if isFood(row,column):
                    DISPLAYSURF.blit(FOOD, (row*TILESIZE, column*TILESIZE))





    #update the display

    fpsClock.tick(FPS)

    pygame.display.update()
