Cope 2.5.0
My personal "standard library" of all the generally useful code I've written for various projects over the years
|
A simplified Gymnasium enviorment that uses pygame and handles some stuff for you, like rendering keeping track of steps, returning the right things from the right functions, event handling, including some default keyboard shortcuts, and some debugging features. More...
Public Member Functions | |
def | __init__ (self, max_steps=None, screen_size=300, fps=None, name='SimpleGym Enviorment', show_vars={}, show_strings=[], start_paused=False, render_mode='pygame', assert_valid_action=True, background_color=(20, 20, 20), print_color=(200, 20, 20, 0), show_events=False, verbose=True) |
This should be called first, if you want to use the members like self.size Parameters: max_steps : if positive, sets the maximum number of steps before the env resets itself. | |
def | reset (self, seed=None, options=None) |
This sets the self.np_random to use the seed given, resets the steps, and returns the observation and info. | |
def | step (self, action) |
Call this last, and return it. | |
def | render (self) |
def | debug_button (self) |
def | handle_event (self, event) |
def | show (self, string) |
Sets a given string to be shown on the pygame window. | |
def | __setitem__ (self, index, string) |
def | close (self) |
def | __new__ (*args, **kwargs) |
Static Public Attributes | |
int | FPS_STEP = 2 |
int | SHOW_HELP_FADE_TIME = 10 |
int | FONT_SIZE = 10 |
str | HELP_TEXT |
else : | |
Protected Member Functions | |
def | _get_obs (self) |
def | _get_info (self) |
def | _get_truncated (self) |
def | _get_terminated (self) |
By default this just terminates after max_steps have been reached. | |
def | _get_reward (self) |
def | _reset (seed=None, options=None) |
def | _step (action) |
def | _init_pygame (self) |
def | _handle_events (self) |
Protected Attributes | |
_previous_step_called | |
_assert_valid_action | |
_prints_surf | |
_original_fps | |
_show_help | |
_rendered_helps | |
A simplified Gymnasium enviorment that uses pygame and handles some stuff for you, like rendering keeping track of steps, returning the right things from the right functions, event handling, including some default keyboard shortcuts, and some debugging features.
Includes easy ways to print to the screen.
Rendering By default, it's set to render the enviorment according to the function render_pygame, which should render everything to self.surf. If you would like to specify other rendering methods, define them as render_{method_name}
, and render() will handle it for you. There's no need to manually overwrite the render() method.
Printing There are 3 ways to print to the screen: show_vars
: accessable either via the constructor or as a member This is a dictionary of {name: member} of members you want to have printed on the screen. The keys can be any string, and the values must be valid members of this class. The screen is updated as the member changes. show_strings
: accessable either via the constructor, as a member, or the show() function This is a list of strings that are printed to the screen. They won't change. Useful for showing config options and the like. They are always printed first. print
: a dictionary member. The keys are arbitrary and not printed. The values are printed to the screen. The reason it's a dictionary and not a list is simply for easy indexing: you can change this in the middle of the running loop, and it will get added to the screen. Just make sure to reuse the keys. Attempting to set an item on an instance will also set it to print (i.e. ‘env['a’] = 'string'is the same as
env.print['a'] = 'string'`)
Default key handling: q closes the window space toggles pause i increments a single frame u runs the debug_button() r runs reset() immediately f toggles whether we're limiting ourselves to FPS or not h shows a help menu on screen >/< increase and decrease the FPS
In order to use this effectively, you need to overload: init(), if you want to add any members _get_obs() _get_reward() _step(action), don't overload step(), step() calls _step _reset(seed=None, options=None), if you need any custom reset code (don't overload reset()) render_pygame(), render to self.surf and optionally: _get_terminated(), if you want to use custom terminated criteria other than just max_steps You don't need to call super()._get_terminated() _get_info(), if you want to include info handle_event(event), for handling events debug_button(), for debugging when you press the u key _get_truncated(), if you want to include truncated
Helpful members provided: width
, height
: the dimentions of the screen size
: set to self.width, for compatibility's sake just_reset
: is set to True by reset() and False by step() steps
: the number of steps in the current episode total_steps
: the total number of steps taken reset_count
: a count of the number of times reset() has been called surf
: the surface to draw to in render_pygame() paused
: True if paused increment
: set to True internally to denote a single step while paused. step() sets to False