{"id":402,"date":"2017-07-09T06:41:13","date_gmt":"2017-07-09T06:41:13","guid":{"rendered":"https:\/\/www.triptera.com.au\/wordpress\/?p=402"},"modified":"2017-07-16T06:29:18","modified_gmt":"2017-07-16T06:29:18","slug":"python-in-minecraft-8-stairs","status":"publish","type":"post","link":"https:\/\/www.triptera.com.au\/wordpress\/2017\/07\/09\/python-in-minecraft-8-stairs\/","title":{"rendered":"Python in Minecraft 8 &#8211; stairs"},"content":{"rendered":"<p>To add stairs we need to know if the tunnel is descending or ascending to the east. We do this by creating a variable called yprev which stores the previous vertical position of the tunnel to the current position. We are creating the tunnel by increasing the value of x which means we are heading east. Hence if the current vertical position is more than the previous vertical position, the stairs are ascending to the east. If the current vertical position is less than the previous vertical position, the stairs are ascending to the west (descending to the east). If they are the same then we are not ascending or descending so no stairs are needed. At the end of the loop we need to store the current value of y in yprev so next time through the loop we will know the previous value. Check the full code to see where we initialise yprev before the loop.<\/p>\n<pre class=\"brush: python; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n    # check if stairs are going up or down \r\n    if y &gt; yprev:\r\n        # variation 0 = stairs ascending to the east\r\n        mc.setBlock(x,y,z-1,STAIRS_STONE.id,0)\r\n    if y &lt; yprev:\r\n        # variation 1 = stairs ascending to the west\r\n        mc.setBlock(x-1,yprev,z-1,STAIRS_STONE.id,1)\r\n    yprev=y\r\n<\/pre>\n<p>We have decided to use stone stairs which are not predefined in Block.py in the modded mcpi. Hence we define it manually here.<\/p>\n<pre class=\"brush: python; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n    STAIRS_STONE = block.Block(109)\r\n<\/pre>\n<p>Here is a screenshot showing the ascending stairs at the end of the tunnel.<\/p>\n<p><a href=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/stairs.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/stairs-300x169.png\" alt=\"\" width=\"300\" height=\"169\" class=\"alignnone size-medium wp-image-403\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/stairs-300x169.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/stairs-768x432.png 768w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/stairs-1024x576.png 1024w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/stairs.png 1920w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Here is the full code.<\/p>\n<pre class=\"brush: python; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nimport mcpi.minecraft as minecraft\r\nimport mcpi.block as block\r\nmc=minecraft.Minecraft.create()\r\nXMIN = -400 # x value at one end of tunnel.\r\nXMAX = -200 # x value at other end of tunnel. Must be greater than XMIN\r\nGROUND = 72 # y position of tunnel at each end\r\nFLOOR = 10  # minimum value of y at bottom of tunnel\r\nZPOS = -244 # z position for full length of tunnel\r\nTAIL = 10   # length of horizontal at each end of tunnel\r\n\r\n# Define blocks currently missing from mcpi.block\r\nSTAIRS_STONE = block.Block(109)\r\n \r\n# constants for tunnelvertical function\r\nkx = (XMAX + XMIN) \/ 2\r\nky = GROUND + TAIL - (XMAX - XMIN) \/ 2\r\n \r\n# tunnelvertical returns a vertical position of the tunnel\r\n# for each value of the x position (eastposition)\r\ndef tunnelvertical(eastposition):\r\n    y = abs(eastposition - kx) + ky\r\n    if y &lt; FLOOR:\r\n        return FLOOR\r\n    if y &gt; GROUND: \r\n        return GROUND\r\n    return y\r\n \r\n# set the z coordinate for the tunnel which doesn't change for the full length    \r\nz = ZPOS\r\n \r\n# Initial loop to create a route made of solid glass with a stone base \r\nfor x in range(XMIN,XMAX+1):\r\n    y = tunnelvertical(x)\r\n    mc.setBlocks(x,y,z-2,x,y+6,z+2,block.GLASS)\r\n    mc.setBlocks(x,y,z-1,x,y,z+1,block.STONE)\r\n \r\n# Initialise previous value of y so can determine if stairs going up or down\r\nyprev = tunnelvertical(XMIN)\r\n# Second loop to convert solid glass into a tunnel\r\nfor x in range(XMIN+1,XMAX):\r\n    y = tunnelvertical(x)\r\n    # replace centre glass with air to make it a tunnel\r\n    mc.setBlocks(x,y+1,z-1,x,y+5,z+1,block.AIR)\r\n    # place a torch every 4 positions to light the tunnel\r\n    # variation 5 = torch facing up\r\n    if x % 4 == 0:\r\n        mc.setBlock(x,y+1,z+1,block.TORCH.id,5)\r\n    # check if stairs are going up or down \r\n    if y &gt; yprev:\r\n        # variation 0 = stairs ascending to the east\r\n        mc.setBlock(x,y,z-1,STAIRS_STONE.id,0)\r\n    if y &lt; yprev:\r\n        # variation 1 = stairs ascending to the west\r\n        mc.setBlock(x-1,yprev,z-1,STAIRS_STONE.id,1)\r\n    yprev=y\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To add stairs we need to know if the tunnel is descending or ascending to the east. We do this by creating a variable called yprev which stores the previous vertical position of the tunnel to the current position. We are creating the tunnel by increasing the value of x which means we are heading [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[13],"tags":[],"class_list":["post-402","post","type-post","status-publish","format-standard","hentry","category-coderdojo"],"_links":{"self":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/comments?post=402"}],"version-history":[{"count":2,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/402\/revisions"}],"predecessor-version":[{"id":407,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/402\/revisions\/407"}],"wp:attachment":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/media?parent=402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/categories?post=402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/tags?post=402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}