Python Tkinter Tutorial | 2D Grid-Based Map Part 3

Поділитися
Вставка
  • Опубліковано 28 січ 2025

КОМЕНТАРІ • 4

  • @ardhenismuhammadaflah7818
    @ardhenismuhammadaflah7818 3 місяці тому +1

    Nice tutorial btw

  • @CodeQuest1
    @CodeQuest1  3 місяці тому +1

    FINAL CODE
    from tkinter import *
    class Root(Tk):
    def __init__(self):
    super().__init__()
    self.title("2D Grid Map")
    self.state("zoomed")

    #Player Coords
    self.player_x = 3
    self.player_y = 7

    #Control Frame
    self.control_fr = Frame(self)
    self.control_fr.pack(side=LEFT, fill=Y)

    #Control - Title
    self.control_lbl = Label(self.control_fr, text="Controls")
    self.control_lbl.grid(row=0, column=0, columnspan=2)

    #Control - World Size
    self.world_size_lbl = Label(self.control_fr, text="World Size:")
    self.world_size_lbl.grid(row=1, column=0)

    self.world_size_var = StringVar()
    self.world_size_var.set("10")
    self.world_size_var.trace_add("write", self.trace)

    self.world_size_ent = Entry(self.control_fr, textvariable=self.world_size_var)
    self.world_size_ent.grid(row=1, column=1)

    #Control - Update
    self.update_btn = Button(self.control_fr, text="Update", command=self.update_map)
    self.update_btn.grid(row=2, column=0, columnspan=2)

    #Map Frame
    self.map_fr = Frame(self)
    self.map_fr.pack(side=LEFT, fill=BOTH, expand=1)

    #Map - Location Label
    self.loc_var = StringVar()
    self.loc_var.set("Location: ")

    self.loc_lbl = Label(self.map_fr, textvariable=self.loc_var)
    self.loc_lbl.pack()

    #Map - Canvas
    self.map_can = Canvas(self.map_fr, bg="gray")
    self.map_can.pack(fill=BOTH, expand=1)

    #Bindings
    self.bind("", lambda e: self.move_player(e, -1, 0))
    self.bind("", lambda e: self.move_player(e, 1, 0))
    self.bind("", lambda e: self.move_player(e, 0, -1))
    self.bind("", lambda e: self.move_player(e, 0, 1))

    self.trace()

    def trace(self, *args):
    try:
    world_size = int(self.world_size_var.get())

    if world_size < 1:
    raise ValueError

    else:
    self.update_btn.config(state=NORMAL)

    except ValueError:
    self.update_btn.config(state=DISABLED)

    def update_map(self):
    self.update_loc()

    #Draw Tiles
    can = self.map_can
    can.delete("all")

    world_size = int(self.world_size_var.get())

    can_w = can.winfo_width()
    can_h = can.winfo_height()

    tile_w = can_w / world_size
    tile_h = can_h / world_size

    for row in range(0, world_size):
    for col in range(0, world_size):
    x = col * tile_w
    y = row * tile_h

    x1 = x + tile_w
    y1 = y + tile_h

    can.create_rectangle(x, y, x1, y1)

    if row == self.player_x and col == self.player_y:
    self.draw_player(x, y, x1, y1)

    def update_loc(self):
    px = str(self.player_x)
    py = str(self.player_y)

    self.loc_var.set("Location: {},{}".format(px, py))

    def draw_player(self, x, y, x1, y1):
    #Calc Midpoint
    mx = (x + x1) / 2
    my = (y + y1) / 2

    self.map_can.create_text(mx, my, text='@')

    def move_player(self, e, x, y):
    try:
    world_size = int(self.world_size_var.get())

    self.player_x += x
    self.player_y += y

    if self.player_x < 0:
    self.player_x = 0

    elif self.player_x > world_size - 1:
    self.player_x = world_size - 1

    if self.player_y < 0:
    self.player_y = 0

    elif self.player_y > world_size - 1:
    self.player_y = world_size - 1

    self.update_map()

    except ValueError:
    pass

    if ___name___ == "__main__":
    root = Root()

    root.mainloop()

  • @ardhenismuhammadaflah7818
    @ardhenismuhammadaflah7818 3 місяці тому +1

    Can you update more bro? I need this type of contenr

    • @CodeQuest1
      @CodeQuest1  3 місяці тому

      Sure, is there something in particular you're looking for?