示例#1
0
 def setUp(self):
     self.block = Block()
     self.block._block_type = 0
     self.block.shape = self.block._shapes[self.block._block_type][0]
     self.field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
     self.block.move()
示例#2
0
    def __init__(self):
        self.current_transactions: [dict] = []
        self.chain: [dict] = []
        self.nodes = set()

        # Create the genesis block
        genesis_block = Block()
        genesis_block.from_value(1, time(), current_transactions=[], proof=100, previous_hash=1)
        self.chain.append(genesis_block.__dict__)
示例#3
0
    def valid_chain(self, chain: []) -> bool:
        """
        Determine if a given blockchain is valid
        :param chain: <list> A blockchain
        :return: <bool> True if valid, False if not
        """

        last_block = Block()
        last_block.from_dict(chain[0])
        current_index = 1

        while current_index < len(chain):
            # block_dict = chain[current_index]
            block = Block()
            block.from_dict(chain[current_index])

            # print(f'{last_block.__dict__}')
            # print(f'{block.__dict__}')
            # print("\n-----------\n")
            # Check that the hash of the block is correct
            if block.previous_hash != last_block.hash:
                return False

            # Check that the Proof of Work is correct
            if not self.valid_proof(last_block.proof, block.proof):
                return False

            last_block = block
            current_index += 1

        return True
示例#4
0
    def new_block(self, proof, previous_hash=None) -> Block:
        """
        Create a new Block in the Blockchain
        :param proof: <int> The proof given by the Proof of Work algorithm
        :param previous_hash: (Optional) <str> Hash of previous Block
        :return: <dict> New Block
        """

        self.resolve_conflicts()

        block = Block()
        block.from_value(len(self.chain) + 1, time(), self.current_transactions, proof,
                         previous_hash or self.chain[-1].hash)

        # Reset the current list of transactions
        self.current_transactions = []

        self.chain.append(block.__dict__)
        return block
示例#5
0
    def total_amount(self, node_id) -> int:

        self.resolve_conflicts()

        current_index = 1
        total_amount = 0

        while current_index < len(self.chain):
            block = Block()
            block.from_dict(self.chain[current_index])

            for transaction in block.transactions:
                if transaction['recipient'] == node_id:
                    total_amount += transaction['amount'];
                elif transaction['sender'] == node_id:
                    total_amount -= transaction['amount']

            current_index += 1

        return total_amount
示例#6
0
    def __init__(self, file_path):
        self.starting_blocks = []
        self.starting_enemies = []
        self.starting_coins = []
        self.starting_powerups = []
        self.starting_flag = []

        self.blocks = pygame.sprite.Group()
        self.enemies = pygame.sprite.Group()
        self.coins = pygame.sprite.Group()
        self.powerups = pygame.sprite.Group()
        self.flag = pygame.sprite.Group()

        self.active_sprites = pygame.sprite.Group()
        self.inactive_sprites = pygame.sprite.Group()

        with open(file_path, 'r') as f:
            data = f.read()

        map_data = json.loads(data)

        self.width = map_data['width'] * constants.GRID_SIZE
        self.height = map_data['height'] * constants.GRID_SIZE

        self.start_x = map_data['start'][0] * constants.GRID_SIZE
        self.start_y = map_data['start'][1] * constants.GRID_SIZE

        for item in map_data['blocks']:
            x, y = item[0] * constants.GRID_SIZE, item[1] * constants.GRID_SIZE
            img = block_images[item[2]]
            self.starting_blocks.append(Block(x, y, img))

        for item in map_data['bears']:
            x, y = item[0] * constants.GRID_SIZE, item[1] * constants.GRID_SIZE
            self.starting_enemies.append(Bear(x, y, bear_images))

        for item in map_data['monsters']:
            x, y = item[0] * constants.GRID_SIZE, item[1] * constants.GRID_SIZE
            self.starting_enemies.append(Monster(x, y, monster_images))

        for item in map_data['coins']:
            x, y = item[0] * constants.GRID_SIZE, item[1] * constants.GRID_SIZE
            self.starting_coins.append(Coin(x, y, coin_img))

        for item in map_data['oneups']:
            x, y = item[0] * constants.GRID_SIZE, item[1] * constants.GRID_SIZE
            self.starting_powerups.append(OneUp(x, y, oneup_img))

        for item in map_data['hearts']:
            x, y = item[0] * constants.GRID_SIZE, item[1] * constants.GRID_SIZE
            self.starting_powerups.append(Heart(x, y, heart_img))

        for i, item in enumerate(map_data['flag']):
            x, y = item[0] * constants.GRID_SIZE, item[1] * constants.GRID_SIZE

            if i == 0:
                img = flag_img
            else:
                img = flagpole_img

            self.starting_flag.append(Flag(x, y, img))

        self.background_layer = pygame.Surface([self.width, self.height],
                                               pygame.SRCALPHA, 32)
        self.scenery_layer = pygame.Surface([self.width, self.height],
                                            pygame.SRCALPHA, 32)
        self.inactive_layer = pygame.Surface([self.width, self.height],
                                             pygame.SRCALPHA, 32)
        self.active_layer = pygame.Surface([self.width, self.height],
                                           pygame.SRCALPHA, 32)

        if map_data['background-color'] != "":
            self.background_layer.fill(map_data['background-color'])

        if map_data['background-img'] != "":
            background_img = pygame.image.load(
                map_data['background-img']).convert_alpha()

            if map_data['background-fill-y']:
                h = background_img.get_height()
                w = int(background_img.get_width() * constants.HEIGHT / h)
                background_img = pygame.transform.scale(
                    background_img, (w, constants.HEIGHT))

            if "top" in map_data['background-position']:
                start_y = 0
            elif "bottom" in map_data['background-position']:
                start_y = self.height - background_img.get_height()

            if map_data['background-repeat-x']:
                for x in range(0, self.width, background_img.get_width()):
                    self.background_layer.blit(background_img, [x, start_y])
            else:
                self.background_layer.blit(background_img, [0, start_y])

        if map_data['scenery-img'] != "":
            scenery_img = pygame.image.load(
                map_data['scenery-img']).convert_alpha()

            if map_data['scenery-fill-y']:
                h = scenery_img.get_height()
                w = int(scenery_img.get_width() * constants.HEIGHT / h)
                scenery_img = pygame.transform.scale(scenery_img,
                                                     (w, constants.HEIGHT))

            if "top" in map_data['scenery-position']:
                start_y = 0
            elif "bottom" in map_data['scenery-position']:
                start_y = self.height - scenery_img.get_height()

            if map_data['scenery-repeat-x']:
                for x in range(0, self.width, scenery_img.get_width()):
                    self.scenery_layer.blit(scenery_img, [x, start_y])
            else:
                self.scenery_layer.blit(scenery_img, [0, start_y])

        pygame.mixer.music.load(map_data['music'])

        self.gravity = map_data['gravity']
        self.terminal_velocity = map_data['terminal-velocity']

        self.completed = False

        self.blocks.add(self.starting_blocks)
        self.enemies.add(self.starting_enemies)
        self.coins.add(self.starting_coins)
        self.powerups.add(self.starting_powerups)
        self.flag.add(self.starting_flag)

        self.active_sprites.add(self.coins, self.enemies, self.powerups)
        self.inactive_sprites.add(self.blocks, self.flag)

        # with this speed up blitting on slower computers?
        for s in self.active_sprites:
            s.image.convert()

        for s in self.inactive_sprites:
            s.image.convert()

        self.inactive_sprites.draw(self.inactive_layer)

        # is converting layers helpful at all?
        self.background_layer.convert()
        self.scenery_layer.convert()
        self.inactive_layer.convert()
        self.active_layer.convert()
示例#7
0
 def __init__(self, chain=None):
     self.chain = chain or [
         Block(transactions=[], prev_block=None).to_dict()
     ]
示例#8
0
class TestBlock(unittest.TestCase):
    def setUp(self):
        self.block = Block()
        self.block._block_type = 0
        self.block.shape = self.block._shapes[self.block._block_type][0]
        self.field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
        self.block.move()

    def test_block_is_appearing_on_the_field(self):
        self.assertEqual((self.block.row, self.block.column), (0, 4))

    def test_block_is_falling(self):
        self.block.move()
        self.assertEqual((self.block.row, self.block.column), (1, 4))

    def test_check_if_the_block_is_able_to_fall(self):
        result = self.block.movable(self.field)
        self.assertTrue(result)
        self.field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 0, 0, 0, 0]]
        result = self.block.movable(self.field)
        self.assertFalse(result)

    def test_move_the_block_to_left(self):
        self.block.move((0, -1))
        self.assertEqual((self.block.row, self.block.column), (0, 3))

    def test_move_the_block_to_right(self):
        self.block.move((0, 1))
        self.assertEqual((self.block.row, self.block.column), (0, 5))

    def test_check_if_the_block_is_movable_to_left(self):
        result = self.block.movable(self.field, (0, -1))
        self.assertTrue(result)

        self.block.move((0, -1))
        self.block.move((0, -1))
        self.block.move((0, -1))
        result = self.block.movable(self.field, (0, -1))
        self.assertFalse(result)

        self.field = [[1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                      [1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                      [1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                      [1, 1, 1, 0, 0, 0, 0, 0, 0, 0]]
        result = self.block.movable(self.field, (0, -1))
        self.assertFalse(result)

    def test_check_if_the_block_is_movable_to_right(self):
        result = self.block.movable(self.field, (0, 1))
        self.assertTrue(result)

        self.block.move((0, 1))
        self.block.move((0, 1))
        self.block.move((0, 1))
        result = self.block.movable(self.field, (0, 1))
        self.assertFalse(result)

        self.field = [[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
                      [0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
                      [0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
                      [0, 0, 0, 0, 0, 0, 0, 1, 1, 1]]
        result = self.block.movable(self.field, (0, 1))
        self.assertFalse(result)

    def test_stop_the_block_on_the_bottom(self):
        self.block.move()
        self.block.move()
        self.block.move()

        result = self.block.movable(self.field)
        self.assertFalse(result)

        result = self.block.stop(self.field)
        self.assertEqual(
            result,
            [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 0, 0, 0]])

    def test_rotate_the_block_clockwise(self):
        self.block.rotate(1)
        self.assertEqual(self.block.shape, [[-1, 0], [0, 0], [1, 0], [2, 0]])

    def test_rotate_the_block_clockwise_when_block_orientation_is_3(self):
        self.block._block_orientation = 3
        self.shape = self.block._shapes[self.block._block_type][
            self.block._block_orientation]
        self.block.rotate(1)
        self.assertEqual(self.block.shape, [[0, -1], [0, 0], [0, 1], [0, 2]])

    def test_rotate_the_block_anti_clockwise(self):
        self.block.rotate(-1)
        self.assertEqual(self.block.shape, [[0, 0], [1, 0], [2, 0], [3, 0]])

    def test_rotate_the_block_anti_clockwise_when_block_orientation_is_1(self):
        self.block._block_orientation = 1
        self.shape = self.block._shapes[self.block._block_type][
            self.block._block_orientation]
        self.block.rotate(-1)
        self.assertEqual(self.block.shape, [[0, -1], [0, 0], [0, 1], [0, 2]])

    def test_return_false_if_the_block_is_O_type(self):
        self.block._block_type = 1
        result = self.block.rotatable(self.field, 1)
        self.assertFalse(result)

    def test_return_true_if_the_block_is_rotatable_clockwise(self):
        result = self.block.rotatable(self.field, 1)
        self.assertTrue(result)

    def test_return_false_if_the_block_is_not_rotatable_clockwise(self):
        self.field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 1, 0, 0, 0]]
        result = self.block.rotatable(self.field, 1)
        self.assertFalse(result)

        self.field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

        self.block.move()
        self.block.move()
        self.block.move()
        self.block.move()
        result = self.block.rotatable(self.field, 1)
        self.assertFalse(result)

    def test_return_true_if_the_block_is_rotatable_clockwise_and_block_orientation_is_3(
            self):
        self.block._block_orientation = 3
        self.shape = self.block._shapes[self.block._block_type][
            self.block._block_orientation]
        result = self.block.rotatable(self.field, 1)
        self.assertTrue(result)

    def test_return_false_if_the_block_is_not_rotatable_clockwise_and_block_orientation_is_3(
            self):
        self.block._block_orientation = 3
        self.shape = self.block._shapes[self.block._block_type][
            self.block._block_orientation]
        self.field = [[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 1, 0, 0, 0]]
        result = self.block.rotatable(self.field, 1)
        self.assertFalse(result)

        self.field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

        self.block.move()
        self.block.move()
        self.block.move()
        self.block.move()
        result = self.block.rotatable(self.field, 1)
        self.assertFalse(result)

    def test_return_true_if_the_block_is_rotatable_anti_clockwise(self):
        result = self.block.rotatable(self.field, -1)
        self.assertTrue(result)

    def test_return_false_if_the_block_is_rotatable_anti_clockwise(self):
        self.field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 1, 0, 0, 0]]
        result = self.block.rotatable(self.field, -1)
        self.assertFalse(result)

        self.field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

        self.block.move()
        self.block.move()
        self.block.move()
        result = self.block.rotatable(self.field, -1)
        self.assertFalse(result)

    def test_true_when_game_is_over_and_false_when_not(self):
        result = self.block.check_game_over(self.field)
        self.assertFalse(result)

        self.field = [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
                      [0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
                      [0, 0, 0, 1, 1, 1, 1, 0, 0, 0]]
        result = self.block.check_game_over(self.field)
        self.assertTrue(result)
示例#9
0
 def last_block(self) -> Block:
     # last_block_dict = self.chain[-1]
     last_block = Block()
     last_block.from_dict(self.chain[-1])
     return last_block
示例#10
0
def set_up_block(game_state, position, name):
    block = Block(position)

    game_state.add_entitiy((name, block))
示例#11
0
 def set_new_block(self):
     '''
     Returns:
         Block-olio
     '''
     return Block()
示例#12
0
def game():
    fps = 60
    game_loop = True

    # player
    player = Player(width, height)

    # ball
    ball = Ball(width, height)
    game_over = False

    # blocks
    blocks = []
    b_width, b_height = width // 5 - 20, height // (4 * 5) - 10
    x, y = 30, 10
    for i in range(5):
        for j in range(5):
            blocks.append(Block(height, width, b_width, b_height, x, y,
                                (random.randint(20, 255), random.randint(20, 255),
                                 random.randint(20, 255))))  # block color never black
            x += 10 + b_width
        y += 10 + b_height
        x = 30

    # score
    max_score = len(blocks)

    while game_loop:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                return

        # check for user input
        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT]:
            player.moveX(player.velocity * -1)
        if keys[pygame.K_RIGHT]:
            player.moveX(player.velocity)

        if not game_over:
            game_over = ball.move(player)
            ball.check_collision(blocks)
            if not game_over:
                game_over = len(blocks) == 0

        # clearing screen
        window.fill((0, 0, 0))

        if game_over:
            # Game Over text
            go_label = font.render("Game Over!", True, (255, 255, 255))
            score_label = font.render("Score: " + str(max_score - len(blocks)), True, (255, 255, 0))
            window.blit(go_label, ((width - 150) // 2, (height - 150) // 2))
            window.blit(score_label, ((width - 120) // 2, (height - 60) // 2))

            # Try Again button
            if _render_btn("Try again", ((width - btn_width) // 2, 0.75 * height), pygame.mouse.get_pos()):
                game_loop = False
                continue
        else:
            # drawing entities
            player.draw(window)
            ball.draw(window)
            for block in blocks:
                block.draw(window)

        # updates
        pygame.display.update()
        clock.tick(fps)