Skip to content

Instantly share code, notes, and snippets.

@ajschumacher
Created January 12, 2017 22:27
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ajschumacher/c5d441e5014527ddab679f2094c709b3 to your computer and use it in GitHub Desktop.
play Ms. Pacman w/ OpenAI gym
import time
import pygame
import gym
frame_time = 1.0 / 15 # seconds
pygame.init()
env = gym.make('MsPacman-v0')
env.frameskip = 3
env.reset()
# directions mapped on keyboard
actions = {'u': 6, 'i': 1, 'o': 5,
'j': 3, 'k': 0, 'l': 2,
'm': 8, ',': 4, '.': 7}
actions = {ord(key): value
for key, value in actions.items()}
then = 0
done = False
last_key = ord('k')
while True:
now = time.time()
if frame_time < now - then:
if not done:
_, _, done, _ = env.step(actions[last_key])
else:
env.reset()
done = False
env.render()
then = now
last_key = ord('k')
event = pygame.event.poll()
if event.type == pygame.KEYDOWN:
last_key = event.dict["key"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment