Esempio n. 1
0
class Env:
    LOSE_PENALTY = -25
    BALL_REWARD = 25
    OBSERVATION_SPACE_VALUES = 3
    ACTION_SPACE_SIZE = 3

    def reset(self):
        self.blob = Blob(20, windowHeight - 110)
        self.ball = Ball(20, windowHeight // 2)
        self.wall = Wall(windowWidth // 2 - 20, windowHeight - 250)
        self.enemyBlob = Blob(windowWidth - 60, windowHeight - 110)

        self.episode_step = 0

        observation = [
            self.ball.x, self.ball.y, self.blob.x, self.ball.velocity
        ]
        return observation

    def step(self, action):
        self.episode_step += 1
        self.blob.move(action, 0, windowWidth // 2)

        self.enemyBlob.follow(self.ball, windowWidth // 2, True)

        self.ball.move()
        self.ball.gravity(windowHeight)
        self.ball.collide(self.blob)
        self.ball.collide(self.enemyBlob)

        new_observation = [
            self.ball.x, self.ball.y, self.blob.x, self.ball.velocity
        ]

        if (self.ball.y >= windowHeight - self.ball.height - 10
                and self.ball.x < windowWidth // 2 - 30):
            reward = self.LOSE_PENALTY
        elif (self.ball.y >= windowHeight - self.ball.height - 10
              and self.ball.x >= windowWidth // 2 - 30
              and self.ball.x < windowWidth):
            reward = self.BALL_REWARD
        elif (self.ball.x <= 10):
            reward = self.LOSE_PENALTY
        elif (self.ball.x >= windowWidth - 10):
            reward = self.BALL_REWARD
        elif (abs(self.ball.x - self.blob.x) > (windowWidth // 2 % 10)):
            reward = 5
        else:
            reward = 1

        done = False
        if reward == self.BALL_REWARD or reward == self.LOSE_PENALTY or self.episode_step >= 200:
            done = True

        return new_observation, reward, done
Esempio n. 2
0
def play():
    blob = Blob(20, windowHeight - 110)
    ball = Ball(20, windowHeight // 2)
    wall = Wall(windowWidth // 2 - 20, windowHeight - 250)
    enemyBlob = Blob(windowWidth - 60, windowHeight - 110)
    run = True
    window = pygame.display.set_mode((windowWidth, windowHeight))
    ballOn = False
    while run:
        pygame.time.delay(50)
        for event in pygame.event.get():
            if (event.type == pygame.QUIT
                    or pygame.key.get_pressed()[pygame.K_ESCAPE]):
                run = False
                pygame.quit()
                return

        keys = pygame.key.get_pressed()
        enemyBlob.follow(ball, windowWidth // 2, True)
        #blob.follow(ball,windowWidth//2,False)

        if (keys[pygame.K_LEFT] and blob.x > blob.movement):
            blob.move(0, 0, windowWidth // 2)
        elif (keys[pygame.K_RIGHT]
              and blob.x < windowWidth // 2 - blob.movement - 50):
            blob.move(1, 0, windowWidth // 2)
        else:
            blob.resetVelocity()
        if (keys[pygame.K_UP] and not blob.isJumping):
            blob.jump()

        if (keys[pygame.K_TAB]):
            ballOn = False
            ball = Ball(50, windowHeight // 2)

        if (keys[pygame.K_SPACE]):
            ballOn = True

        blob.gravity(windowHeight)
        if (ballOn):
            ball.move()
            ball.gravity(windowHeight)
            ball.collide(blob)
            ball.collide(enemyBlob)
        wall.collide(blob)
        wall.collide(enemyBlob)
        wall.collide(wall)

        drawWindow(window, [blob, enemyBlob], [ball], wall)