本文整理汇总了Python中world.World.remove_block方法的典型用法代码示例。如果您正苦于以下问题:Python World.remove_block方法的具体用法?Python World.remove_block怎么用?Python World.remove_block使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类world.World
的用法示例。
在下文中一共展示了World.remove_block方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Game
# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import remove_block [as 别名]
#.........这里部分代码省略.........
# falling / rising.
self.dy = 0
break
return tuple(p)
def on_mouse_press(self, x, y, button, modifiers):
""" Called when a mouse button is pressed. See pyglet docs for button
amd modifier mappings.
Parameters
----------
x, y : int
The coordinates of the mouse click. Always center of the screen if
the mouse is captured.
button : int
Number representing mouse button that was clicked. 1 = left button,
4 = right button.
modifiers : int
Number representing any modifying keys that were pressed when the
mouse button was clicked.
"""
if self.exclusive:
vector = self.get_sight_vector()
block, previous = self.world.hit_test(self.position, vector)
if (button == mouse.RIGHT) or \
((button == mouse.LEFT) and (modifiers & key.MOD_CTRL)):
# ON OSX, control + left click = right click.
if previous:
self.world.add_block(previous, self.block)
elif button == pyglet.window.mouse.LEFT and block:
texture = self.world.world[block]
if texture != Textures.STONE:
self.world.remove_block(block)
else:
self.set_exclusive_mouse(True)
def on_mouse_motion(self, x, y, dx, dy):
""" Called when the player moves the mouse.
Parameters
----------
x, y : int
The coordinates of the mouse click. Always center of the screen if
the mouse is captured.
dx, dy : float
The movement of the mouse.
"""
if self.exclusive:
m = 0.15
x, y = self.rotation
x, y = x + dx * m, y + dy * m
y = max(-90, min(90, y))
self.rotation = (x, y)
def on_key_press(self, symbol, modifiers):
""" Called when the player presses a key. See pyglet docs for key
mappings.
Parameters
----------
symbol : int
Number representing the key that was pressed.
modifiers : int
Number representing any modifying keys that were pressed.
示例2: Game
# 需要导入模块: from world import World [as 别名]
# 或者: from world.World import remove_block [as 别名]
#.........这里部分代码省略.........
self.player.update(dt)
self.apply_gravity(self.player, dt)
new_pos = self.world.collides(self.player)
# new_pos contains the nearest position without collisions
# If we collided on the y-axis stop gravity
if new_pos[1] != self.player.position[1]:
self.player.velocity[1] = 0
self.player.position = new_pos
def hit_test(self, position, direction, max_distance=8):
"""Tests whether a block is hit.
We draw a line from the position towards with a given direction for
max_distance blocks-length. If at any time we hit a block we return
that block and the previous block. The previous block is the block
we pass through before we hit the block.
Args:
position: The position from which we draw a line.
direction: The direction we draw the line in.
max_distance: The maximum length in number of blocks
Returns:
A tuple (prev, curr) with the previous and current block if a block
has been hit, (None, None) otherwise.
"""
x, y, z = position
x_dir, y_dir, z_dir = direction
num_steps = 10
x_step = x_dir/num_steps
y_step = y_dir/num_steps
z_step = z_dir/num_steps
prev_pos = None
for step in xrange(num_steps*max_distance):
block_pos = discretize((x, y, z))
if prev_pos != block_pos and self.world.occupied(block_pos):
return prev_pos, block_pos
prev_pos = block_pos
x, y, z = x+x_step, y+y_step, z+z_step
return None, None
def position_intersects_object(self, position, obj):
"""Checks whether a position intersects with an object.
"""
x, y, z = discretize(obj.position)
for dy in xrange(obj.height):
if position == (x, y-dy, z):
return True
return False
def on_key_press(self, pressed_key, modifiers):
"""Handles key presses.
"""
self.player.on_key_press(pressed_key, modifiers)
if pressed_key == key.ESCAPE:
self.set_exclusive_mouse(False)
def on_key_release(self, pressed_key, modifiers):
"""Handles key releases.
"""
self.player.on_key_release(pressed_key, modifiers)
def on_mouse_motion(self, x, y, dx, dy):
"""Handles mouse motion.
If the window has already captured the mouse (is exclusive), we move
the camera of the player, otherwise we ignore the mouse motion.
"""
if self.exclusive:
self.player.on_mouse_motion(x, y, dx, dy)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
"""Handles mouse drag.
When a mouse drag we just move it.
"""
self.on_mouse_motion(x, y, dx, dy)
def on_mouse_press(self, x, y, button, modifiers):
"""Handles mouse presses.
If the window does not capture the mouse we capture it. If we are
already exclusive we either remove a block (left-mouse click) or we
add a block (right-mouse click).
"""
if not self.exclusive:
self.set_exclusive_mouse(True)
else:
direction = self.player.camera_direction()
prev_block_pos, block_pos = self.hit_test(self.player.position,
direction)
if not block_pos or not prev_block_pos:
return
if button == mouse.LEFT:
self.world.remove_block(block_pos)
elif button == mouse.RIGHT:
if not self.position_intersects_object(prev_block_pos,
self.player):
self.world.add_block(prev_block_pos,
self.player.active_block)