Commit a92e0609 authored by solidarityK123's avatar solidarityK123

initial commit

parents
import pygame
##import of pygame so that it can be used to run the game
import AiRequirements as setup
import birdcode as birdspawn
##imports AiRequirements and birdcode so that the can be used within the main ai code to run the flocking sim
pygame.init()
##intializing pygame so that it can be used
screendisplay = pygame.display.set_mode((setup.Widthofscreen, setup.Heightofscreen))
##setting up the screens display using the variables widthofscreen and heightofscreen from the airequirements
pygame.display.set_caption(setup.GameTitle)
##setting up the display using the gametitle defined in the airequirements class
birdspawn.createbirds(setup.numberofbirdstospawn)
##running the createbirds methods from bird code and setting the number of birds to spawn to be equal to the number set in the airequirements class
flockingon = True
##setting it so that the program runs the simulation via the use of a boolean variable to determine if its on
## while flocking is true the below code is run
while flockingon:
dt = setup.clock.tick(setup.gamesmaxfps)/1000
##creating a variable called dt which uses the setup.clock and then makes use of the setup.gamesmaxfps and then divides the 1000
##if the event it gets is equal to pygame.quit is sets the flockingon value to false which sets the game to end
for event in pygame.event.get():
if event.type == pygame.QUIT:
flockingon = False
birdspawn.allbirds.update()
##runs the sprite group allbirds from birdspawn and runs the update function for each bird which updates a poriton of the screen
screendisplay.fill(setup.backgroundcolour)
##sets the display of the screen to be equal to the backgroundcolour set in AIrequirements
birdspawn.allbirds.draw(screendisplay)
##runs the sprite group allbirds from birdspawn and draws it on to the screen display
pygame.display.flip()
##updates the whole display
##if flocking is false the game is quit
pygame.quit()
exit()
import pygame
import os
##setting up imports that are needed for this file
clock = pygame.time.Clock()
##--These values can be edited to allow for differences in the flocking simulation--
##setting up a clock for later use within the birdcode file as well as for in the AI file
Widthofscreen = 1000
Heightofscreen = 800
##Varirables to set up the height and width of the screen
gamesmaxfps = 30
##setting the max fps which is later used by the clock
GameTitle = "Flocking"
##settting the title for the simulation
backgroundcolour = (0,0,0)
##setting what values the background color will be
Speed = 8
##setting the maximum speed value for the birdcode
alignactive = True
timer = 60
##setting whether alignment will be run in the flocking code
sepactive = True
##setting whether seperation will be run in the flocking code
cohactive = True
##setting whether cohesion will be run in the flocking code
flee = 43
##setting the radius in which a bird can flee
align = 60
##the radius for the align section of the bird code
Cohesion = 400
##the radius for the cohesion range
numberofbirdstospawn = 40
##the amount of birds the program will spawn
alignrandom = True
seprandom = True
cohrandom = True
##these booleans are option customisbale extras that allow for the flocking code to use constantly changing values that keep the flocking attributes changing
##--editible values end here--
vector = pygame.math.Vector2
##setting it so that the variable vector is equal to the pygame vector 2 class which is used to locate where the bird is on the screen
##loading images and setting them as sprites for later use in the birdcode program - if you have your own bird image you can insert it into the img folder of this project and then change the "birb.png" to be equal to "whatevername it has.png"
currentPath = os.path.dirname(__file__)
imagePath = os.path.join(currentPath, "img")
birb = pygame.image.load(os.path.join(imagePath,"birb.png" ))
##sections of code developed and based upon code provided in https://github.com/chief141/boids_using_python
import pygame
##imports pygame for use within the code so that functions from it can be used
from random import randint, uniform, choice
##imports specific methods(randint, uniform, choice) from random
import AiRequirements as setup
##imports AiRequirements
Dt = setup.clock.tick(setup.gamesmaxfps)/1000
##creating a variable called dt which uses the setup.clock and then makes use of the setup.gamesmaxfps and then divides the 1000
#sets up the bird class and sets it so that it makes use of pygame sprites
class bird(pygame.sprite.Sprite):
def __init__(self,x,y): ##delcares that this is an initlization method
pygame.sprite.Sprite.__init__(self)
self.image = setup.birb
self.rect = self.image.get_rect()
self.x = x
self.y = y
##sets it so that sprite is is equal to the image delcared within the setup and then creates a rect hitbox based on the size of the image that is given
self.position = setup.vector(self.x,self.y)##sets it so that the position of the bird is in a random location via the use of the randint method and setting it so thats its between 0 and the maximum value of either the height or width of the screen
self.velocity = setup.vector(choice([-setup.Speed, setup.Speed]),choice([-setup.Speed, setup.Speed])).rotate(uniform(0, 360))##sets the velocity of the code to be equal to a random choice between an array of -speed and speed and the rotates based on a sample from a uniform ranging between 0,360
self.acceleration = setup.vector(0, 0) ##sets the acceleration to zero
self.rect.center = self.position ##sets the center of the bird to be equal to its position
def update(self):##declares that this is an update method
self.acceleration = setup.vector(0,0)##sets the acceleration to zero
for b in birds: ##runs this for each bird in the birds list
if b != self: ##if the bird is not this bird it then runs the below code
if setup.sepactive == True: ##if sepactive is true the below code will run
self.acceleration += self.sep(b.rect.center) ##sets it so that the acceleration is equal to itself plus the value returned by the seperation method and gives the code the center of the rect
else:##if sepactive isnt true this code runs
pass##passes the code
if setup.alignactive == True:##if alignactive is true the below code will run
self.acceleration += self.align()##sets it so that the acceleration is equal to itself plus the value returned by the align method
else:##if alignactive isnt true this code runs
pass##passes the code
if setup.cohactive == True:##if cohactive is true the below code will run
self.acceleration += self.coh()##sets it so that the acceleration is equal to itself plus the value returned by the coh method
else:##if cohactive isnt true this code runs
pass##passes the code
self.velocity += (self.acceleration * Dt)##sets velocity to be equal to the acceleration times by the Dt variable
self.position += self.velocity ##sets the position to be equal to the velocity
if self.position.y > setup.Heightofscreen:##if the y position is greater than the height of the display run the below code
self.position.y = 0 ##sets the y position to be equal to zero
elif self.position.y < 0:##if the y position is less than zero run the below the code
self.position.y = setup.Heightofscreen ##sets the y position to be equal the height of the screen
if self.position.x > setup.Widthofscreen:##if the x position is greater than the Width of the display run the below code
self.position.x = 0 ##sets the x position to be equal to zero
elif self.position.x < 0:##if the x position is less than zero run the below the code
self.position.x = setup.Widthofscreen ##sets the x position to be equal to the width of the screen
self.rect.center = self.position ##sets the rects center to be equal to the position
setup.timer -= Dt
##print(setup.timer)
if setup.seprandom == True and setup.timer <= 0: ##if seprandom is equal to true and the timer is less than equal to zero run the below code
setup.flee= randint(20,120)##flee is equal to a randint value between 20,120
setup.timer = 60 ##sets the timer to 60
if setup.alignrandom == True and setup.timer <= 0:##if alignrandom is equal to true and the timer is less than equal to zero run the below code
setup.align = randint(20,120)##align is equal to a random value betweeen 20,120
setup.timer = 60 ##sets the timer to 60
if setup.cohrandom == True and setup.timer <= 0:##if cohrandom is equal to true and the timer is less than equal to zero run the below code
setup.Cohesion = randint(20,500)##Cohesion is equal to a random value betweeen 20,500
setup.timer = 60 ##sets the timer to 60
def align(self):##declares that this is an align method which is used to steer the average desternation of other birds
tempvector = pygame.math.Vector2
alignment = tempvector(0,0) ##sets up the alignment value to be equal setup.vector
des = tempvector(0,0) ##sets des to be equal to the setup vector
for b in birds:##runs this for each bird in the birds list
if b != self:##if the bird is not this bird it then runs the below code
if b.velocity.x !=0 and b.velocity.y != 0: ## if bird velocity.x is not equal and b.velocity.y is not equal to zero run the below code
if(self.position - b.position).length() < setup.align:## if (self.position - b.position)'s length is less than align run the below code
des += b.velocity.normalize() * setup.Speed ## sets des to be equal to the velocity of other birds normalized and then times by the speed
alignment = des - self.velocity ## sets alignment to be equal to des - by velocity
alignment = alignment // (len(birds))##sets alignment to be equal to alignment floor devided(In order to return the highest number) by the len of the list of birds
return alignment ## returns the alignment value
def coh(self):##declares that this is an cohesion method which is used to move towards the center of mass of the local flockmates
tempvector = pygame.math.Vector2
cohesi = tempvector(0,0)##cohesi is equal to setup.vector
loc = tempvector(0,0)##loc is set to setup.vector
for b in birds:##runs this for each bird in the birds list
if b != self:##if the bird is not this bird it then runs the below code
distance = self.position.x - b.position.x
distance2 = self.position.y - b.position.y
finaldist = tempvector(distance,distance2)##distance is set to be equal to the position of the bird minus the other birds position
##print(finaldist)
if finaldist.length()< setup.Cohesion: ##if the length of distance is less than the setup.Cohesion run the below the code
loc += b.position ##loc is equal to the other bird position
loc = loc/(len(birds) - 1)##sets loc to be equal to alignment devided by the len of the list of birds minus 1
cohesi = loc - self.position##cohesi is equal to loc - devided by the position
cohesi = cohesi.normalize() * setup.Speed##cohesi is equal to cohesi normalized timesed by speed
return cohesi ##returns the cohesi value
def sep(self, target):##declares that this is an align method which is used to steer the average desternation of other birds and to avoid crowding
tempvector = pygame.math.Vector2
steering = tempvector(0,0)##steeriing is equal to vector
distance = self.position - target##distance is equal to the position minues the target
desiredpos = tempvector(0,0)##desiredpos is equal to setup.vector(0,0)
if distance.x != 0 and distance.y !=0:##if distance.x is not equal to 0 and distance.y is not equal to zero
if distance.length() < setup.flee: ##if distance.length is less than setup.flee run the below code
desiredpos = distance.normalize() * setup.Speed ##desiredpos is equal to distance normalized times by speed
else:##else run the code below
desiredpos = self.velocity.normalize() * setup.Speed##desiredpos is equal velocity normalized times by speed
steering = desiredpos - self.velocity ##steering is equal diesiredpos - velocity
return steering ## returns the steering value
def draw(self, screen):##declares that this the draw method and it takes in itself and the game display
screen.blit(self.image, self.rect)##displays the bird on the screen
def createbirds(numberofbirds):##declares that this is the createbirds and it takes in a interger
global allbirds, birds ## declares that these are global variables
allbirds = pygame.sprite.Group() ##creates the sprite group allbirds
birds = [bird(randint(0, setup.Widthofscreen), randint(0, setup.Heightofscreen)) for _ in range(numberofbirds)] ##birds creates a number of birds equal to numberofbirds
allbirds.add(birds)##adds birds to the sprite group allbirds
img/birb.png

204 Bytes

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment