From 131014dce51871e678974dfa8aa37b03da55eeae Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Tue, 22 Jan 2019 00:38:30 +0100 Subject: [PATCH] added shoot interval --- __pycache__/config.cpython-37.pyc | Bin 573 -> 573 bytes __pycache__/events.cpython-37.pyc | Bin 2693 -> 2973 bytes __pycache__/sprites.cpython-37.pyc | Bin 4306 -> 4367 bytes config.py | 2 +- events.py | 25 +++++++++++++++++++++++-- sprites.py | 8 +++++++- 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/__pycache__/config.cpython-37.pyc b/__pycache__/config.cpython-37.pyc index edb7a485462a61cf5ec12942115a0d8a36d96e57..d86aabaf143da4b3ed017ac72020292dc7c140fb 100644 GIT binary patch delta 27 hcmdnXvX_O|iI z^nQ2&U_!q(Mx7+SJ79{rOg__2bofx&bweJgSkqA;!Y*q8Fctsn$q*rzZS%ADTZuDn zcF>uWf5x)4XOcQgpH8`~@fNu&;O>Ca#LE9xGefa^o2p0L2_3;PG#m^-NBKE$$dUtU z7$m<1nj2`CK>iA}-ax}-#~__R?bGiggV3+PL}t>SscO4bIqt>Qm)F)xTkE$M7Z-HK zycDbKJgn3`L)9vEJ#9X{cO5I&xZ>B3hlYSd0)I(xzz&&^L)%8pX>S|kEIL=Pp#CD} zxaV;<{*yBQjP_xf9X>S@l({HJ@c1%EaFt+`z$F+X7$+#|4W2)oB(s-bieQ>xhTs}P z0VG|AwAw&3=Ee7S%8#qExK-QRf22w%{o92tVni?{Nqqo(od{& zxgC6H;6Gq2Z2E0wVq;Nj$rW-N(C@6$+6^@2AVCg+8g;koMdqn%|H$MgUM8 delta 854 zcmdT?J#Q015Z$>r=e@lTU+mcNkr3Gsur7*0;xmCJiV+}?+>io6BnqZVq!fEsP(+HN zBn@3O4F!?Tl(eCsqDG>iNBjZ06#M{Y;uBEPva5M-X71grW_FKf4x74huo+Et0=_!p)-_nd<)U{K(K x;R|xvtkg_fRG9VjF1vPKpd;W5oHZr)Y=0ND0~+|3VSzUN^G3V6skLtC#xIOKqYMB5 diff --git a/__pycache__/sprites.cpython-37.pyc b/__pycache__/sprites.cpython-37.pyc index fcc26b001b0e53c32e14a8e5599c2e18fe89c6cf..02d7a2f324b5773e1c2f3e1d0b3c505375a087c0 100644 GIT binary patch delta 406 zcmX|*O)mps5XYaf+wEhYeYRURO)3$uA?rXK5TruuC5T4S18F3LEm76Va;XUMc5a$+ zace(-Aie=#KpcA$B+d@bW=T9x@}HUCe`Yc-ZKZY@mr`f+7M7-0Udu8HSLN>6xr(T= zw2@J=GHa$oGt3RT{DQfhh@Q31m2{X`y{i+`|5*bu;TpvJ$-lnF`@2U4I&oiphdx{m ze83=%X>Sn2_TVQ>Qr?0++=cmv62AsWqN88JI^OE1vV*SLw@5f}fiY2lMZ6J-)&rIm z6{E_I*s63{iaAR1Q0?8zZ+Sq?j)|%fvuoKDV5dLzD!V#*rVe9H@An`YyUC7U8ta>W8#sPZ5W>Z-2tIy-Jhe*hg8SegI; delta 334 zcmeBIx}?bK#LLUY00dbcZZUlmc_kUUCaQZnWN}CFxHF`%r*O0|q;RA%XR&ATHZw=@ z0mV2|xPW3@a4~+M7csICa`hWA!bH}$+av`7_}xRu|8!qob18&n9*Xg4*Ls6 zdmvkWvI$2Dqub`095WdiJtrG;wKB#|-pSR-D-U!_u>g?ZVC3MM?9QFf=rVabx3fqJ zLokCTvtJd9UO`@Z5hGB6Cii419^1*;Jo-Y3AWdLzF!C{qF!C_+OkT(HjWJ{LQr?M- zR+G*75*Ujo&)_o!vd{4;@+N?kvjGX99qg0;@O@&eocx~OSIh}0Q~{D>0?QSF_?m)~ og9HpGPZF?Ubeeo#KutmfB*!ek2-M5M!Op?X#lpcP;wT~k00Xy4djJ3c diff --git a/config.py b/config.py index a392767..a0d5fb3 100644 --- a/config.py +++ b/config.py @@ -26,4 +26,4 @@ P2_SHOOT=pygame.K_RCTRL # bullets BULLET_SPEED=20 -SHOOT_SPEED=10 +SHOOT_SPEED=200 diff --git a/events.py b/events.py index 433472c..a617eed 100644 --- a/events.py +++ b/events.py @@ -15,6 +15,12 @@ p2_bullet=[] p1_bullet_group=pygame.sprite.Group() p2_bullet_group=pygame.sprite.Group() +clock=pygame.time.Clock() +clock.tick() +timer1=1000 +clock2=pygame.time.Clock() +clock2.tick() +timer2=1000 def events(): global running for event in pygame.event.get(): @@ -42,6 +48,7 @@ def collision_check(p): else: return False + def bullethits(): hits1=pygame.sprite.groupcollide(p2_group, p1_bullet_group, True, True) hits2=pygame.sprite.groupcollide(p1_group, p2_bullet_group, True, True) @@ -52,11 +59,16 @@ def bullethits(): def player1_input(keys): if keys[P1_SHOOT]: - p1_bullet.append(Bullet('bullet', players[0].pos.x, players[0].pos.y, players[0].facing)) + global timer1 + p1_bullet.append(Bullet('bullet', players[0].pos.x, players[0].pos.y, players[0].facing)) + if timer1>SHOOT_SPEED: p1_bullet_group.add(p1_bullet[-1]) all_sprites.add(p1_bullet[-1]) p1_bullet[-1].direction=players[0].facing p1_bullet[-1].shoot() + timer1=clock.tick() + else: + del p1_bullet[-1] if not (keys[P1_UP] or keys[P1_DOWN] or keys[P1_RIGHT] or keys[P1_LEFT]): players[0].stopmoving() elif not collision_check(0): @@ -71,11 +83,16 @@ def player1_input(keys): def player2_input(keys): if keys[P2_SHOOT]: - p2_bullet.append(Bullet('bullet', players[1].pos.x, players[1].pos.y, players[1].facing)) + global timer2 + p2_bullet.append(Bullet('bullet', players[1].pos.x, players[1].pos.y, players[1].facing)) + if timer2>SHOOT_SPEED: p2_bullet_group.add(p2_bullet[-1]) all_sprites.add(p2_bullet[-1]) p2_bullet[-1].direction=players[1].facing p2_bullet[-1].shoot() + timer2=clock.tick() + else: + del p2_bullet[-1] if not (keys[P2_UP] or keys[P2_DOWN] or keys[P2_RIGHT] or keys[P2_LEFT]): players[1].stopmoving() elif not collision_check(1): @@ -89,10 +106,14 @@ def player2_input(keys): players[1].movedown() def event_handler(): + global timer1 + global timer2 events() keys=pygame.key.get_pressed() collision_check(-1) bullethits() + timer1+=clock.tick() + timer2+=clock2.tick() player1_input(keys) player2_input(keys) diff --git a/sprites.py b/sprites.py index d7fd6c7..187320f 100644 --- a/sprites.py +++ b/sprites.py @@ -20,6 +20,10 @@ def load_sound(name): sound=pygame.mixer.Sound(sound_path) return sound +# timer + +clock=pygame.time.Clock() + # sprite groups all_sprites=pygame.sprite.Group() @@ -39,6 +43,7 @@ class Player(pygame.sprite.Sprite): self.friction=-0.25 self.facing=0 # where the player is looking (0-north, 1-east, 2-south, 3-west) self.colliding=False + def moveup(self): if self.facing!=0: self.image=pygame.transform.rotate(self.image, (self.facing*90)) @@ -100,12 +105,13 @@ class Bullet(pygame.sprite.Sprite): self.rect.center=(x, y) self.speed=vec(0, 0) self.direction=direction + self.clock=pygame.time.Clock() def shoot(self): if self.direction==0: self.speed.y=-BULLET_SPEED elif self.direction==1: - self.speed.x=BULLET_SPEED + self.speed.x=BULLET_SPEED elif self.direction==2: self.speed.y=BULLET_SPEED elif self.direction==3: