merge
authorDavid Sharpe <decoydavid@gmail.com>
Sun, 11 May 2014 16:32:11 +0000 (18:32 +0200)
committerDavid Sharpe <decoydavid@gmail.com>
Sun, 11 May 2014 16:32:11 +0000 (18:32 +0200)
15 files changed:
data/images/bits/cyan_off.png [new file with mode: 0644]
data/images/bits/cyan_on.png [new file with mode: 0644]
data/images/bits/magenta_off.png [new file with mode: 0644]
data/images/bits/magenta_on.png [new file with mode: 0644]
data/images/bits/msb_off.png [new file with mode: 0644]
data/images/bits/msb_on.png [new file with mode: 0644]
data/images/bits/yellow_off.png [new file with mode: 0644]
data/images/bits/yellow_on.png [new file with mode: 0644]
naja/actions.py
naja/engine.py
naja/gameboard.py
naja/scenes/menu.py
naja/tests/test_gameboard.py
naja/widgets/text.py
naja/widgets/tile.py [new file with mode: 0644]

diff --git a/data/images/bits/cyan_off.png b/data/images/bits/cyan_off.png
new file mode 100644 (file)
index 0000000..65d69fa
Binary files /dev/null and b/data/images/bits/cyan_off.png differ
diff --git a/data/images/bits/cyan_on.png b/data/images/bits/cyan_on.png
new file mode 100644 (file)
index 0000000..2e50a36
Binary files /dev/null and b/data/images/bits/cyan_on.png differ
diff --git a/data/images/bits/magenta_off.png b/data/images/bits/magenta_off.png
new file mode 100644 (file)
index 0000000..74a1061
Binary files /dev/null and b/data/images/bits/magenta_off.png differ
diff --git a/data/images/bits/magenta_on.png b/data/images/bits/magenta_on.png
new file mode 100644 (file)
index 0000000..4a989ee
Binary files /dev/null and b/data/images/bits/magenta_on.png differ
diff --git a/data/images/bits/msb_off.png b/data/images/bits/msb_off.png
new file mode 100644 (file)
index 0000000..453bb5e
Binary files /dev/null and b/data/images/bits/msb_off.png differ
diff --git a/data/images/bits/msb_on.png b/data/images/bits/msb_on.png
new file mode 100644 (file)
index 0000000..39c7fc8
Binary files /dev/null and b/data/images/bits/msb_on.png differ
diff --git a/data/images/bits/yellow_off.png b/data/images/bits/yellow_off.png
new file mode 100644 (file)
index 0000000..cf724c7
Binary files /dev/null and b/data/images/bits/yellow_off.png differ
diff --git a/data/images/bits/yellow_on.png b/data/images/bits/yellow_on.png
new file mode 100644 (file)
index 0000000..7bc1446
Binary files /dev/null and b/data/images/bits/yellow_on.png differ
index 333d174b75302f72e323a01b44c19a19cea38556..c49f7c61b909ba01f27778b5265b742cfab63821 100644 (file)
@@ -1,4 +1,29 @@
-from naja.gameboard import LocationAction
+from naja.constants import BITS
+
+
+class LocationAction(object):
+    """
+    An action that may be performed on a location.
+    """
+
+    TEXT = None
+
+    def __init__(self, required_bits, **data):
+        self.required_bits = frozenset(required_bits)
+        self.data = data
+
+    def check_available(self, player):
+        return player.bits.check_bits(self.required_bits)
+
+    def perform_action(self, player, board):
+        raise NotImplementedError("TODO")
+
+    def check_and_clear_MSB(self, player):
+        if player.bits.check_bit(BITS.MSB):
+            player.bits.clear_bit(BITS.MSB)
+            return True
+        else:
+            return False
 
 
 class DoNothing(LocationAction):
index 3565f4be0652d5d388f4b42d607c029c33315a2c..84bf9d0efc84e167a637216b9e7572bd1d7b14ee 100644 (file)
@@ -13,6 +13,7 @@ class Engine(object):
     def run(self):
         clock = pygame.time.Clock()
         while True:
+            self._surface.fill((0, 0, 0))
             for ev in pygame.event.get():
                 if ev.type == pgl.QUIT or QuitGameEvent.matches(ev):
                     self.quit_game()
index 9c5af7b3554382158c79df337742e605c6b819a7..fa768dbad5db3481820d1775f277a2e37cb83d1c 100644 (file)
@@ -8,32 +8,52 @@ class GameBoard(object):
     A representation of the game board.
     """
 
-    def __init__(self, player, health, wins, locations=None, state=None):
+    def __init__(self, player, health, wins, state=None):
         self.player = player
-        self.max_health = health
-        self.wins_required = wins
-
-        if locations is None:
-            locations = self.generate_locations()
-        self.locations = locations
 
         if state is None:
-            state = self.generate_state()
+            state = self.generate_state(health, wins)
         self.update_state(state)
 
+    def export(self):
+        return {
+            'max_health': self.max_health,
+            'health': self.health,
+            'wins_required': self.wins_required,
+            'wins': self.wins,
+            'locations': self.export_locations(),
+        }
+
+    def export_locations(self):
+        return dict(
+            (position, location.export())
+            for position, location in self.locations)
+
     @classmethod
     def generate_locations(cls):
-        raise NotImplementedError("TODO")
+        # TODO: Generate some locations.
+        return {}
 
-    def generate_state(self):
+    def generate_state(self, max_health, wins_required):
         return {
-            'health': self.max_health,
+            'max_health': max_health,
+            'health': max_health,
+            'wins_required': wins_required,
             'wins': 0,
+            'locations': self.generate_locations(),
         }
 
     def update_state(self, state):
+        self.max_health = state['max_health']
+        self.wins_required = state['wins_required']
         self.health = state['health']
         self.wins = state['wins']
+        self.locations = self.import_locations(state['locations'])
+
+    def import_locations(self, locations):
+        return dict(
+            (position, LocationCard.import_location(definition))
+            for position, definition in locations.iteritems())
 
     def lose_health(self):
         self.health -= 1
@@ -49,8 +69,26 @@ class LocationCard(object):
         self.bitwise_operand = bitwise_operand
         self.actions = actions
 
-    @staticmethod
-    def generate_location():
+    @classmethod
+    def import_location(cls, state):
+        # TODO: Import real locations.
+        return cls(state['bitwise_operand'], [])
+
+    @classmethod
+    def new_location(cls, definition):
+        return cls.import_location({
+            'bitwise_operand': cls.generate_bitwise_operand(),
+            'actions': cls.build_actions(definition),
+        })
+
+    def export(self):
+        return {
+            'bitwise_operand': self.bitwise_operand,
+            'actions': [action.export() for action in self.actions],
+        }
+
+    @classmethod
+    def build_actions(cls, definition):
         raise NotImplementedError("TODO")
 
     @staticmethod
@@ -68,32 +106,3 @@ class LocationCard(object):
         if choice(range(3)) == 0:
             bits.add(choice(BITS.values()))
         return frozenset(bits)
-
-    @staticmethod
-    def generate_location_actions():
-        raise NotImplementedError("TODO")
-
-
-class LocationAction(object):
-    """
-    An action that may be performed on a location.
-    """
-
-    TEXT = None
-
-    def __init__(self, required_bits, **data):
-        self.required_bits = required_bits
-        self.data = data
-
-    def check_available(self, player):
-        return player.bits.check_bits(self.required_bits)
-
-    def perform_action(self, player, board):
-        raise NotImplementedError("TODO")
-
-    def check_and_clear_MSB(self, player):
-        if player.bits.check_bit(BITS.MSB):
-            player.bits.clear_bit(BITS.MSB)
-            return True
-        else:
-            return False
index a72beff4dd25dceee62a291c005d6654a4bf73fc..b17f611234253b5ad9b69f1aacd1aebcf34eb5d9 100644 (file)
@@ -14,6 +14,7 @@ class MenuScene(Scene):
 
     def __init__(self):
         super(MenuScene, self).__init__()
+        self.widgets.append(TextWidget((10, 10), 'Haai', fontsize=32,
         self.widgets.append(TextWidget((10, 10), 'Naja',
                             colour=(255, 255, 255)))
         self.credits_menu = CreditsScene()
index 0ad0d28a5f218df26b9be12554094bd6a880d1ec..ca973a1dc01b7c7a992594132827e09bd1ee5adf 100644 (file)
@@ -5,11 +5,29 @@ from naja.gameboard import GameBoard, LocationCard
 
 
 class TestGameBoard(TestCase):
+    def test_export_new_board(self):
+        board = GameBoard(None, 4, 4)
+        self.assertEqual(board.export(), {
+            'max_health': 4,
+            'health': 4,
+            'wins_required': 4,
+            'wins': 0,
+            'locations': {},
+        })
+
     def test_lose_health(self):
-        board = GameBoard(None, 4, 4, locations={}, state=None)
+        board = GameBoard(None, 4, 4)
         self.assertEqual(board.health, 4)
+        state_1 = board.export()
+
         board.lose_health()
         self.assertEqual(board.health, 3)
+        state_2 = board.export()
+
+        # Make sure nothing else has changed.
+        state_1.pop('health')
+        state_2.pop('health')
+        self.assertEqual(state_1, state_2)
 
 
 class TestLocationCard(TestCase):
index 39178ed8777ee3e500276b95c975993721e681e2..12df393f1ff855c8a83aa8cd520c181f5ed19984 100644 (file)
@@ -1,3 +1,5 @@
+import pygame
+
 from naja.constants import FONT, FONT_SIZE
 from naja.widgets.base import Widget
 from naja.resources import resources
@@ -10,12 +12,15 @@ class TextWidget(Widget):
 
         self.text = text
         self.fontname = fontname or FONT
-        self.fontsize = fontsize or FONT_SIZE
+        self.fontsize = (fontsize or FONT_SIZE) / 2
         self.colour = colour or (0, 0, 0)
 
     def prepare(self):
         self.font = resources.get_font(self.fontname, self.fontsize)
-        self.surface = self.font.render(self.text, True, self.colour)
+        text = self.font.render(self.text, True, self.colour)
+        text_rect = text.get_rect()
+        self.surface = pygame.transform.scale(text, (text_rect.width * 2,
+                                                     text_rect.height * 2))
         self.size = self.surface.get_rect().size
 
     def draw(self, surface):
diff --git a/naja/widgets/tile.py b/naja/widgets/tile.py
new file mode 100644 (file)
index 0000000..6750584
--- /dev/null
@@ -0,0 +1,15 @@
+#from naja.constants import TILE_SIZE
+from naja.widgets.base import Widget
+from naja.resources import resources
+
+
+class TileWidget(Widget):
+    """Widget which holds a tile on the game board."""
+    def __init__(self, pos, image=None):
+        super(TileWidget, self).__init__(pos, (96, 96))
+
+    def prepare(self):
+        pass
+
+    def draw(self, surface):
+        surface.blit(self.surface, self.rect)