-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
640 lines (476 loc) · 15.3 KB
/
functions.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
'''
All the python equivalent functions to the EBIGU actions
'''
import helpers as h
add_to_main_loop = ''
# ---------- Initializing functions -----------
def init_game(width, height):
py_code = (
'import os\n'
'import re\n'
'import random\n'
'import pygame as pg\n'
'from pygame.locals import *\n'
'def sorted_nicely(l):\n'
'\tconvert = lambda text: int(text) if text.isdigit() else text\n'
'\talphanum_key = lambda key: [convert(c) for c in re.split("([0-9]+)", key)]\n'
'\treturn sorted(l, key=alphanum_key)\n'
'pg.init()\n'
'pg.mixer.init()\n'
'clock = pg.time.Clock()\n'
'class Text(pg.sprite.Sprite):\n'
'\tdef __init__(self, text, font, color):\n'
'\t\tsuper(Text, self).__init__()\n'
'\t\tself.type = "Text"\n'
'\t\tself.string = text\n'
'\t\tself.font = font\n'
'\t\tself.color = color\n'
'\t\tself.surf = font.render(text, True, color)\n'
'\t\tself.rect = self.surf.get_rect()\n'
'\tdef draw(self, x, y):\n'
'\t\tglobal screen, sprite_group\n'
'\t\tself.move(x, y)\n'
'\t\tscreen.blit(self.surf, self.rect)\n'
'\t\tsprite_group.add(self)\n'
'\tdef destroy(self):\n'
'\t\tself.kill()\n'
'\tdef move(self, x, y):\n'
'\t\tself.rect.centerx = x\n'
'\t\tself.rect.centery = y\n'
'class Button(pg.sprite.Sprite):\n'
'\tdef __init__(self, text, color, bg_color, hover_color, font, width, height):\n'
'\t\tsuper(Button, self).__init__()\n'
'\t\tself.type = "Button"\n'
'\t\tself.bg_color = bg_color\n'
'\t\tself.hover_color = hover_color\n'
'\t\tself.font = font\n'
'\t\tself.width = width\n'
'\t\tself.height = height\n'
'\t\tself.text = Text(text, font, color)\n'
'\t\tself.surf = pg.Surface((width, height))\n'
'\t\tself.surf.fill(bg_color)\n'
'\t\tself.rect = self.surf.get_rect()\n'
'\t\tself.is_hover = False\n'
'\tdef draw(self, x, y):\n'
'\t\tglobal screen, sprite_group\n'
'\t\tself.move(x, y)\n'
'\t\tscreen.blit(self.surf, self.rect)\n'
'\t\tsprite_group.add(self)\n'
'\t\tself.text.draw(x, y)\n'
'\tdef destroy(self):\n'
'\t\tself.kill()\n'
'\t\tself.text.kill()\n'
'\tdef move(self, x, y):\n'
'\t\tself.rect.centerx = x\n'
'\t\tself.rect.centery = y\n'
'\tdef hover(self):\n'
'\t\tself.surf.fill(self.hover_color)\n'
'\t\tself.is_hover = True\n'
'\tdef no_hover(self):\n'
'\t\tself.surf.fill(self.bg_color)\n'
'\t\tself.is_hover = False\n'
'class Hero(pg.sprite.Sprite):\n'
'\tdef __init__(self, width, height, sprite, color="#ffffff"):\n'
'\t\tsuper(Hero, self).__init__()\n'
'\t\tself.color = color\n'
'\t\tif sprite == "pravougulniche":\n'
'\t\t\tself.surf = pg.Surface((width, height))\n'
'\t\t\tself.surf.fill(color)\n'
'\t\t\tself.type = "Hero_Pravougulniche"\n'
'\t\telif sprite == "krugche":\n'
'\t\t\tself.surf = pg.Surface((width, height), pg.SRCALPHA)\n'
'\t\t\tpg.draw.circle(self.surf, color, (width // 2, height // 2), width // 2)\n'
'\t\t\tself.type = "Hero_Krugche"\n'
'\t\telse:\n'
'\t\t\tself.surf = pg.image.load(sprite).convert()\n'
'\t\t\tself.surf = pg.transform.scale(self.surf, (width, height))\n'
'\t\t\tself.anmations = {}\n'
'\t\t\tself.index = 0\n'
'\t\t\tself.current_time = 0\n'
'\t\t\tself.animation_time = 0.1\n'
'\t\t\tself.type = "Hero_Image"\n'
'\t\tself.rect = self.surf.get_rect()\n'
'\tdef draw(self, x, y):\n'
'\t\tglobal screen, sprite_group\n'
'\t\tself.move(x, y)\n'
'\t\tscreen.blit(self.surf, self.rect)\n'
'\t\tsprite_group.add(self)\n'
'\tdef destroy(self):\n'
'\t\tself.kill()\n'
'\tdef move_by(self, x, y):\n'
'\t\tself.rect.move_ip(x, y)\n'
'\tdef move(self, x, y):\n'
'\t\tself.rect.centerx = x\n'
'\t\tself.rect.centery = y\n'
'\tdef change_color(self, new_color):\n'
'\t\tif self.type == "Hero_Image":\n'
'\t\t\treturn\n'
'\t\tself.color = new_color\n'
'\t\tself.surf.fill(new_color)\n'
'\tdef add_animation(self, name, path):\n'
'\t\tif not self.type == "Hero_Image":\n'
'\t\t\ttry:\n'
'\t\t\t\traise TypeError("Kak she go animirash kat nqma snimki be...")\n'
'\t\t\texcept Exception as exep:\n'
'\t\t\t\tprint(exep)\n'
'\t\tself.anmations[name] = []\n'
'\t\tfiles = sorted_nicely(os.listdir(path))\n'
'\t\tfor file_name in files:\n'
'\t\t\timage = pg.image.load(path + os.sep + file_name).convert()\n'
'\t\t\timage = pg.transform.scale(image, (self.rect.width, self.rect.height))\n'
'\t\t\tself.anmations[name].append(image)\n'
'\tdef play_animation(self, name, dt):\n'
'\t\tif not self.type == "Hero_Image":\n'
'\t\t\ttry:\n'
'\t\t\t\traise TypeError("Kak she go animirash kat nqma snimki be...")\n'
'\t\t\texcept Exception as exep:\n'
'\t\t\t\tprint(exep)\n'
'\t\tself.current_time += dt\n'
'\t\tif self.current_time > self.animation_time:\n'
'\t\t\tself.current_time = 0\n'
'\t\t\tself.index = (self.index + 1) % len(self.anmations[name])\n'
'\t\t\tself.surf = self.anmations[name][self.index]\n'
f'SCREEN_WIDTH = {width}\n'
f'SCREEN_HEIGHT = {height}\n'
'SHIRINA_EKRAN = SCREEN_WIDTH\n'
'VISOCHINA_EKRAN = SCREEN_HEIGHT\n'
'FPS = 30\n'
f'CENTER = ({int(width) // 2}, {int(height) / 2})\n'
'screen = pg.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])\n'
'sprite_group = pg.sprite.Group()\n'
'button_list = []\n'
'hero_data = {}\n'
'event_number = 1\n'
'izteklo_vreme = 0\n'
'clock_paused = True\n'
'running = True\n'
)
return py_code
def set_icon(tabs, path):
py_code = (
f'\n{tabs}ICON = pg.image.load("{path}")\n'
f'{tabs}pg.display.set_icon(ICON)\n'
)
return py_code
def set_window_caption(caption):
py_code = (
f'pg.display.set_caption("{caption}")\n'
)
return py_code
def set_fps(fps):
py_code = (
f'FPS = {fps}\n'
)
return py_code
def set_background(tabs, background):
global add_to_main_loop
if h.hex2rgb(background):
color = h.hex2rgb(background)
py_code = (
f'\nscreen.fill({color})\n'
)
add_to_main_loop += 'screen.fill({color})\n'
else:
py_code = (
f'\n{tabs}BACKGROUND_IMAGE = pg.image.load("{background}").convert()\n'
f'{tabs}BACKGROUND_IMAGE = pg.transform.scale(BACKGROUND_IMAGE, (SCREEN_WIDTH, SCREEN_HEIGHT))\n'
f'{tabs}screen.blit(BACKGROUND_IMAGE, (0, 0))\n'
)
add_to_main_loop += 'screen.blit(BACKGROUND_IMAGE, (0, 0))\n'
return py_code
# ---------- If functions -----------
def is_mousedown():
py_code = (
f'MOUSEBUTTONDOWN in events'
)
return py_code
def is_mousemotion():
py_code = (
f'MOUSEMOTION in events'
)
return py_code
def is_button_clicked(button):
py_code = (
f'({button}.rect.x + {button}.rect.width > mouse_pos[0] > {button}.rect.x and {button}.rect.y + {button}.rect.height > mouse_pos[1] > {button}.rect.y and MOUSEBUTTONDOWN in events)'
)
return py_code
def is_key_pressed(key):
py_code = (
f'keys[{key}]'
)
return py_code
def has_any_collided(sprite, group):
py_code = (
f'pg.sprite.spritecollideany({sprite}, {group})'
)
return py_code
def has_collided(sprite1, sprite2):
py_code = (
f'pg.sprite.collide_rect({sprite1}, {sprite2})'
)
return py_code
def is_outside(sprite):
py_code = (
f'(SCREEN_WIDTH < {sprite}.rect.x + {sprite}.rect.width or {sprite}.rect.x < 0 \
or SCREEN_HEIGHT < {sprite}.rect.y + {sprite}.rect.height or {sprite}.rect.y < 0)'
)
return py_code
def is_event(event):
py_code = (
f'{event} in events'
)
return py_code
# ---------- New Object functions -----------
def new_hero(name, width, height, sprite, color='#ffffff'):
if not color:
color = '#ffffff'
color = h.hex2rgb(color)
py_code = (
f'hero_data["{name}"] = dict(width={width}, height={height}, sprite="{sprite}", color={color})\n'
)
return py_code
def new_font(name, path, size):
py_code = (
f'{name} = pg.font.Font("{path}", {size})\n'
)
return py_code
def new_text(name, text, font, color):
color = h.hex2rgb(color)
py_code = (
f'{name} = Text({text}, {font}, {color})\n'
)
return py_code
def new_button(name, text, color, bg_color, hover_color, font, width, height):
color = h.hex2rgb(color)
bg_color = h.hex2rgb(bg_color)
hover_color = h.hex2rgb(hover_color)
py_code = (
f'{name} = Button({text}, {color}, {bg_color}, {hover_color}, {font}, {width}, {height})\n'
f'button_list.append({name})\n'
)
return py_code
def new_event(name):
py_code = (
f'{name} = pg.USEREVENT + event_number\n'
f'event_number += 1\n'
)
return py_code
# ---------- Music and Sound -----------
def new_sound(name, path):
py_code = (
f'{name} = pg.mixer.Sound("{path}")\n'
)
return py_code
def play_sound(sound):
py_code = (
f'{sound}.play()\n'
)
return py_code
def set_sound_volume(sound, volume):
py_code = (
f'{sound}.set_volume({volume})\n'
)
return py_code
def set_music_volume(volume):
py_code = (
f'pg.mixer.music.set_volume({volume})\n'
)
return py_code
def play_music(tabs, path):
py_code = (
f'\n{tabs}pg.mixer.music.load("{path}")\n'
f'{tabs}pg.mixer.music.play(loops=-1)\n'
)
return py_code
def unpause_music():
py_code = (
'pg.mixer.music.unpause()\n'
)
return py_code
def pause_music():
py_code = (
'pg.mixer.music.pause()\n'
)
return py_code
def stop_music():
py_code = (
'pg.mixer.music.fadeout(500)\n'
)
return py_code
# ---------- Manipulate Objects -----------
def draw(tabs, sprite, x, y):
py_code = (
f'\n{tabs}if "{sprite}" in hero_data.keys():\n'
f'{tabs}\t{sprite} = Hero(**hero_data["{sprite}"])\n'
f'{tabs}{sprite}.draw({x}, {y})\n'
)
return py_code
def destroy(sprite):
py_code = (
f'{sprite}.destroy()\n'
)
return py_code
def move(sprite, x, y):
py_code = (
f'{sprite}.move({x}, {y})\n'
)
return py_code
def move_by(sprite, x, y):
py_code = (
f'{sprite}.move_by({x}, {y})\n'
)
return py_code
def change_size(sprite, width, height):
py_code = (
f'{sprite}.surf = pg.transform.scale({sprite}.surf, ({width}, {height}))\n'
)
return py_code
def rotate(sprite, angle):
py_code = (
f'{sprite}.surf = pg.transform.rotate({sprite}.surf, {angle})\n'
)
return py_code
# ---------- Get functions -----------
def get_mouse_pos():
py_code = (
'mouse_pos\n'
)
return py_code
def get_pos(sprite):
py_code = (
f'({sprite}.rect.centerx, {sprite}.rect.centery)\n'
)
return py_code
def get_size(sprite):
py_code = (
f'({sprite}.rect.width, {sprite}.rect.height)\n'
)
return py_code
# ---------- Animations -----------
def new_animation(name, sprite, path):
py_code = (
f'{sprite}.add_animation("{name}", "{path}")\n'
)
return py_code
def play_animation(animation, sprite):
py_code = (
f'{sprite}.play_animation("{animation}", dt)\n'
)
return py_code
def play_group_animation(tabs, animation, group):
py_code = (
f'\n{tabs}for entity in {group}:\n'
f'{tabs}\tentity.play_animation("{animation}", dt)\n'
)
return py_code
# ---------- Sprite Groups -----------
def new_hero_group(name):
py_code = (
f'{name} = pg.sprite.Group()\n'
)
return py_code
def add_to_group(tabs, sprite, group, x, y):
py_code = (
f'\n{tabs}{sprite} = Hero(**hero_data["{sprite}"])\n'
f'{tabs}{group}.add({sprite})\n'
f'{tabs}{sprite}.draw({x}, {y})\n'
)
return py_code
def remove_from_group(sprite, group):
py_code = (
f'{group}.remove({sprite})\n'
)
return py_code
def empty_group(tabs, group):
py_code = (
f'\n{tabs}for entity in {group}:\n'
f'{tabs}\tentity.kill()\n'
)
return py_code
# ---------- Other -----------
def exec_after(time, event):
py_code = (
f'pg.time.set_timer({event}, {time}, True)\n'
)
return py_code
def set_timer(time, event):
py_code = (
f'pg.time.set_timer({event}, {time})\n'
)
return py_code
def start_clock():
py_code = (
f'clock_paused = False\n'
)
return py_code
def pause_clock():
py_code = (
f'clock_paused = True\n'
)
return py_code
def reset_clock():
py_code = (
f'izteklo_vreme = 0\n'
)
return py_code
def show_cursor():
py_code = (
'pg.mouse.set_visible(True)\n'
)
return py_code
def hide_cursor():
py_code = (
'pg.mouse.set_visible(False)\n'
)
return py_code
# ---------- Main Function -----------
def update():
py_code = (
'def update(dt, mouse_pos, events, keys):\n'
'\tglobal izteklo_vreme, clock_paused\n'
'\tpass\n'
)
return py_code
# ---------- Quit game action -----------
def game_over():
py_code = (
'return "GAME OVER"\n'
)
return py_code
# ---------- End of file action -----------
def gg():
global add_to_main_loop
add_to_main_loop = add_to_main_loop.replace('\n', '\n\t')
add_to_main_loop = add_to_main_loop[:add_to_main_loop.rfind('\t')]
add_to_main_loop = '\t' + add_to_main_loop if add_to_main_loop else ''
py_code = (
'while running:\n'
'\tdt = clock.tick(FPS) / 1000\n'
'\tif not clock_paused:\n'
'\t\tizteklo_vreme += dt\n'
'\tmouse_pos = pg.mouse.get_pos()\n'
'\tevents = []\n'
'\tkeys = pg.key.get_pressed()\n'
f'{add_to_main_loop}'
'\tfor event in pg.event.get():\n'
'\t\tevents.append(event.type)\n'
'\t\tif event.type == QUIT:\n'
'\t\t\trunning = False\n'
'\tfor btn in button_list:\n'
'\t\t_ = (btn.rect.x, btn.rect.y, btn.rect.width, btn.rect.height)\n'
'\t\tif _[0] + _[2] > mouse_pos[0] > _[0] and _[1] + _[3] > mouse_pos[1] > _[1] and not btn.is_hover:\n'
'\t\t\tbtn.hover()\n'
'\t\telif not (_[0] + _[2] > mouse_pos[0] > _[0] and _[1] + _[3] > mouse_pos[1] > _[3]):\n'
'\t\t\tbtn.no_hover()\n'
'\tif update(dt, mouse_pos, events, keys) == "GAME OVER":\n'
'\t\trunning = False\n'
'\tfor entity in sprite_group:\n'
'\t\tscreen.blit(entity.surf, entity.rect)\n'
'\tpg.display.flip()\n'
'pg.mixer.stop()\n'
'pg.mixer.music.stop()\n'
'pg.mixer.quit()\n'
'pg.quit()\n'
'os._exit(1)'
)
return py_code