def __init__( self, size=(200,200), position=(200,200) \ ,background=(0.0, 0.0, 0.0, 0.0) \ ,name='Default Canvas' ): """ """ # Size and position attributes self.size = size self.position = position self.background = background # Initialize pygame and screen pygame_init() self.resize(*self.size) self.running = True self.run()
def __init__(self, argparser=None, moke_execution=False): if Arbapp.app_declared: raise RuntimeError('Arbapp can be instanciated only once') Arbapp.app_declared = True pygame_init() self.read_args(argparser) self.arbalet = Arbalet(not moke_execution and not self.args.no_gui, not moke_execution and self.args.hardware, self.args.server, self.args.brightness, self.args.factor_sim, self.args.config, interactive=False) self.width = self.arbalet.width self.height = self.arbalet.height self.model = Arbamodel(self.height, self.width, 'black') self.set_model(self.model)
def __init__(self, width: int, height: int, title: str): pygame_init() Notification.init() self.running = False self.display = Display(width, height, title) self.key_manager = KeyManager() self.mouse_manager = MouseManager() self.objet_manager = ObjetManager() self.keyboard_buffer = KeyboardBuffer() self.handler_manager = HandlerManager() self.state_manager = StateManager(self.mouse_manager, self.key_manager, self.keyboard_buffer, self.display) self.notification_manager = NotificationManager() self.handler_manager\ .add(self.mouse_manager) \ .add(self.key_manager) \ .add(self.keyboard_buffer) \ .add(Quit(self.shutdown)) \ .add(HUDManager(self.handler_manager, self.objet_manager))
def main(self, argv: List[str]) -> int: try: pygame_init() self.window_surface = pygame.display.set_mode( (self.window_opts.w, self.window_opts.h), self.window_opts.flags, vsync=int(self.window_opts.vsync), ) pygame.display.set_caption(self.window_opts.caption) self.window_w, self.window_h = self.window_surface.get_size() self.primary_clock = pygame.time.Clock() self.prestart() while self.is_running: self.delta_time = self.primary_clock.tick( self.target_fps) / 1000.0 self.time += self.delta_time self.fixed_update_time_accumulator += self.delta_time while self.fixed_update_time_accumulator >= self.fixed_delta_time: self.fixed_time += self.fixed_delta_time self.fixed_update() self.fixed_update_time_accumulator -= self.fixed_delta_time self._process_events() self.update() self.window_surface.fill((0, 0, 0)) # type: ignore self.render() pygame.display.flip() finally: self.shutdown() pygame_quit() return 0
def __init__(self, arbalet_height, arbalet_width, sim_height, sim_width, rate=30, interactive=True, autorun=True): """ Arbasim constructor: launches the simulation Simulate a "arbalet_width x arbalet_height px" table rendered in a "sim_width x sim_height" window :param arbalet_width: Number of pixels of Arbalet in width :param arbalet_height: Number of pixels of Arbalet in height :param sim_width: :param sim_height: :param rate: Refresh rate in Hertz :param interactive: True if the simulator is run in an interactive python console or if it's used without Arbapp inheritance :return: """ Thread.__init__(self) logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%I:%M:%S') self.sim_state = "idle" self.running = True self.rate = Rate(rate) self.interactive = interactive # Current table model storing all pixels self.arbamodel = None self.sim_width = sim_width self.sim_height = sim_height self.arbalet_width = arbalet_width self.arbalet_height = arbalet_height self.grid = Grid(sim_width/arbalet_width, sim_height/arbalet_height, arbalet_width, arbalet_height, (40, 40, 40)) # Init Pygame if self.interactive: pygame_init() environ['SDL_VIDEO_CENTERED'] = '1' logging.info("Pygame initialized") self.screen = pygame.display.set_mode((self.sim_width, self.sim_height), 0, 32) self.sim_state = "idle" self.font = pygame.font.SysFont('sans', 14) # Autorun if autorun: self.start()
def __init__(self): '''Makes a BetterTimers object. Call pygame.quit to end all timers.''' self.__timers = list() pygame_init() register_quit(self.end_all_timers) fastevent_init()
players[name].size = tuple(data['size']) pellets[data['pellet_index']] = data['new_pellet'] elif msgtype == 'grow': players[name].size = tuple(data['size']) elif msgtype == 'die': left, top, width, height = tuple(data['state']) players[name] = Rect(left, top, width, height) if name == myname: mybox = players[myname] client = Client('localhost', 8888) pygame_init() screen = set_mode((400, 300)) clock = Clock() font = Font(None, 15) # default pygame Font, height in pixels borders = [Rect(0, 0, 2, 300), Rect(0, 0, 400, 2), Rect(398, 0, 2, 300), Rect(0, 298, 400, 2)] dx, dy = 0, 1 # start direction: down delay = 0 # start moving right away while True: clock.tick(50) # frames per second poll() # push and pull network messages for event in pygame_event.get(): # inputs
def vs_q_play(self, qdict): '''Allows you to play as Player 1 against our trained model''' pygame_init() width = self.cols * SQUARESIZE height = (self.rows + 1) * SQUARESIZE size = (width, height) screen = display.set_mode(size) self.draw_board(screen) display.update() myfont = font.Font('sweet purple.ttf', 75) while not self.game_over: for event in pygame_event.get(): if event.type == QUIT: sys_exit() if event.type == MOUSEMOTION: draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE)) posx = event.pos[0] if self.turn == 1: state = self.board_to_string() try: score = qdict[state] col = score.index(max(score)) except: col = choice(list(moves(self))) row = self.get_available_row(col) self.play_move(row, col, 2) if self.check_wins(2): label = myfont.render("Q-Agent wins!", 1, YELLOW) screen.blit(label, (width / 4, 10)) self.game_over = True self.draw_board(screen) break self.draw_board(screen) self.turn = 1 - self.turn else: draw.circle(screen, RED, (posx, int(SQUARESIZE / 2)), RADIUS) display.update() if event.type == MOUSEBUTTONDOWN: draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE)) # Asks for Player's input if self.turn: pass else: # Turn = 0 posx = event.pos[0] col = int(posx / SQUARESIZE) if self.move_is_valid(col): row = self.get_available_row(col) self.play_move(row, col, 1) if self.check_wins(1): label = myfont.render("Player wins!", 1, RED) screen.blit(label, (width / 4, 10)) self.game_over = True self.draw_board(screen) break self.draw_board(screen) self.turn = 1 - self.turn pygame_time.wait(5000)
def play(self): '''Allows you to play as Player 2 against Minimax''' pygame_init() width = self.cols * SQUARESIZE height = (self.rows + 1) * SQUARESIZE size = (width, height) screen = display.set_mode(size) self.draw_board(screen) display.update() myfont = font.Font('sweet purple.ttf', 75) while not self.game_over: for event in pygame_event.get(): if event.type == QUIT: sys_exit() if event.type == MOUSEMOTION: draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE)) posx = event.pos[0] if self.turn == 0: col = best_move(self) row = self.get_available_row(col) self.play_move(row, col, 1) if self.check_wins(1): label = myfont.render("Minimax wins!", 1, RED) screen.blit(label, (width / 4, 10)) self.game_over = True self.draw_board(screen) break self.draw_board(screen) self.turn = 1 - self.turn else: draw.circle(screen, YELLOW, (posx, int(SQUARESIZE / 2)), RADIUS) display.update() if event.type == MOUSEBUTTONDOWN: draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE)) # Asks for Player's input if not self.turn: # Turn = 0 pass else: # Turn = 1 posx = event.pos[0] col = int(posx / SQUARESIZE) if self.move_is_valid(col): row = self.get_available_row(col) self.play_move(row, col, 2) if self.check_wins(2): label = myfont.render("Player wins!", 1, YELLOW) screen.blit(label, (width / 4, 10)) self.game_over = True self.draw_board(screen) break self.draw_board(screen) self.turn = 1 - self.turn pygame_time.wait(5000)
def init(self): pygame_init() self.game_display = display.set_mode( (self.width, self.height), self.constants.SURFACE | self.constants.BUFFERING) self.running = True