From 65073bc55c3e7e86b6c16f2073fe83e3ad59705a Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Wed, 30 Jan 2019 20:21:08 +0100 Subject: [PATCH] Added bullet collision and HP system --- __pycache__/config.cpython-37.pyc | Bin 573 -> 608 bytes __pycache__/events.cpython-37.pyc | Bin 2973 -> 2946 bytes __pycache__/maps.cpython-37.pyc | Bin 0 -> 1236 bytes __pycache__/sprites.cpython-37.pyc | Bin 4367 -> 4629 bytes config.py | 2 ++ data/graphics/hp.png | Bin 0 -> 158 bytes events.py | 6 +++--- maps.py | 25 ++++++++++++++++++++--- mygame.py | 3 ++- sprites.py | 31 ++++++++++++++++++++++++----- 10 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 __pycache__/maps.cpython-37.pyc create mode 100644 data/graphics/hp.png diff --git a/__pycache__/config.cpython-37.pyc b/__pycache__/config.cpython-37.pyc index d86aabaf143da4b3ed017ac72020292dc7c140fb..f988de2ae145f5bb074097856151a76ecb07599e 100644 GIT binary patch delta 259 zcmdnX@_>cciImL6h?($PIp)jJKE=wI(i67DxedUy6W;6F@>!ZsH{uF3tcS z$4J+pc#nX|qKw-_qqv+xeSBO);$3{*S27gw0@W4qPQJqEtfdBIaxii*voL}o6AKF) LBNQ?tSRgq7u5+CP4UqZo>3e;Kffn2-?+CXmuQ_FJK0EY&#Mx*liTnR f^Eo)tF4V&=JiK_&?P&DbcMN~q_4wD$KN)@lhY2Z% diff --git a/__pycache__/events.cpython-37.pyc b/__pycache__/events.cpython-37.pyc index d6af2577f1eca5a4dfbbf1e4ba17e8f2bc9b26e5..0cef001791106b6130597b0ebd1eecea5c27e304 100644 GIT binary patch delta 144 zcmbO$-XzZJ#LLUY00c2_17kEc@*ZH0^#Jlp7_t}_FqJTuu%s}iFfC-Mg^941u%$48 zMHp)sicCt_QhOk%h5HX|p4{0psRhtR1WXYs)a( diff --git a/__pycache__/maps.cpython-37.pyc b/__pycache__/maps.cpython-37.pyc new file mode 100644 index 0000000000000000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Aqw)S@^{Uf-<{Xf+AHhNB zMmUe~3V=r?I=4kRdW*{4@`(C~_o$!jRoeHz?&zsw9J;GdoG2X!&o=eQ3BWVO&%W@* z&v`apT=G|fNpOGPeF;Njj?K{fJVxJEfAN0xhvn9vf(oinp4of`1u=yE~RC@skwP}>L z9y=+Zao)J0{)}JeuO0bzonA&aL%Z#`bETzwz8u=x6ib1h@nUMt9q92uDF+dT5QY&h zAz&PNS^emq>O2c%H3h&ak8$VVHhEh8?dItmuHLo(u3mH9A~jsAXh4dw-S{312P_({ zn0b7~yoCBLF+Lcwps?YG*3D8SzaCn7e0Y|`ua=mC-ib!}#*~}vNUvaCae$+xv(-rQ zs~fr-&1QIx90Qv$KJnh{?9@VTer{@NQtE2IpcjXAcdY7Kr4p3Yh18WgmhdK=>Lfso z?2_84fxz6Mb@Ppq0Q zGQd4JRU3d%PKW7^JF!(N`9X6MZ23CqV-gr;Dh5_Bio$bKl7E0haCE|>AxbESd}IoUl0 zPM@MX4hp9dc`qH>`AT`Eu&TCOyx1EclK92)P4%c{uw7@?fqXjxR##3zunzUCCE0ZX VSZ=1EU!38-MOl<_BR!Gs{1-_xh9Up} 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 0000000000000000000000000000000000000000..e655c46b76eb15c338ac9426bc33a22a900e36f2 GIT binary patch literal 158 zcmeAS@N?(olHy`uVBq!ia0vp^Qb5ek!2~2{CY-thq&N#aB8wRq^pruEv0|xx8AwHm zr>`sfV@5e)4zbVYUpxQ`NtU=qlsM<-=BDPAFgO>bCYGe8D3oWGWGJ|M`UZqI@`(c# yNqD+AhG?8mwt4aHoP+IwBMzYkS$7r~@-aMm$ryWkWoIZ*4TGnvpUXO@geCx^ku2{3 literal 0 HcmV?d00001 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()