def wait_for_input(self): move = input("Enter your next move, 'stop' to interrupt the program\n") try: target, destination = move.upper().split() conversion = lambda letter: 'ABCDEFGH'.index(letter) initial_position = vector.Vector2f(conversion(target[0]), int(target[1]) - 1) final_position = vector.Vector2f(conversion(destination[0]), int(destination[1]) - 1) return (initial_position, final_position) except ValueError: if move == 'stop': return None, None print("Invalid input, try again") return self.wait_for_input()
def render_tkinter_with_canvas(self, renderer): """ Create the graphical interface with the chessboard and the number 1-8 and A-H. """ root = renderer.thread.queue.get(timeout=1) renderer.thread.queue.put(root) self.create_menu(root, renderer) LETTER_LIST = ("A", "B", "C", "D", "E", "F", "G", "H") renderer.canvas = tkinter.Canvas(master=root, height=renderer.CANVAS_SIZE, width=renderer.CANVAS_SIZE) renderer.canvas.grid(row=1, column=2, rowspan=8, columnspan=8) white = True for i in range(1, 9): self.display_labels(root, LETTER_LIST, i) for j in range(1, 9): renderer.canvas.create_rectangle( renderer.CANVAS_SIZE / 8 * (i - 1), renderer.CANVAS_SIZE / 8 * (j - 1), renderer.CANVAS_SIZE / 8 * i, renderer.CANVAS_SIZE / 8 * j, fill=('#eccca1' if white else '#c78b57')) renderer.cases_position.append(( renderer.CANVAS_SIZE / 8 * (i - 1), renderer.CANVAS_SIZE / 8 * (j - 1), renderer.CANVAS_SIZE / 8 * i, renderer.CANVAS_SIZE / 8 * j, )) white = not white white = not white # position where the first piece should be on the canvas renderer.canvas.piece_position = vector.Vector2f(0, 7)
def randomise_position(self): corner_x1 = random.randrange(self.view.borders.x1, self.view.borders.x2) print(corner_x1) corner_x2 = corner_x1 + 40 if corner_x1 + 40 <= self.view.borders.x2 else corner_x1 - 40 corner_y1 = random.randrange(self.view.borders.y1, self.view.borders.y2) corner_y2 = corner_y1 + 40 if corner_y1 + 40 < self.view.borders.y2 else corner_y1 - 40 return vector.Vector2f(corner_x1, corner_y1, corner_x2, corner_y2)
def convert_to_chess_coords(self, position): for i in range(8): if self.renderer.CANVAS_SIZE / 8 * i <= position[ 0] < self.renderer.CANVAS_SIZE / 8 * (i + 1): position_x = i if self.renderer.CANVAS_SIZE / 8 * i <= position[ 1] < self.renderer.CANVAS_SIZE / 8 * (i + 1): position_y = 7 - i return vector.Vector2f(position_x, position_y)
def split_informations(self, file_link): with open(file_link) as file: infos = file.read() info_split = infos.split("; ") name = info_split[0] money = int(info_split[1]) monster = info_split[2] position = vector.Vector2f(int(info_split[3]), int(info_split[4]), int(info_split[5]), int(info_split[6])) msgs = info_split[7].split("\n") msgs.remove("") return name, money, monster, position, msgs
def __init__(self, master): super().__init__(master=master, width=self.WIDTH, height=self.HEIGHT, bg="black") self.reference = [] # photoImage = tk.PhotoImage(file="characters/main_character.png") # self.player_canvas = self.create_image(500, 300, image=photoImage) # self.reference.append(photoImage) self.npcs = [] self.grid(row=0, column=0, columnspan=2) self.borders = vector.Vector2f(self.WIDTH / 2 - 1240, self.HEIGHT - 3508, self.WIDTH / 2 + 1240, self.HEIGHT) self.moving = None self.menu = False
def render_tkinter_with_frame(self, renderer): if self.display_image is None: self.load_display_image_frame(renderer) next = None if self.next_position is None: next = self.position else: next = self.next_position print(next) if next == vector.Vector2f(-1, -1): self.display_image.grid() real_next = self.convert_to_tkinter_coords(next) # there might be a better way of doing this self.display_image.grid(column=int(real_next.x) + 1, row=int(real_next.y) + 1) self.position = next self.next_position = None
def render_tkinter_with_canvas(self, renderer): if self.display_image is None: self.load_display_image_canvas(renderer) next = None current_pos = None if self.next_position is None: next = self.position current_pos = vector.Vector2f(0, 7) else: next = self.next_position current_pos = self.position real_next = self.convert_to_canvas_coords(renderer, next) position_in_canvas_coords = self.convert_to_canvas_coords( renderer, current_pos) renderer.canvas.move(self.display_image, -(position_in_canvas_coords.x - real_next.x), -(position_in_canvas_coords.y - real_next.y)) self.position = next self.next_position = None
def update_view(self, x, y): """ Moves the map so there is an illusion that it is the character that is currently moving. """ self.move("move", x, y) self.borders += vector.Vector2f(x, y, x, y)
def update_position(self, direction): move_x, move_y = self.DIRECTION[direction] self.position += vector.Vector2f(move_x, move_y, move_x, move_y)
def convert_to_tkinter_coords(self, coords): return vector.Vector2f(coords.x, 7 - coords.y)
def convert_to_canvas_coords(self, renderer, coords): return vector.Vector2f( renderer.CANVAS_SIZE / 16 + renderer.CANVAS_SIZE / 8 * coords.x, (renderer.CANVAS_SIZE - renderer.CANVAS_SIZE / 16) - renderer.CANVAS_SIZE / 8 * coords.y)