diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3ae86b1 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,24 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 4 + +[*.py] +indent_style = tab +indent_size = 8 + +[slingshot] +indent_style = tab +indent_size = 8 + +# Tab indentation (no size specified) +[Makefile*] +indent_style = tab + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0fe2c40 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +build/ diff --git a/README b/README index 9636bc9..b716457 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Slingshot v0.9 +Slingshot v0.10 http://github.com/ryanakca/slingshot @@ -12,6 +12,11 @@ at http://github.com/ryanakca/slingshot See http://github.com/ryanakca/slingshot/commits/master for a detailed listing. +v0.10: + - Fixed Planets in Network game + - Introduce command line options + - Introduced option to change player names + v0.9: - Fix deprecation Python warnings and errors - Introduce networking support diff --git a/setup.py b/setup.py index 92072b6..15d7e0c 100755 --- a/setup.py +++ b/setup.py @@ -44,7 +44,7 @@ ] setup(name='slingshot', - version='0.9', + version='0.10', description='Simple 2D shooting strategy game set in space, with gravity', author='See README', license='GNU General Public License version 2, or (at your option) ' +\ @@ -54,5 +54,6 @@ package_data={'slingshot':['data/*.png', 'data/*.ttf']}, package_dir={'slingshot':'src/slingshot'}, + install_requires=['pygame', 'docopt'], data_files=data_files, ) diff --git a/src/bin/slingshot b/src/bin/slingshot index f3e0cf8..5973966 100644 --- a/src/bin/slingshot +++ b/src/bin/slingshot @@ -31,6 +31,20 @@ # Copyright (C) 2009 Marcus Dreier # Copyright (C) 2010 Ryan Kavanagh +"""Slingshot + +Usage: slingshot [options] [--player1=] [--player2=] + +-d, --debug Debug mode. More verbose output and separate logfiles + for multiple instances on one computer. +-v, --version Show version number +-h, --help Show this help +-f, --fullscreen Force fullscreen mode. (Otherwise use the saved setting) + +-1 , --player1 Name of player 1 +-2 , --player2 Name of player 2 +""" + import pygame from pygame.locals import * import math @@ -39,6 +53,8 @@ import sys import time import thread +from docopt import docopt + from random import randint from slingshot.settings import * @@ -66,7 +82,10 @@ class Game: Settings.round_font = pygame.font.Font(get_data_path("FreeSansBold.ttf"), 100) Settings.fineprint = pygame.font.Font(get_data_path("FreeSansBold.ttf"), 8) - def __init__(self): + def __init__(self, debug = False, player_names = [None, None]): + self.debug = debug + self.player_names = player_names + pygame.display.init() self.clock = pygame.time.Clock() @@ -100,7 +119,7 @@ class Game: self.background, r = load_image("backdrop.png") - self.players = (Dummy(), Player(1), Player(2)) + self.players = (Dummy(), Player(1, self.player_names[0]), Player(2, self.player_names[1])) self.playersprites = pygame.sprite.RenderPlain((self.players[1], self.players[2])) self.missile = Missile(self.trail_screen) self.missilesprite = pygame.sprite.RenderPlain((self.missile)) @@ -292,6 +311,10 @@ class Game: self.players[1].init(y_coordlist[0]) self.players[2].init(y_coordlist[1]) + # Overwrite network-game names for now + # TBD: revert when changing to single player + self.players[1].name = "Local Player" + self.players[2].name = "Remote Player" self.missile.flight = 0 @@ -319,6 +342,10 @@ class Game: self.show_planets = 0 if self.net_host: + # Overwrite network-game names for now + # TBD: revert when changing to single player + self.players[1].name = "Remote Player" + self.players[2].name = "Local Player" self.host_round_init() def toggle_menu(self): @@ -387,11 +414,14 @@ class Game: result.add(Planet(result, self.background)) else: for p in planetlist: + if self.debug: print("Planet num: {}/{}".format(p[0],Settings.MAX_PLANETS)) if p[0] > Settings.MAX_PLANETS: # Numbers above Settings.MAX_PLANETS are # allocated to blackholes. + if self.debug: print("Added Blackhole") result.add(Blackhole(None, self.background, p[0], p[1], p[2], p[3])) else: + if self.debug: print("Added Planet") result.add(Planet(None, self.background, p[0], p[1], p[2], p[3])) return result @@ -717,13 +747,14 @@ class Game: for i in xrange(1, 3): if self.players[i].shot: if self.player == 3 - i: - message = "Player %d killed self" %(i) + message = "{} killed self".format(self.players[i].get_name()) score = Settings.SELFHIT score_message = "%d deducted from score" %(score) self.players[i].add_score(-score) killed_self = True else: - message = "Player %d killed player %d" %((3 - i), i) + message = "{} killed {}".format( self.players[(3 - i)].get_name() + , self.players[i].get_name()) if self.players[3 - i].attempts == 1: bonus = Settings.QUICKSCORE1 elif self.players[3 - i].attempts == 2: @@ -815,7 +846,7 @@ class Game: winner = 0 Settings.font.set_bold(True) if winner != 0: - msg = Settings.font.render("Player %d has won the game" %(winner), 1, (255,255,255)) + msg = Settings.font.render("{} has won the game".format(self.players[winner].get_name()), 1, (255,255,255)) else: msg = Settings.font.render("The game has ended in a tie", 1, (255,255,255)) Settings.font.set_bold(False) @@ -1041,7 +1072,7 @@ class Game: if self.net_play(): self.net.close() - self.net = Network(3999) + self.net = Network(3999, debug = self.debug) while 1: # Menu changed - player want no network game anymore if self.menu != self.net_host_menu: @@ -1071,7 +1102,7 @@ class Game: if self.net_play(): self.net.close() - self.net = Network(3999) + self.net = Network(3999, debug = self.debug) if self.net.cnct(hostname) != False: packet = self.net.recv() @@ -1121,18 +1152,31 @@ class Game: def use_window(self): pygame.display.set_mode((800, 600)) -def main(): - +def main(arguments): + debug = arguments['--debug'] #sys.stdout = Blackhole() #sys.stderr = Blackhole() path = os.path.expanduser("~") + "/.slingshot" if not os.path.exists(path): os.mkdir(path) - path += "/logfile.txt" + if debug: + path += "/logfile-{}.txt".format(os.getpid()) + else: + path += "/logfile.txt" + + if debug: + print("My output goes to:\n{}".format(path)) + sys.stderr = open(path,"w") sys.stdout = sys.stderr - game = Game() + if arguments['--fullscreen']: + Settings.FULLSCREEN = True + if debug: print("DocOpt arguments {}".format(arguments)) + player_names = [arguments['--player1'], arguments['--player2']] + game = Game(debug = debug, player_names = player_names) game.run() -if __name__ == '__main__': main() +if __name__ == '__main__': + arguments = docopt(__doc__, version='Slingshot v{}'.format(Settings.VERSION)) + main(arguments) diff --git a/src/slingshot/network.py b/src/slingshot/network.py index 6af5775..5eedf75 100644 --- a/src/slingshot/network.py +++ b/src/slingshot/network.py @@ -31,7 +31,8 @@ class Network: - def __init__(self, port, buf_size = 4096): + def __init__(self, port, buf_size = 4096, debug = False): + self.debug = debug self.port = port self.buf_size = buf_size @@ -100,19 +101,21 @@ def cnct(self, hostname): self.r_stream = self.s.makefile('rb') def send(self, data): -# print(data) + if self.debug: print(data) try: pickle.dump(data ,self.w_stream, 1) self.w_stream.flush() - except: + except BaseException as be: + print(be) return False def recv(self): try: data = pickle.load(self.r_stream) -# print(data) + if self.debug: print(data) return data - except: + except BaseException as be: + print(be) return False def close(self): diff --git a/src/slingshot/planet.py b/src/slingshot/planet.py index a6da9db..84d75fb 100644 --- a/src/slingshot/planet.py +++ b/src/slingshot/planet.py @@ -58,9 +58,11 @@ def __init__(self, planets, background, n=None, radius=None, mass=None, pos=None if n == None and planets != None: unique = False - while not unique: + n = 0 + while n < 8 and not unique: unique = True - self.n = randint(1, 8) + n += 1 + self.n = n for p in planets: if self.n == p.get_n(): unique = False diff --git a/src/slingshot/player.py b/src/slingshot/player.py index 2558f85..4f0b28d 100644 --- a/src/slingshot/player.py +++ b/src/slingshot/player.py @@ -31,11 +31,12 @@ class Player(pygame.sprite.Sprite): - def __init__(self, n): + def __init__(self, n, name=None): pygame.sprite.Sprite.__init__(self) #call Sprite intializer self.player = n self.init() self.score = 0 + self.name = name def init(self, y_coord = None): self.power = 100 @@ -77,6 +78,12 @@ def init(self, y_coord = None): if Settings.FIXED_POWER: self.power = Settings.POWER + def get_name(self): + if self.name is None: + return "Player {}".format(self.player) + else: + return self.name + def reset_score(self): self.score = 0 @@ -181,11 +188,11 @@ def draw_info(self, screen): def draw_status(self, screen): if self.player == 1: - txt = Settings.font.render("Player 1 -- %d" %(self.score), 1, self.color) + txt = Settings.font.render("{} -- {}".format(self.get_name(), self.score), 1, self.color) rect = txt.get_rect() rect.topleft = (5,5) else: - txt = Settings.font.render("%d -- Player 2" %(self.score), 1, self.color) + txt = Settings.font.render("{} -- {}".format(self.score, self.get_name()), 1, self.color) rect = txt.get_rect() rect.topright = (794,5) screen.blit(txt, rect.topleft) diff --git a/src/slingshot/settings.py b/src/slingshot/settings.py index 799fa7b..2afa3a5 100644 --- a/src/slingshot/settings.py +++ b/src/slingshot/settings.py @@ -26,7 +26,7 @@ class Settings: - VERSION = '0.9' + VERSION = '0.10' g = 120 # gravity MAXPOWER = 350