Recently in a scientific programming course at college we were assigned the task of creating a predator-prey evolution simulator. As you may notice, I have gotten into better habits since the gas simulation I posted a few months back. The language is V-python.
And btw, the code isn't finished yet. The predators are yet to come, and the prey AI is fairly simplistic. i am not likely going to have the prey evolve as the predators seem complex enough as it is.
from visual import*
import random
#Predator-Prey
#Adam O'Hara
#Creating the world
worldLength = 1000
worldWidth = 1000
ground=box(pos=(0,-5,0), size=(worldWidth*2,1,worldLength*2), color=color.red)
## Prey
#Prey settings
preyRadius = 5
preySpeed = 2
chanceOfChange = .01
preyList = []
#Function to create prey
def CreatePrey():
prey = sphere(pos=(0,0,0), radius=preyRadius, color=color.green)
prey.speed = preySpeed
prey.swerveChance = chanceOfChange
prey.velX = 0
prey.velZ = 2
preyList.append(prey)
def PreyAI(prey):
prey.swerveProbability = random.random()
if prey.swerveProbability < chanceOfChange:
Swerve(prey)
##Predator
#Predator Settings
predatorResponseTime = 50
predatorDetectionRange = 100
predatorSpeed = 4
predatorEnergy= 2000
#General function for all movement
def Move(char):
char.pos.x += char.velX
char.pos.z += char.velZ
if char.pos.x > worldWidth:
char.pos.x = -worldWidth + (char.pos.x - worldWidth)
if char.pos.x < -worldWidth:
char.pos.x = worldWidth - (char.pos.x - worldWidth)
if char.pos.z > worldLength:
char.pos.z = -worldLength + (char.pos.z - worldLength)
if char.pos.z < -worldLength:
char.pos.z = worldLength - (char.pos.z - worldLength)
#Function for direction changes
def Swerve(char):
char.angle = 6.284*random.random()
char.velZ = char.speed*sin(char.angle)
char.velX = char.speed*cos(char.angle)
#Create a number of prey
n = 20
for i in range(n):
CreatePrey()
while(1==1):
rate(100)
for i in range (len(preyList)):
Move(preyList[i])
PreyAI(preyList[i])