0
from mcpi.minecraft import Minecraft

mc = Minecraft.create()

import time

pos = mc.player.getTilePos()
x = pos.x + 1
y = pos.y
z = pos.z

# Add 10 glass blocks (ID 20) to this empty list
blocks = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
barBlock = 22 # Lapis lazuli


count = 0
while count <= len(blocks): # len (the number of items) of an object

    mc.setBlock(x, y, z, blocks[0])
    mc.setBlock(x, y + 1, z, blocks[1])
    mc.setBlock(x, y + 2, z, blocks[2])
    mc.setBlock(x, y + 3, z, blocks[3])
    mc.setBlock(x, y + 4, z, blocks[4])
    mc.setBlock(x, y + 5, z, blocks[5])
    mc.setBlock(x, y + 6, z, blocks[6])
    mc.setBlock(x, y + 7, z, blocks[7])
    mc.setBlock(x, y + 8, z, blocks[8])
    mc.setBlock(x, y + 9, z, blocks[9])

    count += 1

    # Delete the last block in the list
    del blocks[9]
    # Insert a lapis lazuli block at the first position in the list
    blocks.insert(0, barBlock)
    time.sleep(.5)

This code stacks ten glass cubes (picture 1 below), and then the cubes fill up with blue (barBlock) to give the look of a progress bar (picture 2 below). The progress bar only counts upward and stops when it's full. This code works beautifully. What I'm trying to do now is write code that says 'Once the progress bar fills up, make the progress bar count down in the opposite direction.' Please help me.

This code is all about manipulating lists. I've tried adding an item using block.append(barBlock). I've tried inserting an item using block.insert(9, barBlock). I've tried deleting an item using del block[9]. I've tried changing a list item using block[9] = barBlock. Let me be clear. The code I have thus far is correct. I know this because I'm reading a book, and they give comments that are meant as hints and I've done that efficiently. Now, they add this bonus objective of counting back down on the progress bar in the opposite direction. So, the solution is to add a little bit more code and this is where YOU come in. Please help me. Thank you in advance.

Progress bar not filled up

Progress bar filled up

Knoep
  • 858
  • 6
  • 13
  • Do you mean something like `while count >0: ...; count -=1`? – chepner Oct 22 '17 at 00:31
  • I'm not quite sure I understand your question but maybe refer to https://stackoverflow.com/questions/869885/loop-backwards-using-indices-in-python or https://stackoverflow.com/questions/3476732/how-to-loop-backwards-in-python. I think what you're looking for is a decrementing for loop. – Gaboik1 Oct 22 '17 at 00:56

2 Answers2

0

I figured it out. Here's the code in its entirety. GO ME!

from mcpi.minecraft import Minecraft mc = Minecraft.create()

import time

pos = mc.player.getTilePos()
x = pos.x + 1
y = pos.y
z = pos.z

# Add 10 glass blocks (ID 20) to this empty list
blocks = [20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
barBlock = [22, 22, 22, 22, 22, 22, 22, 22, 22, 22] # Lapis lazuli
glass = 20 # glass

count = 0
while count <= len(blocks): # len (the number of items) of an object

    mc.setBlock(x, y, z, blocks[0])
    mc.setBlock(x, y + 1, z, blocks[1])
    mc.setBlock(x, y + 2, z, blocks[2])
    mc.setBlock(x, y + 3, z, blocks[3])
    mc.setBlock(x, y + 4, z, blocks[4])
    mc.setBlock(x, y + 5, z, blocks[5])
    mc.setBlock(x, y + 6, z, blocks[6])
    mc.setBlock(x, y + 7, z, blocks[7])
    mc.setBlock(x, y + 8, z, blocks[8])
    mc.setBlock(x, y + 9, z, blocks[9])

    count += 1

    # Delete the last block in the list
    del blocks[9]
    # Insert a lapis lazuli block at the first position in the list
    blocks.insert(0, barBlock)
    time.sleep(.2)

count = 10

while count >= 0:

    mc.setBlock(x, y + 9, z, blocks[0])
    mc.setBlock(x, y + 8, z, blocks[1])
    mc.setBlock(x, y + 7, z, blocks[2])
    mc.setBlock(x, y + 6, z, blocks[3])
    mc.setBlock(x, y + 5, z, blocks[4])
    mc.setBlock(x, y + 4, z, blocks[5])
    mc.setBlock(x, y + 3, z, blocks[6])
    mc.setBlock(x, y + 2, z, blocks[7])
    mc.setBlock(x, y + 1, z, blocks[8])
    mc.setBlock(x, y, z, blocks[9])

    count -= 1
    blocks.insert(0, glass)
    time.sleep(.2)
    print("Is this code being reached")
0

This is just a cleaned up version of your code:

from mcpi.minecraft import Minecraft
import time

mc = Minecraft.create()

pos = mc.player.getTilePos()
x = pos.x + 1
y = pos.y
z = pos.z


def stack(delay, block, direction):
    for count in range(10) if direction == 1 else range(9, -1, -1):
        print(count)
        mc.setBlock(x, y + count, z, block)
        time.sleep(delay)

stack(0, 20, 1)
stack(0.5, 22, 1)
stack(0.5, 20, -1)
gommb
  • 1,121
  • 1
  • 7
  • 21