def __init__(self, args, server_ip, client): # arguments self.args = args self.mode = None self.server_ip = server_ip self.client = client # server and client print('CLIENT ENTER room...') self.id = None self.total = None self.thread = Thread(target=self.wait_info, name='wait-info') self.thread.start() # gui self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { 'back': c.Button((self.args.size[0] // 2, 640), (600, 80), 'exit room', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), }
def __init__(self, args, mode): """ mode: server: { 'id': 0, 'ip', 'num': <NUM OF PLAYERS>, 'socket': <SOCKET>, 'clients': {'ip': <CLIENT IP>, 'port': <CLIENT PORT>, 'socket': <CLIENT SOCKET>} } client: {'id': >0, 'ip', 'num': <NUM OF PLAYERS>, 'socket': <SOCKET>} """ self.args = args self.mode = mode self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) # game & game menu if self.mode['id'] == 0: self.game = g_s.Game(self.args, self.mode) self.game_menu = gm.GameMenu(self.args, self.args.get_pos(1, 1), (300, 300), ('continue', 'save', 'quit'), align=(1, 1)) self.saver = sv.Saver(self.args, self.game.name) else: self.game = g_c.Game(self.args, self.mode) self.game_menu = gm.GameMenu(self.args, self.args.get_pos(1, 1), (300, 240), align=(1, 1)) self.saver = sv.Saver(self.args, '') self.ended = False
def __init__(self, args): # arguments self.args = args # regex self.regex = r'(([1-9]?[0-9])|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5]))' self.regex_one = r'^' + self.regex + r'\.?$' self.regex_two = r'^' + self.regex + r'\.' + self.regex + r'\.?$' self.regex_three = r'^(' + self.regex + r'\.){2}' + self.regex + r'\.?$' self.regex_full = r'^(' + self.regex + r'\.){3}' + self.regex + r'$' # socket self.server_ip = '' self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.client.settimeout(1.0) # gui self.background = c.Component(lambda ui: ui.show_div((0, 0), self.args.size, color=(60, 179, 113))) self.error_msg = None self.error_msg_clock = sw.Stopwatch() self.buttons = { 'connect': c.Button( (self.args.size[0] // 2, 540), (400, 60), 'connect', font=f.tnr(22), align=(1, 1), background=(210, 210, 210) ), 'back': c.Button( (self.args.size[0] // 2, 640), (400, 60), 'back', font=f.tnr(22), align=(1, 1), background=(210, 210, 210) ), }
def __init__(self, args): self.args = args self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { 'sing': c.Button((self.args.size[0] // 2, 360), (600, 80), 'Single Player', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), 'mult': c.Button((self.args.size[0] // 2, 460), (600, 80), 'Multi-Player', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), 'back': c.Button((self.args.size[0] // 2, 560), (600, 80), 'Back', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), }
def __init__(self, args): self.args = args self.pos = (0, 0) self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { '1': c.Button((self.args.size[0] // 2, 300), (600, 80), '1 v 1', font=('src', 'timesnewroman.ttf', 25), align=(1, 1), background=(210, 210, 210)), '2': c.Button((self.args.size[0] // 2, 400), (600, 80), '2 v 2', font=('src', 'timesnewroman.ttf', 25), align=(1, 1), background=(210, 210, 210)), 'back': c.Button((self.args.size[0] // 2, 500), (600, 80), 'Back', font=('src', 'timesnewroman.ttf', 25), align=(1, 1), background=(210, 210, 210)), }
def __init__(self, args): self.args = args self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { 'new': c.Button((self.args.size[0] // 2, 310), (600, 80), 'New Game', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), 'join': c.Button((self.args.size[0] // 2, 410), (600, 80), 'Join Game', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), 'replay': c.Button((self.args.size[0] // 2, 510), (600, 80), 'Replay', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), 'quit': c.Button((self.args.size[0] // 2, 610), (600, 80), 'Exit', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), }
def __init__(self, args): self.args = args self.pos = (0, 0) self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { 'new': c.Button((self.args.size[0] // 2, 300), (600, 80), 'New Game', font=('src', 'timesnewroman.ttf', 25), align=(1, 1), background=(210, 210, 210)), 'save': c.Button((self.args.size[0] // 2, 400), (600, 80), 'Load Saved Game', font=('src', 'timesnewroman.ttf', 25), align=(1, 1), background=(210, 210, 210)), 'replay': c.Button((self.args.size[0] // 2, 500), (600, 80), 'Replay', font=('src', 'timesnewroman.ttf', 25), align=(1, 1), background=(210, 210, 210)), 'quit': c.Button((self.args.size[0] // 2, 600), (600, 80), 'Exit', font=('src', 'timesnewroman.ttf', 25), align=(1, 1), background=(210, 210, 210)), }
def __init__(self, args): self.args = args # display self.bar_height = 100 self.margin = 40 self.padding = 20 self.background = c.Component(lambda ui: ui.show_div((0, 0), self.args.size, color=(60, 179, 113))) self.pan = 0 # saves and buttons self.saves = [ SavedFile(self.args, file[:-4]) for file in os.listdir(os.path.join(self.args.save_path, 'replay')) if file.endswith('.gnr') ] self.saves.sort(key=lambda sf: [not sf.err, re.split(r'[\-_]', sf.date), sf.name], reverse=True) self.buttons = { 'open-location': c.Button( (self.args.size[0] - 100, 100), (120, 60), 'open location', font=f.tnr(15), save='tnr-15', align=(1, 1), background=(210, 210, 210)), 'back': c.Button( (self.args.size[0] // 2, self.args.size[1] - self.bar_height // 2), (200, 50), 'back', font=f.tnr(22), save='tnr-22', align=(1, 1), background=(210, 210, 210)) }
def __init__(self, args, replay): self.args = args self.replay = replay self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) # game & game menu self.game = g_r.Game(self.args, self.replay) self.ended = False
def __init__(self, args, mode, id, client): self.args = args self.mode = mode # {'num-players', 'size', 'threshold', 'max-apples'} self.id = id self.client = client self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.game = g.Game(self.args, self.mode, self.id, self.client) self.game_menu = gm.GameMenu(self.args, self.args.get_pos(1, 1), align=(1, 1))
def __init__(self, args, mode): self.args = args self.mode = mode self.threshold = 42 self.pos = (0, 0) self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { 'p1': c.Button((self.args.size[0] // 2 - 280, 260), (360, 180), 'Human', font=('src', 'timesnewroman.ttf', 30), align=(1, 1), background=(255, 204, 204)), 'p2': c.Button((self.args.size[0] // 2 + 280, 260), (360, 180), 'Human', font=('src', 'timesnewroman.ttf', 30), align=(1, 1), background=(204, 204, 255)), '-': c.Button((self.args.size[0] // 2 - 300, 460), (50, 70), '', font=('src', 'timesnewroman.ttf', 25), align=(0, 1), background=(210, 210, 210)), 'threshold': c.Button((self.args.size[0] // 2, 460), (470, 70), f'Red\'s winning threshold: {self.threshold}', font=('src', 'timesnewroman.ttf', 22), align=(1, 1), background=(210, 210, 210)), '+': c.Button((self.args.size[0] // 2 + 300, 460), (50, 70), '', font=('src', 'timesnewroman.ttf', 25), align=(2, 1), background=(210, 210, 210)), 'play': c.Button((self.args.size[0] // 2, 550), (600, 70), 'Play', font=('src', 'timesnewroman.ttf', 25), align=(1, 1), background=(210, 210, 210)), 'back': c.Button((self.args.size[0] // 2, 640), (600, 70), 'Back', font=('src', 'timesnewroman.ttf', 25), align=(1, 1), background=(210, 210, 210)), }
def __init__(self, args): # arguments self.args = args # server and client self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) print(socket.gethostname()) self.ip = '127.0.0.1' try: ips = [ ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if utils.is_private_ip(ip) ] self.ip = ips[0] if len(ips) > 0 else '127.0.0.1' except socket.gaierror as e: print(e) self.server.bind((self.ip, 5051)) self.server.settimeout(1.1) self.server.listen() self.clients = [] # {ip, port, client} self.status = {'running': True} self.player_colors = cl.get_player_colors() self.thread = Thread(target=self.add_clients(self.status), name='add-clients', daemon=True) self.thread.start() # gui self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { 'play': c.Button((self.args.size[0] // 2 - 300, 620), (300, 60), 'start game', font=f.tnr(23), save='tnr-23', align=(1, 1), background=(210, 210, 210)), 'back': c.Button((self.args.size[0] // 2 + 300, 620), (300, 60), 'close room', font=f.tnr(23), save='tnr-23', align=(1, 1), background=(210, 210, 210)), }
def __init__(self, args, mode): # arguments self.args = args self.mode = mode # server and client self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) hostname = socket.gethostname() if not hostname.endswith('.local'): hostname += '.local' fqdn_hostname = socket.getfqdn() if not fqdn_hostname.endswith('.local'): fqdn_hostname += '.local' try: all_ips = socket.gethostbyname_ex(hostname)[2] except: all_ips = socket.gethostbyname_ex(fqdn_hostname)[2] ips = [ ip for ip in all_ips if utils.is_private_ip(ip) ] self.ip = ips[0] if len(ips) > 0 else '127.0.0.1' self.server.bind((self.ip, 5050)) self.server.settimeout(1.0) self.server.listen() self.clients = [] # {ip, port, client} self.status = {'running': True} self.thread = Thread(target=self.add_clients(self.status), name='add-clients', daemon=True) self.thread.start() # gui self.background = c.Component(lambda ui: ui.show_div((0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { 'play': c.Button( (self.args.size[0] // 2 - 300, 600), (300, 60), 'start game', font=f.tnr(23), align=(1, 1), background=(210, 210, 210) ), 'back': c.Button( (self.args.size[0] // 2 + 300, 600), (300, 60), 'close room', font=f.tnr(23), align=(1, 1), background=(210, 210, 210) ), }
def __init__(self, args, room_name, address, server=None): # arguments self.args = args self.mode = None self.room_name = room_name self.displayed_room_name = (self.room_name[:17] + '......') if len(self.room_name) > 20 else self.room_name self.server_ip = address[0] self.port = address[1] self.server = server # client self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.client.settimeout(0.3) self.client.connect((self.server_ip, self.port)) # receive from server print('CLIENT ENTER room...') self.status = 'wait' self.id = None self.ip_list = [] self.thread = Thread(target=self.wait_info, name='wait-info') self.thread.start() # gui self.background = c.Component(lambda ui: ui.show_div((0, 0), self.args.size, color=(60, 179, 113))) if self.server is None: self.buttons = { 'back': c.Button( (self.args.size[0] // 2, 630), (600, 80), 'Exit Room', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), } else: self.buttons = { 'play': c.Button( (self.args.size[0] // 2 - 300, 630), (300, 60), 'Play', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), 'back': c.Button( (self.args.size[0] // 2 + 300, 630), (300, 60), 'Close Room', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), } self.player_colors = cl.get_player_colors()
def __init__(self, args): self.args = args self.background = c.Component(lambda ui: ui.show_div((0, 0), self.args.size, color=(60, 179, 113))) self.btn_slots = [ (self.args.size[0] // 2, 150), (self.args.size[0] // 2, 270), (self.args.size[0] // 2, 390), (self.args.size[0] // 2, 510) ] self.beacon = DiscoveryBeacon('224.0.3.101', list(range(5000, 5100)), b'battleofsnakes') self.beacon.start() self.rooms = [] self.buttons = { 'back': c.Button( (self.args.size[0] // 2, 650), (400, 60), 'Back', font=f.tnr(25), save='tnr-25', align=(1, 1), background=(210, 210, 210)), } self.refresh() self.timer = sw.Stopwatch() self.timer.start()
def __init__(self, args, mode): self.args = args self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) # mode and game self.mode = mode self.game = None if mode['mode'] == 'mult' and mode['connect']['identity'] == 'client': import back.sprites.game_client as g else: import back.sprites.game as g with open(os.path.join('levels', f'{self.mode["level"]}.json')) as file: self.game = g.Game(self.args, self.mode, json.load(file)) # others self.game_menu = gm.GameMenu( (self.args.size[0] // 2, self.args.size[1] // 2), align=(1, 1)) self.saver = s.Saver(self.args, msg=self.game.name) self.score_board = sb.ScoreBoard( (self.args.size[0] // 2, self.args.size[1] // 2), align=(1, 1))
def __init__(self, args, mode): self.args = args self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) # mode and game self.mode = mode self.game = None if self.is_client(): import back.sprites.game_client as g else: import back.sprites.game as g with open( os.path.join(self.args.path, 'levels', f'{self.mode["level"]}.json')) as file: self.game = g.Game(self.args, self.mode, json.load(file)) # others self.game_menu = gm.GameMenu( (self.args.size[0] // 2, self.args.size[1] // 2), align=(1, 1)) self.score_board = sb.ScoreBoard( (self.args.size[0] // 2, self.args.size[1] // 2), align=(1, 1))
def __init__(self, args, mode): self.args = args self.bar_height = 100 self.mode = mode self.margin = 40 self.padding = 20 self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.saves = [ SavedFile(file[:file.index('.')], self.mode, (800, 120)) for file in os.listdir(os.path.join('.', self.mode)) if file.endswith('.cns' if self.mode == 'save' else '.cnsr') ] self.pan = 0 self.button = c.Button( (self.args.size[0] // 2, self.args.size[1] - self.bar_height // 2), (200, 50), 'back', font=('src', 'timesnewroman.ttf', 22), align=(1, 1), background=(210, 210, 210))
def __init__(self, args, mode): # arguments self.args = args self.mode = mode # server and client self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.ip = [ ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if utils.is_private_ip(ip) ][0] self.server.bind((self.ip, 5050)) self.server.settimeout(1.0) self.server.listen() self.clients = [] # {ip, port, client} self.status = {'running': True} self.thread = Thread(target=self.add_clients(self.status), name='add-clients', daemon=True) self.thread.start() # gui self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { 'play': c.Button((self.args.size[0] // 2 - 300, 600), (300, 60), 'start game', font=f.tnr(23), align=(1, 1), background=(210, 210, 210)), 'back': c.Button((self.args.size[0] // 2 + 300, 600), (300, 60), 'close room', font=f.tnr(23), align=(1, 1), background=(210, 210, 210)), }
def __init__(self, args, mode, player_mode, threshold, file=None): self.args = args self.pos = (0, 0) self.background = c.Component(lambda ui: ui.show_div( (0, 0), self.args.size, color=(60, 179, 113))) # game self.mode = None self.player_mode = player_mode self.game = None if mode in ['1', '2']: self.mode = mode self.game = g.Game(self.args, self.mode, self.player_mode, threshold) self.game.prepare() elif mode == 'save': self.mode = file.mode self.game = file elif mode == 'replay': self.mode = file.mode self.game = r.Replay(self.args, self.mode, self.player_mode, threshold) self.game.prepare(log=file) self.saver = s.Saver(self.args, msg=self.game.name)
def __init__(self, args): self.args = args self.background = c.Component(lambda ui: ui.show_div((0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { 'new': c.Button( (self.args.size[0] // 2, 360), (600, 80), 'New Game', font=f.tnr(25), align=(1, 1), background=(210, 210, 210) ), 'join': c.Button( (self.args.size[0] // 2, 460), (600, 80), 'Join Game', font=f.tnr(25), align=(1, 1), background=(210, 210, 210) ), 'quit': c.Button( (self.args.size[0] // 2, 560), (600, 80), 'Exit', font=f.tnr(25), align=(1, 1), background=(210, 210, 210) ), 'reload_settings': c.Button( (self.args.size[0] - 140, self.args.size[1] - 3), (280, 30), '>> Reload settings <<', font=f.get_font('courier-prime', 20), align=(1, 2), color=(None, (255, 255, 255)), background=None, border=1 ) } self.settings_reloaded = False self.timer = sw.Stopwatch()
def __init__(self, args): self.args = args self.pos = (0, 0) self.background = c.Component(lambda ui: ui.show_div((0, 0), self.args.size, color=(60, 179, 113))) self.buttons = { '1': c.Button((240, 250), (80, 120), '1', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), '2': c.Button((440, 250), (80, 120), '2', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), '3': c.Button((640, 250), (80, 120), '3', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), '4': c.Button((840, 250), (80, 120), '4', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), '5': c.Button((1040, 250), (80, 120), '5', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), '6': c.Button((240, 400), (80, 120), '6', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), '7': c.Button((440, 400), (80, 120), '7', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), '8': c.Button((640, 400), (80, 120), '8', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), '9': c.Button((840, 400), (80, 120), '9', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), '10': c.Button((1040, 400), (80, 120), '10', font=f.tnr(25), align=(1, 1), background=(210, 210, 210)), 'mode': c.Button( (self.args.size[0] // 2, 540), (200, 60), 'single player', font=f.tnr(20), align=(1, 1), background=(210, 210, 210) ), 'back': c.Button( (self.args.size[0] // 2, 640), (600, 80), 'Back', font=f.tnr(25), align=(1, 1), background=(210, 210, 210) ), }