Python-project/events.py

120 lines
3.6 KiB
Python
Raw Normal View History

2019-01-21 20:37:58 +01:00
import os, sys
import pygame
from pygame.locals import *
from sprites import *
2019-01-21 22:55:58 +01:00
from config import *
2019-01-21 20:37:58 +01:00
running=True
players=[]
2019-01-21 23:43:13 +01:00
p1_group=pygame.sprite.Group()
p2_group=pygame.sprite.Group()
2019-01-21 22:55:58 +01:00
p1_bullet=[]
p2_bullet=[]
p1_bullet_group=pygame.sprite.Group()
p2_bullet_group=pygame.sprite.Group()
2019-01-22 00:38:30 +01:00
clock=pygame.time.Clock()
clock.tick()
timer1=1000
clock2=pygame.time.Clock()
clock2.tick()
timer2=1000
2019-01-21 20:37:58 +01:00
def events():
global running
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=False
def collision_check(p):
# collision between players
if players[0].rect.colliderect(players[1].rect):
if p==1:
players[0].colliding=True
direction=players[1].facing
2019-01-21 21:56:43 +01:00
players[0].image=pygame.transform.rotate(players[0].image, players[0].facing*90-((direction+2)%4)*90)
2019-01-21 20:37:58 +01:00
players[0].facing=(direction+2)%4
return True
elif p==0:
players[1].colliding=True
direction=players[0].facing
2019-01-21 21:56:43 +01:00
players[1].image=pygame.transform.rotate(players[1].image, players[1].facing*90-((direction+2)%4)*90)
2019-01-21 20:37:58 +01:00
players[1].facing=(direction+2)%4
return True
else:
players[0].colliding=True
players[1].colliding=True
else:
return False
2019-01-22 00:38:30 +01:00
2019-01-21 23:43:13 +01:00
def bullethits():
2019-01-21 23:45:35 +01:00
hits1=pygame.sprite.groupcollide(p2_group, p1_bullet_group, True, True)
hits2=pygame.sprite.groupcollide(p1_group, p2_bullet_group, True, True)
if hits1:
print("Player 1 killed Player 2")
if hits2:
print("Player 2 killed Player 1")
2019-01-21 20:37:58 +01:00
def player1_input(keys):
2019-01-21 22:55:58 +01:00
if keys[P1_SHOOT]:
2019-01-22 00:38:30 +01:00
global timer1
p1_bullet.append(Bullet('bullet', players[0].pos.x, players[0].pos.y, players[0].facing))
if timer1>SHOOT_SPEED:
2019-01-21 22:55:58 +01:00
p1_bullet_group.add(p1_bullet[-1])
all_sprites.add(p1_bullet[-1])
p1_bullet[-1].direction=players[0].facing
p1_bullet[-1].shoot()
2019-01-22 00:38:30 +01:00
timer1=clock.tick()
else:
del p1_bullet[-1]
2019-01-21 20:37:58 +01:00
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):
if keys[P1_RIGHT]:
players[0].moveright()
if keys[P1_LEFT]:
players[0].moveleft()
if keys[P1_UP]:
players[0].moveup()
if keys[P1_DOWN]:
players[0].movedown()
def player2_input(keys):
2019-01-21 22:55:58 +01:00
if keys[P2_SHOOT]:
2019-01-22 00:38:30 +01:00
global timer2
p2_bullet.append(Bullet('bullet', players[1].pos.x, players[1].pos.y, players[1].facing))
if timer2>SHOOT_SPEED:
2019-01-21 23:45:35 +01:00
p2_bullet_group.add(p2_bullet[-1])
2019-01-21 22:55:58 +01:00
all_sprites.add(p2_bullet[-1])
p2_bullet[-1].direction=players[1].facing
p2_bullet[-1].shoot()
2019-01-22 00:38:30 +01:00
timer2=clock.tick()
else:
del p2_bullet[-1]
2019-01-21 20:37:58 +01:00
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):
if keys[P2_RIGHT]:
players[1].moveright()
if keys[P2_LEFT]:
players[1].moveleft()
if keys[P2_UP]:
players[1].moveup()
if keys[P2_DOWN]:
players[1].movedown()
def event_handler():
2019-01-22 00:38:30 +01:00
global timer1
global timer2
2019-01-21 20:37:58 +01:00
events()
keys=pygame.key.get_pressed()
collision_check(-1)
2019-01-21 23:43:13 +01:00
bullethits()
2019-01-22 00:38:30 +01:00
timer1+=clock.tick()
timer2+=clock2.tick()
2019-01-21 20:37:58 +01:00
player1_input(keys)
player2_input(keys)