Пример #1
0
    def update(self, model):
        '''
        Update the simulation.
        '''
        if len(self.prev_obs) == 0:
            action = random.randrange(0, 4)
        else:
            self.prev_obs = np.reshape(self.prev_obs, [1, 2])
            try:
                action = np.argmax(
                    self.model.predict(
                        self.prev_obs.reshape(-1, len(self.prev_obs)))[0])
            except:
                action = np.argmax(self.model.serial_activate(self.prev_obs))

        new_observation, reward, done, _ = self.step(action)
        self.prev_obs = np.array(new_observation)
        window.clear()
        self.space.debug_draw(options)

        if done: window.close()

        # Draw text
        title = pyglet.text.Label(
            self.title,
            font_name='Helvetica',
            font_size=32,
            x=1065,
            y=660,
            anchor_x='center',
            anchor_y='center')  # Create label of current angle
        title.draw()  # Draw angle label
        time = pyglet.text.Label(
            "Time: " + str(round(self.time, 1)) + "s",
            font_name='Helvetica',
            font_size=24,
            x=990,
            y=610,
            anchor_x='left',
            anchor_y='center')  # Create label of current time
        time.draw()  # Draw time label
        angle = pyglet.text.Label(
            "Angle: " + str(round(self.angle, 1)) + "°",
            font_name='Helvetica',
            font_size=24,
            x=990,
            y=580,
            anchor_x='left',
            anchor_y='center')  # Create label of current angle
        angle.draw()  # Draw angle label
        velocity = pyglet.text.Label(
            "Velocity: " + str(round(self.velocity, 1)) + "°/s",
            font_name='Helvetica',
            font_size=24,
            x=950,
            y=550,
            anchor_x='left',
            anchor_y='center')  # Create label of current angle
        velocity.draw()  # Draw angle label
    def on_draw(self):
        '''
        Method called each time the screen is drawn (i.e. each frame).
        '''
        self.clear()  # Clear screen
        self.space.debug_draw(
            options
        )  # Draw space objects (Pymunk bodies, shapes, pivots, gears etc.)

        title = pyglet.text.Label(
            self.title,
            font_name='Helvetica',
            font_size=32,
            x=1065,
            y=660,
            anchor_x='center',
            anchor_y='center')  # Create label of current angle
        title.draw()  # Draw angle label
        time = pyglet.text.Label(
            "Time: " + str(round(self.time, 1)) + "s",
            font_name='Helvetica',
            font_size=24,
            x=990,
            y=610,
            anchor_x='left',
            anchor_y='center')  # Create label of current time
        time.draw()  # Draw time label
        angle = pyglet.text.Label(
            "Angle: " + str(round(self.angle, 1)) + "°",
            font_name='Helvetica',
            font_size=24,
            x=990,
            y=580,
            anchor_x='left',
            anchor_y='center')  # Create label of current angle
        angle.draw()  # Draw angle label
        velocity = pyglet.text.Label(
            "Velocity: " + str(round(self.velocity, 1)) + "°/s",
            font_name='Helvetica',
            font_size=24,
            x=950,
            y=550,
            anchor_x='left',
            anchor_y='center')  # Create label of current angle
        velocity.draw()  # Draw angle label

        try:
            self.image1.blit(0, 0)  # Draw image1
            self.image2.blit(890, 0)  # Draw image2
        except:
            pass
Пример #3
0
 def on_draw(self):
     self.clear()
     self.space.debug_draw(options)
     time = pyglet.text.Label(str(round(self.time, 1)) + "s",
                       font_name='Helvetica',
                       font_size=36,
                       x=1130, y=660,
                       anchor_x='center', anchor_y='center')
     time.draw()
     angle = pyglet.text.Label(str(round(self.angle, 1)) + "°",
                       font_name='Helvetica',
                       font_size=36,
                       x=1130, y=610,
                       anchor_x='center', anchor_y='center')
     angle.draw()
     self.image.blit(0, 0)
Пример #4
0
def draw(canvas):
    global interactions, addedInteractions, numberOfPlatforms, framecount, timer,numberOfGoats,goats
    time = Timer()
    lv1background.update(canvas)
    platforms.draw(canvas)
    time.draw(canvas)
    scoreCount.update(canvas)
    lifeBar.update(canvas)
    flood.draw(canvas)

    if not addedInteractions:
        for i in range(len(platforms.getCoords())):
            interaction = Interaction(player1, platforms.coords[i])
            interactions.append(interaction)
        addedInteractions = True

    for i in range(len(platforms.getCoords())):
        platforms.coords[i].p1 = Vector(platforms.coords[i].getp1().getP()[0],
                                        platforms.coords[i].getp1().getP()[1] + 1)
        platforms.coords[i].p2 = Vector(platforms.coords[i].getp2().getP()[0],
                                        platforms.coords[i].getp2().getP()[1] + 1)

        tempPlatform = Platform2(0)
        if platforms.coords[i].p1.y > 700:
            platforms.coords.pop(i)
            if i > 1:
                while not (abs(tempPlatform.getx1() - platforms.coords[
                        i - 1].getx1()) > platforms.DISTANCE * platforms.difficulty or \
                                       abs(tempPlatform.getx2() - platforms.coords[
                                               i - 1].getx2()) > platforms.DISTANCE * platforms.difficulty):
                    tempPlatform = Platform2(0)
            platforms.coords.insert(i, tempPlatform)
            tempInteraction = Interaction(player1, tempPlatform)
        else:
            tempInteraction = Interaction(player1, platforms.coords[i])
        interactions.pop(i)
        interactions.insert(i, tempInteraction)
        if random.randint(0,1) > 0.5 and framecount % 360 == 0 and numberOfGoats <3 and platforms.coords[i].p1.y < player1.pos.y :  # numberOfPlatforms % 5 == 0 and not madeGoat
            makeGoat(platforms.coords[i])
            numberOfGoats+=1
        goatsToPop = []
        for j in range(len(goats)):
            if platforms.coords[i] == goats[j].platform:
                goats[j].pos = Vector(goats[j].pos.x,platforms.coords[i].p1.y-platforms.coords[i].thickness-goats[j].frameHeight/2)
                goats[j].update(canvas)
                if goats[j].isColliding(player1):
                    if goats[j].harmsPlayer(player1):
                        player1.lifePoints -=1
                        print("rekt")
                        goatsToPop.append(goats[j])
                        numberOfGoats -= 1
            if goats[j].pos.y > 630:
                goatsToPop.append(goats[j])
                numberOfGoats-=1
        for j in range(len(goatsToPop)):
            goats.remove(goatsToPop[j])

    framecount += 1

    for i in range(len(interactions)):

        if platforms.coords[i].covers(player1.pos):
            interactions[i].update()

    platforms.draw(canvas)

    player1.update(canvas)
Пример #5
0
 def on_draw(self):
     '''
     Method called each time the screen is drawn (i.e. each frame).
     '''
     self.clear()  # Clear screen
     self.space.debug_draw(
         options
     )  # Draw space objects (Pymunk bodies, shapes, pivots, gears etc.)
     episode = pyglet.text.Label(
         "Episode " + str(self.episode),
         font_name='Helvetica',
         font_size=32,
         x=1050,
         y=650,
         anchor_x='center',
         anchor_y='center')  # Create label of current time
     episode.draw()  # Draw time label
     time = pyglet.text.Label(
         str(round(self.time, 1)) + "s",
         font_name='Helvetica',
         font_size=24,
         x=1050,
         y=600,
         anchor_x='center',
         anchor_y='center')  # Create label of current time
     time.draw()  # Draw time label
     angle = pyglet.text.Label(
         str(round(self.angle, 1)) + "°",
         font_name='Helvetica',
         font_size=24,
         x=1050,
         y=560,
         anchor_x='center',
         anchor_y='center')  # Create label of current angle
     angle.draw()  # Draw angle label
     velocity = pyglet.text.Label(
         str(round(self.velocity, 1)) + "°/s",
         font_name='Helvetica',
         font_size=24,
         x=1050,
         y=520,
         anchor_x='center',
         anchor_y='center')  # Create label of current time
     velocity.draw()  # Draw time label
     state = pyglet.text.Label(
         str(round(self.state, 1)) + "",
         font_name='Helvetica',
         font_size=24,
         x=1050,
         y=480,
         anchor_x='center',
         anchor_y='center')  # Create label of current time
     state.draw()  # Draw time label
     q_s = pyglet.text.Label(
         str(self.movements) + "/" + str(self.stills) + "/" +
         str(self.randoms) + "/" + str(self.total),
         font_name='Helvetica',
         font_size=24,
         x=1050,
         y=440,
         anchor_x='center',
         anchor_y='center')  # Create label of current time
     q_s.draw()  # Draw time label
     state = pyglet.text.Label(
         "Previous Episode:",
         font_name='Helvetica',
         font_size=16,
         x=1000,
         y=250,
         anchor_x='center',
         anchor_y='center')  # Create label of current time
     state.draw()  # Draw time label
     self.image.blit(0, 0)  # Draw image
     try:
         self.history.blit(890, 0)  # Draw history
     except:
         pass