0
votes

I am trying to extract frames from my previously coded pygames module in order to train a RL Agent on it. However, I am unsure as to how to extract frames from the game engine.

More specifically, I have looked this up and found this blog post on it, however I don't really understand how I can access and wrap the pygame.display without hijacking my game object main loop.
https://danielslater.net/2015/12/29/how-to-run-learning-agents-against-pygame/

This is the game that I am trying to extract frames from: https://github.com/Jh123x/Orbital/blob/master/Space%20Invaders/classes/game.py

Could I get some advise on how to do this?

edit: More specifically, how would i call the function to extract frames from the game loop while it is running?

frame extractor code

import pygame  
import pygame.surfarray  

 # function that we can give two functions to and will return us a new function that calls both
 def function_combine(screen_update_func, our_intercepting_func):  
   def wrap(*args, **kwargs):  
     screen_update_func(*args,  
               **kwargs) # call the screen update func we intercepted so the screen buffer is updated  
     our_intercepting_func() # call our own function to get the screen buffer  
   return wrap  

 def on_screen_update():  
   surface_array = pygame.surfarray.array3d(pygame.display.get_surface())  
   print("We got the screen array")  
   print(surface_array)  

 # set our on_screen_update function to always get called whenever the screen updated  
 pygame.display.update = function_combine(pygame.display.update, on_screen_update)
1

1 Answers

0
votes

https://www.pygame.org/docs/ref/image.html#pygame.image.save

You could try pygame.image.save(window, "frame.png")

Where window is the active window object.