Added bullet collision and HP system
This commit is contained in:
parent
407c96e3ae
commit
65073bc55c
Binary file not shown.
Binary file not shown.
BIN
__pycache__/maps.cpython-37.pyc
Normal file
BIN
__pycache__/maps.cpython-37.pyc
Normal file
Binary file not shown.
Binary file not shown.
@ -8,6 +8,7 @@ WINDOW_HEIGHT = 720
|
||||
FPS = 30
|
||||
WIN_NAME = "Kostschevsky's shooter"
|
||||
|
||||
PLAYER_HP=100
|
||||
# Controls
|
||||
|
||||
# player 1
|
||||
@ -27,3 +28,4 @@ P2_SHOOT=pygame.K_RCTRL
|
||||
# bullets
|
||||
BULLET_SPEED=20
|
||||
SHOOT_SPEED=200
|
||||
BULLET_DMG=20
|
||||
|
BIN
data/graphics/hp.png
Normal file
BIN
data/graphics/hp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 158 B |
@ -50,12 +50,12 @@ def collision_check(p):
|
||||
return False
|
||||
|
||||
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)
|
||||
if hits1:
|
||||
print("Player 1 killed Player 2")
|
||||
players[1].gothit()
|
||||
if hits2:
|
||||
print("Player 2 killed Player 1")
|
||||
players[0].gothit()
|
||||
|
||||
def player1_input(keys):
|
||||
if keys[P1_SHOOT]:
|
||||
|
25
maps.py
25
maps.py
@ -2,10 +2,14 @@ from os import path
|
||||
import pygame
|
||||
|
||||
walls=pygame.sprite.Group()
|
||||
bwalls=pygame.sprite.Group()
|
||||
|
||||
class Hitbox(pygame.sprite.Sprite):
|
||||
def __init__(self, x, y):
|
||||
self.groups=walls
|
||||
def __init__(self, x, y, instance):
|
||||
if instance=='player':
|
||||
self.groups=walls
|
||||
elif instance=='bullet':
|
||||
self.groups=bwalls
|
||||
pygame.sprite.Sprite.__init__(self, self.groups)
|
||||
self.image=pygame.Surface((20,20))
|
||||
self.rect=self.image.get_rect()
|
||||
@ -16,6 +20,9 @@ class Hitbox(pygame.sprite.Sprite):
|
||||
|
||||
game_folder=path.dirname(__file__)
|
||||
map_pdata=[]
|
||||
map_bdata=[]
|
||||
|
||||
# player collison map
|
||||
|
||||
with open(path.join(game_folder, 'map_player_collision.txt')) as 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 col, tile in enumerate(tiles):
|
||||
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')
|
||||
|
||||
|
@ -13,7 +13,7 @@ from pygame.locals import *
|
||||
# initialization
|
||||
|
||||
pygame.init()
|
||||
pygame.mixer.init()
|
||||
#pygame.mixer.init()
|
||||
screen=pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
|
||||
pygame.display.set_caption(WIN_NAME)
|
||||
clock=pygame.time.Clock()
|
||||
@ -45,6 +45,7 @@ def draw_debug_text():
|
||||
count+=1
|
||||
|
||||
bg, bg_rect=load_img("bg.png")
|
||||
|
||||
# game loop
|
||||
|
||||
while events.running:
|
||||
|
31
sprites.py
31
sprites.py
@ -33,14 +33,17 @@ class Player(pygame.sprite.Sprite):
|
||||
def __init__(self, fname, s_pos_x, s_pos_y, speed):
|
||||
pygame.sprite.Sprite.__init__(self)
|
||||
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.speed=vec(0, 0)
|
||||
self.acc=vec(0, 0)
|
||||
self.friction=-0.25
|
||||
self.facing=0 # where the player is looking (0-north, 1-east, 2-south, 3-west)
|
||||
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):
|
||||
if self.facing!=0:
|
||||
@ -79,10 +82,25 @@ class Player(pygame.sprite.Sprite):
|
||||
return True
|
||||
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):
|
||||
if self.colliding or self.wallcollide():
|
||||
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.acc=vec(0, 0)
|
||||
self.colliding=False
|
||||
@ -94,13 +112,16 @@ class Player(pygame.sprite.Sprite):
|
||||
self.rect.center=self.pos
|
||||
|
||||
def update(self):
|
||||
#if self.alive==False:
|
||||
#print("rip")
|
||||
self.move()
|
||||
#self.draw_healthbar()
|
||||
|
||||
class Bullet(pygame.sprite.Sprite):
|
||||
def __init__(self, fname, x, y, direction):
|
||||
pygame.sprite.Sprite.__init__(self)
|
||||
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.direction=direction
|
||||
|
||||
@ -117,5 +138,5 @@ class Bullet(pygame.sprite.Sprite):
|
||||
|
||||
def update(self):
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user