当前位置: 首页>>代码示例>>Python>>正文


Python Engine.rotate方法代码示例

本文整理汇总了Python中engine.Engine.rotate方法的典型用法代码示例。如果您正苦于以下问题:Python Engine.rotate方法的具体用法?Python Engine.rotate怎么用?Python Engine.rotate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在engine.Engine的用法示例。


在下文中一共展示了Engine.rotate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: App

# 需要导入模块: from engine import Engine [as 别名]
# 或者: from engine.Engine import rotate [as 别名]
class App():
    def __init__(self):
        self.root = tk.Tk()
        self.e = Engine()
        self.e.update_position(0, 0, 0, 0, 0, 0)
        self.canvas = tk.Canvas(self.root, width=canvas_width, height=canvas_height)
        self.canvas.pack()
        self.speed_up = self.speed_right = self.speed_forward = self.speed_alpha = self.speed_beta = self.speed_gamma = 0
        self.root.focus_set()
        self.root.bind("<KeyPress>", self.key_down)
        self.root.bind("<KeyRelease>", self.key_up)
        self.last_time = time.time()
        self.update_clock()
        self.root.mainloop()

    def update_clock(self):
        deltat = time.time() - self.last_time
        self.last_time = time.time()
        self.e.move(deltat * speed * self.speed_forward, deltat * speed * self.speed_up, deltat * speed * self.speed_right)
        self.e.rotate(deltat * rot_speed * self.speed_alpha, deltat * rot_speed * self.speed_beta, deltat * rot_speed * self.speed_gamma)
        self.canvas.delete('all')
        self.canvas.create_text(30, 10, text='FPS: ' + str(int(1/deltat)))
        self.draw_cube(2, 5, -0.5, 0.5, -0.5, 0.5, 'red')
        self.draw_cube(3.45, 3.55, 0.5, 0.6, -0.05, 0.05, 'red')
        self.draw_poly([[2, -0.5, -0.5], [2, -0.5, 0.5], [5, -0.5, 0.5], [5, -0.5, -0.5]], 'red')
        #self.draw_cube(5, 7, 3, 1, -1, 1)
        #world.draw(self.canvas)
        self.root.after(1, self.update_clock)

    def draw_poly(self, p, color=None):
        allCoords = []
        for i in p:
            a = self.e.get_2d_point(*i)

            allCoords.append(self.screen_pos_x(a[0]))
            allCoords.append(self.screen_pos_y(a[1]))
        if color:
            self.canvas.create_polygon(*allCoords, fill=color)
        else:
            self.canvas.create_polygon(*allCoords)

    def draw_cube(self, x1, x2, y1, y2, z1, z2, color=None):
        self.draw_poly([[x1, y1, z1], [x2, y1, z1], [x2, y2, z1], [x1, y2, z1]], color)
        self.draw_poly([[x1, y1, z2], [x2, y1, z2], [x2, y2, z2], [x1, y2, z2]], color)

        self.draw_poly([[x1, y2, z2], [x2, y2, z2], [x2, y2, z1], [x1, y2, z1]], color)
        self.draw_poly([[x1, y2, z2], [x2, y2, z2], [x2, y2, z2], [x1, y2, z2]], color)

        # self.draw_line([x1, y1, z1], [x2, y1, z1])
        # self.draw_line([x1, y1, z1], [x1, y2, z1])
        # self.draw_line([x1, y1, z1], [x1, y1, z2])
        #
        # self.draw_line([x2, y2, z1], [x1, y2, z1])
        # self.draw_line([x2, y2, z1], [x2, y1, z1])
        # self.draw_line([x2, y2, z1], [x2, y2, z2])
        #
        # self.draw_line([x2, y1, z2], [x1, y1, z2])
        # self.draw_line([x2, y1, z2], [x2, y2, z2])
        # self.draw_line([x2, y1, z2], [x2, y1, z1])
        #
        # self.draw_line([x1, y2, z2], [x2, y2, z2])
        # self.draw_line([x1, y2, z2], [x1, y1, z2])
        # self.draw_line([x1, y2, z2], [x1, y2, z1])

    def draw_line(self, start3, end3):
        start2 = self.e.get_2d_point(start3[0], start3[1], start3[2])
        end2 = self.e.get_2d_point(end3[0], end3[1], end3[2])
        self.canvas.create_line(self.screen_pos_x(start2[0]), self.screen_pos_y(start2[1]), self.screen_pos_x(end2[0]),  self.screen_pos_y(end2[1]), fill="#476042")

    def screen_pos_x(self, x):
        return x*zoom+canvas_width/2

    def screen_pos_y(self, x):
        return x*zoom+canvas_height/2

    def key_down(self, event):
        if event.keycode == 852087:
            self.speed_forward = 1
        elif event.keycode == 65651:
            self.speed_forward = -1
        elif event.keycode == 131172:
            self.speed_right = 1
        elif event.keycode == 97:
            self.speed_right = -1
        elif event.keycode == 131074:
            self.speed_up = 1
        elif event.keycode == 3211296:
            self.speed_up = -1
        elif event.keycode == 5636148:
            self.speed_alpha = 1
        elif event.keycode == 5767222:
            self.speed_alpha = -1
        elif event.keycode == 5505074:
            self.speed_beta = -1
        elif event.keycode == 5963832:
            self.speed_beta = 1
        elif event.keycode == 5832759:
            self.speed_gamma = 1
        elif event.keycode == 6029369:
            self.speed_gamma = -1
#.........这里部分代码省略.........
开发者ID:NoahPeeters,项目名称:CubeWorld,代码行数:103,代码来源:main2.py

示例2: __init__

# 需要导入模块: from engine import Engine [as 别名]
# 或者: from engine.Engine import rotate [as 别名]

#.........这里部分代码省略.........
            self.block_y = new_block_y
        else:
            self.block_y = round(new_block_y)

        if not (self.e.block_exists(self.block_x, self.block_y, new_block_z + 0.5) or self.e.block_exists(self.block_x,
                                                                                                          self.block_y,
                                                                                                          new_block_z - 0.5)):
            self.block_z = new_block_z
        else:
            self.block_z = round(new_block_z)

        if self.block_state == 3:
            if abs(self.block_x - self.target_block_x) < 0.005 and abs(
                            self.block_y - self.target_block_y) < 0.005 and abs(
                        self.block_z - self.target_block_z) < 0.005:
                self.block_state = 0
                key = self.e.get_key(self.target_block_x, self.target_block_y, self.target_block_z)
                self.e.world[key] = 1

        self.block_dist_speed = self.block_dist_speed + self.block_dist_accel * delta_t - self.block_dist_speed / 6

        self.block_dist += self.block_dist_speed * delta_t
        self.speed_forward = self.speed_forward + self.accel_forward * delta_t - self.speed_forward / 3
        self.speed_right = self.speed_right + self.accel_right * delta_t - self.speed_right / 3
        self.e.move(delta_t * self.speed_forward, delta_t * self.speed_up,
                    delta_t * self.speed_right)

        if self.speed_up <= 0 and self.e.block_exists(self.e.x, self.e.y - 1.50000001, self.e.z):
            self.speed_up = 0
            self.e.y = round(self.e.y)
        else:
            self.speed_up -= 9.81 * delta_t

        self.e.rotate(delta_t * rot_speed * self.alpha, delta_t * rot_speed * self.beta)
        self.alpha = 0
        self.beta = 0
        self.canvas.delete('all')
        self.render()
        self.canvas.create_text(90, 10, text='FPS: ' + str(int(1 / delta_t)))
        # self.canvas.create_text(90, 25, text='x: ' + str(self.e.x))
        # self.canvas.create_text(90, 40, text='y: ' + str(self.e.y))
        # self.canvas.create_text(90, 55, text='z: ' + str(self.e.z))
        # self.canvas.create_text(90, 70, text='a: ' + str(self.e.alpha))
        # self.canvas.create_text(90, 85, text='b: ' + str(self.e.beta))
        self.root.after(1, self.update_clock)

    @staticmethod
    def get_key(x, y, z):
        return str(x) + '/' + str(y) + '/' + str(z)

    def render(self):
        all_sites = []
        for x in range(int(self.e.x - render_dist), int(self.e.x + render_dist + 1)):
            for y in range(int(self.e.y - render_dist - 10), int(self.e.y + render_dist + 1)):
                for z in range(int(self.e.z - render_dist), int(self.e.z + render_dist + 1)):
                    a = -math.atan2(self.e.x - x, self.e.z - z) - math.pi / 2
                    dx = self.e.x - x
                    dz = self.e.z - z
                    if abs(self.e.alpha - a) < math.pi / 3 or \
                                    abs(self.e.alpha - a - math.pi * 2) < math.pi / 3 or \
                                    math.sqrt(dx * dx + dz * dz) < 2:
                        block_id = self.e.get_block(x, y, z)
                        if block_id != 0:
                            if self.block_marked and x == self.target_block_x and y == self.target_block_y and z == self.target_block_z:
                                block_id = -1
                            for i in self.get_sites(x, y, z, block_id):
开发者ID:NoahPeeters,项目名称:CubeWorld,代码行数:70,代码来源:main.py


注:本文中的engine.Engine.rotate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。