-
Notifications
You must be signed in to change notification settings - Fork 0
/
EarthLanderManual.py
215 lines (192 loc) · 6.26 KB
/
EarthLanderManual.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*-
"""
@author: LJ
-----------
Modified to be usable in the project by NXabo
"""
from tkinter import *
import math
import numpy as np
#Variables du jeu
altitude = 8100
speed = 268
throttle = 0
fuel = 1000
gameState = 0
actualPixelPos = 70
userInput = 0
reset = 0
alt_poss = [[0,800],[800,2000],[2000,3000],[3000,4000],[4000,5000],[5000,999999]]
speed_poss = [[-999999,0],[0,40],[40,70],[70,200],[200,999999]]
#environement (s'éxécute 10X par seconde)
def environment():
global altitude
global speed
global throttle
global fuel
global gameState
global state
global alt_poss
global speed_poss
############ EL parameters
state = 0
trans_matrix = np.loadtxt("trans_matrix.txt")
############
############ Gestion de l'état
for i in range(len(alt_poss)):
if alt_poss[i][0] <= altitude and alt_poss[i][1] > altitude:
state = i
break
for i in range(len(speed_poss)):
if speed_poss[i][0] <= speed and speed_poss[i][1] > speed:
state = state * len(speed_poss) + i
break
############
throttle = agent_QL(trans_matrix,state)
if fuel <= 0:
fuel = 0
throttle = 0
a = 9.8-throttle
altitude -= speed*0.1 - a*0.01/2
speed += a*0.1
fuel -= throttle*0.1
if altitude <= 0:
if speed <= 40:
altitude = 0
throttle = 0
speed = 0
gameState = 2
else:
altitude = 0
throttle = 0
speed = 0
gameState = 1
#agent qui éxecute l'entrée utilisateur
def agentManual():
return userInput
def agent_QL(trans_matrix,state):
return np.argmax(trans_matrix,axis=1)[state]
# gestion de l'interface graphique (il n'est pas indispensable de comprendre le code ci-dessous pour réussir le TP)
def onTimer():
global altitude
global speed
global throttle
global fuel
global gameState
global reset
global userInput
global state
if reset == 1:
altitude = 10000
speed = 0
throttle = 0
fuel = 1000
gameState = 0
userInput = 0
reset = 0
state = 0
if gameState == 0:
environment()
moveRocket()
updateData()
root.after(100,onTimer)
def moveRocket():
global actualPixelPos
newPos = round(400 - altitude * 330 /10000)
offset = newPos - actualPixelPos
c.move(p0,0,offset)
if throttle == 0:
c.itemconfig(p0,fill="#000",outline="#000")
else:
c.itemconfig(p0,fill="#ff0",outline="#ff0")
c.move(p1,0,offset)
c.move(p2,0,offset)
c.move(p3,0,offset)
c.move(o1,0,offset)
actualPixelPos = newPos
if gameState == 1:
c.itemconfig(pexplo,state="normal")
elif gameState == 2:
c.itemconfig(l1,fill="#fff")
c.itemconfig(pfla,fill="#fff",outline="#fff")
else:
c.itemconfig(pexplo,state="hidden")
c.itemconfig(l1,fill="#000")
c.itemconfig(pfla,fill="#000",outline="#000")
def updateData():
c.itemconfig(t1, text="Altitude: {0:.2f}".format(altitude))
c.itemconfig(t2, text="Speed: {0:.2f}".format(speed))
c.itemconfig(t3, text="Fuel: {0:.2f}".format(fuel))
c.itemconfig(t4, text="Throttle: {0:d}".format(throttle))
c.itemconfig(t5, text="State: {0:d}".format(state))
def onKeyPressed(e):
global userInput
global reset
c = e.char
if c == 'a' or c == '0':
userInput = 0
elif c == 'z':
userInput = 1
elif c == 'e' or c == '1':
userInput = 2
elif c == 'r':
userInput = 3
elif c == 't' or c == '2':
userInput = 4
elif c == 'y':
userInput = 5
elif c == 'u' or c == '3':
userInput = 6
elif c == 'i':
userInput = 7
elif c == 'o' or c == '4':
userInput = 8
elif c == 'p':
userInput = 9
elif c == 'q' or c == '5':
userInput = 10
elif c == 's':
userInput = 11
elif c == 'd' or c == '6':
userInput = 12
elif c == 'f':
userInput = 13
elif c == 'g' or c == '7':
userInput = 14
elif c == 'h':
userInput = 15
elif c == 'j' or c == '8':
userInput = 16
elif c == 'k':
userInput = 17
elif c == 'l' or c == '9':
userInput = 18
elif c == 'm':
userInput = 19
elif c == 'w':
reset = 1
root = Tk()
root.title("Earth Lander")
root.geometry("640x480")
c = Canvas(bg ="#000")
p0 = c.create_polygon([158,50,152,70,160,80,168,70,162,50],fill="#ff0",outline ="#ff0",width=2.0)
p1 = c.create_polygon([150,50,150,70,140,70],fill="#f00",outline="#f00",width=2.0)
p2 = c.create_polygon([170,50,170,70,180,70],fill="#f00",outline="#f00",width=2.0)
p3 = c.create_polygon([160,10,175,30,170,50,150,50,145,30],fill="#fff",outline="#f00",width=2.0)
o1 = c.create_oval(150,25,170,35,fill="#0ff",outline="#f00",width=2.0)
l1 = c.create_line(100,400,100,350,fill="#000",width=2.0)
pfla = c.create_polygon([100,375,75,362,100,350],fill="#000",outline="#000",width=2.0)
pexplo = c.create_polygon([110,320,140,350,140,300,160,355,190,320,180,360,220,375, 180,380,200,420,170,385,160,450,150,385,120,430,125,380,90,375,130,360],fill="#ff0",outline="#ff0",width=2.0)
c.itemconfig(pexplo,state="hidden")
c.create_polygon([640,400,480,250,320,400],fill="#a55",outline="#a55",width=2.0)
c.create_line(0,400,640,400,fill="#0f0",width=3.0)
t1 = c.create_text(320, 30, anchor=W, font="arial",text="Altitude: 10000",fill ="#fff")
t2 = c.create_text(320, 60, anchor=W, font="arial",text="Speed: 0",fill ="#fff")
t3 = c.create_text(320, 90, anchor=W, font="arial",text="Fuel: 1000",fill ="#fff")
t4 = c.create_text(320, 120, anchor=W, font="arial",text="Throttle: 0",fill ="#fff")
t5 = c.create_text(320, 150, anchor=W, font="arial",text="State: 0",fill ="#fff")
c.pack(fill=BOTH, expand=1)
root.bind_all("<Key>",onKeyPressed)
root.focus_force()
root.after(100,onTimer)
root.mainloop()