-
Notifications
You must be signed in to change notification settings - Fork 2
/
sgle.py
338 lines (259 loc) · 8.51 KB
/
sgle.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SGLE|SuperGravitronLeetEdition*
# Pwn2Win2018
# author: [email protected]
import curses
from curses import KEY_RIGHT, KEY_LEFT
from random import randint
import time
import locale
locale.setlocale(locale.LC_ALL, '')
WIDTH = 30
HEIGHT = 10
MAX_X = WIDTH - 2
MAX_Y = HEIGHT - 2
JUMPER_REFRESH = 0.03
BULLET_REFRESH = 0.05
TARGET_TIME = 137
FLAG = "[xxFLAGxx]"
KEY_ESC = 27
KEY_Q1 = 81
KEY_Q2 = 113
KEY_ENTER = 10
KEY_SPACE = 32
class Jumper(object):
def __init__(self, window, char='I'):
self.window = window
self.char = char
self.x = MAX_X / 2
self.y = MAX_Y / 2
self.updown = 1
self.start_time = time.time()
self.direction = KEY_LEFT
self.direction_map = {
KEY_LEFT: self.move_left,
KEY_RIGHT: self.move_right
}
def render(self):
self.window.addstr(0, 1, "[{}:{}]".format(self.y, self.x))
self.window.addstr(self.y, self.x, self.char)
def update(self):
elapsed_time = time.time() - self.start_time
if elapsed_time < JUMPER_REFRESH:
return
self.start_time = time.time()
if self.y == MAX_Y:
self.updown = 0
if self.y == 1:
self.updown = 1
if self.updown:
self.y += 1
else:
self.y -= 1
self.direction_map[self.direction]()
def change_direction(self, direction):
self.direction = direction
def move_left(self):
self.x -= 1
if self.x < 1:
self.x = MAX_X
def move_right(self):
self.x += 1
if self.x > MAX_X:
self.x = 1
@property
def getx(self):
return self.x
@property
def gety(self):
return self.y
class Bullet(object):
CHAR = ['*', '>', '=', '+']
def __init__(self, window, y, x):
self.window = window
self.y_init = y
self.x_init = x
self.dir_init = (x < 1)
self.start()
def start(self):
self.ended = False
self.start_time = time.time()
self.y = self.y_init
self.x = self.x_init
self.dir = self.dir_init
self.char = self.CHAR[randint(0, len(self.CHAR) - 1)]
def update(self):
if time.time() - self.start_time < BULLET_REFRESH:
return self.ended
self.start_time = time.time()
if self.dir:
self.ended = self.x > MAX_X
if not self.ended:
self.x += 1
else:
self.ended = self.x < 1
if not self.ended:
self.x -= 1
return self.ended
def blast(self):
char = "X****"
self.window.addstr(self.y, self.x, char[0])
if self.y - 1 > 0:
self.window.addstr(self.y - 1, self.x, char[1])
if self.x + 1 <= MAX_X:
self.window.addstr(self.y, self.x + 1, char[2])
if self.y + 1 <= MAX_Y:
self.window.addstr(self.y + 1, self.x, char[3])
if self.x - 1 > 0:
self.window.addstr(self.y, self.x - 1, char[4])
def collided(self, y, x):
done = self.y == y and self.x == x
if done:
self.blast()
return done
def render(self):
if self.x > 0 and self.x <= MAX_X and not self.ended:
self.window.addstr(self.y, self.x, self.char)
class BulletSet(object):
def __init__(self, window, bullets):
self.window = window
self.bullets = []
for bullet in bullets:
self.bullets.append(Bullet(window, bullet[0], bullet[1]))
def render(self):
for bullet in self.bullets:
bullet.render()
def update(self):
ended = True
for bullet in self.bullets:
bullet_ended = bullet.update()
if not bullet_ended:
ended = False
return ended
def collided(self, y, x):
for bullet in self.bullets:
if bullet.collided(y, x):
return True
return False;
def restart(self):
for bullet in self.bullets:
bullet.start()
class GameBoard(object):
def __init__(self, window, bulletsets):
self.window = window
self.bulletsets = []
self.pointer = 0
self.start_time = time.time()
for bulletset in bulletsets:
self.bulletsets.append(BulletSet(window, bulletset))
def random_init(self):
self.pointer = randint(0, len(self.bulletsets) - 1)
def render(self):
self.render_time()
# step debug
#self.window.addstr(0, 22, "[{}/{}]".format(self.pointer, len(self.bulletsets)))
self.bulletsets[self.pointer].render()
def render_time(self):
self.window.addstr(0, 10, "[{0:.2f}]".format(time.time() - self.start_time))
def update(self):
if self.bulletsets[self.pointer].update():
self.bulletsets[self.pointer].restart()
self.pointer += 1
if self.pointer >= len(self.bulletsets):
self.pointer = 0
def bye(self):
key = -1
while key not in [KEY_ENTER, KEY_ESC, KEY_SPACE, KEY_Q1, KEY_Q2]:
key = window.getch()
def collided(self, y, x):
if self.bulletsets[self.pointer].collided(y, x):
self.bye()
return True
return False
def time_achieved(self):
if time.time() - self.start_time < TARGET_TIME:
return False
self.render_time()
self.window.addstr(MAX_Y + 1, 1, FLAG)
self.bye()
return True
if __name__ == '__main__':
curses.initscr()
window = curses.newwin(HEIGHT, WIDTH, 0, 0)
window.timeout(20)
window.keypad(1)
curses.noecho()
curses.curs_set(0)
window.border(0)
bsTopLeft2 = [[2,0], [5,-6]]
bsBottomRight2 = [[7,MAX_X], [4,MAX_X+6]]
bsLeftRight4 = bsTopLeft2 + bsBottomRight2
bsTopLeft3 = [[2,0], [5,-6], [3,-12]]
bsBottomRight3 = [[7,MAX_X], [4,MAX_X+6], [6,MAX_X+12]]
bsLeftRight6 = bsTopLeft3 + bsBottomRight3
bsLeftWall3 = [[2,0], [4,0], [6,0]]
bsRightWall3 = [[8,MAX_X], [6,MAX_X], [4,MAX_X]]
bsLeftRightWall6 = bsLeftWall3 + bsRightWall3
bsLeftTopBottom2 = [[1,0], [8,0]]
bsRightMiddle2 = [[4,MAX_X], [5,MAX_X]]
bsLeftTopBottomRightMiddle4 = bsLeftTopBottom2 + bsRightMiddle2
bsRightTopBottom2 = [[1,MAX_X], [8,MAX_X]]
bsLeftMiddle2 = [[4,0], [5,0]]
bsRightTopBottomLeftMiddle4 = bsRightTopBottom2 + bsLeftMiddle2
bsLeftTopBottom4 = [[1,0], [3,-5], [8,0], [5,-5]]
bsRightMiddle4 = [[4,MAX_X], [5,MAX_X], [2,MAX_X+5], [7,MAX_X+5]]
bsLeftTopBottomRightMiddle8 = bsLeftTopBottom4 + bsRightMiddle4
bsRightTopBottom4 = [[1,MAX_X], [3,MAX_X+5], [8,MAX_X], [5,MAX_X+5]]
bsLeftMiddle4 = [[4,0], [5,0], [2,-5], [7,-5]]
bsRightTopBottomLeftMiddle8 = bsLeftTopBottom4 + bsRightMiddle4
bsLeftMiddleBlock8 = [[3,-1], [4,-2], [4,-1], [4,0], [5,-2], [5,-1], [5,0], [6,-1]]
bsLeftMiddleBlockRightTopBottom10 = bsLeftMiddleBlock8 + bsRightTopBottom2
bsRightMiddleBlock8 = [[3,MAX_X+1], [4,MAX_X+2], [4,MAX_X+1], [4,MAX_X], [5,MAX_X+2], [5,MAX_X+1], [5,MAX_X], [6,MAX_X+1]]
bsRightMiddleBlockLeftTopBottom10 = bsRightMiddleBlock8 + bsLeftTopBottom2
jumper = Jumper(window)
gameboard = GameBoard(window, [
bsTopLeft2,
bsRightTopBottom2,
bsBottomRight2,
bsLeftMiddle2,
bsLeftRight4,
bsRightTopBottomLeftMiddle4,
bsTopLeft3,
bsLeftTopBottom4,
bsBottomRight3,
bsRightMiddle4,
bsLeftRight6,
bsLeftTopBottomRightMiddle8,
bsLeftWall3,
bsRightTopBottom4,
bsRightWall3,
bsLeftMiddle4,
bsLeftRightWall6,
bsRightTopBottomLeftMiddle8,
bsLeftTopBottom2,
bsLeftMiddleBlock8,
bsRightMiddle2,
bsLeftMiddleBlockRightTopBottom10,
bsLeftTopBottomRightMiddle4,
bsRightMiddleBlock8,
bsRightMiddleBlockLeftTopBottom10,
])
while True:
window.clear()
window.border()
jumper.render()
gameboard.render()
event = window.getch()
if event in [KEY_LEFT, KEY_RIGHT]:
jumper.change_direction(event)
if event in [KEY_ESC, KEY_Q1, KEY_Q2]:
break
if gameboard.collided(jumper.gety, jumper.getx):
break
if gameboard.time_achieved():
break
jumper.update()
gameboard.update()
curses.endwin()