{"id":408,"date":"2017-07-16T06:45:43","date_gmt":"2017-07-16T06:45:43","guid":{"rendered":"https:\/\/www.triptera.com.au\/wordpress\/?p=408"},"modified":"2017-07-16T06:55:39","modified_gmt":"2017-07-16T06:55:39","slug":"python-in-minecraft-9-railway-track","status":"publish","type":"post","link":"https:\/\/www.triptera.com.au\/wordpress\/2017\/07\/16\/python-in-minecraft-9-railway-track\/","title":{"rendered":"Python in Minecraft 9 &#8211; railway track"},"content":{"rendered":"<p>To add railway track we need to have some normal rail and some powered rail. Next to every section of powered rail we need to provide a redstone torch. <\/p>\n<p>First we define the items which are not defined in mcpi.block yet<\/p>\n<pre class=\"brush: python; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n    RAIL = block.Block(66)\r\n    RAIL_POWERED = block.Block(27)\r\n    TORCH_REDSTONE = block.Block(76)\r\n<\/pre>\n<p>We are already placing a plain torch every 4 blocks. Now we want to place a redstone torch every 4 blocks but offset by 2. Next to the redstone torch we want two pieces of powered rail, at positions 1 and 2. The rest of the rail is normal rail. This code goes in the second loop where the full tunnel profile is created.<\/p>\n<pre class=\"brush: python; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n    if x % 4 == 2:\r\n        mc.setBlock(x,y+1,z+1,TORCH_REDSTONE.id,5)\r\n    if x % 4 == 1 or x % 4 == 2:\r\n        mc.setBlock(x,y+1,z,RAIL_POWERED)\r\n    else:\r\n        mc.setBlock(x,y+1,z,RAIL)\r\n<\/pre>\n<p>Here is a screenshot showing the railway track in the tunnel.<\/p>\n<p><a href=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/rail.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/rail-300x169.png\" alt=\"\" width=\"300\" height=\"169\" class=\"alignnone size-medium wp-image-410\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/rail-300x169.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/rail-768x432.png 768w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/rail.png 854w\" 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\nRAIL = block.Block(66)\r\nRAIL_POWERED = block.Block(27)\r\nTORCH_REDSTONE = block.Block(76)\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    # every 4 blocks\r\n    # pos 0: rail and normal torch\r\n    # pos 1: powered rail\r\n    # pos 2: powered rail and redstone torch\r\n    # pos 3: rail \r\n    if x % 4 == 0:\r\n        mc.setBlock(x,y+1,z+1,block.TORCH.id,5)\r\n    if x % 4 == 2:\r\n        mc.setBlock(x,y+1,z+1,TORCH_REDSTONE.id,5)\r\n    if x % 4 == 1 or x % 4 == 2:\r\n        mc.setBlock(x,y+1,z,RAIL_POWERED)\r\n    else:\r\n        mc.setBlock(x,y+1,z,RAIL)\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 railway track we need to have some normal rail and some powered rail. Next to every section of powered rail we need to provide a redstone torch. First we define the items which are not defined in mcpi.block yet RAIL = block.Block(66) RAIL_POWERED = block.Block(27) TORCH_REDSTONE = block.Block(76) We are already placing a [&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-408","post","type-post","status-publish","format-standard","hentry","category-coderdojo"],"_links":{"self":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/408","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=408"}],"version-history":[{"count":3,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":412,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/408\/revisions\/412"}],"wp:attachment":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/media?parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/categories?post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/tags?post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}