Skip to content

Commit

Permalink
Merge pull request #2 from araffin/patch-1
Browse files Browse the repository at this point in the history
Render fixes
  • Loading branch information
markub3327 authored Aug 2, 2023
2 parents 3918786 + 8828737 commit 92513f1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Like with other `gymnasium` environments, it's very easy to use `flappy-bird-gym
Simply import the package and create the environment with the `make` function.
Take a look at the sample code below:

```
```python
import time
import flappy_bird_gymnasium
import gymnasium
Expand Down
2 changes: 1 addition & 1 deletion flappy_bird_gymnasium/envs/flappy_bird_env_rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(
pipe_gap: int = 100,
bird_color: str = "yellow",
pipe_color: str = "green",
render_mode=None,
render_mode: Optional[str] = None,
background: Optional[str] = None,
) -> None:
self.action_space = gymnasium.spaces.Discrete(2)
Expand Down
39 changes: 25 additions & 14 deletions flappy_bird_gymnasium/envs/flappy_bird_env_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
numerical information about the game's state as observations.
"""

import time
from typing import Dict, Optional, Tuple, Union

import gymnasium
Expand Down Expand Up @@ -70,16 +71,17 @@ class FlappyBirdEnvSimple(gymnasium.Env):
be drawn.
"""

metadata = {"render_modes": ["human"], "render_fps": 30}
metadata = {"render_modes": ["human", "rgb_array"], "render_fps": 30}

def __init__(
self,
screen_size: Tuple[int, int] = (288, 512),
audio_on: bool = True,
audio_on: bool = False,
normalize_obs: bool = True,
pipe_gap: int = 100,
bird_color: str = "yellow",
pipe_color: str = "green",
render_mode: Optional[str] = None,
background: Optional[str] = "day",
) -> None:
self.action_space = gymnasium.spaces.Discrete(2)
Expand All @@ -98,6 +100,18 @@ def __init__(
self._pipe_color = pipe_color
self._bg_type = background

assert render_mode is None or render_mode in self.metadata["render_modes"]
self.render_mode = render_mode

if render_mode is not None:
self._renderer = FlappyBirdRenderer(
screen_size=self._screen_size,
audio_on=audio_on,
bird_color=bird_color,
pipe_color=pipe_color,
background=background,
)

def _get_observation(self):
pipes = []
for up_pipe, low_pipe in zip(self._game.upper_pipes, self._game.lower_pipes):
Expand Down Expand Up @@ -193,19 +207,16 @@ def set_color(self, color):

def render(self) -> None:
"""Renders the next frame."""
if self._renderer is None:
self._renderer = FlappyBirdRenderer(
screen_size=self._screen_size,
audio_on=self._audio_on,
bird_color=self._bird_color,
pipe_color=self._pipe_color,
background=self._bg_type,
)
self._renderer.game = self._game
self._renderer.make_display()

self._renderer.draw_surface(show_score=True)
self._renderer.update_display()
if self.render_mode == "rgb_array":
# Flip the image to retrieve a correct aspect
return np.transpose(pygame.surfarray.array3d(self._renderer.surface), axes=(1, 0, 2))
else:
if self._renderer.display is None:
self._renderer.make_display()

self._renderer.update_display()
time.sleep(1 / self.metadata["render_fps"])

def close(self):
"""Closes the environment."""
Expand Down
1 change: 1 addition & 0 deletions flappy_bird_gymnasium/envs/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def update_display(self) -> None:
"call the `make_display()` method."
)

pygame.event.get()
self.display.blit(self.surface, [0, 0])
pygame.display.update()

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import setuptools

_VERSION = "0.2.1"
_VERSION = "0.2.2"

# Short description.
short_description = "A Gymnasium environment for the Flappy Bird game."
Expand Down

0 comments on commit 92513f1

Please sign in to comment.