-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Add test gym utils play. Fix #2729 #2743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
87f34da
7982347
2a73571
02b81e5
7c65be7
1b3485f
48d5f07
3b9e08d
9a1d4b6
a6a7cd9
42b5ead
7dabd13
97fa4e9
808d0a1
bf4d2d8
52b37cc
5a8f6a4
b5aae1b
e9d333b
b0081c2
a02323c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import argparse | ||
| from typing import Tuple | ||
|
|
||
| import pygame | ||
| from pygame.event import Event | ||
|
|
||
| import gym | ||
| from gym import logger | ||
| from gym import Env, logger | ||
|
|
||
| try: | ||
| import matplotlib | ||
|
|
@@ -20,15 +22,15 @@ | |
|
|
||
|
|
||
| class PlayableGame: | ||
| def __init__(self, env, keys_to_action=None, zoom=None): | ||
| def __init__(self, env: Env, keys_to_action: dict = None, zoom: float = None): | ||
| self.env = env | ||
| self.relevant_keys = self._get_relevant_keys(keys_to_action) | ||
| self.video_size = self._get_video_size(zoom) | ||
| self.screen = pygame.display.set_mode(self.video_size) | ||
| self.pressed_keys = [] | ||
| self.running = True | ||
|
|
||
| def _get_relevant_keys(self, keys_to_action): | ||
| def _get_relevant_keys(self, keys_to_action: dict) -> set: | ||
|
||
| if keys_to_action is None: | ||
| if hasattr(self.env, "get_keys_to_action"): | ||
| keys_to_action = self.env.get_keys_to_action() | ||
|
|
@@ -43,7 +45,7 @@ def _get_relevant_keys(self, keys_to_action): | |
| relevant_keys = set(sum(map(list, keys_to_action.keys()), [])) | ||
|
||
| return relevant_keys | ||
|
|
||
| def _get_video_size(self, zoom=None): | ||
| def _get_video_size(self, zoom: float = None) -> Tuple[int, int]: | ||
|
||
| rendered = self.env.render(mode="rgb_array") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put like a TODO here so that we remember to update this when the render API change goes through? |
||
| video_size = [rendered.shape[1], rendered.shape[0]] | ||
|
|
||
|
|
@@ -52,7 +54,7 @@ def _get_video_size(self, zoom=None): | |
|
|
||
| return video_size | ||
|
|
||
| def process_event(self, event): | ||
| def process_event(self, event: Event) -> None: | ||
| if event.type == pygame.KEYDOWN: | ||
| if event.key in self.relevant_keys: | ||
| self.pressed_keys.append(event.key) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keys_to_action: Optional[dict]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even better if we can specify the key/value types on the dict (I guess values are arbitrary actions, but keys should be keypresses? how are they represented?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's
Dict[Tuple[int], int]where the key is(ord(key),)gym/gym/envs/classic_control/mountain_car.py
Lines 240 to 242 in 5ae6bf9
reference of the actions:
gym/gym/envs/classic_control/mountain_car.py
Lines 50 to 54 in 5ae6bf9
gym/gym/utils/play.py
Lines 125 to 135 in 9a1d4b6