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


Python Engine.move方法代码示例

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


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

示例1: App

# 需要导入模块: from engine import Engine [as 别名]
# 或者: from engine.Engine import move [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 move [as 别名]

#.........这里部分代码省略.........
        self.board = dict()
        back = Frame(self.window)
        back.pack(fill=BOTH, expand=1)

        for row in range(8):
            frame = Frame(back)
            frame.pack(fill=BOTH, expand=1)
            for column in range(8):
                button = Button(frame,
                                state=DISABLED,
                                command=lambda position=(row, column): self.play(position))
                button["bg"] = "gray"
                button.pack(side=LEFT, fill=BOTH, expand=1)
                self.board.update({(row, column): button})

    def create_options(self):
        pass_turn = Button(self.window, text="Pass", command=self.pass_turn)
        pass_turn.pack(side=RIGHT)
        self.status = Label(self.window)
        self.update_status("Welcome to MonOthello!")
        self.status.pack(side=LEFT)

    def create_game(self):
        message="Are you sure you want to restart?"
        if self.game and \
           not tkMessageBox.askyesno(title="New", message=message):
                return

        self.game = Engine()
        self.update_board()
        message = "Let's play! Now it's the %s's turn." % (self.game.turn.name,)
        self.update_status(message)

    def toggle_show_valid_positions(self):
        self.show_valid_positions = not self.show_valid_positions
        if self.game:
            self.update_board()

    def pass_turn(self):
        if not self.game:
            return
        self.game.change_turn()
        self.update_board()
        message = "%s's turn." % (self.game.turn.name,)
        self.update_status(message)

    def show_credits(self):
        message = "MonOthello\nv.: 1.0"
        tkMessageBox.showinfo(title="About", message=message)

    def bye(self):
        if tkMessageBox.askyesno(title="Quit", message="Really quit?"):
            quit()

    def play(self, position):
        """Move a piece to the given position."""

        if not self.game:
            return
        if not self.game.move(position, True):
            message = "Wrong move. %s's turn." % (self.game.turn.name,)
            self.update_status(message)
        else:
            self.update_board()
            if self.game.check_end():
                message = "End of game. "
                if self.game.someone_winning():
                    message += self.game.who_is_winning() + " won!"
                else:
                    message += "Tie."
                tkMessageBox.showinfo(title="End of game", message=message)
                self.game = False
            else:
                message = "%s's turn." % (self.game.turn.name,)
                self.update_status(message)

    def update_board(self):
        for row in range(8):
            for column in range(8):
                position = self.board[(row, column)]
                current_cell = self.game.board[(row, column)]

                position["bg"] = current_cell.color
                position["state"] = DISABLED
                if isinstance(current_cell, EmptyPiece):
                    position["state"] = NORMAL

        if self.show_valid_positions:
            valid_positions = self.game.find_valid_positions()
            for position in valid_positions:
                self.board[position]["bg"] = "green"
        self.update_score()

    def update_score(self):
        self.score["text"] = "Black: %s | White: %s" % \
                             (self.game.black_score,
                              self.game.white_score)

    def update_status(self, message):
        self.status["text"] = message
开发者ID:rhcarvalho,项目名称:monothello,代码行数:104,代码来源:monothello.py

示例3: __init__

# 需要导入模块: from engine import Engine [as 别名]
# 或者: from engine.Engine import move [as 别名]
class App:
    def __init__(self):
        self.root = tk.Tk()
        self.canvas = tk.Canvas(self.root, width=canvas_width, height=canvas_height)
        self.canvas.pack()
        self.e = Engine(self.canvas, canvas_width, canvas_height)
        self.e.update_position(0, 2, 0, 0, 0)
        self.speed_up = self.speed_right = self.speed_forward = self.alpha = self.beta = 0
        self.accel_right = self.accel_forward = 0
        self.block_x = self.block_z = self.block_y = 0
        self.target_block_x = self.target_block_y = self.target_block_z = 0
        self.block_state = 0
        self.block_dist = 3
        self.block_dist_speed = self.block_dist_accel = 0
        self.jumped = self.block_marked = False
        self.mouse_start_x = self.mouse_start_y = 0
        self.root.focus_set()
        self.root.bind("<KeyPress>", self.key_down)
        self.root.bind("<KeyRelease>", self.key_up)
        self.root.bind("<Button-1>", self.mouse_down)
        self.root.bind("<B1-Motion>", self.mouse_moved)

        self.load()

        self.last_time = time.time()
        self.update_clock()
        self.root.mainloop()

    def load(self):
        if os.path.isfile('save.p'):
            data = pickle.load(open("save.p", "rb"))
            self.e.world = data[0]
            self.e.x = data[1]
            self.e.y = data[2]
            self.e.z = data[3]
            self.e.alpha = data[4]
            self.e.beta = data[5]
            self.block_marked = data[6]
            self.block_x = data[7]
            self.block_y = data[8]
            self.block_z = data[9]
            self.block_state = data[10]
            self.block_dist = data[11]

    def save(self):
        data = [self.e.world, self.e.x, self.e.y, self.e.z, self.e.alpha, self.e.beta, self.block_marked, self.block_x,
                self.block_y, self.block_z, self.block_state, self.block_dist]
        pickle.dump(data, open("save.p", "wb"))

    def update_clock(self):
        delta_t = time.time() - self.last_time
        self.last_time = time.time()

        self.block_marked = False
        if self.block_state == 0:
            for i in range(5):
                x = self.e.x + self.e.player_wx * i
                y = self.e.y + self.e.player_wy * i
                z = self.e.z + self.e.player_wz * i
                if self.e.block_exists(x, y, z):
                    self.block_marked = True
                    self.target_block_x = round(x)
                    self.target_block_y = round(y)
                    self.target_block_z = round(z)
                    break

        if self.block_state == 1:
            self.target_block_x = self.e.x + self.e.player_wx * self.block_dist
            self.target_block_y = self.e.y + self.e.player_wy * self.block_dist
            self.target_block_z = self.e.z + self.e.player_wz * self.block_dist
        elif self.block_state == 2:
            self.target_block_x = round(self.block_x)
            self.target_block_y = round(self.block_y)
            self.target_block_z = round(self.block_z)
            self.block_state = 3

        new_block_x = self.block_x + (self.target_block_x - self.block_x) / 2 * delta_t * 15
        new_block_y = self.block_y + (self.target_block_y - self.block_y) / 2 * delta_t * 15
        new_block_z = self.block_z + (self.target_block_z - self.block_z) / 2 * delta_t * 15

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

        if not (self.e.block_exists(self.block_x, new_block_y + 0.5, self.block_z) or self.e.block_exists(self.block_x,
                                                                                                          new_block_y - 0.5,
                                                                                                          self.block_z)):
            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
#.........这里部分代码省略.........
开发者ID:NoahPeeters,项目名称:CubeWorld,代码行数:103,代码来源:main.py

示例4: Player

# 需要导入模块: from engine import Engine [as 别名]
# 或者: from engine.Engine import move [as 别名]
class Player(object):
    side = None
    position = set()
    engine = None
    desk_coords = None
    steps = []

    def __init__(self):
        d = Desk()
        self.position = d.get_position()
        self.side = d.side
        self.desk_coords = d.desk_coords
        self.desk = d

    def refresh_desk(self):
        time.sleep(2)
        d = Desk(self.side, self.desk_coords)
        self.position = d.get_position()
        print 'refreshed'

    def change_step_side(self):
        if self.step_side == 'w':
            self.step_side = 'b'
        else:
            self.step_side = 'w'


    def change_position(self, move):
        print u'изменение позиции %s' %move
        from_move = move[:2]
        to_move = move[2:4]
        figure = move[4:]
        position_dict = {i[0]: i[1] for i in self.position}
        # white short castle
        if from_move == 'e1' and to_move == 'g1':
            del position_dict[from_move]
            del position_dict['h1']
            position_dict[to_move] = 'kw'
            position_dict['f1'] = 'rw'
        # white long castle
        elif from_move == 'e1' and to_move == 'c1':
            del position_dict[from_move]
            del position_dict['a1']
            position_dict[to_move] = 'kw'
            position_dict['d1'] = 'rw'
        # black short castle
        elif from_move == 'e8' and to_move == 'g8':
            del position_dict[from_move]
            del position_dict['h8']
            position_dict[to_move] = 'kb'
            position_dict['f8'] = 'rb'
        # black long castle
        elif from_move == 'e8' and to_move == 'c8':
            del position_dict[from_move]
            del position_dict['a8']
            position_dict[to_move] = 'kb'
            position_dict['d8'] = 'rb'
        # p to q
        elif figure:
            del position_dict[from_move]
            position_dict[to_move] = figure + self.step_side
        # atack or step
        else:
            move_fig = position_dict[from_move]
            position_dict[to_move] = move_fig
            del position_dict[from_move]
        position_set = set()
        for field, figure in position_dict.iteritems():
            position_set.add((field, figure))
        self.position = position_set


    def start(self):
        self.engine = Engine()
        self.engine.new_game()
        self.engine.set_start_position()
        if self.side == 'WHITE':
            self.step_side = 'w'
            move = self.engine.calulate()
            print 'game start'
            print move
            print 'game go'
            self.steps.append(move)
            self.go(move)
            self.change_step_side()
            self.change_position(move)
        else:
            self.step_side = 'b'

    def go(self, move=None):
        _from = move[:2]
        _to = move[2:4]
        delta = self.desk_coords
        from_position = self.desk.get_field_coord(_from)
        to_position = self.desk.get_field_coord(_to)
        # Двигает мышку
        subprocess.call('xdotool mousemove %d %d' %(from_position[0] + delta[0], from_position[1] + delta[1]), shell=True)
        subprocess.call('xdotool mousedown 1', shell=True)
        subprocess.call('xdotool mousemove %d %d' %(to_position[0] + delta[0], to_position[1] + delta[1]), shell=True)
        subprocess.call('xdotool mouseup 1', shell=True)
#.........这里部分代码省略.........
开发者ID:metallicon1,项目名称:chess,代码行数:103,代码来源:player.py


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