def create_tv(self): o = self.origin self.world.blocks[o:o + self.size + 1] = Block('#ffffff') self.world.blocks[o + V(0, 1, 1):o + self.size - V(0, 1, 1) + 1] = Block('#000000') self.button_vec = o + V(z=2) self.world.blocks[self.button_vec] = Block('#800000')
def make_it_rain(event): rain = Vector(p.x + randint(-10, 10), p.y + 20, p.z + randint(-10, 10)) rain_end = world.height[rain] world.blocks[rain] = Block('wool', randint(1, 15)) while rain != rain_end: with world.connection.batch_start(): world.blocks[rain] = Block('air') rain -= Y world.blocks[rain] = Block('wool', randint(1, 15))
def create_tv(self): o = self.origin self.world.blocks[o:o + self.size + 1] = Block('#ffffff') self.world.blocks[o + V(0, 1, 1):o + self.size - V(0, 2, 2) + 1] = Block('#000000') self.button_pos = o + V(z=3) self.quit_pos = o + V(z=1) self.world.blocks[self.button_pos] = Block('#0080ff') self.world.blocks[self.quit_pos] = Block('#800000') self.world.say('Behold the Minecraft TV!')
def operation(world, position, data): #center block world.blocks[position] = Block(53, data) #left block new_position = translate_left(position, data) new_data = rotate_clockwise(data) world.blocks[new_position] = Block(53, new_data) #right block new_position = translate_right(position, data) new_data = rotate_anticlockwise(data) world.blocks[new_position] = Block(53, new_data)
def annihilateEverything(self): x = w.player.pos.x y = w.player.pos.y - 1 z = w.player.pos.z repoX = 0 repoY = 0 repoZ = 0 fieldWidth = 14 #must be an even number #repo to ground level -1 w.player.pos = Vector(x, 0, z) time.sleep(1) for iy1 in range(0, fieldWidth / 2): for ix1 in range(0, fieldWidth): for iz1 in range(0, fieldWidth): if iz1 == fieldWidth / 2 and ix1 == fieldWidth / 2 and iy1 == 0: #at the midpoint repoX = x + ix1 repoY = 0 repoZ = z + iz1 w.blocks[Vector(x + ix1, y + iy1, z + iz1)] = Block(block.AIR.id, 0) #repo to center of the field we just cleared w.player.pos = Vector(repoX, repoY - 1, repoZ)
def test_blocks_set_sequence_same(): conn = mock.MagicMock() l = list(line(O, 4 * X)) picraft.block.Blocks(conn)[l] = Block(0, 0) for v in l: conn.send.assert_any_call('world.setBlock(%d,%d,%d,0,0)' % (v.x, v.y, v.z))
def test_blocks_set_none(): conn = mock.MagicMock() with warnings.catch_warnings(): warnings.simplefilter('error') with pytest.raises(EmptySliceWarning): picraft.block.Blocks( conn)[Vector(1, 1, 1):Vector(3, 3, -1)] = Block(0, 0)
def test_model_render_groups(): m = Model( io.StringIO(""" usemtl brick_block g group1 v 0 0 0 v 4 0 0 v 4 0 4 v 0 0 4 f -1 -2 -3 -4 g group2 v 0 1 0 v 4 1 0 v 4 1 4 v 0 1 4 f -1 -2 -3 -4 """)) b = Block('brick_block') assert m.render(groups='group1') == { v: b for v in vector_range(O, 4 * X + 4 * Z + 1) } assert m.render(groups=b'group1') == { v: b for v in vector_range(O, 4 * X + 4 * Z + 1) } assert m.render(groups={'group1'}) == { v: b for v in vector_range(O, 4 * X + 4 * Z + 1) }
def animation_frames(count): cube_range = vector_range(Vector() - 2, Vector() + 2 + 1) for frame in range(count): yield { v.rotate(15 * frame, about=X).round() + (5 * Y): Block('stone') for v in cube_range }
def test_block_from_color(): assert Block.from_color(b'#ffffff') == Block(35, 0) assert Block.from_color('#ffffff') == Block(35, 0) assert Block.from_color((255, 255, 255)) == Block(35, 0) assert Block.from_color((0.0, 0.0, 0.0)) == Block(35, 15) with pytest.raises(ValueError): Block.from_color('white') with pytest.raises(ValueError): Block.from_color((1, 2))
def makeBrickSampler(self): x = w.player.pos.x y = w.player.pos.y z = w.player.pos.z blockIndex = 0 for iy1 in range(0, 20): for ix1 in range(0, 20): blockIndex += 1 #filter out water, lava and sands if blockIndex >= 8 and blockIndex <= 12: w.blocks[Vector(ix1 + x, iy1 + y, z)] = Block(1, 0) else: #print(blockIndex) w.blocks[Vector(ix1 + x, iy1 + y, z)] = Block(blockIndex, 0)
def test_blocks_set_sequence_different(): conn = mock.MagicMock() l = list(line(O, 4 * X)) blocks = [Block(1, 1) for v in l] picraft.block.Blocks(conn)[l] = blocks for v in l: conn.send.assert_any_call('world.setBlock(%d,%d,%d,1,1)' % (v.x, v.y, v.z))
def test_blocks_get_sequence(): l = list(line(O, 4 * X)) conn = mock.MagicMock() conn.transact.return_value = '1,1' assert picraft.block.Blocks(conn)[l] == [Block(1, 1) for v in l] for v in l: conn.transact.assert_any_call('world.getBlockWithData(%d,%d,%d)' % (v.x, v.y, v.z))
def animation_frames(count): cube_range = vector_range(Vector() - 2, Vector() + 2 + 1) for frame in range(count): state = {} for v in cube_range: state[v.rotate(15 * frame, about=X).round() + (5 * Y)] = Block('stone') yield state
def test_block_init(): assert Block(1, 1) == Block.from_id(1, 1) assert Block(b'stone') == Block.from_name('stone') assert Block('stone') == Block.from_name('stone') assert Block('air', 1) == Block.from_name('air', 1) assert Block('#ffffff') == Block.from_color('#ffffff') assert Block((0, 0, 0)) == Block.from_color((0, 0, 0)) assert Block(id=1) == Block.from_id(1) assert Block(name='grass') == Block.from_name('grass') assert Block(color='#ffffff', exact=False) == Block.from_color('#ffffff') with pytest.raises(TypeError): Block() with pytest.raises(TypeError): Block(1, 2, 3)
def test_blocks_set_vrange_different(): conn = mock.MagicMock() v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) blocks = [Block(1, 1) for v in vector_range(v_from, v_to)] picraft.block.Blocks(conn)[v_from:v_to] = blocks for v in vector_range(v_from, v_to): conn.send.assert_any_call('world.setBlock(%d,%d,%d,1,1)' % (v.x, v.y, v.z))
def render(self, jpeg): o = self.origin img = Image.open(jpeg) img = img.resize(self.size, Image.BILINEAR) img = img.quantize(self.palette_len, palette=self.palette) with self.world.connection.batch_start(): for x in range(img.size[0]): for y in range(img.size[1]): self.world.blocks[o + V(0, y, x)] = Block.from_id(35, img.getpixel((x, y)))
def test_blocks_set_vrange_same_stepped(): conn = mock.MagicMock() v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) v_step = Vector(2, 2, 2) picraft.block.Blocks(conn)[v_from:v_to:v_step] = Block(0, 0) for v in vector_range(v_from, v_to, v_step): conn.send.assert_any_call('world.setBlock(%d,%d,%d,0,0)' % (v.x, v.y, v.z))
def render(self, jpeg): o = self.origin img = Image.open(jpeg) img = img.resize(self.size, Image.BILINEAR) img = img.quantize(self.palette_len, palette=self.palette) with self.world.connection.batch_start(): for x in range(img.size[0]): for y in range(img.size[1]): self.world.blocks[o + V(0, y, x)] = Block.from_id( 35, img.getpixel((x, y)))
def test_blocks_get_vrange(): v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) conn = mock.MagicMock() conn.transact.return_value = '1,1' assert picraft.block.Blocks(conn)[v_from:v_to] == [ Block(1, 1) for v in vector_range(v_from, v_to) ] for v in vector_range(v_from, v_to): conn.transact.assert_any_call('world.getBlockWithData(%d,%d,%d)' % (v.x, v.y, v.z))
def simulateEarthquake(self): blockType = block.STONE.id for iy1 in range(0, 4): for ix1 in range(0, 6): for iz1 in range(0, 6): # first build a dict of the building blueprint w.blocks[Vector(self.storedX + ix1, self.storedY + iy1, self.storedZ + iz1)] = \ Block(blockType, 0)
def test_blocks_get_vrange_fast(): v_from = Vector(1, 2, 3) v_to = Vector(2, 3, 5) conn = mock.MagicMock() conn.server_version = 'raspberry-juice' conn.transact.return_value = '1,1' picraft.block.Blocks(conn)[v_from:v_to] == [ Block(1, 0) for v in vector_range(v_from, v_to) ] conn.transact.assert_called_once_with('world.getBlocks(%s,%s)' % (v_from, v_to - 1))
def track_changes(states, default=Block('air')): old_state = None for state in states: # Assume the initial state of the blocks is the default ('air') if old_state is None: old_state = {v: default for v in state} # Build a dict of those blocks which changed from old_state to state changes = {v: b for v, b in state.items() if old_state.get(v) != b} # Blank out blocks which were in old_state but aren't in state changes.update({v: default for v in old_state if v not in state}) yield changes old_state = state
def test_model_render_defaults(): m = Model( io.StringIO(""" usemtl brick_block v 0 0 0 v 4 0 0 v 4 0 4 v 0 0 4 f -1 -2 -3 -4""")) b = Block('brick_block') assert m.render() == {v: b for v in vector_range(O, 4 * X + 4 * Z + 1)}
def buildGenericFloor(self, pIndex, pX, pY, pZ): x = w.player.pos.x y = w.player.pos.y z = w.player.pos.z newX = pX newY = pY newZ = pZ floorWidth = 4 blockType = block.IRON_BLOCK.id offset = 1 for iy1 in range(0, 2): if iy1 == 1: offset = 0 floorWidth = 6 for ix1 in range(0, floorWidth): for iz1 in range(0, floorWidth): if floorWidth == 4: if ((iz1 == 0 or iz1 == 3) and (ix1 == 1 or ix1 == 2)): blockType = block.GLASS_PANE.id elif ((ix1 == 0 or ix1 == 3) and (iz1 == 1 or iz1 == 2)): blockType = block.GLASS_PANE.id else: blockType = block.DIAMOND_BLOCK.id if floorWidth == 6: if ((iz1 == 0 or iz1 == 5) and (ix1 == 2 or ix1 == 3)): blockType = block.STONE_SLAB.id elif ((ix1 == 0 or ix1 == 5) and (iz1 == 2 or iz1 == 3)): blockType = block.STONE_SLAB.id else: blockType = block.DIAMOND_BLOCK.id ''' if floorWidth == 6: if ((iz1 == 0 or iz1 == 5) and (ix1 > 0 and ix1 < 5)): blockType = block.STONE_SLAB.id elif ((ix1 == 0 or ix1 == 5) and (iz1 > 0 and iz1 < 5)): blockType = block.STONE_SLAB.id else: blockType = block.LAPIS_LAZULI_BLOCK.id ''' w.blocks[Vector(newX + ix1 + offset, newY + iy1, newZ + iz1 + offset)] = Block( blockType, 0)
def render(self, jpeg): o = self.origin img = Image.open(jpeg) img = img.resize(self.size, Image.BILINEAR) img = img.quantize(len(self.palette), palette=self.palette_img) new_state = { o + V(0, y, x): Block.from_color(self.palette[img.getpixel((x, y))], exact=True) for x in range(img.size[0]) for y in range(img.size[1]) } with self.world.connection.batch_start(): for v, b in track_changes(self.state, new_state).items(): self.world.blocks[v] = b self.state = new_state
def test_model_render_materials_dict(): m = Model( io.StringIO(""" usemtl brick v 0 0 0 v 4 0 0 v 4 0 4 v 0 0 4 f -1 -2 -3 -4""")) b = Block('brick_block') assert m.render(materials={'brick': b}) == { v: b for v in vector_range(O, 4 * X + 4 * Z + 1) }
def render(self, jpeg): o = self.origin img = Image.open(jpeg) img = img.resize(self.size, Image.BILINEAR) img = img.quantize(len(self.palette), palette=self.palette_img) new_state = { o + V(0, y, x): Block.from_color(self.palette[img.getpixel( (x, y))], exact=True) for x in range(img.size[0]) for y in range(img.size[1]) } with self.world.connection.batch_start(): for v, b in track_changes(self.state, new_state).items(): self.world.blocks[v] = b self.state = new_state
from random import randint from picraft import World, X, Y, Z, Vector, Block world = World() p = world.player.tile_pos white_pos = p - 2 * X black_pos = p - 3 * X world.blocks[white_pos] = Block('#ffffff') world.blocks[black_pos] = Block('#000000') running = True while running: for event in world.events.poll(): if event.pos == white_pos: rain = Vector(p.x + randint(-10, 10), p.y + 20, p.z + randint(-10, 10)) rain_end = world.height[rain] world.blocks[rain] = Block('wool', randint(1, 15)) while rain != rain_end: with world.connection.batch_start(): world.blocks[rain] = Block('air') rain -= Y world.blocks[rain] = Block('wool', randint(1, 15)) elif event.pos == black_pos: running = False
def test_block_from_string(): assert Block.from_string('1,1') == Block(1, 1) with pytest.raises(ValueError): Block.from_string('foo') with pytest.raises(ValueError): Block.from_string('1.0,2.0')
def test_block_platforms(): assert Block.from_name('air').pi assert Block.from_name('air').pocket assert not Block.from_name('piston').pi assert not Block.from_name('piston').pocket
from time import sleep from picraft import World, Vector, X, Y, Z, vector_range, Block def animation_frames(count): cube_range = vector_range(Vector() - 2, Vector() + 2 + 1) for frame in range(count): state = {} for v in cube_range: state[v.rotate(15 * frame, about=X).round() + (5 * Y)] = Block('stone') yield state world = World() world.checkpoint.save() try: for frame in animation_frames(10): # Draw frame with world.connection.batch_start(): for v, b in frame.items(): world.blocks[v] = b sleep(0.2) # Wipe frame with world.connection.batch_start(): for v, b in frame.items(): world.blocks[v] = Block('air') finally: world.checkpoint.restore()
from picraft import World, Model, Block print('Loading model airboat.obj') m = Model('airboat.obj') print('Model has the following materials:') print('\n'.join(s or '<None>' for s in m.materials)) materials_map = { None: Block('stone'), 'bluteal': Block('diamond_block'), 'bronze': Block('gold_block'), 'dkdkgrey': Block('#404040'), 'dkteal': Block('#000080'), 'red': Block('#ff0000'), 'silver': Block('#ffffff'), 'black': Block('#000000'), } with World() as w: with w.connection.batch_start(): for v, b in m.render(materials=materials_map).items(): w.blocks[v] = b
from picraft import World, Vector, Block, O, X, Y, Z, lines def polygon(sides, center=O, radius=5): angle = 2 * math.pi / sides for side in range(sides): yield Vector(center.x + radius * math.cos(side * angle), center.y + radius * math.sin(side * angle), center.z).round() def shapes(center=O): for sides in range(3, 9): yield lines(polygon(sides, center=center)) w = World() for shape in shapes(w.player.tile_pos + 15 * Y + 10 * Z): # Copy the generator into a list so we can re-use # the coordinates shape = list(shape) # Draw the shape with w.connection.batch_start(): for p in shape: w.blocks[p] = Block('gold_block') sleep(0.5) # Wipe the shape with w.connection.batch_start(): for p in shape: w.blocks[p] = Block('air')
def test_block_from_color_exact(): with pytest.raises(ValueError): Block.from_color(b'#ffffff', exact=True)
#Load the picraft API from picraft import World, Block, Vector #Connect API to the world world = World() ############ Write your program below ############# while True: #Get the player position position = world.player.tile_pos #lower the position below the player position -= Vector(y=1) #place a block there world.blocks[position] = Block('gold_block')
from picraft import World, Vector, Block from collections import deque world = World(ignore_errors=True) world.say('Auto-bridge active') try: bridge = deque() last_pos = None while True: this_pos = world.player.pos if last_pos is not None: # Has the player moved more than 0.1 units in a horizontal direction? movement = (this_pos - last_pos).replace(y=0.0) if movement.magnitude > 0.1: # Find the next tile they're going to step on next_pos = (this_pos + movement.unit).floor() - Vector(y=1) if world.blocks[next_pos] == Block('air'): with world.connection.batch_start(): bridge.append(next_pos) world.blocks[next_pos] = Block('diamond_block') while len(bridge) > 10: world.blocks[bridge.popleft()] = Block('air') last_pos = this_pos time.sleep(0.01) except KeyboardInterrupt: world.say('Auto-bridge deactivated') with world.connection.batch_start(): while bridge: world.blocks[bridge.popleft()] = Block('air')
def test_block_from_name(): assert Block.from_name(b'air') == Block(0, 0) assert Block.from_name('air') == Block(0, 0) assert Block.from_name('stone') == Block(1, 0) with pytest.raises(ValueError): Block.from_name('foobarbaz')