Quality of life improvements

In diesem Update habe ich einen Rand hinzugefügt welcher bis jetzt die gleiche Farbe hat wie der Hintergrund und somit nicht zu unterscheiden ist. Ebenfalls habe ich eine anzeige für die Position hinzugefügt mit genug Fläche für mehr anzeigen in der Zukunft.

import tkinter as tk

class MoveCharacter(tk.Canvas):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        self.dx = 0
        self.dy = 0

        self.text_field()

        self.height = 150
        self.width = 150

        self.start_X = 50
        self.start_Y = 50

        self.x = self.start_X
        self.y = self.start_Y

        # Create the box (character)
        self.box = self.create_rectangle(self.start_X, self.start_Y, self.height, self.width, fill="black")

        # Set update interval in milliseconds
        self.dt = 100
        self.tick()


    # Movement handler that gets called every dt milliseconds
    def tick(self):

        self.coordinats(self.dx, self.dy)
        wall_check(self.center)
        if not Dteck:
            self.move(self.box, self.dx, self.dy)

        if Dteck:
            self.coordinats(self.dx, self.dy)

        if not Dteck:
            # Reset the movement after each tick
            self.dx = 0
            self.dy = 0
        self.itemconfig(self.output, text=self.text)

        # Repeatedly call tick method to continue the movement loop
        self.after(self.dt, self.tick)

    # Method to change direction of movement
    def change_heading(self, dx, dy):
        self.dx = dx
        self.dy = dy


    def coordinats(self, stepx, stepy):
        self.x = self.x + stepx
        self.y = self.y + stepy

        self.center = [self.x , self.y ]
        self.text = "X:" + str(self.x - 50) + " Y: " + str(self.y - 50)

    def text_field(self):
        self.create_rectangle(1000, 950, 0, 1000, fill="brown")
        self.output = self.create_text(
            500, 975,  # x and y coordinates of the text
            text="0, 0",  # Text to display
            font=("Arial", 24, "bold"),  # Font style and size
            fill="black"  # Text color
        )



def main():
    global root,character_canvas
    # Create the main window
    root = tk.Tk()
    root.title("2D Game")
    root.geometry("1080x720")
    root.configure(bg="black")

    # Create the MoveCharacter canvas (size 900x900)
    character_canvas = MoveCharacter(root, width=1000, height=1000, bg="lightblue")
    character_canvas.pack()

    keybindings()

    # Start the main loop
    root.mainloop()

def keybindings():
    global ds
    # Define movement step size
    ds = 100  # Smaller step for smoother movement

    # Bind key presses for movement
    root.bind("<KeyPress-a>", lambda _: character_canvas.change_heading(-ds, 0))  # Move left
    root.bind("<KeyPress-d>", lambda _: character_canvas.change_heading(ds, 0))  # Move right
    root.bind("<KeyPress-w>", lambda _: character_canvas.change_heading(0, -ds))  # Move up
    root.bind("<KeyPress-s>", lambda _: character_canvas.change_heading(0, ds))  # Move down

def wall_check(coords):
    global Dteck
    Dteck = False
    if coords[0] < 50 :
        character_canvas.change_heading(0, 0)
        Dteck = True
        character_canvas.coordinats(100,0)


    if coords[0] > 900 :
        character_canvas.change_heading(0, 0)
        Dteck = True
        character_canvas.coordinats(-100,0)

    if coords[1] > 900 :
        character_canvas.change_heading(0, 0)
        Dteck = True
        character_canvas.coordinats(0,-100)

    if coords[1] < 50 :
        character_canvas.change_heading(0, 0)
        Dteck = True
        character_canvas.coordinats(0,100)

    print(coords)

# Run the program
main()

Leave a Reply

Your email address will not be published. Required fields are marked *