Skip to content

Commit

Permalink
update sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
stoneskin committed Jun 16, 2020
1 parent bc8fcee commit 306028d
Show file tree
Hide file tree
Showing 16 changed files with 876 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ print("pos: x:{},y:{},z:{}".format(pos.x,pos.y,pos.z))
```

Use your faverate python editor to open the sample1.py file.
When you install python, it come with a python editor call IDLE.
When you install python, it come with a python editor call IDLE.j

### 1.2. Frequently used `mcpi` commands

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## 2.1 Understand the coordinates of minecraft

Minecraft coordinates is different than what we learn from geomestry. you need keep below picture in mind when you do the minecraft coding.
Minecraft coordinates is different than what we learn from geometry. you need keep below picture in mind when you do the minecraft coding.
![coordinates of minecraft](../minecraft_Coordinates.png)

For basic python syntax, pleas check [Python syntax](https://www.w3schools.com/python/python_syntax.asp) for details.
Expand Down Expand Up @@ -93,5 +93,20 @@ Try place a watermelon beside you.
mc.setBlock(x,y,z,103)

```
#### - [Mission-1.6] Use Python place multiple blocks point direction of x,y,z

Use python code to place color block like below

```python
mc=Minecraft.create(serverAddress,pythonApiPort,playerName)
pos=mc.player.getTilePos()
(x,y,z)=pos=mc.player.getPos()

mc.setBlock(x+1,y,z,block.WOOL_GREEN)
mc.setBlock(x+2,y,z,block.WOOL_RED)
mc.setBlock(x+3,y,z,block.WOOL_BLUE)
```

![xyz](./../xyz.jpg)

[back to main](../../README.md)
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ for k in range(0,5):

```

### - [Challenge] [Mission-2.5] Build a 10x10X10 pyramid in Minecraft
### - [Challenge] [Mission-2.5] Build a 10x10X5 pyramid in Minecraft

Modified your code in mission-2.4, build a 10X10X10 pyramid.
Modified your code in mission-2.4, build a 10X10X5 pyramid.
please show you work.

[back to main](../../README.md)
Binary file added documents/xyz.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions samples/dna.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

from functions.drawing import *
from random import choice
import mcpi_e.minecraft as minecraft
from mcpi_e.block import *

address="server"
name="stoneskin"
mc = minecraft.Minecraft.create(address,4712,name)

drawing = Drawing(mc)

pos = mc.player.getTilePos()
radius = 10
A = WOOL_ORANGE = Block(WOOL.id, 1)
C = WOOL_GREEN = Block(WOOL.id, 13)
G= WOOL_BLUE = Block(WOOL.id, 11)
T= WOOL_RED = Block(WOOL.id, 14)


blocks = { "A":(A,T), "C":(C,G), "T":(T,A), "G":(G,C) }
# sequence taken from somewhere in chromosome 5, retrieved via Wolfram Alpha
sequence = "CAAAAGTGTGGGGAAATTAATTTGGGAATTACTCTCCTCATTGAAAAATATCTCATTTGCTAAAATAAGACAGTAAAACAGTACAGTTTAAATATTTATAAAAATAGGAAAGTTTGGCAAAAAGAGAGGAGTACACACCTGTGACTACTGAGTTGCTGTGAAAATTTCATTTCCTGATACAAAATTGTCTAAAGCACTTG"

def getPair(i):
return blocks[sequence[-i-1]]

prev = None
skip = 2
for y in range(240):
theta = y * pi / (skip*10)
y1 = pos.y + y
x1 = pos.x - radius * cos(theta)
x2 = pos.x + radius * cos(theta)
z1 = pos.z + radius * sin(theta)
z2 = pos.z - radius * sin(theta)
if y % skip == 0:
pair = getPair(y//skip)
drawing.penwidth(1)
drawing.line(x1,y1,z1,pos.x,y1,pos.z,pair[0])
drawing.line(x2,y1,z2,pos.x,y1,pos.z,pair[1])

if prev is not None:
drawing.penwidth(2)
drawing.line(prev[0],prev[1],prev[2],x1,y1,z1,block.WOOL_WHITE)
drawing.line(prev[3],prev[4],prev[5],x2,y1,z2,block.WOOL_WHITE)
prev = x1,y1,z1,x2,y1,z2
30 changes: 30 additions & 0 deletions samples/donut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from mcpi_e.minecraft import Minecraft
from mcpi_e import block
from time import sleep
from random import *
from math import *

def draw_donut(mcx,mcy,mcz,R,r,mcblock):
for x in range(-R-r,R+r):
for y in range(-R-r,R+r):
xy_dist = sqrt(x**2 + y**2)
if (xy_dist > 0):
ringx = x / xy_dist * R # nearest point on major ring
ringy = y / xy_dist * R
ring_dist_sq = (x-ringx)**2 + (y-ringy)**2

for z in range(-R-r,R+r):
if (ring_dist_sq + z**2 <= r**2):
mc.setBlock(mcx+x, mcy+z, mcz+y, mcblock)

serverAddress="server" # change to your minecraft server
playerName ="stoneskin"
pythonApiPort=4712

mc=Minecraft.create(serverAddress,pythonApiPort,playerName)
playerPos=mc.player.getTilePos()

draw_donut(playerPos.x, playerPos.y + 9, playerPos.z, 18, 9, block.GLASS)
mc.postToChat("Glass donut done")
draw_donut(playerPos.x, playerPos.y + 9, playerPos.z, 18, 6, block.WATER_STATIONARY)
mc.postToChat("Water donut done")
41 changes: 41 additions & 0 deletions samples/egg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sys
import mcpi_e.block as block
from mcpi_e.minecraft import Minecraft
from math import *
Block = block.Block

def egg(block=block.GOLD_BLOCK, h=40, a=2.5, b=1, c=0.1):
for y in range(0,h+1):
l = y / float(h)
# Formula from: http://www.jolyon.co.uk/myresearch/image-analysis/egg-shape-modelling/
r = h*a*exp((-0.5*l*l+c*l-.5*c*c)/(b*b))*sqrt(1-l)*sqrt(l)/(pi*b)
r2 = r*r
for x in range(-h,h+1):
for z in range(-h,h+1):
myr2 = x*x + z*z
if myr2 <= r2:
if x==0 and z==0:
theta = 0
else:
theta = atan2(z,x)
yield (x,y,z,block,theta)

address="server"
name="stoneskin"
mc = Minecraft.create(address,4712,name)


if len(sys.argv) >= 2:
height = int(sys.argv[1])
else:
height = 50

if len(sys.argv) >= 3:
material = Block.byName(sys.argv[2])
else:
material = block.GOLD_BLOCK

pos = mc.player.getPos()

for (x,y,z,block,theta) in egg(h=height,block=material):
mc.setBlock(x+pos.x,y+pos.y,z+pos.z,block)
Loading

0 comments on commit 306028d

Please sign in to comment.