def kill(this): '''destroys the asteroid object''' enemy.kill(this) global score if (this.radius > 10): GlobalVariables.sounds[8].play() # splits into 3 smaller pieces for i in range(3): p = randPoint(1) a1 = asteroid(addPoints(this.pos, multPoint(p, this.radius)), this.radius / 2) a1.vel = multPoint(p, 2) GlobalVariables.enemies.append(a1) else: GlobalVariables.sounds[9].play() for i in range(3 + int(this.radius / 5)): # emits particles of death part = particle(addPoints(this.pos, randPoint(this.radius)), 0, (200, 120, 90), 4) if (randChance(50)): part.color = (130, 100, 50) part.vel = multPoint(subtractPoints(part.pos, this.pos), 0.3) part.life = random.randrange(50, 75) GlobalVariables.particles.append(part) getPoints(math.ceil(this.radius / 20) * 10) # worth more points the bigger the asteroid
def update_position(self, pbest, gbest): size = len(pbest.get_Pbest()) - 1 #getting the size of pbest index = random.randint( 0, size) #getting a random index in within the range 0 and size new_tour = [] #new_particle[] default_point = [ -1, -1 ] #initializing the default as point with coordinates(-1,-1) #for loop to initialize the variable new tour with default point (-1,-1) for i in range(0, len(pbest.get_Pbest())): new_tour.append( default_point) #adding the point (-1,-1) to the point new_particle = particle( new_tour ) #creating a particle which has (-1,-1) as its coordiantes initially (initializing a new particles) counter = 0 #for loop to extract the coordinates from the pbest and adding them in the new particle for i in range(index, len(pbest.get_Pbest())): if i > index: new_particle.set_Position_index( counter, pbest.get_Pbest() [i]) #setting the coordinates in pbest in the new particle counter = counter + 1 #incrementing counter missing_points = [ ] #missing points to store the points that were not added from pbest #for loop to populate missing point with points from gbest for i in range(0, len(gbest.get_Position())): exists = 0 #control variable to determine whether to add the point in the missinglist varibale point = gbest.get_Position()[i] #getting the current point for r in range(0, len(new_particle.get_Position())): #if statement to set the control variable to 1 if the point already exists if point == new_particle.get_Position()[r]: exists = 1 #if exists is equals to 0 then add the point to the missing points list if exists == 0: missing_points.append(point) counter2 = 0 #for loop to add the points from missing points list to the new particle. for i in range(0, len(new_particle.get_Position())): if new_particle.get_Position()[i] == default_point: new_particle.set_Position_index(i, missing_points[counter2]) counter2 = counter2 + 1 return new_particle
def burst(this): for i in range(15): part = particle(this.pos, GlobalVariables.p1.vel, (0, 200, 255), 3) off = randCirc(10) part.vel = addPoints(part.vel, off) part.damping = 0.95 particle.life = 100 part.pos = addPoints(GlobalVariables.p1.pos, off * 3) GlobalVariables.particles.append(part)
def burst(this): '''creates a small flash and emits some explosion particles''' GlobalVariables.sounds[13].play() for i in range(random.randrange(5, 10)): part = particle(this.pos, randCirc(5), (255, 255, 0)) GlobalVariables.particles.append(part) blast = circ() blast.pos = this.pos blast.scale = 30 blast.color = (255, 255, 100) GlobalVariables.maincam.toDraw(blast)
def thrustParticle(this): '''emits particles to show that it is seeking an enemy''' force = multPoint(xyComponent(this.form.angle - math.pi), 0.7) part = particle(addPoints(this.pos, randPoint(randRange(4, 6))), multPoint(force, 5), (255, 255, 0)) part.vel = addPoints(part.vel, this.vel) part.life = random.randrange(5, 10) part.damping = 0.8 if (randChance(50)): part.color = (200, 200, 0) part.thickness = 2 GlobalVariables.particles.append(part)
def burst(this): '''the visual effect of the projectile's collision''' if (this.damage >= 1): GlobalVariables.sounds[12].play() else: GlobalVariables.sounds[14].play() for i in range(random.randrange(4, 8)): # add some pretty particles part = particle(this.pos, randPoint(5), (255, 255, 0)) part.damping = 0.9 GlobalVariables.particles.append(part)
def thrustParticle(this, reldir): '''emits particles to show acceleration''' force = multPoint(xyComponent(this.angle + reldir), 0.7) part = particle(addPoints(this.pos, randPoint(5)), multPoint(force, 5), (255, 150, 0)) part.vel = addPoints(part.vel, this.vel) part.life = random.randrange(5, 10) if (randChance(50)): part.color = (200, 200, 0) part.thickness = 3 GlobalVariables.particles.append(part)
def burst(this): '''the bullet produces a small green flash when it collides''' for i in range(2): part = particle(this.pos, randPoint(30), (0, 255, 50)) part.damping = 0.8 part.thickness = 4 GlobalVariables.particles.append(part) blast = circ() blast.pos = this.pos blast.scale = 10 blast.color = (0, 255, 100) GlobalVariables.maincam.toDraw(blast)
def burst(this): blast = circ(random.randrange(20, 35)) if (randChance(50)): blast.color = (255, 150, 0) else: blast.color = (255, 255, 0) tpos = addPoints(this.pos, randPoint(20)) blast.pos = tpos GlobalVariables.maincam.toDraw(blast) for p in range(6): part = particle(addPoints(tpos, randCirc(blast.scale / 2)), randCirc(5), blast.color, 3) part.life = randRange(30, 10) GlobalVariables.particles.append(part)
def shortest_round_trip(): """ Returns a list of indices that should be visited to start at the point at index 0 and end at the same point while visiting each other point at least once. """ # replace this implementation particles_list = [] shortest_path = [] for i in range(0, 20): shuffle(points) #partitioning the points list p = particle(points) #instatiating the Particle object particles_list.append(p) #adding the points in the particle list min = 1000 #initializing the current mininmum disstance bit = 0 #bit (integer) to be used as a variable to find the optimal solution(path) #if statement to fidn the new best solution(path) for i in range(0, 10): pso = PSO(particles_list) # instatiating the PSO object gbest = pso.startPso() # statring the PSO algorithm if min >= gbest.get_distance(gbest.get_Position()): min = gbest.get_distance( gbest.get_Position() ) #assigning the new and best solution or path to the min variable shortest_path = gbest.get_Position( ) #assising the new and best solution or path to the shortest_position variable print(str(min)) print(shortest_path) optimal_particle = [] #list to store the indices of the shortest path for r in range(0, len(shortest_path)): p = shortest_path[ r] #assising the current point in the shortest path to p for i in range(0, len(points)): #if statement to store the index in the optimal_particle list if p == points[i]: optimal_particle.append(i) optimal_particle.append( optimal_particle[0] ) #adding the first point as the last point in the list return optimal_particle
def kill(this): '''kills the alien instance''' enemy.kill(this) GlobalVariables.sounds[10].play() global score getPoints(100) # adds points to the player's score for i in range(10): # releases some particles to make frags more satisfying off = randPoint(5) part = particle(addPoints(this.pos, off), addPoints(this.vel, off), (255, 0, 0), 3) part.life = randRange(10, 25) if (randChance(50)): part.color = (150, 0, 0) GlobalVariables.particles.append(part)
def kill(this): '''kills the player''' global iteration this.vel = (0, 0) this.health = None # a health of None is used to determine that the player has been dead for more than a frame, used so the dead player isn't constantly exploding for i in range(20): # shoots out green particles on death off = randPoint(10) part = particle(addPoints(this.pos, off), off, (0, 255, 0)) part.thickness = 4 part.life = random.randrange(40, 100) if (randChance(50)): # chance the the partcle will be a darker green part.color = (0, 150, 0) GlobalVariables.particles.append(part) iteration = 0 # resets the global iteration to act as a makeshift timer so the transition to the end game menu isn't instantaneous
def burst(this): for i in range(20): if (randChance(50)): vel = multPoint( xyComponent( direction(this.vel) - (math.pi / 2) * randRange(1)), randRange(4, 1)) else: vel = multPoint( xyComponent( direction(this.vel) + (math.pi / 2) * randRange(1)), randRange(4, 1)) if (randChance(50)): col = (255, 150, 0) else: col = (255, 255, 0) part = particle(this.pos, vel, col, 4) part.life = randRange(30, 10) GlobalVariables.particles.append(part)