class PaddleEnv(gym.Env):
    def __init__(self):
        super(PaddleEnv, self).__init__()
        self.game = Paddle()

        # Actions: left right hold
        self.action_space = spaces.Discrete(3)

        # Observation: paddle x coordinates, ball location and ball direction
        self.observation_space = spaces.Box(low=-1,
                                            high=1,
                                            shape=(1, 6),
                                            dtype=np.float16)

    def _next_observation(self):
        # Get the state of the environment
        p_xcor = self.game.paddle.xcor()
        b_xcor = self.game.ball.xcor()
        b_ycor = self.game.ball.ycor()

        # normalize the state to -1 to 1
        state = [
            p_xcor / 300, b_xcor / 300, b_ycor / 300, (p_xcor - b_xcor) / 600,
            self.game.ball.dx / 4, self.game.ball.dy / 3
        ]
        state = np.reshape(state, (1, 6))
        return state

    def reset(self):
        # Reset the state of the environment to an initial state
        self.game.reset()
        return self._next_observation()

    def step(self, action):
        # Execute one time step within the environment
        reward = 0
        if action == 0:
            self.game.movement(action='left')
            reward -= 0.01
        elif action == 2:
            self.game.movement(action='right')
            reward -= 0.01

        hit, done = self.game.run_frame()
        if (hit):
            reward += 10
        elif (done):
            reward -= 10
        state = self._next_observation()
        return state, reward, done, {}

    def render(self):
        pass

    def close(self):
        pass
Exemplo n.º 2
0
			#this appends to the created bricklist-which is then turned black in the brick_obj.update_bricks() function
			print(brick, ball_obj.ball)
			game_brick_list.append(brick)
			# print(len(game_brick_list))
			brick_obj.brick_list.remove(brick)

#-------------------------------------------------------------------


#--Loop will run forever unless disrupted
while True:
	event_handler()
	game_display.fill(Config['colors']['black'])
	
	#--Deals with paddle
	paddle_obj.draw()
	paddle_obj.movement(x_change)

#---------------------------------------------------------------------	
	#--Deals with bricks
	brick_obj.create_bricks()
	brick_obj.update_bricks(game_brick_list)
#------------------------------------------------------------------------

	#--Deals with the ball
	ball_obj.draw()

	#--Updating the display with the ball
	pygame.display.update()
	clock.tick(Config['game']['fps'])