How to do it...

We'll develop the Flappy Bird game environment using Pygame as follows:

  1. We start by developing a utility function that loads images and transforms them into the right format:
>>> from pygame.image import load >>> from pygame.surfarray import pixels_alpha >>> from pygame.transform import rotate >>> def load_images(sprites_path): ...     base_image = load(sprites_path +                              'base.png').convert_alpha() ...     background_image = load(sprites_path +                              'background-black.png').convert() ...     pipe_images = [rotate(load(sprites_path +                         'pipe-green.png').convert_alpha(), 180), ...                    load(sprites_path +                              'pipe-green.png').convert_alpha()] ...     bird_images = [load(sprites_path +                            'redbird-upflap.png').convert_alpha(), ...                    load(sprites_path +  'redbird-midflap.png').convert_alpha(), ...

Get PyTorch 1.x Reinforcement Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.