-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytet.py
353 lines (273 loc) · 8.12 KB
/
pytet.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from matrix import *
import LED_display as LMD
import threading
import random
import keyboard #sudo pip3 install keyboard
import time
import copy
import os
NowRoad=[10,10,10,10,10,10,10,10,0,0,0,0,0,0,0,0,10,10,10,10,10,10,10,10]
NList=[4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
#모든 방향은 LED Matrix 출력 기준임(좌우가 바꿔어 있음)
def NR():
return NowRoad
def R1(): #길전체가 오른쪽으로 이동
if NowRoad[5]==0: #벽에 붙었다면 실행X
return NowRoad
for i in NList:
if NowRoad[i]==0:
NowRoad[i-1]=0
break
for i in reversed(NList):
if NowRoad[i]==0:
NowRoad[i]=10
break
return NowRoad
def L1(): #길전체가 왼쪽으로 이동
if NowRoad[18]==0: #벽에 붙었다면 실행X
return NowRoad
for i in NList:
if NowRoad[i]==0:
NowRoad[i]=10
break
for i in reversed(NList):
if NowRoad[i]==0:
NowRoad[i+1]=0
break
return NowRoad
def LNar1(): #왼쪽 길이 한칸 줄어듬
Nl=NowRoad[4:20]
f1=Nl.index(0)
e1=Nl[f1+1:].index(10)+f1+1
if (e1-f1)<=5: #길 사이가 두칸 이내라면 실행X
return NowRoad
for i in reversed(NList):
if NowRoad[i]==0:
NowRoad[i]=10
return NowRoad
def RNar1(): #오른쪽 길이 한칸 줄어듬
Nl=NowRoad[4:20]
f1=Nl.index(0)
e1=Nl[f1+1:].index(10)+f1+1
if (e1-f1)<=5: #길 사이가 두칸 이내라면 실행X
return NowRoad
for i in NList:
if NowRoad[i]==0:
NowRoad[i]=10
return NowRoad
def RWid1(): #오른쪽 길이 한칸 늘어남
if NowRoad[5]==0: #벽에 붙어있다면 실행X
return NowRoad
for i in NList:
if NowRoad[i]==0:
NowRoad[i-1]=0
return NowRoad
def LWid1(): #왼쪽 길이 한칸 늘어남
if NowRoad[18]==0: #벽에 붙어 있다면 실행X
return NowRoad
for i in reversed(NList):
if NowRoad[i]==0:
NowRoad[i+1]=0
return NowRoad
def RptRoad(CR,n): #한가지 함수를 n번 반복
for i in range(n):
road.append(copy.deepcopy(CR()))
#출력 함수 변경 하기
def DRptRoad(CR1,CR2,n): #두가지 함수를 n번 번갈아가며 반복
for i in range(n):
#road.append()
road.append(copy.deepcopy(CR1()))
road.append(copy.deepcopy(CR2()))
# LookGood(NowRoad)
def MakeP():
Nt=NowRoad[4:20]
f1=Nt.index(0)
e1=Nt[f1+1:].index(10)+f1+1
tmp = copy.deepcopy(NowRoad)
#tmp[f1+(e1-f1)//2+4] = 5
tmp[random.randint(f1+4,e1+3)] = 5
road.append(tmp)
### 출력 예시
road = []
class Key :
def __init__(self) :
self.key = ''
def key_input(self, e) :
for code in keyboard._pressed_events :
if code == 30 :
self.key = 'a'
elif code == 32 :
self.key = 'd'
elif code == 16 :
self.key = 'q'
def LED_init():
thread=threading.Thread(target=LMD.main, args=())
thread.setDaemon(True)
thread.start()
return
#빨 1
#초 2
#노 3
#파 4
#핑 5
#민 6
#흰 7
score = 0
def draw_matrix(m):
global score
array = m.get_array()
for y in range(m.get_dy()-4):
for x in range(4, m.get_dx()-4):
if array[y][x] == 0:
LMD.set_pixel(y, 19-x, 0)
elif array[y][x] == 10:
if score < 10:
LMD.set_pixel(y, 19-x, 2)
elif score < 20:
LMD.set_pixel(y, 19-x, 6)
elif score < 30:
LMD.set_pixel(y, 19-x, 4)
elif score < 40:
LMD.set_pixel(y, 19-x, 3)
else:
LMD.set_pixel(y, 19-x, 7)
elif array[y][x] == 2:
LMD.set_pixel(y, 19-x, 2)
elif array[y][x] == 3:
LMD.set_pixel(y, 19-x, 3)
elif array[y][x] == 1:
LMD.set_pixel(y, 19-x, 1)
elif array[y][x] == 7:
LMD.set_pixel(y, 19-x, 7)
elif array[y][x] == 5:
LMD.set_pixel(y, 19-x, 5)
else:
continue
###
### initialize variables
###
cntarr = [
[[0,2,0],
[0,2,0],
[0,2,0],
[0,2,0],
[0,2,0]],
[[3,3,3],
[0,0,3],
[3,3,3],
[3,0,0],
[3,3,3]],
[[1,1,1],
[1,0,0],
[1,1,1],
[1,0,0],
[1,1,1]],]
cnttop = 18
####################################
arrayBlk = [[1,1,0],
[1,1,0],
[1,1,0]]
### integer variables: must always be integer!
iScreenDy = 32
iScreenDx = 16
iScreenDw = 4
top = 3
left = iScreenDw + iScreenDx//2 - 1
newBlockNeeded = False
arrayScreen = [[ 10, 10, 10, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 10, 10, 10 ]] * 36
###
### prepare the initial screen output
###
iScreen = Matrix(arrayScreen)
oScreen = Matrix(iScreen)
currBlk = Matrix(arrayBlk)
tempBlk = iScreen.clip(top, left, top+currBlk.get_dy(), left+currBlk.get_dx())
tempBlk = tempBlk + currBlk
oScreen.paste(tempBlk, top, left)
LED_init()
draw_matrix(oScreen);
cnt = 3
while cnt:
currBlk = Matrix(cntarr[cnt-1])
tempBlk = iScreen.clip(cnttop, left, cnttop+currBlk.get_dy(), left+currBlk.get_dx())
tempBlk = tempBlk + currBlk
oScreen.paste(tempBlk, cnttop, left)
draw_matrix(oScreen);
cnt -= 1
time.sleep(1)
###
### execute the loop
###
st = time.time()
K = Key()
idx = 0
flag = False
realscore = 0
cntp = 0
while True:
print("time :",time.time() - st)
print("score : ", realscore)
K.key = ''
time.sleep(0.1)
keyboard.hook(K.key_input)
rand = random.randint(0,4)
if flag:
rand = random.randint(0,3)
flag = False
if K.key == 'q':
print('Game terminated...')
break
elif K.key == 'a': # move left
left += 1
elif K.key == 'd': # move right
left -= 1
if len(road) < 5:
if rand == 0:
DRptRoad(NR,R1,3)
DRptRoad(NR,L1,3)
elif rand == 1:
DRptRoad(NR,L1,5)
DRptRoad(NR,R1,5)
elif rand == 2:
RptRoad(RNar1,3)
DRptRoad(NR,R1,6)
DRptRoad(NR,L1,6)
elif rand == 3:
RptRoad(RWid1,5)
RptRoad(RNar1,5)
elif rand == 4:
flag = True
#print("item")
MakeP()
tempBlk = iScreen.clip(top, left, top+currBlk.get_dy(), left+currBlk.get_dx())
tempBlk = tempBlk + currBlk
if tempBlk.anyGreaterThan(10):
print("crush")
print("score : ", realscore + cntp * 50)
break
if tempBlk.getItem(6):
cntp += 1
for i in range(3,6):
for j in range(24):
if arrayScreen[i][j] == 5:
arrayScreen[i][j] = 0
break
arrayScreen.pop(0)
#arrayScreen.append(road[idx])
arrayScreen.append(road.pop(0))
#idx += 1
#if idx >= len(road):
# idx = 0
score = time.time() - st
realscore = (time.time()-st) * 10
iScreen = Matrix(arrayScreen)
oScreen = Matrix(iScreen)
currBlk = Matrix(arrayBlk)
tempBlk = iScreen.clip(top, left, top+currBlk.get_dy(), left+currBlk.get_dx())
tempBlk = tempBlk + currBlk
oScreen.paste(tempBlk, top, left)
draw_matrix(oScreen);
os.system('clear')
###
### end of the loop
###