Added blackjack toytext rendering support (#2550)
@@ -1,4 +1,8 @@
|
||||
from typing import Optional
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
import pygame
|
||||
|
||||
import gym
|
||||
from gym import spaces
|
||||
@@ -75,6 +79,8 @@ class BlackjackEnv(gym.Env):
|
||||
http://incompleteideas.net/book/the-book-2nd.html
|
||||
"""
|
||||
|
||||
metadata = {"render.modes": ["human", "rgb_array"]}
|
||||
|
||||
def __init__(self, natural=False, sab=False):
|
||||
self.action_space = spaces.Discrete(2)
|
||||
self.observation_space = spaces.Tuple(
|
||||
@@ -124,3 +130,107 @@ class BlackjackEnv(gym.Env):
|
||||
self.dealer = draw_hand(self.np_random)
|
||||
self.player = draw_hand(self.np_random)
|
||||
return self._get_obs()
|
||||
|
||||
def render(self, mode="human"):
|
||||
player_sum, dealer_card_value, usable_ace = self._get_obs()
|
||||
screen_width, screen_height = 600, 500
|
||||
card_img_height = screen_height // 3
|
||||
card_img_width = int(card_img_height * 142 / 197)
|
||||
spacing = screen_height // 20
|
||||
|
||||
bg_color = (7, 99, 36)
|
||||
white = (255, 255, 255)
|
||||
|
||||
if not hasattr(self, "screen"):
|
||||
if mode == "human":
|
||||
pygame.init()
|
||||
self.screen = pygame.display.set_mode((screen_width, screen_height))
|
||||
else:
|
||||
pygame.font.init()
|
||||
self.screen = pygame.Surface((screen_width, screen_height))
|
||||
|
||||
self.screen.fill(bg_color)
|
||||
|
||||
def get_image(path):
|
||||
cwd = os.path.dirname(__file__)
|
||||
image = pygame.image.load(cwd + "/" + path)
|
||||
return image
|
||||
|
||||
def get_font(path, size):
|
||||
cwd = os.path.dirname(__file__)
|
||||
font = pygame.font.Font((cwd + "/" + path), size)
|
||||
return font
|
||||
|
||||
small_font = get_font(
|
||||
os.path.join("font", "Minecraft.ttf"), screen_height // 15
|
||||
)
|
||||
dealer_text = small_font.render(
|
||||
"Dealer: " + str(dealer_card_value), True, white
|
||||
)
|
||||
dealer_text_rect = self.screen.blit(dealer_text, (spacing, spacing))
|
||||
|
||||
suits = ["C", "D", "H", "S"]
|
||||
dealer_card_suit = self.np_random.choice(suits)
|
||||
|
||||
if dealer_card_value == 1:
|
||||
dealer_card_value_str = "A"
|
||||
elif dealer_card_value == 10:
|
||||
dealer_card_value_str = self.np_random.choice(["J", "Q", "K"])
|
||||
else:
|
||||
dealer_card_value_str = str(dealer_card_value)
|
||||
|
||||
def scale_card_img(card_img):
|
||||
return pygame.transform.scale(card_img, (card_img_width, card_img_height))
|
||||
|
||||
dealer_card_img = scale_card_img(
|
||||
get_image(
|
||||
os.path.join("img", dealer_card_suit + dealer_card_value_str + ".png")
|
||||
)
|
||||
)
|
||||
dealer_card_rect = self.screen.blit(
|
||||
dealer_card_img,
|
||||
(
|
||||
screen_width // 2 - card_img_width - spacing // 2,
|
||||
dealer_text_rect.bottom + spacing,
|
||||
),
|
||||
)
|
||||
|
||||
hidden_card_img = scale_card_img(get_image(os.path.join("img", "Card.png")))
|
||||
self.screen.blit(
|
||||
hidden_card_img,
|
||||
(
|
||||
screen_width // 2 + spacing // 2,
|
||||
dealer_text_rect.bottom + spacing,
|
||||
),
|
||||
)
|
||||
|
||||
player_text = small_font.render("Player", True, white)
|
||||
player_text_rect = self.screen.blit(
|
||||
player_text, (spacing, dealer_card_rect.bottom + 1.5 * spacing)
|
||||
)
|
||||
|
||||
large_font = get_font(os.path.join("font", "Minecraft.ttf"), screen_height // 6)
|
||||
player_sum_text = large_font.render(str(player_sum), True, white)
|
||||
player_sum_text_rect = self.screen.blit(
|
||||
player_sum_text,
|
||||
(
|
||||
screen_width // 2 - player_sum_text.get_width() // 2,
|
||||
player_text_rect.bottom + spacing,
|
||||
),
|
||||
)
|
||||
|
||||
if usable_ace:
|
||||
usable_ace_text = small_font.render("usable ace", True, white)
|
||||
self.screen.blit(
|
||||
usable_ace_text,
|
||||
(
|
||||
screen_width // 2 - usable_ace_text.get_width() // 2,
|
||||
player_sum_text_rect.bottom + spacing // 2,
|
||||
),
|
||||
)
|
||||
if mode == "human":
|
||||
pygame.display.update()
|
||||
else:
|
||||
return np.transpose(
|
||||
np.array(pygame.surfarray.pixels3d(self.screen)), axes=(1, 0, 2)
|
||||
)
|
||||
|
BIN
gym/envs/toy_text/font/Minecraft.ttf
Normal file
BIN
gym/envs/toy_text/img/C2.png
Normal file
After Width: | Height: | Size: 6.1 KiB |
BIN
gym/envs/toy_text/img/C3.png
Normal file
After Width: | Height: | Size: 6.8 KiB |
BIN
gym/envs/toy_text/img/C4.png
Normal file
After Width: | Height: | Size: 6.9 KiB |
BIN
gym/envs/toy_text/img/C5.png
Normal file
After Width: | Height: | Size: 7.8 KiB |
BIN
gym/envs/toy_text/img/C6.png
Normal file
After Width: | Height: | Size: 8.3 KiB |
BIN
gym/envs/toy_text/img/C7.png
Normal file
After Width: | Height: | Size: 8.5 KiB |
BIN
gym/envs/toy_text/img/C8.png
Normal file
After Width: | Height: | Size: 9.6 KiB |
BIN
gym/envs/toy_text/img/C9.png
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
gym/envs/toy_text/img/CA.png
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
gym/envs/toy_text/img/CJ.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
gym/envs/toy_text/img/CK.png
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
gym/envs/toy_text/img/CQ.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
gym/envs/toy_text/img/CT.png
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
gym/envs/toy_text/img/Card.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gym/envs/toy_text/img/D2.png
Normal file
After Width: | Height: | Size: 5.8 KiB |
BIN
gym/envs/toy_text/img/D3.png
Normal file
After Width: | Height: | Size: 6.3 KiB |
BIN
gym/envs/toy_text/img/D4.png
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
gym/envs/toy_text/img/D5.png
Normal file
After Width: | Height: | Size: 7.3 KiB |
BIN
gym/envs/toy_text/img/D6.png
Normal file
After Width: | Height: | Size: 7.7 KiB |
BIN
gym/envs/toy_text/img/D7.png
Normal file
After Width: | Height: | Size: 7.9 KiB |
BIN
gym/envs/toy_text/img/D8.png
Normal file
After Width: | Height: | Size: 8.8 KiB |
BIN
gym/envs/toy_text/img/D9.png
Normal file
After Width: | Height: | Size: 9.3 KiB |
BIN
gym/envs/toy_text/img/DA.png
Normal file
After Width: | Height: | Size: 6.1 KiB |
BIN
gym/envs/toy_text/img/DJ.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
gym/envs/toy_text/img/DK.png
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
gym/envs/toy_text/img/DQ.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
gym/envs/toy_text/img/DT.png
Normal file
After Width: | Height: | Size: 9.3 KiB |
BIN
gym/envs/toy_text/img/H2.png
Normal file
After Width: | Height: | Size: 5.8 KiB |
BIN
gym/envs/toy_text/img/H3.png
Normal file
After Width: | Height: | Size: 6.3 KiB |
BIN
gym/envs/toy_text/img/H4.png
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
gym/envs/toy_text/img/H5.png
Normal file
After Width: | Height: | Size: 7.2 KiB |
BIN
gym/envs/toy_text/img/H6.png
Normal file
After Width: | Height: | Size: 7.7 KiB |
BIN
gym/envs/toy_text/img/H7.png
Normal file
After Width: | Height: | Size: 7.9 KiB |
BIN
gym/envs/toy_text/img/H8.png
Normal file
After Width: | Height: | Size: 8.7 KiB |
BIN
gym/envs/toy_text/img/H9.png
Normal file
After Width: | Height: | Size: 9.2 KiB |
BIN
gym/envs/toy_text/img/HA.png
Normal file
After Width: | Height: | Size: 6.7 KiB |
BIN
gym/envs/toy_text/img/HJ.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
gym/envs/toy_text/img/HK.png
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
gym/envs/toy_text/img/HQ.png
Normal file
After Width: | Height: | Size: 20 KiB |
BIN
gym/envs/toy_text/img/HT.png
Normal file
After Width: | Height: | Size: 9.1 KiB |
BIN
gym/envs/toy_text/img/S2.png
Normal file
After Width: | Height: | Size: 6.1 KiB |
BIN
gym/envs/toy_text/img/S3.png
Normal file
After Width: | Height: | Size: 6.7 KiB |
BIN
gym/envs/toy_text/img/S4.png
Normal file
After Width: | Height: | Size: 6.8 KiB |
BIN
gym/envs/toy_text/img/S5.png
Normal file
After Width: | Height: | Size: 7.6 KiB |
BIN
gym/envs/toy_text/img/S6.png
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
gym/envs/toy_text/img/S7.png
Normal file
After Width: | Height: | Size: 8.4 KiB |
BIN
gym/envs/toy_text/img/S8.png
Normal file
After Width: | Height: | Size: 9.3 KiB |
BIN
gym/envs/toy_text/img/S9.png
Normal file
After Width: | Height: | Size: 9.7 KiB |
BIN
gym/envs/toy_text/img/SA.png
Normal file
After Width: | Height: | Size: 6.5 KiB |
BIN
gym/envs/toy_text/img/SJ.png
Normal file
After Width: | Height: | Size: 18 KiB |
BIN
gym/envs/toy_text/img/SK.png
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
gym/envs/toy_text/img/SQ.png
Normal file
After Width: | Height: | Size: 19 KiB |
BIN
gym/envs/toy_text/img/ST.png
Normal file
After Width: | Height: | Size: 9.6 KiB |
@@ -5,5 +5,6 @@ mujoco_py>=1.50, <2.0
|
||||
scipy>=1.4.1
|
||||
numpy>=1.18.0
|
||||
pyglet>=1.4.0
|
||||
pygame==2.1.0
|
||||
cloudpickle>=1.2.0
|
||||
lz4>=3.1.0
|
||||
|