Clean code and fix projectiles collisions
This commit is contained in:
parent
c16f439569
commit
fdca88ae20
3 changed files with 22 additions and 22 deletions
3
main.py
3
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')]
|
||||
|
|
31
models.py
31
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
|
||||
|
|
10
utils.py
10
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)
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue