Exemple #1
0
def main():
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('Smart Rocket')

    obs = np.empty(3, dtype=object)
    obs[0] = Obstacle(obsPosition1[0], obsPosition1[1])
    obs[1] = Obstacle(obsPosition2[0], obsPosition2[1])
    obs[2] = Obstacle(obsPosition3[0], obsPosition3[1])
    target = Target(targetPosition)
    pop = Population(screen, amountOfRockets, lifeSpan, mutationRate, target)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.display.quit()
                sys.exit()
        ## evolving code
        pop.run(obs, target)
        obs[0].draw(screen)
        obs[1].draw(screen)
        obs[2].draw(screen)
        target.draw(screen)
        ##

        pygame.display.update()
        screen.fill((0, 0, 0))
class Game(object):
    def __init__(self):
        pygame.init()
        size = (WIDTH, HEIGHT)
        if FULLSCREEN:
            self.displaySurf = pygame.display.set_mode(
                size, pygame.HWSURFACE | pygame.FULLSCREEN)
        else:
            self.displaySurf = pygame.display.set_mode(size, pygame.HWSURFACE)
        self.population = Population()
        self.clock = pygame.time.Clock()

        self.target = Target()
        self.target.set_center(TARGETPOS)

        self.text = Text()

        self.obstacles = [Obstacle()]

        # for i in range(0,MAXOBSTACLES):
        #     print 'new obstacle',len(self.obstacles)
        #     obs = Obstacle(self.obstacles)
        #     self.obstacles.append(obs)

    def draw(self):
        self.displaySurf.fill((0, 0, 0))
        self.target.draw(self.displaySurf)
        self.text.set_text("Lifetime: " + str(self.population.count),
                           "Generation: " + str(self.population.gen))
        self.text.draw(self.displaySurf)
        if self.population.gen > len(self.obstacles) and len(
                self.obstacles) < MAXOBSTACLES:
            self.obstacles.append(Obstacle(self.obstacles))
            print('novo obstaculo')
        for i in self.obstacles:
            i.draw(self.displaySurf)
        self.population.checkCollide(self.obstacles)
        self.population.draw(self.displaySurf)

    def main_loop(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == K_f:
                        pygame.display.toggle_fullscreen()
                    if event.key == K_ESCAPE:
                        sys.exit()
                        pygame.quit()
                        exit()
                if event.type == pygame.QUIT:
                    sys.exit()
            self.draw()
            pygame.display.flip()
Exemple #3
0
    def read_from_file(self, filename):
        input_file = open(filename, 'r')
        lines = input_file.readlines()
        for line in lines:
            cell_type = line.split("(")[0]
            parenthesis = line.split('(', 1)[1].split(')')[0]
            values = list(map(int, parenthesis.split(",")))

            if cell_type == "GRID":
                self.cols = values[0]
                self.rows = values[1]
            elif cell_type == "P":
                self.elements['P'].append(Pedestrian((values[0], values[1])))
            elif cell_type == "T":
                self.elements['T'] = Target((values[0], values[1]))
            elif cell_type == "O":
                self.elements['O'].append(Obstacle((values[0], values[1])))
            elif cell_type == "CM1":
                self.elements['M'].append(
                    Measure((values[0], values[1]), 'CM', 1, self.rows))
            elif cell_type == "CM2":
                self.elements['M'].append(
                    Measure((values[0], values[1]), 'CM', 2, self.rows))
            elif cell_type == "MM":
                self.elements['M'].append(
                    Measure((values[0], values[1]), 'MM', 0, self.rows))
    def __init__(self):
        pygame.init()
        size = (WIDTH, HEIGHT)
        if FULLSCREEN:
            self.displaySurf = pygame.display.set_mode(
                size, pygame.HWSURFACE | pygame.FULLSCREEN)
        else:
            self.displaySurf = pygame.display.set_mode(size, pygame.HWSURFACE)
        self.population = Population()
        self.clock = pygame.time.Clock()

        self.target = Target()
        self.target.set_center(TARGETPOS)

        self.text = Text()

        self.obstacles = [Obstacle()]
	def testShouldNotBeClose( self ):
		from classes.target import Target
		t1 = Target()
		t2 = Target()
		t1.x = 550
		t1.y = 550
		t2.x = 100
		t2.y = 100
		c = self.Helper.is_close(t1,t2)
		assert c == False, "objects close"
	def testShouldBeClose( self ):
		from classes.target import Target
		t1 = Target()
		t2 = Target()
		t1.x = 150
		t1.y = 150
		t2.x = 100
		t2.y = 100
		c = self.Helper.is_close(t1,t2)
		assert c == True, "objects are not close"
	def testShouldNotCollide( self ):
		from classes.target import Target
		t1 = Target()
		t2 = Target()
		t1.x = 200
		t1.y = 200
		t2.x = 100
		t2.y = 100
		c = self.Helper.is_collided(t1,t2)
		assert c == False, "collision detected"
Exemple #8
0
def main():
    t = Target(1)
    t.start()
root = tk.Tk()
root.title("Genetic Algorithm in Heightmaps")
canvas = tk.Canvas(master=root, width=width, height=height)

photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(cv_img))

canvas.create_image(0, 0, image=photo, anchor=tk.NW)

target_radius = 5
target_x = width / 2
target_y = 25
target = Target(
    canvas.create_oval(target_x - target_radius,
                       target_y - target_radius,
                       target_x + target_radius,
                       target_y + target_radius,
                       width=0,
                       fill=('red')), [target_x, 25], target_radius)

canvas.pack()

new_generation = ''
previous_generation = ''
generation = 1
n = 0

while generation <= 150:

    previous_generation = new_generation
    dots_number = dots_number_init