This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygamewrapper.py
139 lines (108 loc) · 3.3 KB
/
pygamewrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import pygame
from pygame.constants import USEREVENT, MOUSEBUTTONDOWN, MOUSEMOTION
class PyGameWrapper(object):
def __init__(self, width, height, actions={}):
self.width = width
self.height = height
self.screen_dim = (width, height)
self.actions = actions
self.scores = {}
self.lives = 0
self.screen = None
self.clock = None
self.winner = 0
def setup(self):
"""
Setups up the pygame env, the display and game clock.
"""
pygame.init()
self.screen = pygame.display.set_mode(self.get_screen_dims(), 0, 32)
self.clock = pygame.time.Clock()
def draw_frame(self, draw_screen):
"""
Decides if the screen will be drawn.
"""
if draw_screen:
pygame.display.update()
def set_action(self, pos, last_pos, event_type):
"""
Push the actions to the pygame event qeueu.
"""
if event_type == MOUSEMOTION:
e = pygame.event.Event(MOUSEMOTION, {'pos': pos})
pygame.event.post(e)
if event_type == USEREVENT:
e = pygame.event.Event(USEREVENT, {'pos': pos})
pygame.event.post(e)
def get_game_state(self):
"""
Gets a non-visual state representation of the game.
Returns
-------
dict or None
dict if the game supports it and None otherwise.
"""
return None
def get_screen_dims(self):
"""
Gets the screen dimensiions of the game in tuple form.
Returns
-------
tuple of int
(width, height)
"""
return self.screen_dim
def get_actions(self):
"""
Gets the actions used within the game.
Returns
-------
list of int
"""
return self.actions.values()
def get_winner(self):
"""
Get the winner for the game.
Returns
------
int
"""
return self.winner
def init(self):
"""
This is used to initialize the game.
This is game dependent.
"""
raise NotImplementedError("Please override this method")
def reset(self):
"""
Wraps the init() function, can be setup to reset certain portions of the game only if needed.
"""
self.init()
def get_scores(self):
"""
Return the current score of the game.
Returns
-------
int
The current reward the agent has received since the last init() or reset() call.
"""
return self.scores
def game_over(self):
"""
Gets the status of the game, returns True if the game has hit a terminal state. False otherwise.
This is game dependent.
Returns
-------
bool
"""
raise NotImplementedError("Please override this method")
def step(self, dt):
"""
This method steps the game forward one step in time equl to the dt parameter. The game does not run unless this method is called.
Parameters
----------
dt: integer
This is the amount of time elapsed since the last frame in milliseconds.
"""
raise NotImplementedError("Please override this method")