Player Position
Sometimes I like to view a structure from the sky. Due to the gravity in CodeCraft world, a player cannot stay still in the air to take a screenshot. To solve that problem, we can build a floating platform for the player to stand on.
Let's introduce two more methods in CodeCraft: getter and setter of the player's current position.
The getter: game.get_player_position()
returns player's position as a Position
object that contains the coordinates x, y
and z
.
The setter: game.set_player_position(pos)
moves player to location indicated by pos
which is also a Position
object.
Examples:
# call the getter to show the player's position
p = game.get_player_position()
print(p) # (x:10.3, y:1.5, z:-5)
# call the setter to relocate the player
game.set_player_position(Position(0, 1, -10))
# call the getter again to see the player's position changed
print(game.get_player_position()) # (x:0, y:1, z:-10)
Run the above example. You will be teleported to the new location immediately.
A floating platform
Steps to build a platform in the air above the player's ground location:
- Find out the ground location of the player
Position(x, y, z)
- Build a floating platform at
(x, y + 10, z)
- Let the player's position at
(x, y + 11, z)
Note that player's current coordinates can be floating-point numbers (decimals, when you're standing between blocks), but blocks can only be built at integer coordinates. We are going to use int()
for the conversion.
# Step 1, get player position
p = game.get_player_position()
# Step 2, build a platform above player's head in the sky
flat_xz(int(p.x) - 2, 10, int(p.z) - 2, 5, 5, 'brick')
# Step 3, set the player position
game.set_player_position(Position(p.x, 11, p.z))
Step 1 gets the player's position p
as a Position object.
In step 2, p.x
tells the computer to access the x
variable of the Position object p
using the dot notation.
Step 3 creates a new Position object right on top of the platform and move the player there.
Now you can see the block array from the sky:
Here is the house viewed from a high angle:
Wait, we didn't learn to build a house yet, and what's that huge red ball? We'll get there soon!