-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface.py
More file actions
125 lines (99 loc) · 4.06 KB
/
interface.py
File metadata and controls
125 lines (99 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import tkinter as tk
from PIL import Image, ImageTk
import random
import time
from matplotlib.pyplot import grid
import robot
# Définir les dimensions de la grille
GRID_WIDTH = 20
GRID_HEIGHT = 16
# Définir les dimensions des images de bâtiments
BUILDING_WIDTH = 50
BUILDING_HEIGHT = 50
# Définir les couleurs pour les cases de la grille
GRID_COLORS = ["white", "light gray", "gray"]
# Créer une fenêtre Tkinter
root = tk.Tk()
root.title("Robot City")
# Charger les images de bâtiments à partir des fichiers PNG
building_images = {
"commerce": ImageTk.PhotoImage(Image.open("commerce.png").resize((BUILDING_WIDTH, BUILDING_HEIGHT))),
"entreprise": ImageTk.PhotoImage(Image.open("entreprise.png").resize((BUILDING_WIDTH, BUILDING_HEIGHT))),
"loisir": ImageTk.PhotoImage(Image.open("loisir.png").resize((BUILDING_WIDTH, BUILDING_HEIGHT))),
"rue": ImageTk.PhotoImage(Image.open("rue.png").resize((BUILDING_WIDTH, BUILDING_HEIGHT))),
"maison": ImageTk.PhotoImage(Image.open("maison.png").resize((BUILDING_WIDTH, BUILDING_HEIGHT))),
"robot": ImageTk.PhotoImage(Image.open("tes2.png").resize((BUILDING_WIDTH, BUILDING_HEIGHT))),
}
robot_position = (100,100)
robot_image = Image.open("tes2.png")
robot_image = robot_image.resize((50, 50)) # Redimensionner l'image
robot_photo = ImageTk.PhotoImage(robot_image)
# Dessiner l'image du robot à sa position actuelle sur la grille
def draw_robot():
global robot_position
x, y = robot_position
if x < 0 or x >= GRID_WIDTH*BUILDING_WIDTH or y < 0 or y >= GRID_HEIGHT*BUILDING_HEIGHT:
# Le robot est sorti de la grille
return
i = y // BUILDING_HEIGHT
j = x // BUILDING_WIDTH
if grid[i][j] != "R":
# Le robot est sur une case qui n'est pas une rue
return
global image
image = canvas.create_image(x, y, image=robot_photo, anchor="nw")
# Déplacer le robot d'une case dans la direction indiquée
def move_robot():
canvas.delete(image)
global robot_position
x, y = robot_position
# Choisir une direction aléatoire
direction = random.choice(["up", "down", "left", "right"])
# Déplacer le robot d'une case dans la direction choisie
if direction == "up":
y -= BUILDING_HEIGHT
elif direction == "down":
y += BUILDING_HEIGHT
elif direction == "left":
x -= BUILDING_WIDTH
elif direction == "right":
x += BUILDING_WIDTH
robot_position = (x, y)
# Supprimer l'image du robot de la grille et la redessiner à sa nouvelle position
draw_robot()
button2 = tk.Button(root, text="Draw", command=draw_robot)
button2.pack()
button3 = tk.Button(root, text="Play", command=move_robot)
button3.pack()
button = tk.Button(root, text="Quit", command=root.destroy)
button.pack()
# Créer un canevas Tkinter pour afficher la grille
canvas = tk.Canvas(root, width=GRID_WIDTH*BUILDING_WIDTH, height=GRID_HEIGHT*BUILDING_HEIGHT)
canvas.pack()
# Lire le fichier texte mapcity.txt pour placer les bâtiments sur la grille
with open("mapcity.txt", "r") as f:
grid = [[cell for cell in line.strip()] for line in f]
for i in range(GRID_HEIGHT):
for j in range(GRID_WIDTH):
building_type = grid[i][j]
if building_type == "M":
color_index = 2
elif building_type == "C":
color_index = 0
elif building_type == "E":
color_index = 1
elif building_type == "R":
color_index = 0
elif building_type == "D":
color_index = 2
else:
color_index = 0
color = GRID_COLORS[color_index]
x = j * BUILDING_WIDTH
y = i * BUILDING_HEIGHT
canvas.create_rectangle(x, y, x+BUILDING_WIDTH, y+BUILDING_HEIGHT, fill=color)
if building_type != "0":
building_image = building_images[{"M": "maison", "R": "rue", "C": "commerce", "E": "entreprise", "D": "loisir"}[building_type]]
canvas.create_image(x, y, anchor="nw", image=building_image)
# Démarrer la boucle Tkinter
root.mainloop()