Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port from Python 2 to Python 3 #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python3
# Copyright (C) 2010 Ryan Kavanagh <[email protected]>
#
# Slingshot is free software; you can redistribute it and/or modify
Expand Down
26 changes: 13 additions & 13 deletions src/bin/slingshot
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -37,7 +37,7 @@ import math
import os
import sys
import time
import thread
import _thread as thread

from random import randint

Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand Down
10 changes: 5 additions & 5 deletions src/slingshot/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
'''
Expand Down
6 changes: 3 additions & 3 deletions src/slingshot/inputbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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:
Expand Down
60 changes: 29 additions & 31 deletions src/slingshot/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 <[email protected]>",
" Copyright (C) 2007 Bart Mak",
Expand All @@ -123,22 +123,21 @@ 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()
rect.midtop = (w / 2, Settings.MENU_LINEFEED)
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:
Expand Down Expand Up @@ -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 <[email protected]>",
" Copyright (C) 2007 Bart Mak",
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions src/slingshot/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ 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:
connect_s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
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:
Expand All @@ -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:
Expand Down
Loading