Added bullet collision and HP system

This commit is contained in:
Marcin Kostrzewski 2019-01-30 20:21:08 +01:00
parent 407c96e3ae
commit 65073bc55c
10 changed files with 55 additions and 12 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -8,6 +8,7 @@ WINDOW_HEIGHT = 720
FPS = 30 FPS = 30
WIN_NAME = "Kostschevsky's shooter" WIN_NAME = "Kostschevsky's shooter"
PLAYER_HP=100
# Controls # Controls
# player 1 # player 1
@ -27,3 +28,4 @@ P2_SHOOT=pygame.K_RCTRL
# bullets # bullets
BULLET_SPEED=20 BULLET_SPEED=20
SHOOT_SPEED=200 SHOOT_SPEED=200
BULLET_DMG=20

BIN
data/graphics/hp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 158 B

View File

@ -50,12 +50,12 @@ def collision_check(p):
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, False, True)
hits2=pygame.sprite.groupcollide(p1_group, p2_bullet_group, True, True) hits2=pygame.sprite.groupcollide(p1_group, p2_bullet_group, True, True)
if hits1: if hits1:
print("Player 1 killed Player 2") players[1].gothit()
if hits2: if hits2:
print("Player 2 killed Player 1") players[0].gothit()
def player1_input(keys): def player1_input(keys):
if keys[P1_SHOOT]: if keys[P1_SHOOT]:

23
maps.py
View File

@ -2,10 +2,14 @@ from os import path
import pygame import pygame
walls=pygame.sprite.Group() walls=pygame.sprite.Group()
bwalls=pygame.sprite.Group()
class Hitbox(pygame.sprite.Sprite): class Hitbox(pygame.sprite.Sprite):
def __init__(self, x, y): def __init__(self, x, y, instance):
if instance=='player':
self.groups=walls self.groups=walls
elif instance=='bullet':
self.groups=bwalls
pygame.sprite.Sprite.__init__(self, self.groups) pygame.sprite.Sprite.__init__(self, self.groups)
self.image=pygame.Surface((20,20)) self.image=pygame.Surface((20,20))
self.rect=self.image.get_rect() self.rect=self.image.get_rect()
@ -16,6 +20,9 @@ class Hitbox(pygame.sprite.Sprite):
game_folder=path.dirname(__file__) game_folder=path.dirname(__file__)
map_pdata=[] map_pdata=[]
map_bdata=[]
# player collison map
with open(path.join(game_folder, 'map_player_collision.txt')) as f: with open(path.join(game_folder, 'map_player_collision.txt')) as f:
for line in f: for line in f:
@ -24,4 +31,16 @@ with open(path.join(game_folder, 'map_player_collision.txt')) as f:
for row, tiles in enumerate(map_pdata): for row, tiles in enumerate(map_pdata):
for col, tile in enumerate(tiles): for col, tile in enumerate(tiles):
if tile=='1': if tile=='1':
Hitbox(col, row) Hitbox(col, row, 'player')
# bullet collision map
with open(path.join(game_folder, 'map_bullet_collision.txt')) as f:
for line in f:
map_bdata.append(line)
for row, tiles in enumerate(map_bdata):
for col, tile in enumerate(tiles):
if tile=='1':
Hitbox(col, row, 'bullet')

View File

@ -13,7 +13,7 @@ from pygame.locals import *
# initialization # initialization
pygame.init() pygame.init()
pygame.mixer.init() #pygame.mixer.init()
screen=pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) screen=pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption(WIN_NAME) pygame.display.set_caption(WIN_NAME)
clock=pygame.time.Clock() clock=pygame.time.Clock()
@ -45,6 +45,7 @@ def draw_debug_text():
count+=1 count+=1
bg, bg_rect=load_img("bg.png") bg, bg_rect=load_img("bg.png")
# game loop # game loop
while events.running: while events.running:

View File

@ -33,14 +33,17 @@ class Player(pygame.sprite.Sprite):
def __init__(self, fname, s_pos_x, s_pos_y, speed): def __init__(self, fname, s_pos_x, s_pos_y, speed):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.image, self.rect=load_img(fname +'.png') self.image, self.rect=load_img(fname +'.png')
self.rect.center=(s_pos_x, s_pos_y) self.rect.center=vec(s_pos_x, s_pos_y)
self.pos=vec(s_pos_x, s_pos_y) self.pos=vec(s_pos_x, s_pos_y)
self.speed=vec(0, 0) self.speed=vec(0, 0)
self.acc=vec(0, 0) self.acc=vec(0, 0)
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
self.lastpos=self.rect.center self.lastpos=(0,0)
self.hp=PLAYER_HP
self.alive=True
self.hp_visible=False
def moveup(self): def moveup(self):
if self.facing!=0: if self.facing!=0:
@ -79,10 +82,25 @@ class Player(pygame.sprite.Sprite):
return True return True
return False return False
def draw_healthbar(self):
width=int(self.rect.width*self.hp/100)
self.hp_bar=pygame.Rect(0, 0, width, 7)
if self.hp_visible:
pygame.draw.rect(pygame.display.get_surface(), (255,0,0), self.hp_bar)
def gothit(self):
self.hp_visible=True
if self.hp>0:
self.hp-=BULLET_DMG
else:
self.alive=False
print(self.hp)
def move(self): def move(self):
if self.colliding or self.wallcollide(): if self.colliding or self.wallcollide():
self.rect.center=self.lastpos self.rect.center=self.lastpos
self.pos=self.lastpos self.pos=vec(self.lastpos[0], self.lastpos[1]) ## tuple to vector
self.speed=vec(0, 0) self.speed=vec(0, 0)
self.acc=vec(0, 0) self.acc=vec(0, 0)
self.colliding=False self.colliding=False
@ -94,13 +112,16 @@ class Player(pygame.sprite.Sprite):
self.rect.center=self.pos self.rect.center=self.pos
def update(self): def update(self):
#if self.alive==False:
#print("rip")
self.move() self.move()
#self.draw_healthbar()
class Bullet(pygame.sprite.Sprite): class Bullet(pygame.sprite.Sprite):
def __init__(self, fname, x, y, direction): def __init__(self, fname, x, y, direction):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.image, self.rect=load_img(fname + '.png') self.image, self.rect=load_img(fname + '.png')
self.rect.center=(x, y) self.rect.center=vec(x, y)
self.speed=vec(0, 0) self.speed=vec(0, 0)
self.direction=direction self.direction=direction
@ -117,5 +138,5 @@ class Bullet(pygame.sprite.Sprite):
def update(self): def update(self):
self.shoot() self.shoot()
if self.rect.bottom<0 or self.rect.right<0 or self.rect.left>WINDOW_WIDTH or self.rect.top>WINDOW_HEIGHT: if not pygame.sprite.spritecollideany(self, maps.bwalls, collided = None)==None:
self.kill() self.kill()