From 3757e52c05b60b636155a3205a579c118ae82cc1 Mon Sep 17 00:00:00 2001 From: Jeremy Thurgood Date: Sun, 11 May 2014 15:12:14 +0200 Subject: [PATCH] Prototype player object. --- naja/constants.py | 10 +++++ naja/player.py | 59 +++++++++++++++++++++++++++ naja/tests/__init__.py | 0 naja/tests/test_player.py | 85 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 naja/player.py create mode 100644 naja/tests/__init__.py create mode 100644 naja/tests/test_player.py diff --git a/naja/constants.py b/naja/constants.py index faa8c55..0dc6b84 100644 --- a/naja/constants.py +++ b/naja/constants.py @@ -16,3 +16,13 @@ CHANNELS = 2 # 1 == mono, 2 == stereo BUFFER = 1024 # audio buffer size in no. of samples DEFAULT_SOUND_VOLUME = 1.0 # sound volume DEFAULT_MUSIC_VOLUME = 0.3 # music volume + +# Player bits +NORTH = 0 +SOUTH = 1 +EAST = 2 +WEST = 3 +CYAN = 4 +MAGENTA = 5 +YELLOW = 6 +MSB = 7 diff --git a/naja/player.py b/naja/player.py new file mode 100644 index 0000000..acae919 --- /dev/null +++ b/naja/player.py @@ -0,0 +1,59 @@ + + +class PlayerBits(object): + """ + A glorified byte. + """ + + def __init__(self, bits): + self.bits = bits + + @property + def bits(self): + return self._bits + + @bits.setter + def bits(self, value): + assert 0 <= value <= 0xff + self._bits = value + + # Operate on individual bits + + def check_bit(self, bit): + return bool(self.bits & (1 << bit)) + + def set_bit(self, bit): + self.bits |= (1 << bit) + + def clear_bit(self, bit): + self.bits &= (0xff ^ (1 << bit)) + + def toggle_bit(self, bit): + self.bits ^= (1 << bit) + + # Operate on sets of bits + + def check_bits(self, *bits): + return all(self.check_bit(bit) for bit in bits) + + def set_bits(self, *bits): + for bit in bits: + self.set_bit(bit) + + def clear_bits(self, *bits): + for bit in bits: + self.clear_bit(bit) + + def toggle_bits(self, *bits): + for bit in bits: + self.toggle_bit(bit) + + +class Player(object): + """ + A representation of the player. + """ + + def __init__(self, bits, position): + self.bits = PlayerBits(bits) + self.position = position diff --git a/naja/tests/__init__.py b/naja/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/naja/tests/test_player.py b/naja/tests/test_player.py new file mode 100644 index 0000000..48b2ef8 --- /dev/null +++ b/naja/tests/test_player.py @@ -0,0 +1,85 @@ +from unittest import TestCase + +from naja.constants import NORTH, SOUTH, CYAN, MSB +from naja.player import PlayerBits + + +class TestPlayerBits(TestCase): + def test_range_limits(self): + bits = PlayerBits(0x00) + + def set_value(value): + bits.bits = value + + self.assertEqual(bits.bits, 0x00) + self.assertRaises(AssertionError, set_value, -1) + self.assertEqual(bits.bits, 0x00) + bits.bits = 255 + self.assertEqual(bits.bits, 0xff) + bits.bits = 0 + self.assertEqual(bits.bits, 0x00) + self.assertRaises(AssertionError, set_value, 256) + self.assertEqual(bits.bits, 0x00) + + def test_check_bit(self): + bits = PlayerBits(0x01) + self.assertEqual(bits.check_bit(NORTH), True) + self.assertEqual(bits.check_bit(SOUTH), False) + + def test_set_bit(self): + bits = PlayerBits(0x00) + self.assertEqual(bits._bits, 0x00) + bits.set_bit(NORTH) + self.assertEqual(bits._bits, 0x01) + bits.set_bit(NORTH) + self.assertEqual(bits._bits, 0x01) + + def test_clear_bit(self): + bits = PlayerBits(0x01) + self.assertEqual(bits._bits, 0x01) + bits.clear_bit(NORTH) + self.assertEqual(bits._bits, 0x00) + bits.clear_bit(NORTH) + self.assertEqual(bits._bits, 0x00) + + def test_toggle_bit(self): + bits = PlayerBits(0x00) + self.assertEqual(bits._bits, 0x00) + bits.toggle_bit(NORTH) + self.assertEqual(bits._bits, 0x01) + bits.toggle_bit(NORTH) + self.assertEqual(bits._bits, 0x00) + + def test_check_bits(self): + bits = PlayerBits(0x03) + self.assertEqual(bits.check_bits(NORTH), True) + self.assertEqual(bits.check_bits(NORTH, SOUTH), True) + self.assertEqual(bits.check_bits(CYAN), False) + self.assertEqual(bits.check_bits(CYAN, MSB), False) + self.assertEqual(bits.check_bits(NORTH, SOUTH, CYAN), False) + + def test_set_bits(self): + bits = PlayerBits(0x03) + self.assertEqual(bits._bits, 0x03) + bits.set_bits(NORTH, CYAN) + self.assertEqual(bits._bits, 0x13) + bits.set_bits(NORTH, CYAN, MSB) + self.assertEqual(bits._bits, 0x93) + + def test_clear_bits(self): + bits = PlayerBits(0x03) + self.assertEqual(bits._bits, 0x03) + bits.clear_bits(NORTH, CYAN) + self.assertEqual(bits._bits, 0x02) + bits.clear_bits(NORTH, CYAN, MSB) + self.assertEqual(bits._bits, 0x02) + + def test_toggle_bits(self): + bits = PlayerBits(0x03) + self.assertEqual(bits._bits, 0x03) + bits.toggle_bits(NORTH, CYAN) + self.assertEqual(bits._bits, 0x12) + bits.toggle_bits(NORTH, CYAN) + self.assertEqual(bits._bits, 0x03) + bits.toggle_bits(NORTH, CYAN, MSB) + self.assertEqual(bits._bits, 0x92) -- 2.34.1