From fdca88ae20b94565432125bbcf3bf65acd4341cd Mon Sep 17 00:00:00 2001 From: Arthur 'Grizzly' Grisel-Davy Date: Sun, 3 May 2020 22:27:44 -0400 Subject: [PATCH] Clean code and fix projectiles collisions --- main.py | 3 ++- models.py | 31 +++++++++++++------------------ utils.py | 10 +++++++--- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index 1476e67..a41e2f5 100644 --- a/main.py +++ b/main.py @@ -22,10 +22,11 @@ WHITE=(255,255,255) BLUE=(0,0,255) RED=(255,0,0) -carte = Carte('map1') +carte = Carte(n=5) # possible positions: 300,300: 1400,1000: 2700,400, 2800,1600 # click = 323 start_pos = carte.player_start_pos +print(start_pos) player = Player('Alice',start_pos,center_screen,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,323,'perso.png','canon.png','projectile1_right.png') camera = Camera(start_pos,screen_width,screen_height,0.3) #enemies = [Enemy('Plop',(300,300),'perso.png')] diff --git a/models.py b/models.py index 7a34a9a..b2f36e3 100644 --- a/models.py +++ b/models.py @@ -69,27 +69,22 @@ class Hud(): surface.blit(text, (int(surface.get_width()-1.5*text.get_width()),int(surface.get_height()-1.5*text.get_height()))) - class Carte(): - def __init__(self,mapname): - self.posx = 0 - self.posy = 0 - self.back = pygame.image.load('maps/'+mapname+'_back.png').convert() - self.wall = pygame.image.load('maps/'+mapname+'_wall.png').convert_alpha() - self.mask = pygame.mask.from_surface(self.wall) - self.carte, self.player_start_pos = map_generator(5) + def __init__(self,n): + self.carte, self.player_start_pos, self.back = map_generator(n) + + for room in self.carte: + print(room[2]) def draw(self,surface,camera): offsetx,offsety = camera.get_offset() for room in self.carte: - print(room) + surface.blit(self.back,(room[2][0]-offsetx,room[2][1]-offsety)) surface.blit(room[0],(room[2][0]-offsetx,room[2][1]-offsety)) - #surface.blit(self.back,(self.posx-offsetx,self.posy-offsety)) - #surface.blit(self.wall,(self.posx-offsetx,self.posy-offsety)) - def collision(self,player): + def collision(self,thing): for room in self.carte: - if room[1].overlap(player.mask, (player.posx-player.player_rect.center[0]-room[2][0],player.posy-player.player_rect.center[1]-room[2][1])): + if room[1].overlap(thing.mask, (thing.posx-thing.rect.center[0]-room[2][0],thing.posy-thing.rect.center[1]-room[2][1])): return True return False @@ -130,7 +125,7 @@ class Player(): self.img_player = pygame.image.load(img_path+texture).convert_alpha() self.player = self.img_player - self.player_rect = self.player.get_rect() + self.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() @@ -254,11 +249,11 @@ class Player(): self.canon = pygame.transform.rotate(self.img_canon,self.degres_canon) # Get rects - self.player_rect = self.player.get_rect() + self.rect = self.player.get_rect() self.canon_rect = self.canon.get_rect() # blit player and canon - surface.blit(self.player,(posx_screen-self.player_rect.center[0],posy_screen-self.player_rect.center[1])) + surface.blit(self.player,(posx_screen-self.rect.center[0],posy_screen-self.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) @@ -292,7 +287,7 @@ class Enemy(): del game.enemies[index] -class Projectile(pygame.sprite.Sprite): +class Projectile(): def __init__(self,name,texture,position,speed,angle): self.name = name @@ -324,7 +319,7 @@ class Projectile(pygame.sprite.Sprite): impact = False # Touchs a wall - if carte.mask.overlap(self.mask, (self.posx-self.rect.center[0],self.posy-self.rect.center[1])): + if carte.collision(self): impact = True # Touchs an enemy diff --git a/utils.py b/utils.py index b69c2e4..c4e8779 100644 --- a/utils.py +++ b/utils.py @@ -32,7 +32,9 @@ def fetch_rooms(path): if 'L' in label: assembly['R'].append((label,filename)) - return assembly + background = glob(path+'*back*')[0] + + return assembly,background @@ -44,7 +46,7 @@ def map_generator(n): room_side = 1000 - assembly = fetch_rooms('./maps/rooms/') + assembly,back_path = fetch_rooms('./maps/rooms/') #assembly = {'T/R/D/L':[(label1,path1),(label2,path2),...]} start_key = random.choice(list(assembly.keys())) @@ -94,7 +96,9 @@ def map_generator(n): current_label = next_label position = next_position - return(carte,start_pos) + background = pygame.image.load(back_path).convert() + + return(carte,start_pos,background)