From ad665ca3e322b5c20df0d96d50794c99b1f0681a Mon Sep 17 00:00:00 2001 From: s452635 Date: Fri, 18 Jun 2021 03:49:25 +0200 Subject: [PATCH] added an explosion animation (type SPACE while in menu) that can be called by an event --- assets/explosion.py | 74 ++++++++++++++++++ main.py | 23 +++++- project_constants.py | 2 + .../assets/explosion_frames/explosion-0.png | Bin 0 -> 93 bytes .../assets/explosion_frames/explosion-1.png | Bin 0 -> 264 bytes .../assets/explosion_frames/explosion-10.png | Bin 0 -> 258 bytes .../assets/explosion_frames/explosion-2.png | Bin 0 -> 330 bytes .../assets/explosion_frames/explosion-3.png | Bin 0 -> 398 bytes .../assets/explosion_frames/explosion-4.png | Bin 0 -> 629 bytes .../assets/explosion_frames/explosion-5.png | Bin 0 -> 718 bytes .../assets/explosion_frames/explosion-6.png | Bin 0 -> 882 bytes .../assets/explosion_frames/explosion-7.png | Bin 0 -> 813 bytes .../assets/explosion_frames/explosion-8.png | Bin 0 -> 611 bytes .../assets/explosion_frames/explosion-9.png | Bin 0 -> 467 bytes 14 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 assets/explosion.py create mode 100644 resources/assets/explosion_frames/explosion-0.png create mode 100644 resources/assets/explosion_frames/explosion-1.png create mode 100644 resources/assets/explosion_frames/explosion-10.png create mode 100644 resources/assets/explosion_frames/explosion-2.png create mode 100644 resources/assets/explosion_frames/explosion-3.png create mode 100644 resources/assets/explosion_frames/explosion-4.png create mode 100644 resources/assets/explosion_frames/explosion-5.png create mode 100644 resources/assets/explosion_frames/explosion-6.png create mode 100644 resources/assets/explosion_frames/explosion-7.png create mode 100644 resources/assets/explosion_frames/explosion-8.png create mode 100644 resources/assets/explosion_frames/explosion-9.png diff --git a/assets/explosion.py b/assets/explosion.py new file mode 100644 index 0000000..ffa03d9 --- /dev/null +++ b/assets/explosion.py @@ -0,0 +1,74 @@ + +import pygame +import os + +from project_constants import V_TILE_SIZE, DIR_ASSETS, SCREEN +import assets.display_assets + + +class Explosion(pygame.sprite.Sprite): + + def __init__(self, coords: (int, int)): + + pygame.sprite.Sprite.__init__(self) + + self.explosion_animation = [ + pygame.transform.scale( + pygame.image.load(os.path.join(DIR_ASSETS, "explosion_frames", "explosion-" + str(x) + ".png")), + (V_TILE_SIZE, V_TILE_SIZE) + ) + for x in range(11) + ] + + self.last_update = pygame.time.get_ticks() + self.frame_rate = 100 + self.frame = 0 + self.image = self.explosion_animation[0] + self.rect = pygame.Rect( + assets.display_assets.calculate_screen_position(coords), + (V_TILE_SIZE, V_TILE_SIZE) + ) + + def update(self, *args, **kwargs): + + now = pygame.time.get_ticks() + if now - self.last_update > self.frame_rate: + self.last_update = now + self.frame += 1 + + if self.frame == len(self.explosion_animation): + self.kill() + + else: + self.image = self.explosion_animation[self.frame] + + +# only for explosion animation testing +def main(): + + pygame.init() + + explosions = pygame.sprite.Group() + + running = True + while running: + + explosions.update() + SCREEN.fill((0, 0, 0)) + explosions.draw(SCREEN) + pygame.display.flip() + + keys = pygame.key.get_pressed() + for event in pygame.event.get(): + + if event.type == pygame.QUIT: + running = False + + elif keys[pygame.K_g]: + explosions.add(Explosion((2, 3))) + elif keys[pygame.K_h]: + explosions.add(Explosion((3, 6))) + + +if __name__ == '__main__': + main() diff --git a/main.py b/main.py index 4b72059..f001cc2 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,21 @@ # libraries import pygame from pyglet.gl import * # for blocky textures + # other files of this project from game import Game import project_constants as const +from assets.explosion import Explosion + + +def update_graphics(): + const.EXPLOSIONS.update() + const.EXPLOSIONS.draw(const.SCREEN) + pygame.display.flip() def main(): + pygame.init() pygame.display.set_caption(const.V_NAME_OF_WINDOW) @@ -43,6 +52,12 @@ def main(): # checking if game should stop running running = not is_quit_button_pressed(events) + # TODO : added for testing, remove after moving the explosion line + keys = pygame.key.get_pressed() + if keys[pygame.K_SPACE]: + # TODO : move this line to where explosion is called + const.EXPLOSIONS.add(Explosion((2, 3))) + # drawing minefield and agent instances game.draw_minefield() @@ -53,7 +68,7 @@ def main(): clock.tick(const.V_FPS) # updating window graphics - pygame.display.flip() + update_graphics() # if ok button is clicked then leave menu section in_menu = not game.button_ok.is_clicked(pygame.mouse.get_pos(), events) @@ -109,7 +124,7 @@ def main(): game.set_is_active_flag_for_all_in_game_gui_components(False) # updating graphics - pygame.display.flip() + update_graphics() # update turn game.update_time(time) @@ -127,7 +142,7 @@ def main(): game.cleanup_after_game_loop() game.draw_minefield() game.run_in_game_menu_overlay(pygame.mouse.get_pos(), events) - pygame.display.flip() + update_graphics() if auto: if not game.agent.defuse_a_mine(game.get_mine(game.goal)): @@ -164,7 +179,7 @@ def main(): auto = False # updating graphics - pygame.display.flip() + update_graphics() if __name__ == "__main__": diff --git a/project_constants.py b/project_constants.py index 28e683e..1ea70e6 100644 --- a/project_constants.py +++ b/project_constants.py @@ -41,7 +41,9 @@ V_BUTTON_HEIGHT = 40 SCREEN_WIDTH = V_TILE_AREA_WIDTH + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING + V_SIDE_MENU_WIDTH + 10 SCREEN_HEIGHT = V_TILE_AREA_HEIGHT + 2 * V_SCREEN_PADDING + V_NUMBER_PADDING + SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) +EXPLOSIONS = pygame.sprite.Group() # =============== # diff --git a/resources/assets/explosion_frames/explosion-0.png b/resources/assets/explosion_frames/explosion-0.png new file mode 100644 index 0000000000000000000000000000000000000000..70654e1c1926b0a5c8e2b7992e97ba7d03dc714a GIT binary patch literal 93 zcmeAS@N?(olHy`uVBq!ia0vp^HXzKw1|+Ti+$;i8DxNNmAsLNt4;nH8d50E!^?#em cxd6yS0(llS42-$evB@AYPgg&ebxsLQ0DnakRsaA1 literal 0 HcmV?d00001 diff --git a/resources/assets/explosion_frames/explosion-1.png b/resources/assets/explosion_frames/explosion-1.png new file mode 100644 index 0000000000000000000000000000000000000000..d8616960fa9bfec41af31cd3a6a60aeae244aa83 GIT binary patch literal 264 zcmeAS@N?(olHy`uVBq!ia0vp^HXzKw1|+Ti+$;i8=RI8xE=%j8rEsJZH;kzbtb{i<1s3P%(qRf@{^avKK%9+-Y)9_fuiD{C_FG0^PJ4 z{_XP*edC_0wqy1ag^Dtlaj!mW_l=x`Vr&JLy7iCk>QB2O9J0e?G`i&50PK(&O02<7@mqxnKYTxO=#J zta6_2Xp-CZWrMFCZ)Ra;rx$G60?P%rDy2jzW#%I> z_GewWU;qFxPf961$6e=L_SI7^xHq|Qx!|00`{{Lpxv4Kdb~min`r_7004kLVYyAqr}W-m zbJ7S~vBzc2^Sb7A;%S9<(LJ>-t_n9?q0$Pg7OstbCG5Jree((LNa1MyE77n@a>mtIXM?|j>rJu1V(e%Ra)~dY>lor4V!ePQ*OV$=Ze3sPDL4<#W z{2g03bc$n-qGnx$qr?vj)e4)|M>vXaTUxHCh91JfsExI{&Upn2C#`Vz140z(gaH5m c005Hl0{%CSdteJD)Bpeg07*qoM6N<$fb85*1K^a}g;(u(Nm4`2*UWxeT!0jpihCI4FvuC>uy8$Q-y z<%Oel2pk3VZe{Cn#NY^=OW;-@8Wv@FkD>B&8OSaq0!M+Z7lPW01_Cp1V!Z07*qoM6N<$f^hn*g#Z8m literal 0 HcmV?d00001 diff --git a/resources/assets/explosion_frames/explosion-4.png b/resources/assets/explosion_frames/explosion-4.png new file mode 100644 index 0000000000000000000000000000000000000000..cf49f9b825593174b4eb29afcef2a574614d39b2 GIT binary patch literal 629 zcmV-*0*d{KP)b3$8$lq@|+f7QR4kK*Q0L!j_Atyu6hxdk9+3Y!N20^eL;= z54&CEC!7xu(=qO27*~?i}zH6Hv?81hujpG?}|`JZSyj?bc53j z82=z?5L0i8++rr-3&F+z*=E3W0|8K0WNHmSY?m!}4$hss#fOE3#f@z_8CY#>9E?ya z7++j%U;z9)?!)uAmtdCS4$e(w7Wcjn_g_*gfr}y+!4ZmKEq)U2V1QV|oZ{?|KvLSl zYlB&eVX)$tq;3Q&I2iy3F9l<&Ca}^2J{GTBI4NojY+jb$A3Qb0z!JbV5L1|!DRE6} z1?!DRD`3_z1uH0t+FMU7lF9LM`aK6TFs8OzE=s`&w_XU~R7sAP&_4&yFk{iEmD07q z^Lt4D9IOrlR;b!)ssXTG0a>9AE(NPYMC@QL+7D$mTQfY{E0o!;IcV+3YE zS^>E(0$5$&{8i~5xC$DMhP}@7`Z~|4W4s>g%wiq`{4qaltPI{gumRFy9v#$@^gL~h z0pon$m%uTv8N54SN;i|T0W)w{2LQ@4Mqrb?CClapr#oOvF0qk;TXIogIK*;vH1N&k zsXO2plvIFNOPMAsNSTcWElZLaaM%FOKyL+RnK00!fH#A81563hu?*P2830R)X14}t z0GpHvYUL)AQfo3}OK5>=1s}jxh77DKB{meFZ`1nuHl@G@el!3BI0Lo@p(?Fwl6rsI z8l^1YRjCi)DzJg_FkuJtDtJiTj*nG|XJvB$8*sHjus|3%rOjlPJutBS1~7ql8vvXe z>?9`At2z?*4d7kbV)+tKD}%e%vO8c)3Ix_NQA{r3NcIzrB}22U6^{J}Fc+GXcS)oO z%=@=Kl*=qTr1T4H4po7(uY}pX9^NS^{2Rb@ge_ldl48hq}sUH{9UpvUbnJ2fMY|ZeQVixC@_n8)fU#u zpuXmP2Cx9fumazjOsw@g3TzJoxR0$Hz%jCBz6xlqBwIiR_}k*#X@^&Ft?;0Z=_261W+(TVMk}+A>xH*lKqJ?H0HS zo;LN6{87M={*Qrn3(P@~*7eooM*9ht()g>uUmqK36W?PdX#fBK07*qoM6N<$f}HI@ Ay#N3J literal 0 HcmV?d00001 diff --git a/resources/assets/explosion_frames/explosion-6.png b/resources/assets/explosion_frames/explosion-6.png new file mode 100644 index 0000000000000000000000000000000000000000..187dc95d91dd0fcfc8df12e39e0fffdb40fd8d75 GIT binary patch literal 882 zcmV-&1C9KNP)2&M5Jmd{lNf9alkMWb>3|anYzzWjVKWb6VQ!!U_yEF*lS!29C43a$rZ?F6!_2q7 zs#@zcEZqeajHK?aSE{ZqwKO3l$~ca39LKobZqL+e7k_+@Q<^&dmrFLl0EPB_AN#&P z{{H!Gb*6mY>ThP~-9abYDJ{TWFh1UOUF^Ef1YlsVL1^bTn8r_@D%k^Dplm4gscGU< z(@0=Wb~ui9@jjd>_owW5Z?58dbL9um9vGks7=U$M$GWbMhCz|U0Epk5?AT$P1i4=Y z4q!KUcEAj10Rt2&J0@WVC}6$;0ERt$ex|@VTi)^oR!OF+s#sOk2cWiXW81ciD;xC7 z%b4Y8%WfpQlS>Nl`kVR9f_o)9Ap(p1sK4b#u*gLhFX>&3W&kITS7}k zffrx~_JTrG$fZ8e!ww!l)lmf)z{4=aVHhN^cUe*egts)rc>9|W_ctLv->%~G?doU; zCsTDJ*wOfXbXc~)P`?vPh2It~kZQ@ygOQ%SxC_CKWXJrEnN&Rfe8`7z- zT)dz&n|40Q4j5vEstO^i)P(W}ZBXk4yjlkTdJ1I+Y=Nti(Yryw;HiM04w@~n17=A4#V-eB`Coy51EUE1uNDAgjQ{`u07*qo IM6N<$f+(JdrT_o{ literal 0 HcmV?d00001 diff --git a/resources/assets/explosion_frames/explosion-7.png b/resources/assets/explosion_frames/explosion-7.png new file mode 100644 index 0000000000000000000000000000000000000000..7f666e3d89c06922ae54e31097dd014b59f494a3 GIT binary patch literal 813 zcmV+|1JeA7P)32T5Qeu?lPV;TGK3Ur1A#<>KtP}_b-EZg?*=+RPLSG-np`Tknb+hu)^GoBMzR&i z(+*4_d3R^NWi&f0ty3B)ecxN(_qJNC4k~_E?Z(>Mkw2tABZ~oMkgn^j>pBVMY1EDf zIK#yL=fDRH4(_kl>w$86H6cpbKKiLg#q(FS%fQ|M49W^ep>IuNZ%rdXRp_A~6zy=W z{QYV!?W?(T$IrmW3Ol^Hzp%~y#nu6Py)k>eaYLH>DFBSa<>d^@ z{nULAe5XkU9yr8~0oXIl!Ucf`>VbRmk5Y5tzzP&0JQkP>5akG#pQ@^?s;a$YGqrHY zzzUc_;V^)`f(gO9b()Fk~O1P+JKu$_g;P>*(-pjMS7Xi^D5>ZfyJtecJl!5mI4k&N{ z!vQ{w!sDs_3Jkwvg1CP)l*5nMVu00Q6)x#`Q{a6fVE}#<)DZ_LUhzahGO!Xj!rQp$ z$FFEH@2N_LQ4l1n49qh3(Lyx=C=H=gK}Q!tiw8c{1bE=5lF{}S2h1I#st$yW4M|A! z)6U6caljA-$b|w53-9OxD-OCvk0nn%?2>_}f^)H-Qu_SX!QXD8kA{~tr@s8lz+(j= riDb3&Q|J#>W0T@fFK>JDTY>)qpt@LW1jEHV00000NkvXXu0mjf7|(le literal 0 HcmV?d00001 diff --git a/resources/assets/explosion_frames/explosion-8.png b/resources/assets/explosion_frames/explosion-8.png new file mode 100644 index 0000000000000000000000000000000000000000..e13974e5d164067b024acf8aecc8f60f5972e052 GIT binary patch literal 611 zcmV-p0-XJcP)VjU8v_ z1@r~&8Vv)?0Jd#w+qQ*|W1JHb=F@P%M};67(7x|&-}fd7x^1Fv11uesQd%iR?MRGD zV+J%3yp92DiL@pvfxXfUcuNkATN8Bo7#<$DcfTwip7$$J=WJMp1HM`^nHna?1QtD} zX?l1UeygNEZ^_GYXFdN#{|WqL2#rN2A#GB@jfxk@8!v%1&~;tgx~_kWMgz0fYX~K< z2CRWvOPc}VdA)~_S;GNaOHC4?^@F#E+7Q5g=Xq}PJpWuLKc9A+m%z{j)Cw$3dvJiI zjDYxi<~xDCz!Ff53n(*S6;4vtEhm6 zY5lxN>^0A{J{}m2;N-_1=v(`>Zb1|HC`i#5{3~q|OkhwLSli@d ziANJ)Em6FgVQ~|f8Nd#4tLX)18x!wjWQ~`=%oqj`U@0y97=;nDwsn)Q{528crijpA|omr7s)EFI%*oCcfDDuY zc$dipjwXPCo+Wqu;ehc%YwK;?`f8_}h6BDz%d)g(S=1qP*2gbz(`|uYyo!1Hw;Suw xV3X$2$GqM2@xZuY&_K28y_4d*m%G~ne*=msXdq|k_0j+U002ovPDHLkV1lym4aNWf literal 0 HcmV?d00001 diff --git a/resources/assets/explosion_frames/explosion-9.png b/resources/assets/explosion_frames/explosion-9.png new file mode 100644 index 0000000000000000000000000000000000000000..cba3caaac26a0403c4942361a2d147f6a44c4f6a GIT binary patch literal 467 zcmV;^0WAKBP)(z~Ah0pVh@2U3r+WfjBUihwx4r`dl2_r-Aee~XcWK0Y0J@{B_qqVkQ z13zAvW(3icaBcpj72d+Lj^?d^FclwNuWpDF-ps->p<*kR5v~p9>7as?dkz81T>;Ds z@xmzBITxLCY+)?$xeb;oHz!{G>WCNSic(>OqNR%qT-RDhtu*({9p<6W(mvGHuA&i{*qdeWXG|41SoVN&}MV5GM>$(-BQqHv^|@AaTOIprZ7Z zp!Y8y;B*&8mn#h^?*faNl-L&LLc37zzPksLEu8s1OQ}GuIG!ur{G)8)9IcK;xumv$ zt{#>ZH@G