-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGui.py
138 lines (106 loc) · 3.79 KB
/
Gui.py
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
126
127
128
129
130
131
132
133
134
135
136
137
138
from Tkinter import *
import time
import random
master = Tk()
Width = 80
(x, y) = (7, 7)
actions = ["up", "down", "left", "right"]
board = Canvas(master, width=x*Width, height=y*Width)
player = (0, y-1)
score = 1
restart = False
walk_reward = -0.1
start_time = 0.0
wall_count = 5
walli = 0
walls_gui = []
dynamic_walls = {0:[(1, 1), (2, 2), (3,0), (4,2), (6,3),(3,4), (4,5)],
4:[(0, 4), (0, 5), (2,6), (4,5), (3,4),(1,3), (2,3), (6,2), (5,1), (4,1), (3,1)],
2:[(4, 0), (6,2), (3,3),(3,1), (3,2), (2,6), (1,5), (1,4), (1,3)],
5:[(4, 0), (5,5), (1,2), (6,2), (5,4), (4,3),(3,1), (3,2), (2,6), (1,5), (1,4), (1,3)],
3:[(0, 2), (0, 3), (1, 3), (2,3), (3,1), (4,1), (1,5), (2,5), (5,1), (6,2), (3,3)],
1:[(1, 1), (2, 2), (5,0), (4,2), (5,3),(3,3), (6,3)]}
walls = dynamic_walls[walli]
specials = [(6, 0, "red", 30)]
tile = 0;
count = 0;
total_move = 0;
def render_grid():
global specials, walls, Width, x, y, player
for i in range(x):
for j in range(y):
board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill="white", width=1)
temp = {}
for (i, j, c, w) in specials:
board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill=c, width=1)
for (i, j) in walls:
w=board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill="black", width=1)
walls_gui.append(w)
render_grid()
def env_change():
global walls, walls_gui, walli,start_time,restart,total_move
for gui in walls_gui :
board.delete(gui)
if(walli == wall_count):
walli = 0
else :
walli += 1
walls = dynamic_walls[walli]
print "/n CHANGE MAZE ",walli
start_time = time.time()
for (i, j) in walls:
w=board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill="black", width=1)
walls_gui.append(w)
total_move = 0
restart = True
def try_move(dx, dy):
global player, x, y, score, walk_reward, agent, restart,tile,total_move
if restart == True:
restart_game()
new_x = player[0] + dx
new_y = player[1] + dy
score += walk_reward
if (new_x >= 0) and (new_x < x) and (new_y >= 0) and (new_y < y) and not ((new_x, new_y) in walls):
board.coords(agent, new_x*Width+Width*2/10, new_y*Width+Width*2/10, new_x*Width+Width*8/10, new_y*Width+Width*8/10)
player = (new_x, new_y)
tile += 1
total_move += 1
for (i, j, c, w) in specials:
if new_x == i and new_y == j:
score -= walk_reward
score += w
if score > 0:
print "Goal! : ", score , " move : ", tile, " tot_mov :",total_move,(" time : %s sec--" % (time.time() - start_time))
# else:
# print "Goal! : ", score
restart = True
return
#print "score: ", score
def call_up(event):
try_move(0, -1)
def call_down(event):
try_move(0, 1)
def call_left(event):
try_move(-1, 0)
def call_right(event):
try_move(1, 0)
def restart_game():
global player, score, agent, restart, tile
player = (0, y-1)
score = 1
restart = False
board.coords(agent, player[0]*Width+Width*2/10, player[1]*Width+Width*2/10, player[0]*Width+Width*8/10, player[1]*Width+Width*8/10)
tile = 0
def has_restarted():
return restart
master.bind("<Up>", call_up)
master.bind("<Down>", call_down)
master.bind("<Right>", call_right)
master.bind("<Left>", call_left)
agent = board.create_rectangle(player[0]*Width+Width*2/10, player[1]*Width+Width*2/10,
player[0]*Width+Width*8/10, player[1]*Width+Width*8/10, fill="yellow", width=1, tag="agent")
board.grid(row=0, column=0)
def start_game():
global start_time
start_time = time.time()
master.mainloop()