Keyboard Control
Key Board Control
def key_pressed(key_code):
print(key_code)
game.on_key_press(key_pressed)
# my practice
def pressKey(key):
block(0,5,-20, 'box_yellow')
game.on_key_press(pressKey) # when any key is pressed, a yellow block appear
// press r, red block appear; press other keys, red block disappear
materials["air"] = 0
def redBlock(key):
if key=="KeyR":
block(0, 5, -20, "box_red")
else:
block(0,5,-20, "air")
game.on_key_press(redBlock)
material = 0
def key_pressed(c):
global material
if c == 'Digit1':
material = 1
elif c == 'Digit2':
material = 2
game.on_key_press(key_pressed)
def put_block(mouse_p):
game.set_block(mouse_p, material)
game.on_click(put_block)
# material string name
material = 0
def key_pressed(c):
global material
if c == 'Digit1':
material = "box_red"
elif c == 'Digit2':
material = "box_yellow"
game.on_key_press(key_pressed)
def put_block(mouse_p):
game.set_block(mouse_p, materials[material])
game.on_click(put_block)
Makeup Topic: variable scope
Global or local
===
JavaScript:
game.onKeyPress(function(keyCode) {
console.log(keyCode);
});
===
on_click(make_door)
def openDoor(mp):
for i in range(3):
for j in range(5):
block(mp.x+i, mp.y+j, mp.z, 'air')
# block(x,y,z,material)
# barx,y,z
def bar_x(x,y,z,length, material):
for i in range(x, (x+length)):
block(i,y,z, material)
#3-13 bar_y
def bar_y(x,y,z,height, material):
for j in range(y, (y+height)):
block(x,j,z, material)
def openWindow(mp):
for j in [mp-3,mp,mp+3]
bar_x(mp.x-3, mp.y+j, mp.z, 7, 'log_big_oak')