changeset 632:0675f390653c

Initial port to Python 3 and Pygame 2.
author Simon Cross <hodgestar@gmail.com>
date Fri, 20 Jan 2023 20:01:06 +0100
parents 672e6e7ecfe9
children cd3514bf79b5
files skaapsteker/__main__.py skaapsteker/cutscene.py skaapsteker/dialogue.py skaapsteker/engine.py skaapsteker/gamestate.py skaapsteker/level.py skaapsteker/levelscene.py skaapsteker/menuscene.py skaapsteker/sound.py skaapsteker/sprites/enemies.py skaapsteker/sprites/player.py skaapsteker/utils.py skaapsteker/widgets/text.py
diffstat 13 files changed, 40 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/skaapsteker/__main__.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/__main__.py	Fri Jan 20 20:01:06 2023 +0100
@@ -6,7 +6,7 @@
 import optparse
 
 import pygame
-from pygame.locals import SWSURFACE
+from pygame.locals import SWSURFACE, FULLSCREEN
 
 from . import options
 from .constants import SCREEN
@@ -62,7 +62,7 @@
     pygame.display.init()
     pygame.font.init()
     soundsystem = SoundSystem(options['sound'])
-    pygame.display.set_mode(SCREEN, SWSURFACE)
+    pygame.display.set_mode(SCREEN, SWSURFACE | FULLSCREEN)
     pygame.display.set_icon(pygame.image.load(filepath('icons/program/icon_24.png')))
     pygame.display.set_caption("Nine Tales")
 
--- a/skaapsteker/cutscene.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/cutscene.py	Fri Jan 20 20:01:06 2023 +0100
@@ -29,7 +29,7 @@
         ChangeScene.post((MenuScene,))
 
     def dispatch(self, ev):
-        if ev.type is KEYDOWN:
+        if ev.type == KEYDOWN:
             if ev.key in(K_q, K_ESCAPE):
                 self.done()
         super(CutScene, self).dispatch(ev)
--- a/skaapsteker/dialogue.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/dialogue.py	Fri Jan 20 20:01:06 2023 +0100
@@ -25,8 +25,8 @@
         self.world = world
         self._me = getattr(self.world.npcs, name)
         self.states = AttrDict()
-        src = json.loads(data.load(json_filename).read(), encoding='utf-8')
-        for state, state_src in src.iteritems():
+        src = json.loads(data.load(json_filename).read())
+        for state, state_src in src.items():
             pseudo_path = [json_filename, state]
             self.states[state] = DsmState(state, state_src, pseudo_path)
         assert self.state in self.states, "DSM must have start state %r" % (self.state,)
--- a/skaapsteker/engine.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/engine.py	Fri Jan 20 20:01:06 2023 +0100
@@ -40,7 +40,7 @@
         while True:
             events = pygame.event.get()
             for ev in events:
-                if ev.type is QUIT:
+                if ev.type == QUIT:
                     return
                 if ChangeScene.matches(ev):
                     next_scene = ev.next_scene
@@ -99,7 +99,7 @@
 
     @classmethod
     def matches(cls, ev):
-        return ev.type is USEREVENT and ev.utype == cls.utype
+        return ev.type == USEREVENT and ev.utype == cls.utype
 
 
 class ChangeScene(UserEvent):
@@ -110,6 +110,7 @@
     def post(cls, next_scene):
         super(ChangeScene, cls).post(next_scene=next_scene)
 
+
 class PlayerDied(UserEvent):
 
     utype = "PLAYER_DIED"
--- a/skaapsteker/gamestate.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/gamestate.py	Fri Jan 20 20:01:06 2023 +0100
@@ -30,7 +30,7 @@
         self._data[key] = value
 
     def __iter__(self):
-        return self._data.iterkeys()
+        return self._data.keys()
 
     def __contains__(self, key):
         return key in self._data
@@ -55,7 +55,7 @@
         if game_file is None:
             game_file = self._game_file
         raw_data = open(game_file, "rb").read()
-        self.data = json.loads(raw_data, encoding='utf-8')
+        self.data = json.loads(raw_data)
         self.world = StateProxy(self.data, self)
 
     def new_game(self):
@@ -80,13 +80,14 @@
         if not os.path.exists(save_dir):
             try:
                 os.makedirs(save_dir)
-            except:
-                print "Cannot create save game directory."
+            except Exception:
+                print("Cannot create save game directory.")
                 return
         try:
-            json.dump(self.data, open(self._game_file, "wb"), indent=4)
-        except:
-            print "Cannot create save game file."
+            with open(self._game_file, "w") as f:
+                json.dump(self.data, f, indent=4)
+        except Exception:
+            print("Cannot create save game file.")
 
     def create_sprites(self, level):
         sprites = []
--- a/skaapsteker/level.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/level.py	Fri Jan 20 20:01:06 2023 +0100
@@ -177,7 +177,7 @@
                 break
         else:
             tile = self.get_debug_tile(x, y)
-            print "Debug tile at (%i, %i)" % (x, y)
+            print("Debug tile at (%i, %i)" % (x, y))
             self.tiles.add(tile)
             self.sprites.add(tile)
 
--- a/skaapsteker/levelscene.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/levelscene.py	Fri Jan 20 20:01:06 2023 +0100
@@ -53,7 +53,7 @@
         self._tofu = data.load_image('icons/tofu.png')
         self._scroll = data.load_image('icons/haiku-scroll.png')
         self._tails = {}
-        for tail in constants.FoxHud.TAIL_POSITIONS.iterkeys():
+        for tail in constants.FoxHud.TAIL_POSITIONS.keys():
             image = data.load_image('icons/tails/%s.png' % tail)
             grey_image = data.load_image('icons/tails/g_%s.png' % tail)
             self._tails[tail] = (grey_image, image)
@@ -120,7 +120,7 @@
                 }
 
     def _quit(self, pause=True):
-        import menuscene # avoid circular import
+        from . import menuscene  # avoid circular import
         if pause:
             engine.ChangeScene.post(menuscene.MenuScene(self.game_state, self._soundsystem))
         else:
@@ -146,7 +146,7 @@
             self._paused = True
 
     def _open_dialogue(self, npc):
-        if isinstance(npc, basestring):
+        if isinstance(npc, str):
             npc = self._npcs[npc]
         if npc.dsm.has_text():
             if self._dialogue is not None:
@@ -163,7 +163,7 @@
         self._dialogue = NotificationWidget(text)
 
     def _close_dialogue(self, npc):
-        if isinstance(npc, basestring):
+        if isinstance(npc, str):
             npc = self._npcs[npc]
         # below works for notifications too since they don't have .npc and send None
         if self._dialogue is not None and getattr(self._dialogue, 'npc', None) is npc:
@@ -257,7 +257,7 @@
                                   - fox_hud.TAILS_BG_MARGIN))
 
         # Draw tails
-        for tail, position in constants.FoxHud.TAIL_POSITIONS.iteritems():
+        for tail, position in constants.FoxHud.TAIL_POSITIONS.items():
             has_tail = tail in fox.tails
             tail_pos = pygame.Rect(self._clip_rect.left + fox_hud.TAILS_BG_MARGIN,
                                    self._clip_rect.top + fox_hud.TAIL_POSITIONS[tail],
@@ -312,7 +312,7 @@
             self._key_sequence = []
             return False
 
-        if ev.type is KEYUP:
+        if ev.type == KEYUP:
             if (not self._key_sequence
                 or ev.key != self._key_sequence[-1]
                 or (time.time() - self._last_keydown_time) > constants.DOUBLE_TAP_TIME):
@@ -321,7 +321,7 @@
             self._last_keyup_time = time.time()
             return False
 
-        if ev.type is KEYDOWN:
+        if ev.type == KEYDOWN:
             if self._last_keyup_time is None or (time.time() - self._last_keyup_time) > constants.DOUBLE_TAP_TIME:
                 self._key_sequence = []
             self._key_sequence.append(ev.key)
@@ -334,7 +334,7 @@
 
 
     def dispatch(self, ev):
-        if ev.type is KEYDOWN:
+        if ev.type == KEYDOWN:
 
             if self._player_dead:
                 if ev.key in self._restart_keys:
@@ -357,7 +357,7 @@
             if action is not None:
                 action()
 
-        elif ev.type is KEYUP:
+        elif ev.type == KEYUP:
             self._detect_key_sequence(ev)
 
             if ev.key in self._fast_key_map:
--- a/skaapsteker/menuscene.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/menuscene.py	Fri Jan 20 20:01:06 2023 +0100
@@ -45,7 +45,7 @@
         surface.blit(self._cursor, (cursor_x, cursor_y))
 
     def dispatch(self, ev):
-        if ev.type is KEYDOWN:
+        if ev.type == KEYDOWN:
             if ev.key in (K_q, K_ESCAPE):
                 pygame.event.post(pygame.event.Event(QUIT))
             elif ev.key == K_DOWN:
--- a/skaapsteker/sound.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/sound.py	Fri Jan 20 20:01:06 2023 +0100
@@ -29,7 +29,7 @@
                 test_sound.play()
                 self.sound_enabled = True
             except pygame.error:
-                print 'Unable to enable sound'
+                print('Unable to enable sound')
                 self.sound_enabled = False
         else:
             self.sound_enabled = False
@@ -45,7 +45,7 @@
                 mixer.music.play(-1)  # Loop forever
                 mixer.music.set_volume(volume)
             except pygame.error:
-                print 'Unable to load track'
+                print('Unable to load track')
 
     def stop_music(self):
         if self.sound_enabled:
--- a/skaapsteker/sprites/enemies.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/sprites/enemies.py	Fri Jan 20 20:01:06 2023 +0100
@@ -1,4 +1,4 @@
-from base import Monster, PatrollingMonster
+from .base import Monster, PatrollingMonster
 from pygame import transform
 
 
@@ -75,5 +75,3 @@
             self.heading = 'down'
         else:
             self.heading = 'up'
-
-
--- a/skaapsteker/sprites/player.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/sprites/player.py	Fri Jan 20 20:01:06 2023 +0100
@@ -183,13 +183,13 @@
                 self._last_time = time.time()
         else:
             old_frame = self._animation_frame
-            self._animation_frame += abs(v_x) / 300
+            self._animation_frame += abs(v_x) // 300
             time_diff = time.time() - self._last_time
             if int(self._animation_frame) - int(old_frame) > 0:
                 # Check time diff
                 if time_diff < 0.10:
                     # Delay animation frame jump
-                    self._animation_frame -= abs(v_x) / 300
+                    self._animation_frame -= abs(v_x) // 300
                 else:
                     self._last_time = time.time()
             elif time_diff > 0.20:
@@ -235,8 +235,8 @@
                     continue
                 clip = obj.collide_rect.clip(self.collide_rect)
                 clip_area += clip.width * clip.height
-                if clip.width > TILE_SIZE[0] / 2 and \
-                        self.collide_rect.bottom < obj.collide_rect.top + TILE_SIZE[1] / 3:
+                if clip.width > TILE_SIZE[0] // 2 and \
+                        self.collide_rect.bottom < obj.collide_rect.top + TILE_SIZE[1] // 3:
                    delta = self.rect.bottom - self.collide_rect.bottom
                    self.collide_rect.bottom = obj.collide_rect.top - 1
                    self.rect.bottom = self.collide_rect.bottom + delta
@@ -294,7 +294,7 @@
 
     def steal_life(self, damage_done):
         if 'steal' in self._me.tails:
-            self._me.cur_health += damage_done * len(self._me.tails) / 32
+            self._me.cur_health += damage_done * len(self._me.tails) // 32
             self._me.cur_health = min(self._me.cur_health, self._me.max_health)
 
     def restore(self):
@@ -547,10 +547,10 @@
         image = sprite.image
         if image.get_width() > image.get_height():
             new_width = FoxHud.INVENTORY_SIZE
-            new_height = int(image.get_height() * (float(FoxHud.INVENTORY_SIZE) / image.get_width()))
+            new_height = int(image.get_height() * (float(FoxHud.INVENTORY_SIZE) // image.get_width()))
         else:
             new_height = FoxHud.INVENTORY_SIZE
-            new_width = int(image.get_width() * (float(FoxHud.INVENTORY_SIZE) / image.get_height()))
+            new_width = int(image.get_width() * (float(FoxHud.INVENTORY_SIZE) // image.get_height()))
         if image.get_width() <= FoxHud.INVENTORY_SIZE and image.get_height() <= FoxHud.INVENTORY_SIZE:
             self.inventory_image = image
         else:
@@ -590,4 +590,3 @@
         self.shape = 'human_with_fan'
         self._me.shape = self.shape
         self.set_image()
-
--- a/skaapsteker/utils.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/utils.py	Fri Jan 20 20:01:06 2023 +0100
@@ -25,7 +25,7 @@
 cadd = mk_cop(operator.add)
 csub = mk_cop(operator.sub)
 cmul = mk_cop(operator.mul)
-cdiv = mk_cop(operator.div)
+cdiv = mk_cop(operator.truediv)
 cclamp = mk_cop(lambda a, b: max(min(a, b), -b))
 cabsmax = mk_cop(lambda a, b: a if abs(a) > abs(b) else b)
 
@@ -52,4 +52,3 @@
     if abs(x_projection) < abs(y_projection):
         return (x_projection, 0)
     return (0, y_projection)
-
--- a/skaapsteker/widgets/text.py	Tue Mar 17 22:40:45 2020 +0200
+++ b/skaapsteker/widgets/text.py	Fri Jan 20 20:01:06 2023 +0100
@@ -135,7 +135,7 @@
         self.selector.rect.left = self.rect.left
 
     def dispatch(self, ev):
-        if ev.type is KEYDOWN:
+        if ev.type == KEYDOWN:
             if ev.key == K_UP:
                 self.selected -= 1
             elif ev.key == K_DOWN:
@@ -176,7 +176,7 @@
         self.options.append((widget, data))
 
     def dispatch(self, ev):
-        if ev.type is KEYDOWN:
+        if ev.type == KEYDOWN:
             if ev.key == K_UP:
                 self.selected -= 1
             elif ev.key == K_DOWN: