diff --git a/README b/README index 9636bc9..75ef6be 100644 --- a/README +++ b/README @@ -19,8 +19,8 @@ v0.9: --- Requirements --- -python 2.4 (or higher) http://www.python.org/ -python-pygame 1.7.1 (or higher) http://www.pygame.org/ +python 3.0 (or higher) http://www.python.org/ +python-pygame 1.9.2 (or higher) http://www.pygame.org/ --- Installation --- diff --git a/setup.py b/setup.py index 92072b6..db6b69d 100755 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # Copyright (C) 2010 Ryan Kavanagh # # Slingshot is free software; you can redistribute it and/or modify diff --git a/src/bin/slingshot b/src/bin/slingshot index 68185fb..6a44b89 100644 --- a/src/bin/slingshot +++ b/src/bin/slingshot @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 # This file is part of Slingshot. # # Slingshot is a two-dimensional strategy game where two players attempt to shoot one @@ -37,7 +37,7 @@ import math import os import sys import time -import thread +import _thread as thread from random import randint @@ -349,10 +349,10 @@ class Game: def create_particlesystem(self, pos, n, size): if Settings.PARTICLES: if Settings.BOUNCE: - nn = n / 2 + nn = n // 2 else: nn = n - for i in xrange(nn): + for i in range(nn): self.particlesystem.add(Particle(pos, size)) def create_planets(self, planetlist=None): @@ -361,13 +361,13 @@ class Game: if planetlist is None: if Settings.MAX_BLACKHOLES > 0: n = randint(1, Settings.MAX_BLACKHOLES) - for i in xrange(n): + for i in range(n): result.add(Blackhole(result, self.background)) else: # Only have planets if we don't have any # blackholes. n = randint(2, Settings.MAX_PLANETS) - for i in xrange(n): + for i in range(n): result.add(Planet(result, self.background)) else: for p in planetlist: @@ -414,7 +414,7 @@ class Game: zoom_screen.blit(normal_screen, (200, 150)) missilesprite = self.missile.get_image() - missilesprite = pygame.transform.scale(missilesprite, (missilesprite.get_size()[0] / 3, missilesprite.get_size()[1] / 3)) + missilesprite = pygame.transform.scale(missilesprite, (missilesprite.get_size()[0] // 3, missilesprite.get_size()[1] // 3)) pos = self.missile.get_pos() pos = (200 + pos[0] / 4 - missilesprite.get_width() / 2, 150 + pos[1] / 4 - missilesprite.get_height() / 2) zoom_screen.blit(missilesprite, pos) @@ -698,7 +698,7 @@ class Game: offset1 = 0 power_penalty = self.missile.get_score() - for i in xrange(1, 3): + for i in range(1, 3): if self.players[i].shot: if self.player == 3 - i: message = "Player %d killed self" % (i) @@ -939,7 +939,7 @@ class Game: if not os.path.exists(path): os.mkdir(path) path += "/settings" - f = file(path, 'wt') + f = open(path, 'wt') if self.bounce: f.write("Bounce: 1\n") else: @@ -1092,11 +1092,11 @@ class Game: self.net.close() return ret - def use_fullscreen(self): - pygame.display.set_mode((0, 0), pygame.FULLSCREEN | pygame.NOFRAME) + def use_fullscreen(self): + pygame.display.set_mode((0, 0), pygame.FULLSCREEN | pygame.NOFRAME) - def use_window(self): - pygame.display.set_mode((800, 600)) + def use_window(self): + pygame.display.set_mode((800, 600)) def main(): diff --git a/src/slingshot/general.py b/src/slingshot/general.py index e6b6c22..ddea114 100644 --- a/src/slingshot/general.py +++ b/src/slingshot/general.py @@ -30,12 +30,12 @@ from slingshot.settings import Settings def load_image(name, colorkey=None): - fullname = os.path.join(Settings.DATA_PATH, name) + fullname = os.path.join(Settings.DATA_PATH, name) try: image = pygame.image.load(fullname) - except pygame.error, message: - print 'Cannot load image:', fullname - raise SystemExit, message + except pygame.error as message: + print('Cannot load image:', fullname) + raise SystemExit(message) image = image.convert_alpha() if colorkey is not None: if colorkey is -1: @@ -87,7 +87,7 @@ def get_intersect(center, r, pos1, pos2): return pos def get_data_path(file): - return os.path.join(Settings.DATA_PATH, file) + return os.path.join(Settings.DATA_PATH, file) def prep_text(text, antialias, font, linespacing, color): ''' diff --git a/src/slingshot/inputbox.py b/src/slingshot/inputbox.py index e6012e7..1dd22f5 100644 --- a/src/slingshot/inputbox.py +++ b/src/slingshot/inputbox.py @@ -33,7 +33,7 @@ def __init__(self, screen, question): self.screen = screen self.question = question self.new_str = [] - self.input_box(question + ": " + string.join(self.new_str,"")) + self.input_box(question + ": " + "".join(self.new_str)) def input_box(self, msg): pygame.draw.rect(self.screen, (0,0,0), @@ -61,8 +61,8 @@ def ask(self): return False elif key <= 127 and len(self.new_str) < 19: self.new_str.append(chr(key)) - self.input_box(self.question + ": " + string.join(self.new_str,"")) - return string.join(self.new_str,"") + self.input_box(self.question + ": " + "".join(self.new_str)) + return "".join(self.new_str) def get_key(self): while 1: diff --git a/src/slingshot/menu.py b/src/slingshot/menu.py index 04a6d24..7b1e30e 100644 --- a/src/slingshot/menu.py +++ b/src/slingshot/menu.py @@ -35,10 +35,10 @@ def __init__(self, name, dim = True, copyright = False): self.dim = dim self.count = 0 self.inc = 15 - self.copyright = copyright + self.copyright = copyright def change_active(self, item, a): - for i in xrange(0, self.items.__len__()): + for i in range(0, self.items.__len__()): if self.items[i][0] == item: self.items[i] = (self.items[i][0], self.items[i][1], self.items[i][2], a) @@ -100,8 +100,8 @@ def draw(self): result = pygame.Surface((w, h)) #result.fill((100,0,0)) result.blit(Settings.menu_background, (0,0)) - if self.copyright: - for line, (x, y) in prep_text([ + if self.copyright: + for line, (x, y) in prep_text([ "Slingshot is:", " Copyright (C) 2007 Jonathan Musther ", " Copyright (C) 2007 Bart Mak", @@ -123,14 +123,13 @@ def draw(self): "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA", "", "http://github.com/ryanakca/slingshot"], - True, Settings.fineprint, 0, (10, 10, 10)): - # We want our text to start at 20px from the left - # side and 305 px from the top. - result.blit(line, (20, 305 + y)) - version = Settings.fineprint.render( - 'Version '+ Settings.VERSION, True, - (230, 230, 230)) - result.blit(version, (345 - version.get_width(), 3)) + True, Settings.fineprint, 0, (10, 10, 10)): + # We want our text to start at 20px from the left + # side and 305 px from the top. + result.blit(line, (20, 305 + y)) + version = Settings.fineprint.render( + 'Version '+ Settings.VERSION, True, (230, 230, 230)) + result.blit(version, (345 - version.get_width(), 3)) txt = Settings.menu_font.render(self.name, 1, (255,255,255)) rect = txt.get_rect() @@ -138,7 +137,7 @@ def draw(self): result.blit(txt, rect.topleft) n = self.items.__len__() - for i in xrange(0, n): + for i in range(0, n): if i == self.selected: color = (self.count,self.count,255) else: @@ -191,24 +190,23 @@ class Welcome(Menu): def __init__(self): Menu.__init__(self, "") - self.img = pygame.surface.Surface((600, 300), pygame.SRCALPHA) + self.img = pygame.surface.Surface((600, 300), pygame.SRCALPHA) self.choice = "" - # header font - hfont = pygame.font.Font(get_data_path("FreeSansBold.ttf"), 40) - header = prep_text(["Welcome to Slingshot!"], True, hfont, - 0, (255, 255, 255))[0] - self.img.blit(header[0], - ((self.img.get_width() - header[1][0]) / 2, 0) - ) - # Instructions font - ifont = pygame.font.Font(get_data_path("FreeSansBold.ttf"), 15) - instructions = prep_text( - ["Press space to play or escape for the menu and help!"], - True, ifont, 0, (255, 255, 255))[0] - self.img.blit(instructions[0], - ((self.img.get_width() - instructions[1][0]) / 2, 60) - ) - for line, (x, y) in prep_text([ + # header font + hfont = pygame.font.Font(get_data_path("FreeSansBold.ttf"), 40) + header = prep_text(["Welcome to Slingshot!"], True, hfont, 0, (255, 255, 255))[0] + self.img.blit(header[0], + ((self.img.get_width() - header[1][0]) / 2, 0) + ) + # Instructions font + ifont = pygame.font.Font(get_data_path("FreeSansBold.ttf"), 15) + instructions = prep_text( + ["Press space to play or escape for the menu and help!"], + True, ifont, 0, (255, 255, 255))[0] + self.img.blit(instructions[0], + ((self.img.get_width() - instructions[1][0]) / 2, 60) + ) + for line, (x, y) in prep_text([ "Slingshot is:", " Copyright (C) 2007 Jonathan Musther ", " Copyright (C) 2007 Bart Mak", @@ -326,7 +324,7 @@ def draw(self): rect.midtop = (w / 2, Settings.MENU_LINEFEED + offset) result.blit(txt, rect.topleft) - for i in xrange(0, 2): + for i in range(0, 2): if i == self.selected: color = (self.count,self.count,255) else: diff --git a/src/slingshot/network.py b/src/slingshot/network.py index 6af5775..8ea6d64 100644 --- a/src/slingshot/network.py +++ b/src/slingshot/network.py @@ -42,7 +42,7 @@ def wait_for_cnct(self): af, socktype, proto, canonname, sa = res try: connect_s = socket.socket(af, socktype, proto) - except socket.error, msg: + except socket.error as msg: connect_s = None continue try: @@ -50,12 +50,12 @@ def wait_for_cnct(self): connect_s.bind(sa) connect_s.listen(1) connect_s.settimeout(2) - except socket.error, msg: + except socket.error as msg: connect_s.close() connect_s = None continue break - except socket.error, msg: + except socket.error as msg: connect_s = None if connect_s is None: @@ -77,18 +77,18 @@ def cnct(self, hostname): af, socktype, proto, canonname, sa = res try: self.s = socket.socket(af, socktype, proto) - except socket.error, msg: + except socket.error as msg: self.s = None continue try: self.s.settimeout(3) self.s.connect(sa) - except socket.error, msg: + except socket.error as msg: self.s.close() self.s = None continue break - except socket.error, msg: + except socket.error as msg: self.s = None if self.s is None: diff --git a/src/slingshot/particle.py b/src/slingshot/particle.py index cd875bb..1541663 100644 --- a/src/slingshot/particle.py +++ b/src/slingshot/particle.py @@ -32,7 +32,7 @@ class Particle(pygame.sprite.Sprite): def __init__(self, pos = (0.0, 0.0), size = 10): - ''' Initialize the particle. ''' + ''' Initialize the particle. ''' pygame.sprite.Sprite.__init__(self) if size == 5: self.image = Settings.particle_image5 @@ -59,7 +59,7 @@ def max_flight(self): return False def update(self, planets): - """ + """ Updates information about ourselves, namely our location. @param planets: list of planets @@ -70,7 +70,7 @@ def update(self, planets): 1 otherwise @rtype: int - """ + """ self.flight = self.flight - 1 self.last_pos = self.pos @@ -78,20 +78,20 @@ def update(self, planets): for p in planets: p_pos = p.get_pos() mass = p.get_mass() - dx = self.pos[0] - p_pos[0] - dy = self.pos[1] - p_pos[1] + dx = self.pos[0] - p_pos[0] + dy = self.pos[1] - p_pos[1] d = dx**2 + dy**2 - # a is the acceleration in pixels/tick - # -> [ G * m_p * \delta d_x G * m_p * \delta d_y ] - # a = [ ---------------------- , ---------------------- ] - # [ r ^ (1/3) r ^ (1/3) ] - try: - a = ((Settings.g * mass * dx) / (d * math.sqrt(d)), (Settings.g * mass * dy) / (d * math.sqrt(d))) - except ZeroDivisionError: - # Hackishly take any silly particles out of the game. - a = (10000, 10000) - # It's been a tick, update our velocity according to our - # acceleration + # a is the acceleration in pixels/tick + # -> [ G * m_p * \delta d_x G * m_p * \delta d_y ] + # a = [ ---------------------- , ---------------------- ] + # [ r ^ (1/3) r ^ (1/3) ] + try: + a = ((Settings.g * mass * dx) / (d * math.sqrt(d)), (Settings.g * mass * dy) / (d * math.sqrt(d))) + except ZeroDivisionError: + # Hackishly take any silly particles out of the game. + a = (10000, 10000) + # It's been a tick, update our velocity according to our + # acceleration self.v = (self.v[0] - a[0], self.v[1] - a[1]) self.pos = (self.pos[0] + self.v[0], self.pos[1] + self.v[1]) @@ -102,19 +102,19 @@ def update(self, planets): for p in planets: p_pos = p.get_pos() r = p.get_radius() - # d is not the distance from the planet, it's the distance squared. + # d is not the distance from the planet, it's the distance squared. d = (self.pos[0] - p_pos[0])**2 + (self.pos[1] - p_pos[1])**2 - if p.type == "Blackhole": - min_dist = p.get_mass() - if d <= min_dist: - self.impact_pos = p_pos - self.pos = self.impact_pos - return -1 - elif d <= (r)**2: - # This is a planet - self.impact_pos = get_intersect(p_pos, r, self.last_pos, self.pos) - self.pos = self.impact_pos - return 0 + if p.type == "Blackhole": + min_dist = p.get_mass() + if d <= min_dist: + self.impact_pos = p_pos + self.pos = self.impact_pos + return -1 + elif d <= (r)**2: + # This is a planet + self.impact_pos = get_intersect(p_pos, r, self.last_pos, self.pos) + self.pos = self.impact_pos + return 0 if Settings.BOUNCE: if self.pos[0] > 799: @@ -146,10 +146,10 @@ def in_range(self): return False def visible(self): - """ - Returns whether or not the particle is within the playing area. + """ + Returns whether or not the particle is within the playing area. - """ + """ if pygame.Rect(0, 0, 800, 600).collidepoint(self.pos): return True else: @@ -186,7 +186,7 @@ def launch(self, player): def update_players(self, players): result = 1 - for i in xrange(10): + for i in range(10): pos = (self.last_pos[0] + i * 0.1 * self.v[0], self.last_pos[1] + i * 0.1 * self.v[1]) if players[1].hit(pos): result = 0 @@ -215,9 +215,9 @@ def draw_status(self, screen): def update(self, planets, players): result = Particle.update(self, planets) result = result * self.update_players(players) - # Draws the missile's trajectory only if we haven't entered a black hole. - if result != -1: - pygame.draw.aaline(self.trail_screen, self.trail_color, self.last_pos, self.pos) + # Draws the missile's trajectory only if we haven't entered a black hole. + if result != -1: + pygame.draw.aaline(self.trail_screen, self.trail_color, self.last_pos, self.pos) return result def get_image(self): diff --git a/src/slingshot/planet.py b/src/slingshot/planet.py index a6da9db..7626506 100644 --- a/src/slingshot/planet.py +++ b/src/slingshot/planet.py @@ -31,10 +31,10 @@ from slingshot.general import * class Planet(pygame.sprite.Sprite): - """ A planet sprite """ + """ A planet sprite """ def __init__(self, planets, background, n=None, radius=None, mass=None, pos=None): - """ + """ Initialize a Planet. @param planets: list of Planets @@ -54,7 +54,7 @@ def __init__(self, planets, background, n=None, radius=None, mass=None, pos=None """ pygame.sprite.Sprite.__init__(self) - self.type = "Planet" + self.type = "Planet" if n == None and planets != None: unique = False @@ -75,8 +75,8 @@ def __init__(self, planets, background, n=None, radius=None, mass=None, pos=None positioned = False while not positioned: self.mass = randint(8,512) - # radius is between 25 and 100 when mass is - # between 8 and 512 + # radius is between 25 and 100 when mass is + # between 8 and 512 self.r = self.mass**(1.0/3.0) * 12.5 self.pos = (randint(Settings.PLANET_SHIP_DISTANCE + round(self.r), 800 - Settings.PLANET_SHIP_DISTANCE - round(self.r)), randint(Settings.PLANET_EDGE_DISTANCE + round(self.r), 600 - Settings.PLANET_EDGE_DISTANCE - round(self.r))) positioned = True @@ -129,15 +129,15 @@ def fade(self, f): # self.image.set_at((randint(0, round(self.r * 2)), randint(0, round(self.r * 2))), (0,0,0,0)) class Blackhole(Planet): - def __init__(self, planets, background, n=None, radius=None, mass=None, pos=None): + def __init__(self, planets, background, n=None, radius=None, mass=None, pos=None): pygame.sprite.Sprite.__init__(self) - self.type = "Blackhole" + self.type = "Blackhole" - self.image = pygame.surface.Surface((2, 2)) - self.image.fill((0,0,0)) - self.image.set_alpha(0) - self.rect = self.image.get_rect() + self.image = pygame.surface.Surface((2, 2)) + self.image.fill((0,0,0)) + self.image.set_alpha(0) + self.rect = self.image.get_rect() if n == None and planets != None: unique = False @@ -153,7 +153,7 @@ def __init__(self, planets, background, n=None, radius=None, mass=None, pos=None if radius == None or mass == None or pos == None: positioned = False while not positioned: - # We can't accurately represent blackholes in + # We can't accurately represent blackholes in # this game. According to my (feeble) # understanding of the Schwarzschild radius, to # have a radius of 1m and be a black hole, we'd @@ -162,7 +162,7 @@ def __init__(self, planets, background, n=None, radius=None, mass=None, pos=None # our largest planet. self.mass = randint(600, 700) self.r = 1 # radius - # Slightly more distance from the sides than + # Slightly more distance from the sides than # planets because of our massive gravit. # field. self.pos = (randint(3 * Settings.PLANET_SHIP_DISTANCE + round(self.r), @@ -179,10 +179,10 @@ def __init__(self, planets, background, n=None, radius=None, mass=None, pos=None self.r = radius self.pos = pos - self.orig = self.image - self.rect = self.orig.get_rect() + self.orig = self.image + self.rect = self.orig.get_rect() self.rect.center = self.pos - def fade(self, f): - """ Don't mess with our alpha, we're invilible! """ - pass + def fade(self, f): + """ Don't mess with our alpha, we're invilible! """ + pass diff --git a/src/slingshot/player.py b/src/slingshot/player.py index 2558f85..94cb71c 100644 --- a/src/slingshot/player.py +++ b/src/slingshot/player.py @@ -192,7 +192,7 @@ def draw_status(self, screen): def update_explosion(self): self.e = self.e + 1 - s = self.e * (6 - self.e) * 100 / 9 + s = self.e * (6 - self.e) * 100 // 9 if s >= 0: self.image = pygame.transform.scale(self.exp, (s,s)) pos = self.rect.center @@ -200,7 +200,7 @@ def update_explosion(self): self.rect.center = pos def draw_line(self, screen): - ''' Draws the aiming line out of the ship's gun. ''' + ''' Draws the aiming line out of the ship's gun. ''' (sx,sy) = self.get_launchpoint() pygame.draw.aaline(screen, self.color, (sx,sy), (sx + self.power * math.sin(math.radians(self.angle)), sy - self.power * math.cos(math.radians(self.angle)))) @@ -218,10 +218,10 @@ def draw(self, screen): # if img2 == 7: # f = f - 8.0 - print - print img1 - print img2 - print f + print() + print(img1) + print(img2) + print(f) rect1 = pygame.Rect(img1 * 40, 0, 40, 33) rect2 = pygame.Rect(img2 * 40, 0, 40, 33) diff --git a/src/slingshot/settings.py b/src/slingshot/settings.py index 799fa7b..4f3bd3e 100644 --- a/src/slingshot/settings.py +++ b/src/slingshot/settings.py @@ -26,7 +26,7 @@ class Settings: - VERSION = '0.9' + VERSION = '0.9' g = 120 # gravity MAXPOWER = 350 @@ -52,7 +52,7 @@ class Settings: MAX_FLIGHT = 750 MAX_PLANETS = 4 - MAX_BLACKHOLES = 0 + MAX_BLACKHOLES = 0 HITSCORE = 1500 SELFHIT = 2000 @@ -71,6 +71,6 @@ class Settings: MAX_ROUNDS = 0 - DATA_PATH = os.path.join(os.path.dirname(__file__), 'data/') + DATA_PATH = os.path.join(os.path.dirname(__file__), 'data/') - FULLSCREEN = False + FULLSCREEN = False