added shoot interval

This commit is contained in:
Marcin Kostrzewski 2019-01-22 00:38:30 +01:00
parent 8add7a077f
commit 131014dce5
6 changed files with 31 additions and 4 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -26,4 +26,4 @@ P2_SHOOT=pygame.K_RCTRL
# bullets # bullets
BULLET_SPEED=20 BULLET_SPEED=20
SHOOT_SPEED=10 SHOOT_SPEED=200

View File

@ -15,6 +15,12 @@ p2_bullet=[]
p1_bullet_group=pygame.sprite.Group() p1_bullet_group=pygame.sprite.Group()
p2_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(): def events():
global running global running
for event in pygame.event.get(): for event in pygame.event.get():
@ -42,6 +48,7 @@ def collision_check(p):
else: else:
return False return False
def bullethits(): def bullethits():
hits1=pygame.sprite.groupcollide(p2_group, p1_bullet_group, True, True) hits1=pygame.sprite.groupcollide(p2_group, p1_bullet_group, True, True)
hits2=pygame.sprite.groupcollide(p1_group, p2_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): def player1_input(keys):
if keys[P1_SHOOT]: if keys[P1_SHOOT]:
global timer1
p1_bullet.append(Bullet('bullet', players[0].pos.x, players[0].pos.y, players[0].facing)) 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]) p1_bullet_group.add(p1_bullet[-1])
all_sprites.add(p1_bullet[-1]) all_sprites.add(p1_bullet[-1])
p1_bullet[-1].direction=players[0].facing p1_bullet[-1].direction=players[0].facing
p1_bullet[-1].shoot() 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]): if not (keys[P1_UP] or keys[P1_DOWN] or keys[P1_RIGHT] or keys[P1_LEFT]):
players[0].stopmoving() players[0].stopmoving()
elif not collision_check(0): elif not collision_check(0):
@ -71,11 +83,16 @@ def player1_input(keys):
def player2_input(keys): def player2_input(keys):
if keys[P2_SHOOT]: if keys[P2_SHOOT]:
global timer2
p2_bullet.append(Bullet('bullet', players[1].pos.x, players[1].pos.y, players[1].facing)) 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]) p2_bullet_group.add(p2_bullet[-1])
all_sprites.add(p2_bullet[-1]) all_sprites.add(p2_bullet[-1])
p2_bullet[-1].direction=players[1].facing p2_bullet[-1].direction=players[1].facing
p2_bullet[-1].shoot() 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]): if not (keys[P2_UP] or keys[P2_DOWN] or keys[P2_RIGHT] or keys[P2_LEFT]):
players[1].stopmoving() players[1].stopmoving()
elif not collision_check(1): elif not collision_check(1):
@ -89,10 +106,14 @@ def player2_input(keys):
players[1].movedown() players[1].movedown()
def event_handler(): def event_handler():
global timer1
global timer2
events() events()
keys=pygame.key.get_pressed() keys=pygame.key.get_pressed()
collision_check(-1) collision_check(-1)
bullethits() bullethits()
timer1+=clock.tick()
timer2+=clock2.tick()
player1_input(keys) player1_input(keys)
player2_input(keys) player2_input(keys)

View File

@ -20,6 +20,10 @@ def load_sound(name):
sound=pygame.mixer.Sound(sound_path) sound=pygame.mixer.Sound(sound_path)
return sound return sound
# timer
clock=pygame.time.Clock()
# sprite groups # sprite groups
all_sprites=pygame.sprite.Group() all_sprites=pygame.sprite.Group()
@ -39,6 +43,7 @@ class Player(pygame.sprite.Sprite):
self.friction=-0.25 self.friction=-0.25
self.facing=0 # where the player is looking (0-north, 1-east, 2-south, 3-west) self.facing=0 # where the player is looking (0-north, 1-east, 2-south, 3-west)
self.colliding=False self.colliding=False
def moveup(self): def moveup(self):
if self.facing!=0: if self.facing!=0:
self.image=pygame.transform.rotate(self.image, (self.facing*90)) self.image=pygame.transform.rotate(self.image, (self.facing*90))
@ -100,6 +105,7 @@ class Bullet(pygame.sprite.Sprite):
self.rect.center=(x, y) self.rect.center=(x, y)
self.speed=vec(0, 0) self.speed=vec(0, 0)
self.direction=direction self.direction=direction
self.clock=pygame.time.Clock()
def shoot(self): def shoot(self):
if self.direction==0: if self.direction==0: