Skip to content

Commit 565285e

Browse files
committed
Fix assets path and pygame display no closing
1 parent 7123379 commit 565285e

6 files changed

Lines changed: 76 additions & 55 deletions

File tree

flappy_bird_gym/envs/flappy_bird_env.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import gym
2929
import numpy as np
30+
import pygame
3031
from gym import spaces
3132

3233
from flappy_bird_gym.envs.game_logic import FlappyBirdLogic, PIPE_WIDTH
@@ -143,3 +144,9 @@ def render(self, mode='human'):
143144
self._renderer.game = self._game
144145

145146
self._renderer.render()
147+
148+
def close(self):
149+
if self._renderer is not None:
150+
pygame.display.quit()
151+
self._renderer = None
152+
super().close()

flappy_bird_gym/envs/renderer.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(self,
6161
self._screen_width = screen_size[0]
6262
self._screen_height = screen_size[1]
6363

64-
self._display = pygame.display.set_mode(screen_size)
64+
self.display = pygame.display.set_mode(screen_size)
6565
self.images = utils.load_images(bird_color=bird_color,
6666
pipe_color=pipe_color,
6767
bg_type=background)
@@ -83,8 +83,8 @@ def _show_score(self) -> None:
8383
x_offset = (self._screen_width - total_width) / 2
8484

8585
for digit in score_digits:
86-
self._display.blit(self.images['numbers'][digit],
87-
(x_offset, self._screen_height * 0.1))
86+
self.display.blit(self.images['numbers'][digit],
87+
(x_offset, self._screen_height * 0.1))
8888
x_offset += self.images['numbers'][digit].get_width()
8989

9090
def render(self) -> None:
@@ -97,17 +97,17 @@ def render(self) -> None:
9797
self.sounds[self.game.sound_cache].play()
9898

9999
# Images:
100-
self._display.blit(self.images['background'], (0, 0))
100+
self.display.blit(self.images['background'], (0, 0))
101101

102102
for up_pipe, low_pipe in zip(self.game.upper_pipes,
103103
self.game.lower_pipes):
104-
self._display.blit(self.images['pipe'][0],
105-
(up_pipe['x'], up_pipe['y']))
106-
self._display.blit(self.images['pipe'][1],
107-
(low_pipe['x'], low_pipe['y']))
104+
self.display.blit(self.images['pipe'][0],
105+
(up_pipe['x'], up_pipe['y']))
106+
self.display.blit(self.images['pipe'][1],
107+
(low_pipe['x'], low_pipe['y']))
108108

109-
self._display.blit(self.images['base'], (self.game.base_x,
110-
self.game.base_y))
109+
self.display.blit(self.images['base'], (self.game.base_x,
110+
self.game.base_y))
111111
# print score so player overlaps the score
112112
self._show_score()
113113

@@ -121,7 +121,7 @@ def render(self) -> None:
121121
visible_rot,
122122
)
123123

124-
self._display.blit(player_surface, (self.game.player_x,
125-
self.game.player_y))
124+
self.display.blit(player_surface, (self.game.player_x,
125+
self.game.player_y))
126126

127127
pygame.display.update()

flappy_bird_gym/envs/utils.py

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -81,43 +81,47 @@ def load_images(bg_type: str = "day",
8181
""" Loads and returns the image assets of the game. """
8282
images = {}
8383

84-
# Sprites with the number for the score display:
85-
images["numbers"] = tuple([
86-
pyg_image.load(f"{SPRITES_PATH}/{n}.png").convert_alpha()
87-
for n in range(10)
88-
])
89-
90-
# Game over sprite:
91-
images["gameover"] = pyg_image.load(
92-
f"{SPRITES_PATH}/gameover.png").convert_alpha()
93-
94-
# Welcome screen message sprite:
95-
images["message"] = pyg_image.load(
96-
f"{SPRITES_PATH}/message.png").convert_alpha()
97-
98-
# Sprite for the base (ground):
99-
images["base"] = pyg_image.load(
100-
f"{SPRITES_PATH}/base.png").convert_alpha()
101-
102-
# Background sprite:
103-
images["background"] = pyg_image.load(
104-
f"{SPRITES_PATH}/background-{bg_type}.png").convert()
105-
106-
# Bird sprites:
107-
images["player"] = (
108-
pyg_image.load(
109-
f"{SPRITES_PATH}/{bird_color}bird-upflap.png").convert_alpha(),
110-
pyg_image.load(
111-
f"{SPRITES_PATH}/{bird_color}bird-midflap.png").convert_alpha(),
112-
pyg_image.load(
113-
f"{SPRITES_PATH}/{bird_color}bird-downflap.png").convert_alpha(),
114-
)
115-
116-
# Pipe sprites:
117-
pipe_sprite = pyg_image.load(
118-
f"{SPRITES_PATH}/pipe-{pipe_color}.png").convert_alpha()
119-
images["pipe"] = (img_flip(pipe_sprite, False, True),
120-
pipe_sprite)
84+
try:
85+
# Sprites with the number for the score display:
86+
images["numbers"] = tuple([
87+
pyg_image.load(f"{SPRITES_PATH}/{n}.png").convert_alpha()
88+
for n in range(10)
89+
])
90+
91+
# Game over sprite:
92+
images["gameover"] = pyg_image.load(
93+
f"{SPRITES_PATH}/gameover.png").convert_alpha()
94+
95+
# Welcome screen message sprite:
96+
images["message"] = pyg_image.load(
97+
f"{SPRITES_PATH}/message.png").convert_alpha()
98+
99+
# Sprite for the base (ground):
100+
images["base"] = pyg_image.load(
101+
f"{SPRITES_PATH}/base.png").convert_alpha()
102+
103+
# Background sprite:
104+
images["background"] = pyg_image.load(
105+
f"{SPRITES_PATH}/background-{bg_type}.png").convert()
106+
107+
# Bird sprites:
108+
images["player"] = (
109+
pyg_image.load(
110+
f"{SPRITES_PATH}/{bird_color}bird-upflap.png").convert_alpha(),
111+
pyg_image.load(
112+
f"{SPRITES_PATH}/{bird_color}bird-midflap.png").convert_alpha(),
113+
pyg_image.load(
114+
f"{SPRITES_PATH}/{bird_color}bird-downflap.png").convert_alpha(),
115+
)
116+
117+
# Pipe sprites:
118+
pipe_sprite = pyg_image.load(
119+
f"{SPRITES_PATH}/pipe-{pipe_color}.png").convert_alpha()
120+
images["pipe"] = (img_flip(pipe_sprite, False, True),
121+
pipe_sprite)
122+
except FileNotFoundError as ex:
123+
raise FileNotFoundError("Can't find the sprites folder! No such file or"
124+
f" directory: {SPRITES_PATH}") from ex
121125

122126
return images
123127

@@ -132,10 +136,14 @@ def load_sounds() -> Dict[str, pyg_mixer.Sound]:
132136
else:
133137
soundExt = ".ogg"
134138

135-
sounds["die"] = pyg_mixer.Sound(AUDIO_PATH + "/die" + soundExt)
136-
sounds["hit"] = pyg_mixer.Sound(AUDIO_PATH + "/hit" + soundExt)
137-
sounds["point"] = pyg_mixer.Sound(AUDIO_PATH + "/point" + soundExt)
138-
sounds["swoosh"] = pyg_mixer.Sound(AUDIO_PATH + "/swoosh" + soundExt)
139-
sounds["wing"] = pyg_mixer.Sound(AUDIO_PATH + "/wing" + soundExt)
139+
try:
140+
sounds["die"] = pyg_mixer.Sound(AUDIO_PATH + "/die" + soundExt)
141+
sounds["hit"] = pyg_mixer.Sound(AUDIO_PATH + "/hit" + soundExt)
142+
sounds["point"] = pyg_mixer.Sound(AUDIO_PATH + "/point" + soundExt)
143+
sounds["swoosh"] = pyg_mixer.Sound(AUDIO_PATH + "/swoosh" + soundExt)
144+
sounds["wing"] = pyg_mixer.Sound(AUDIO_PATH + "/wing" + soundExt)
145+
except FileNotFoundError as ex:
146+
raise FileNotFoundError("Can't find the audio folder! No such file or "
147+
f"directory: {AUDIO_PATH}") from ex
140148

141149
return sounds

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from typing import List
2828
import setuptools
2929

30-
_VERSION = "0.1.1"
30+
_VERSION = "0.1.2"
3131

3232
# Short description.
3333
short_description = "An OpenAI gym environment for the Flappy Bird game."
@@ -62,6 +62,8 @@
6262
download_url="https://github.com/Talendar/flappy-bird-gym/releases",
6363
# Contained modules and scripts:
6464
packages=setuptools.find_packages(),
65+
package_data={"flappy_bird_gym": ["assets/sprites/*",
66+
"assets/audio/*"]},
6567
install_requires=REQUIRED_PACKAGES,
6668
tests_require=REQUIRED_PACKAGES + TEST_PACKAGES,
6769
# PyPI package information:

tests/test_env_human.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def main():
6464
time.sleep(0.5)
6565
break
6666

67+
env.close()
68+
6769

6870
if __name__ == "__main__":
6971
main()

tests/test_env_random.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def main():
5353
time.sleep(0.5)
5454
break
5555

56+
env.close()
57+
5658

5759
if __name__ == "__main__":
5860
main()

0 commit comments

Comments
 (0)