king_pong.py
def frame_step(self, input_actions):
                """
                Moves the state of the game forward
                one step with the given input actions

                input_actions[0] == 1: do nothing
                input_actions[1] == 1: move up
                input_actions[2] == 1: move down

                sum(input_actions) == 1
                """
                pygame.event.pump()

                if sum(input_actions) != 1:
                        raise ValueError('Multiple input actions!')

                # move player
                if input_actions[1] == 1:
                        # player moves up
                        self.playery = np.maximum(0,
                                                  self.playery - self.top_speed)
                elif input_actions[2] == 1:
                        # player moves down
                        self.playery = np.minimum(self.playery + self.top_speed,
                                                  SCREEN_HEIGHT - PADDLE_HEIGHT)

                # move cpu
                if self.cpuy + (PADDLE_HEIGHT/2) > self.bally:
                    self.cpuy = np.maximum(0,
                                           self.cpuy - self.top_speed)
                elif self.cpuy + (PADDLE_HEIGHT/2) < self.bally:
                    self.cpuy = np.minimum(self.cpuy + self.top_speed,
                                           SCREEN_HEIGHT - PADDLE_HEIGHT)

                # move ball get reward the it produced
                reward = self.move_ball()

                # check for losing
                terminal_good = self.ballx <= 0
                terminal_bad = self.ballx + BALL_SIZE >= SCREEN_WIDTH
                terminal = terminal_good or terminal_bad
                if terminal: self.reset_positions()

                self.score[0] += terminal_bad
                self.score[1] += terminal_good

                reward = -1.0 if terminal_bad else 1.0 if terminal_good else reward

                # redraw game onto screen
                SCREEN.fill((0, 0, 0)) # black screen
                pygame.draw.rect(SCREEN, # left 'cpu' player
                                 (255, 255, 255),
                                 (self.cpux, self.cpuy, PADDLE_WIDTH, PADDLE_HEIGHT))
                pygame.draw.rect(SCREEN, # right player
                                 (255, 255, 255),
                                 (self.playerx, self.playery, PADDLE_WIDTH, PADDLE_HEIGHT))
                pygame.draw.rect(SCREEN, # ball
                                 (255, 255, 255),
                                 (self.ballx, self.bally, BALL_SIZE, BALL_SIZE))

                # update pygame
                image_data = pygame.surfarray.array3d(pygame.display.get_surface())
                if self.auto_draw: self.complete_drawing()

                if terminal: self.score_changed = True

                # calculate who would be the winner
                if self.score[0] == self.first_to[1]:
                    self.score = [0, 0]
                    self.games[0] += 1
                elif self.score[1] == self.first_to[1]:
                    self.score = [0, 0]
                    self.games[1] += 1
                return image_data, reward

                Moves the state of the game forward
                one step with the given input actions

                input_actions[0] == 1: do nothing
                input_actions[1] == 1: move up
                input_actions[2] == 1: move down

                sum(input_actions) == 1
                
Moves the state of the game forward
one step with the given input actions




Moves the state of the game forward one step with the given input actions
def frame_step(self, input_actions):
                pygame.event.pump()
                if sum(input_actions) != 1:
                        raise ValueError('Multiple input actions!')
                if input_actions[1] == 1:
                        self.playery = np.maximum(0,
                                                  self.playery - self.top_speed)
                elif input_actions[2] == 1:
                        self.playery = np.minimum(self.playery + self.top_speed,
                                                  SCREEN_HEIGHT - PADDLE_HEIGHT)
                if self.cpuy + (PADDLE_HEIGHT/2) > self.bally:
                    self.cpuy = np.maximum(0,
                                           self.cpuy - self.top_speed)
                elif self.cpuy + (PADDLE_HEIGHT/2) < self.bally:
                    self.cpuy = np.minimum(self.cpuy + self.top_speed,
                                           SCREEN_HEIGHT - PADDLE_HEIGHT)
                reward = self.move_ball()
                terminal_good = self.ballx <= 0
                terminal_bad = self.ballx + BALL_SIZE >= SCREEN_WIDTH
                terminal = terminal_good or terminal_bad
                if terminal: self.reset_positions()
                self.score[0] += terminal_bad
                self.score[1] += terminal_good
                reward = -1.0 if terminal_bad else 1.0 if terminal_good else reward
                SCREEN.fill((0, 0, 0)) 
                pygame.draw.rect(SCREEN, 
                                 (255, 255, 255),
                                 (self.cpux, self.cpuy, PADDLE_WIDTH, PADDLE_HEIGHT))
                pygame.draw.rect(SCREEN, 
                                 (255, 255, 255),
                                 (self.playerx, self.playery, PADDLE_WIDTH, PADDLE_HEIGHT))
                pygame.draw.rect(SCREEN, 
                                 (255, 255, 255),
                                 (self.ballx, self.bally, BALL_SIZE, BALL_SIZE))
                image_data = pygame.surfarray.array3d(pygame.display.get_surface())
                if self.auto_draw: self.complete_drawing()
                if terminal: self.score_changed = True
                if self.score[0] == self.first_to[1]:
                    self.score = [0, 0]
                    self.games[0] += 1
                elif self.score[1] == self.first_to[1]:
                    self.score = [0, 0]
                    self.games[1] += 1
                return image_data, reward
def frame_step ( self , input_actions ) : pygame . event . pump ( ) if sum ( input_actions ) != 1 : raise ValueError ( 'Multiple input actions!' ) if input_actions [ 1 ] == 1 : self . playery = np . maximum ( 0 , self . playery - self . top_speed ) elif input_actions [ 2 ] == 1 : self . playery = np . minimum ( self . playery + self . top_speed , SCREEN_HEIGHT - PADDLE_HEIGHT ) if self . cpuy + ( PADDLE_HEIGHT / 2 ) > self . bally : self . cpuy = np . maximum ( 0 , self . cpuy - self . top_speed ) elif self . cpuy + ( PADDLE_HEIGHT / 2 ) < self . bally : self . cpuy = np . minimum ( self . cpuy + self . top_speed , SCREEN_HEIGHT - PADDLE_HEIGHT ) reward = self . move_ball ( ) terminal_good = self . ballx <= 0 terminal_bad = self . ballx + BALL_SIZE >= SCREEN_WIDTH terminal = terminal_good or terminal_bad if terminal : self . reset_positions ( ) self . score [ 0 ] += terminal_bad self . score [ 1 ] += terminal_good reward = - 1.0 if terminal_bad else 1.0 if terminal_good else reward SCREEN . fill ( ( 0 , 0 , 0 ) ) pygame . draw . rect ( SCREEN , ( 255 , 255 , 255 ) , ( self . cpux , self . cpuy , PADDLE_WIDTH , PADDLE_HEIGHT ) ) pygame . draw . rect ( SCREEN , ( 255 , 255 , 255 ) , ( self . playerx , self . playery , PADDLE_WIDTH , PADDLE_HEIGHT ) ) pygame . draw . rect ( SCREEN , ( 255 , 255 , 255 ) , ( self . ballx , self . bally , BALL_SIZE , BALL_SIZE ) ) image_data = pygame . surfarray . array3d ( pygame . display . get_surface ( ) ) if self . auto_draw : self . complete_drawing ( ) if terminal : self . score_changed = True if self . score [ 0 ] == self . first_to [ 1 ] : self . score = [ 0 , 0 ] self . games [ 0 ] += 1 elif self . score [ 1 ] == self . first_to [ 1 ] : self . score = [ 0 , 0 ] self . games [ 1 ] += 1 return image_data , reward
Moves the state of the game forward
one step with the given input actions
Moves the state of the game forward one step with the given input actions
"""
                Moves the state of the game forward
                one step with the given input actions

                input_actions[0] == 1: do nothing
                input_actions[1] == 1: move up
                input_actions[2] == 1: move down

                sum(input_actions) == 1
                """ # move player # player moves up # player moves down # move cpu # move ball get reward the it produced # check for losing # redraw game onto screen # black screen # left 'cpu' player # right player # ball # update pygame # calculate who would be the winner
self
input_actions