Files
2024-10-29 17:18:30 +01:00

186 lines
5.7 KiB
Python

from tkinter import *
from Game import Game
# maze.pac Datei wird ausgelesen
def read_current_maze(filename):
cells = []
try:
with open("../Game_Package/" + filename, "r") as file:
data = file.read()
lines = data.split("\n")
for line in lines:
inner_list = []
cells.append(inner_list)
for f in line:
inner_list.append(int(f))
# rotate
temp = []
for i in range(len(cells)):
temp.append([])
for j in range(len(cells)):
temp[i].append(
cells[j][i]) # füllt das 2d Array an richtiger Stelle mit den Werten aus der maze.pac Datei
return temp
except FileNotFoundError:
print("File Not Found")
# überschreibt aktuelles Labyrinth in maze.pac mit neu gezeichnetem Labyrinth
def write_maze():
output = ""
for x, i in enumerate(cells):
for y, j in enumerate(i):
if cells[y][x] == 3:
cells[y][x] = 1
if cells[y][x] == 4:
cells[y][x] = 0
output += str(cells[y][x])
if x < 29:
output += "\n"
with open("../Game_Package/maze.pac", "w") as file:
file.write(output)
cells = read_current_maze("maze.pac")
def on_mouse_down(e, left):
x = e.x // 26
y = e.y // 26
if cells[x][y] == 0 or cells[x][y] == 1:
if left:
cells[x][y] = 1
canvas.create_rectangle(x * 26 + 1, y * 26 + 1, x * 26 + 26, y * 26 + 26, fill="#969696", width=0)
else:
cells[x][y] = 0
canvas.create_rectangle(x * 26 + 1, y * 26 + 1, x * 26 + 26, y * 26 + 26, fill="#FFFFFF", width=0)
def draw_canvas_content():
canvas.delete("all")
canvas.create_rectangle(0, 0, 780, 780, fill="#FFFFFF", width=0)
for x, i in enumerate(cells):
for y, j in enumerate(i):
if cells[x][y] == 1:
canvas.create_rectangle(x * 26, y * 26, x * 26 + 26, y * 26 + 26, fill="#969696", width=0)
if cells[x][y] == 2 or cells[x][y] == 3:
canvas.create_rectangle(x * 26, y * 26, x * 26 + 26, y * 26 + 26, fill="#556dad", width=0)
if cells[x][y] == 4:
canvas.create_rectangle(x * 26, y * 26, x * 26 + 26, y * 26 + 26, fill="#55ad68", width=0)
canvas.create_line(0, y * 26, 780, y * 26, fill="#919191", width=1)
canvas.create_line(x * 26, 0, x * 26, 780, fill="#919191", width=1)
def on_start_clicked():
write_maze()
root.withdraw()
Game()
root.deiconify()
block_important_cells()
def reload_standard_maze():
global cells
cells = read_current_maze("standardmaze.pac")
block_important_cells()
draw_canvas_content()
def block_important_cells():
# Idle-Points werden blockiert
cells[2][1] = cells[2][27] = cells[27][1] = cells[27][27] = 4
for x, i in enumerate(cells):
for y, j in enumerate(i):
if x <= 1 or y < 1 or y >= 28 or x >= 28:
cells[x][y] = 3
# Labyrinth-Öffnung an den Seiten wird blockiert.
cells[0][13] = cells[1][13] = cells[28][13] = cells[29][13] = 4
# Labyrinth wird geleert
def clear_all():
for x, i in enumerate(cells):
for y, j in enumerate(i):
cells[x][y] = 0 if cells[x][y] != 2 else 2
# wichtige Zellen werden blockiert
block_important_cells()
draw_canvas_content()
root = Tk()
# Center Window
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (1000 / 2)
y = (screen_height / 2) - (780 / 2)
root.geometry('%dx%d+%d+%d' % (1000, 780, x, y))
# End Center Window
# Tkinter wird angepasst
root.resizable(False, False)
root.title("PacMan")
root.iconphoto(False, PhotoImage(file="../Game_Package/PacMan.png"))
canvas = Canvas(root, width=780, height=780)
canvas.pack(side=LEFT)
canvas.bind("<B1-Motion>", lambda x: on_mouse_down(x, True))
canvas.bind("<B3-Motion>", lambda x: on_mouse_down(x, False))
canvas.bind("<1>", lambda x: on_mouse_down(x, True))
canvas.bind("<3>", lambda x: on_mouse_down(x, False))
font = ("Comic Sans MS", 15)
# Buttons werden generiert und mit Funktionen ausgestattet
button = Button(root, text="Start", width=10, height=1, command=on_start_clicked, borderwidth=0, background="#55ad68",
font=font)
button.pack(anchor=CENTER, side=TOP, pady=10)
button_reload = Button(root, text="Standard", width=10, height=1, command=reload_standard_maze, borderwidth=0,
background="#e08c70", font=font)
button_reload.pack(anchor=CENTER, side=TOP, pady=10)
button_reload = Button(root, text="Speichern", width=10, height=1,
command=lambda: [write_maze(), block_important_cells(), draw_canvas_content()], borderwidth=0,
background="#55ad68", font=font)
button_reload.pack(anchor=CENTER, side=TOP, pady=10)
button_reload = Button(root, text="Clear", width=10, height=1, command=clear_all, borderwidth=0, background="#e08c70",
font=font)
button_reload.pack(anchor=CENTER, side=TOP, pady=10)
label_hello = Label(root, font=font,
text="Willkommen bei PacMan! \n male dein eigenes Labyrinth oder verwende das Vorhandene",
wraplength=220)
label_hello.pack(anchor=CENTER, side=TOP, pady=10)
label = Label(root, font=font,
text="Verwende \n 'Linke Maustaste' \n um eine Wand zu malen, \n 'Rechte Maustaste'"
" um eine Wand zu löschen \n <--",
wraplength=220)
label.pack(anchor=CENTER, side=TOP, pady=10)
block_important_cells()
draw_canvas_content()
root.mainloop()