def __init__(self, verbose=True): self.curr_state = GameState() self.agents = [ RandomPieceGenerator(PIECE_GENERATOR_AGENT_INDEX), ExpectimaxAgent(PLAYER_AGENT_INDEX, 3, 2) ] self.agent_index = 0 self.verbose = verbose
class Game: def __init__(self, verbose=True): self.curr_state = GameState() self.agents = [ RandomPieceGenerator(PIECE_GENERATOR_AGENT_INDEX), ExpectimaxAgent(PLAYER_AGENT_INDEX, 3, 2) ] self.agent_index = 0 self.verbose = verbose def step(self): action = self.agents[self.agent_index].get_action(self.curr_state) if action is None: self.curr_state.lose = True print("Score: %0.2f, lines cleared = %d, pieces placed = %d" % (self.curr_state.score, self.curr_state.lines_cleared, self.curr_state.pieces_placed)) return self.curr_state.score, self.curr_state.lines_cleared, self.curr_state.pieces_placed self.curr_state = self.curr_state.generate_successor(action, self.agent_index) self.agent_index = (self.agent_index + 1) % len(self.agents) if self.verbose: print(self.curr_state.board) return self.curr_state.score, self.curr_state.lines_cleared, self.curr_state.pieces_placed def run(self): while not self.curr_state.lose: score, lines_cleared, pieces_placed = self.step() return score, lines_cleared, pieces_placed
def __init__(self): self.screen = pygame.display.set_mode([Game.WIDTH, Game.HEIGHT]) pygame.display.set_caption('the orange box') self.clock = pygame.time.Clock() self.state_manager = StateManager( [MenuState(), GameState(), DeathState()], 1) self.start()
def __init__(self): pygame.init() self.clock = pygame.time.Clock() self.fps = SETTINGS['default_fps'] # fullscreen on/off if SETTINGS['fullscreen_mode']: self.screen = pygame.display.set_mode(( SETTINGS['window_x_size'], SETTINGS['window_y_size'] ), pygame.FULLSCREEN ) else: self.screen = pygame.display.set_mode(( SETTINGS['window_x_size'], SETTINGS['window_y_size'] )) # hide mouse cursor pygame.mouse.set_visible(False) # load resource manager self.rm = ResourceManager() # load input manager self.im = InputManager() # state setup self.cur_state = GameState(self.screen,self.rm, self.im) # initialize input buffers self.mouse_x,self.mouse_y = (0,0) self.mousestate = {} self.keystate = {}
import pygame from constants import * import utils from states import GameState # TODO: add an animation module for pygame surfaces running = True if pygame.display.get_surface() is not None else False # set up clock for limiting framerate and getting dt clock = pygame.time.Clock() states = utils.StateStack() # Stack that holds all the States state_data = dict(screen=screen, states=states) states.push(GameState(state_data)) # ====== Main Game Loop ====== while running: clock.tick(FPS) dt = clock.get_time() / 1000 # Update if states.isEmpty() is not True: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False states.top().update_events(dt, event) states.top().update(dt) if states.top().get_quit():
class Game(): def __init__(self): pygame.init() self.clock = pygame.time.Clock() self.fps = SETTINGS['default_fps'] # fullscreen on/off if SETTINGS['fullscreen_mode']: self.screen = pygame.display.set_mode(( SETTINGS['window_x_size'], SETTINGS['window_y_size'] ), pygame.FULLSCREEN ) else: self.screen = pygame.display.set_mode(( SETTINGS['window_x_size'], SETTINGS['window_y_size'] )) # hide mouse cursor pygame.mouse.set_visible(False) # load resource manager self.rm = ResourceManager() # load input manager self.im = InputManager() # state setup self.cur_state = GameState(self.screen,self.rm, self.im) # initialize input buffers self.mouse_x,self.mouse_y = (0,0) self.mousestate = {} self.keystate = {} def _step(self): # let state handle step self.cur_state._step() def _draw(self): self.screen.fill(SETTINGS['default_bg_color']) # let state handle drawing self.cur_state._draw() def mainloop(self): while(1): # reset im key events self.im.reset_events() # event handling for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.display.quit() sys.exit() if event.type == pygame.KEYDOWN: self.im.set_key_event(event.type, event.key) if event.type == pygame.JOYBUTTONDOWN: self.im.set_joy_button_event(event.type, event.button) if event.type == pygame.MOUSEBUTTONDOWN: self.im.set_mouse_event(event.type, event.button) # let state handle input self.im.update() self.cur_state._input() # draw frame self._draw() self.clock.tick(self.fps) pygame.display.flip() # step frame self._step()