From f0eec65dad83aff266ab3ccd2cfe3b4d1b35a4c5 Mon Sep 17 00:00:00 2001 From: Arthur 'Grizzly' Grisel-Davy Date: Fri, 1 May 2020 23:34:46 -0400 Subject: [PATCH] Add basic lifebar --- main.py | 9 ++++---- models.py | 61 ++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/main.py b/main.py index 1c1d0ac..919c223 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ import pygame -from models import Game, Perso, Carte, Camera, Enemy +from models import Game, Player, Carte, Camera, Enemy, Hud pygame.init() @@ -25,10 +25,11 @@ RED=(255,0,0) carte = Carte('map1') # possible positions: 300,300: 1400,1000: 2700,400, 2800,1600 # click = 323 -perso = Perso('Alice',660,1570,center_screen,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,323,'perso.png','canon.png','projectile1_right.png') +player = Player('Alice',660,1570,center_screen,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,323,'perso.png','canon.png','projectile1_right.png') camera = Camera(660,1570,screen_width,screen_height,0.3) enemies = [Enemy('Plop',(300,300),'perso.png')] -game = Game(carte,perso,camera,enemies) +hud = Hud(player) +game = Game(carte,player,camera,enemies,hud) clock = pygame.time.Clock() @@ -40,7 +41,7 @@ while not done: keystate = pygame.key.get_pressed() + pygame.mouse.get_pressed() - perso.check_keys(keystate,screen_width,screen_height,carte,camera) + player.check_keys(keystate,screen_width,screen_height,carte,camera) screen.fill(BACKGROUND) diff --git a/models.py b/models.py index 2afc7a0..04478e8 100644 --- a/models.py +++ b/models.py @@ -18,11 +18,12 @@ deceleration = 1.1 class Game(): - def __init__(self,carte,perso,camera,enemies): + def __init__(self,carte,player,camera,enemies,hud): self.carte = carte - self.perso = perso + self.player = player self.camera = camera self.enemies = enemies + self.hud = hud def draw(self,surface): # draw the map @@ -30,7 +31,7 @@ class Game(): # draw the projectiles and remove them if needed to_remove = [] - for k,proj in enumerate(self.perso.projectiles): + for k,proj in enumerate(self.player.projectiles): if not proj.is_out(self.carte,self): proj.move() proj.draw(surface,self.camera) @@ -38,18 +39,28 @@ class Game(): to_remove.append(k) if to_remove != []: for k in to_remove[::-1]: - del self.perso.projectiles[k] + del self.player.projectiles[k] for enemy in self.enemies: enemy.draw(surface,self.camera) - self.perso.draw(surface,self.camera) + self.player.draw(surface,self.camera) self.camera.draw(surface) + self.hud.draw(surface) def get_offset(self): #Return the position of the elements on the screen (camera) - return (self.perso.posx-self.camera.posx,self.perso.posy-self.camera.posy) + return (self.player.posx-self.camera.posx,self.player.posy-self.camera.posy) +class Hud(): + def __init__(self,player): + self.player = player + self.ratio = 1 + + def draw(self,surface): + # draw the life bar + pygame.draw.rect(surface,(255,0,0),(10,10,round(self.player.life/100*surface.get_width()/3),30),0) + pygame.draw.rect(surface,(255,255,255),(10,10,int(surface.get_width()/3),30),3) class Carte(): @@ -87,7 +98,7 @@ class Camera(): return (int(self.posx-self.screen_width/2),int(self.posy-self.screen_height/2)) -class Perso(): +class Player(): def __init__(self,name,posx,posy,center_screen,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon,texture_proj): self.name = name self.posx = posx @@ -100,21 +111,26 @@ class Perso(): self.key_right = key_right self.key_fire = key_fire - self.img_perso = pygame.image.load(img_path+texture).convert_alpha() - self.perso = self.img_perso - self.perso_rect = self.perso.get_rect() - self.mask = pygame.mask.from_surface(self.img_perso) + self.img_player = pygame.image.load(img_path+texture).convert_alpha() + self.player = self.img_player + self.player_rect = self.player.get_rect() + self.mask = pygame.mask.from_surface(self.img_player) self.img_canon = pygame.image.load(img_path+texture_canon).convert_alpha() self.canon = self.img_canon self.canon_rect = self.canon.get_rect() - self.degres_perso = 0 + self.degres_player = 0 self.degres_canon = 0 self.texture_proj = texture_proj self.projectiles = [] self.last_fire = time() + # RP attributes: + self.life = 100 + self.ammo = 50 + + def check_keys(self,keystate,screen_width,screen_height,carte,camera): # If an interresting key is pressed @@ -147,13 +163,13 @@ class Perso(): posx_screen = self.posx-offsetx posy_screen = self.posy-offsety - # perso in the box: + # player in the box: if camera.box.collidepoint(posx_screen,posy_screen): # X AXIS temp_pos = self.posx self.posx = self.posx+self.speed[0] - if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])): + if carte.mask.overlap(self.mask, (self.posx-self.player_rect.center[0],self.posy-self.player_rect.center[1])): self.posx = temp_pos self.speed[0] = 0 @@ -166,7 +182,7 @@ class Perso(): # Y AXIS temp_pos = self.posy self.posy = self.posy+self.speed[1] - if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])): + if carte.mask.overlap(self.mask, (self.posx-self.player_rect.center[0],self.posy-self.player_rect.center[1])): self.posy = temp_pos self.speed[1] = 0 @@ -177,25 +193,25 @@ class Perso(): camera.posy = camera.posy + self.speed[1] - # perso not in the box + # player not in the box else: # X AXIS temp_pos = self.posx self.posx = self.posx+self.speed[0] - if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])): + if carte.mask.overlap(self.mask, (self.posx-self.player_rect.center[0],self.posy-self.player_rect.center[1])): self.posx = temp_pos self.speed[0] = 0 - # If the perso really move, camera follow + # If the player really move, camera follow else: camera.posx = camera.posx + self.posx-temp_pos # Y AXIS temp_pos = self.posy self.posy = self.posy+self.speed[1] - if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])): + if carte.mask.overlap(self.mask, (self.posx-self.player_rect.center[0],self.posy-self.player_rect.center[1])): self.posy = temp_pos self.speed[1] = 0 - # If the perso really move, camera follow + # If the player really move, camera follow else: camera.posy = camera.posy + self.posy-temp_pos @@ -221,11 +237,11 @@ class Perso(): self.canon = pygame.transform.rotate(self.img_canon,self.degres_canon) # Get rects - self.perso_rect = self.perso.get_rect() + self.player_rect = self.player.get_rect() self.canon_rect = self.canon.get_rect() # blit player and canon - surface.blit(self.perso,(posx_screen-self.perso_rect.center[0],posy_screen-self.perso_rect.center[1])) + surface.blit(self.player,(posx_screen-self.player_rect.center[0],posy_screen-self.player_rect.center[1])) surface.blit(self.canon,(posx_screen-self.canon_rect.center[0],posy_screen-self.canon_rect.center[1])) #pygame.draw.rect(surface, (255,0,0), (int(move_box_ratio*scwidth),int(move_box_ratio*scheight),int(scwidth*(1-2*move_box_ratio)),int(scheight*(1-2*move_box_ratio))), 2) @@ -258,7 +274,6 @@ class Enemy(): del game.enemies[index] - class Projectile(pygame.sprite.Sprite): def __init__(self,name,texture,position,speed,angle):