From 7bd13996fbd4a45ac0875bb73719d13fed5a94ec Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Fri, 2 Mar 2018 00:13:23 +0700 Subject: Add command-line options for socket server --- brutalmaze/__init__.py | 2 -- brutalmaze/characters.py | 4 +-- brutalmaze/game.py | 78 +++++++++++++++++++++++++++++------------------- brutalmaze/settings.ini | 18 +++++------ setup.py | 2 +- wiki | 2 +- 6 files changed, 61 insertions(+), 45 deletions(-) diff --git a/brutalmaze/__init__.py b/brutalmaze/__init__.py index 46e1d6d..19bf3a7 100644 --- a/brutalmaze/__init__.py +++ b/brutalmaze/__init__.py @@ -1,3 +1 @@ """Brutal Maze is a minimalist hack and slash game with fast-paced action""" - -from .main import main diff --git a/brutalmaze/characters.py b/brutalmaze/characters.py index 4fb3b8f..070f697 100644 --- a/brutalmaze/characters.py +++ b/brutalmaze/characters.py @@ -268,8 +268,8 @@ class Enemy: def draw(self): """Draw the enemy.""" if get_ticks() < self.maze.next_move and not self.awake: return - radious = self.maze.distance/SQRT2 - self.awake*2 - square = regpoly(4, radious, self.angle, *self.get_pos()) + radius = self.maze.distance/SQRT2 - self.awake*2 + square = regpoly(4, radius, self.angle, *self.get_pos()) fill_aapolygon(self.maze.surface, square, self.get_color()) def update(self): diff --git a/brutalmaze/game.py b/brutalmaze/game.py index d6e46e8..41be931 100644 --- a/brutalmaze/game.py +++ b/brutalmaze/game.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Brutal Maze. If not, see . -__version__ = '0.5.5' +__version__ = '0.5.6' import re from argparse import ArgumentParser, FileType, RawTextHelpFormatter @@ -66,38 +66,38 @@ class ConfigReader: def parse(self): """Parse configurations.""" - self.server = self.config.getboolean('Server', 'Enable') - if self.server: - self.host = self.config.get('Server', 'Host') - self.port = self.config.getint('Server', 'Port') - self.headless = self.config.getboolean('Server', 'Headless') - else: - self.key, self.mouse = {}, {} - for cmd, alias in self.CONTROL_ALIASES: - i = self.config.get('Control', cmd) - if re.match('mouse[1-3]$', i.lower()): - if alias not in ('shot', 'slash'): - raise ValueError(self.WEIRD_MOUSE_ERR.format(cmd)) - self.mouse[alias] = int(i[-1]) - 1 - continue - if len(i) == 1: - self.key[alias] = ord(i.lower()) - continue - try: - self.key[alias] = getattr(pygame, 'K_{}'.format(i.upper())) - except AttributeError: - raise ValueError(self.INVALID_CONTROL_ERR.format(cmd, i)) - self.size = (self.config.getint('Graphics', 'Screen width'), self.config.getint('Graphics', 'Screen height')) self.opengl = self.config.getboolean('Graphics', 'OpenGL') self.max_fps = self.config.getint('Graphics', 'Maximum FPS') self.muted = self.config.getboolean('Sound', 'Muted') self.musicvol = self.config.getfloat('Sound', 'Music volume') + self.server = self.config.getboolean('Server', 'Enable') + self.host = self.config.get('Server', 'Host') + self.port = self.config.getint('Server', 'Port') + self.headless = self.config.getboolean('Server', 'Headless') + + if self.server: return + self.key, self.mouse = {}, {} + for cmd, alias in self.CONTROL_ALIASES: + i = self.config.get('Control', cmd) + if re.match('mouse[1-3]$', i.lower()): + if alias not in ('shot', 'slash'): + raise ValueError(self.WEIRD_MOUSE_ERR.format(cmd)) + self.mouse[alias] = int(i[-1]) - 1 + continue + if len(i) == 1: + self.key[alias] = ord(i.lower()) + continue + try: + self.key[alias] = getattr(pygame, 'K_{}'.format(i.upper())) + except AttributeError: + raise ValueError(self.INVALID_CONTROL_ERR.format(cmd, i)) def read_args(self, arguments): """Read and parse a ArgumentParser.Namespace.""" - for option in 'size', 'opengl', 'max_fps', 'muted', 'musicvol': + for option in ('size', 'opengl', 'max_fps', 'muted', 'musicvol', + 'server', 'host', 'port', 'headless'): value = getattr(arguments, option) if value is not None: setattr(self, option, value) @@ -107,7 +107,8 @@ class Game: def __init__(self, config): pygame.mixer.pre_init(frequency=44100) pygame.init() - if config.muted or config.headless: + self.headless = config.headless and config.server + if config.muted or self.headless: pygame.mixer.quit() else: pygame.mixer.music.load(MUSIC) @@ -127,7 +128,6 @@ class Game: else: self.server = self.sockinp = None - self.headless = config.headless # self.fps is a float to make sure floordiv won't be used in Python 2 self.max_fps, self.fps = config.max_fps, float(config.max_fps) self.musicvol = config.musicvol @@ -314,7 +314,8 @@ def main(): config.parse() # Parse command-line arguments - parser = ArgumentParser(formatter_class=RawTextHelpFormatter) + parser = ArgumentParser(usage='%(prog)s [options]', + formatter_class=RawTextHelpFormatter) parser.add_argument('-v', '--version', action='version', version='Brutal Maze {}'.format(__version__)) parser.add_argument( @@ -337,13 +338,29 @@ def main(): '-f', '--max-fps', type=int, metavar='FPS', help='the desired maximum FPS (fallback: {})'.format(config.max_fps)) parser.add_argument( - '--mute', '-m', action='store_true', default=None, + '--mute', '-m', action='store_true', default=None, dest='muted', help='mute all sounds (fallback: {})'.format(config.muted)) parser.add_argument('--unmute', action='store_false', dest='muted', help='unmute sound') parser.add_argument( '--music-volume', type=float, metavar='VOL', dest='musicvol', help='between 0.0 and 1.0 (fallback: {})'.format(config.musicvol)) + parser.add_argument( + '--server', action='store_true', default=None, + help='enable server (fallback: {})'.format(config.server)) + parser.add_argument('--no-server', action='store_false', dest='server', + help='disable server') + parser.add_argument( + '--host', help='host to bind server to (fallback: {})'.format(config.host)) + parser.add_argument( + '--port', type=int, + help='port for server to listen on (fallback: {})'.format(config.port)) + parser.add_argument( + '--head', action='store_false', default=None, dest='headless', + help='run server with graphics and sound (fallback: {})'.format( + not config.headless)) + parser.add_argument('--headless', action='store_true', + help='run server without graphics or sound') args = parser.parse_args() if args.defaultcfg is not None: with open(SETTINGS) as settings: args.defaultcfg.write(settings.read()) @@ -351,9 +368,10 @@ def main(): exit() # Manipulate config - if args.config: config.config.read(args.config) + if args.config: + config.config.read(args.config) + config.parse() config.read_args(args) - config.parse() # Main loop with Game(config) as game: diff --git a/brutalmaze/settings.ini b/brutalmaze/settings.ini index 67357a0..cd31696 100644 --- a/brutalmaze/settings.ini +++ b/brutalmaze/settings.ini @@ -1,16 +1,8 @@ -[Server] -# Enabling remote control will disable control via keyboard and mouse. -Enable: no -Host: localhost -Port: 8089 -# Disable graphics and sounds (only if socket server is enabled). -Headless: no - [Graphics] Screen width: 640 Screen height: 480 # OpenGL should be supported on all machines with hardware acceleration. -OpenGL: no +OpenGL: yes # FPS should not be greater than refresh rate. Maximum FPS: 60 @@ -34,3 +26,11 @@ Move up: Up Move down: Down Long-range attack: Mouse1 Close-range attack: Mouse3 + +[Server] +# Enabling remote control will disable control via keyboard and mouse. +Enable: no +Host: localhost +Port: 8089 +# Disable graphics and sound (only if socket server is enabled). +Headless: no diff --git a/setup.py b/setup.py index 9b3e3da..91f480c 100755 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ with open('README.rst') as f: setup( name='brutalmaze', - version='0.5.5', + version='0.5.6', description='A minimalist hack and slash game with fast-paced action', long_description=long_description, url='https://github.com/McSinyx/brutalmaze', diff --git a/wiki b/wiki index 34af1cf..91e65bd 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit 34af1cf8b3e3ea8272d6793a794484a239794d50 +Subproject commit 91e65bda2aaf4563623b1faeae0afbf2f5840420 -- cgit 1.4.1