diff --git a/__pycache__/config.cpython-37.pyc b/__pycache__/config.cpython-37.pyc index d86aaba..f988de2 100644 Binary files a/__pycache__/config.cpython-37.pyc and b/__pycache__/config.cpython-37.pyc differ diff --git a/__pycache__/events.cpython-37.pyc b/__pycache__/events.cpython-37.pyc index d6af257..0cef001 100644 Binary files a/__pycache__/events.cpython-37.pyc and b/__pycache__/events.cpython-37.pyc differ diff --git a/__pycache__/maps.cpython-37.pyc b/__pycache__/maps.cpython-37.pyc new file mode 100644 index 0000000..5615569 Binary files /dev/null and b/__pycache__/maps.cpython-37.pyc differ diff --git a/__pycache__/sprites.cpython-37.pyc b/__pycache__/sprites.cpython-37.pyc index 02d7a2f..0a48609 100644 Binary files a/__pycache__/sprites.cpython-37.pyc and b/__pycache__/sprites.cpython-37.pyc differ diff --git a/config.py b/config.py index e093e84..9029dfe 100644 --- a/config.py +++ b/config.py @@ -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 diff --git a/data/graphics/hp.png b/data/graphics/hp.png new file mode 100644 index 0000000..e655c46 Binary files /dev/null and b/data/graphics/hp.png differ diff --git a/events.py b/events.py index a617eed..5308875 100644 --- a/events.py +++ b/events.py @@ -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]: diff --git a/maps.py b/maps.py index 4a919f0..4dc65bd 100644 --- a/maps.py +++ b/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') + diff --git a/mygame.py b/mygame.py index b918b04..2e3e9ae 100644 --- a/mygame.py +++ b/mygame.py @@ -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: diff --git a/sprites.py b/sprites.py index d1f1f9d..37cd184 100644 --- a/sprites.py +++ b/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: @@ -78,11 +81,26 @@ class Player(pygame.sprite.Sprite): if self.rect.colliderect(wall.rect): 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()