Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
build/
7 changes: 6 additions & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Slingshot v0.9
Slingshot v0.10

http://github.com/ryanakca/slingshot

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) ' +\
Expand All @@ -54,5 +54,6 @@
package_data={'slingshot':['data/*.png',
'data/*.ttf']},
package_dir={'slingshot':'src/slingshot'},
install_requires=['pygame', 'docopt'],
data_files=data_files,
)
68 changes: 56 additions & 12 deletions src/bin/slingshot
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@
# Copyright (C) 2009 Marcus Dreier <[email protected]>
# Copyright (C) 2010 Ryan Kavanagh <[email protected]>

"""Slingshot

Usage: slingshot [options] [--player1=<Player Name>] [--player2=<Player Name>]

-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 <Player Name>, --player1 <Player Name> Name of player 1
-2 <Player Name>, --player2 <Player Name> Name of player 2
"""

import pygame
from pygame.locals import *
import math
Expand All @@ -39,6 +53,8 @@ import sys
import time
import thread

from docopt import docopt

from random import randint

from slingshot.settings import *
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
13 changes: 8 additions & 5 deletions src/slingshot/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions src/slingshot/planet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions src/slingshot/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/slingshot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class Settings:

VERSION = '0.9'
VERSION = '0.10'

g = 120 # gravity
MAXPOWER = 350
Expand Down