模型类
现在我们来实现_show_block
和_hide_block
函数:
class Model:
...
def _show_block(self, position, block):
x, y, z = position
# 得到立方体的坐标
vertex_data = cube_vertices(x, y, z, 0.5)
# == A ==
self._shown[position] = self.batch.add(24, GL_QUADS, blocks[block].textures,
('v3f/static', vertex_data),
# 还记得吗?这个 tex_coords 是全局变量哦,不记得的话赶紧看一眼第二篇
('t2f/static', tex_coords))
def _hide_block(self, position):
try:
# 删除方块的显示数据
self._shown.pop(position).delete()
except KeyError:
pass
A:pyglet.graphics.Batch
的用法可以参考这里的文档。
还有两个函数,现在还用不到,不过和_deque
有关,就先讲了吧。感觉现在结束这篇文章的话篇幅不太够。
class Model:
...
def process_queue(self):
start = time.perf_counter()
while self.queue and time.perf_counter() - start < 1.0 / Settings.ticks_per_sec:
self._dequeue()
def process_entire_queue(self):
while self.queue:
self._dequeue()
这两个函数很简单,第一个是为了每隔一段时间执行一下操作减轻计算机的负担 。第二个就是一下子把整个队列全部处理完。等到用到的时候我们再详细说。
到这里,我们的模型类就暂时先告一段落。下一篇文章,我们要编写Window
类了。不懂的地方欢迎评论区讨论哦。大家点个赞吧~