Correct issue #1 about projectiles

This commit is contained in:
grisel-davy 2020-04-18 23:03:48 -04:00
parent 7d5fbe0cde
commit 0ae1d56bae

View file

@ -20,7 +20,21 @@ class Game():
self.perso = perso
def draw(self,surface):
self.carte.draw(surface,self.perso)
# draw the map
self.carte.draw(surface,self.perso.get_draw_offset())
# draw the projectiles and remove them if needed
to_remove = []
for k,proj in enumerate(self.perso.projectiles):
if not proj.is_out(surface):
proj.move()
proj.draw(surface, self.perso.get_draw_offset())
else:
to_remove.append(k)
if to_remove != []:
for k in to_remove[::-1]:
del self.perso.projectiles[k]
self.perso.draw(surface)
@ -30,8 +44,8 @@ class Carte():
self.rect = self.img.get_rect()
self.mask = pygame.mask.from_surface(self.img)
def draw(self,surface,perso):
surface.blit(self.img,(perso.posx_screen-perso.posx,perso.posy_screen-perso.posy))
def draw(self,surface,offset):
surface.blit(self.img,offset)
class Perso():
@ -53,7 +67,6 @@ class Perso():
self.img = pygame.image.load(img_path+texture).convert_alpha()
self.perso_rect = self.img.get_rect()
self.mask = pygame.mask.from_surface(self.img)
print(self.mask)
self.canon = pygame.image.load(img_path+texture_canon).convert_alpha()
self.canon_rect = self.canon.get_rect()
@ -92,12 +105,14 @@ class Perso():
if not (keystate[self.key_up] or keystate[self.key_down]):
self.speed[1] = int(self.speed[1]/deceleration)
# Update the position
# Update the position on x axis
self.posx = self.posx+self.speed[0]
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])):#,pygame.sprite.collide_mask):
if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])):
self.posx = self.posx-self.speed[0]
# Update the position on y axis
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])):
self.posy = self.posy-self.speed[1]
@ -106,18 +121,6 @@ class Perso():
def draw(self,surface):
# Make all projectiles move or disepear and draw them
to_remove = []
for k,proj in enumerate(self.projectiles):
if not proj.is_out(surface):
proj.move()
proj.draw(surface)
else:
to_remove.append(k)
if to_remove != []:
for k in to_remove[::-1]:
del self.projectiles[k]
#Calculate player rotation:
if self.speed[0]!=0:
self.degres_perso = -1*degrees(atan(self.speed[1]/self.speed[0]))
@ -142,35 +145,42 @@ class Perso():
# Blits
surface.blit(perso,(self.posx_screen-self.perso_rect.center[0],self.posy_screen-self.perso_rect.center[1]))
hitbox = pygame.Rect(self.posx_screen-self.perso_rect.center[0],self.posy_screen-self.perso_rect.center[1],self.perso_rect[2],self.perso_rect[3])
#print(hitbox)
surface.blit(canon,(self.posx_screen-self.canon_rect.center[0],self.posy_screen-self.canon_rect.center[1]))
#pygame.draw.rect(surface, (255,0,0), hitbox, 2)
def fire(self,name,texture):
if (time()-self.last_fire> 0.2):
new_proj = Projectile(name,self.texture_proj,(self.posx_screen,self.posy_screen),20,self.degres_canon)
new_proj = Projectile(name,self.texture_proj,(self.posx,self.posy),20,self.degres_canon)
self.projectiles.append(new_proj)
self.last_fire = time()
def get_draw_offset(self):
return((self.posx_screen-self.posx,self.posy_screen-self.posy))
class Projectile():
def __init__(self,name,texture,position,speed,angle):
self.name = name
self.speed = speed
self.position = position
self.pos_init = position
self.deplacement = [0,0]
self.direction = direction = [cos(radians(angle)),-sin(radians(angle))]
self.img = pygame.transform.rotate(pygame.image.load(img_path+texture).convert_alpha(),angle)
self.rect = self.img.get_rect()
def move(self):
self.position = (round(self.position[0]+self.speed*self.direction[0]),round(self.position[1]+self.speed*self.direction[1]))
def draw(self,surface):
surface.blit(self.img,(self.position[0]-self.rect[0],self.position[1]-self.rect[1]))
self.deplacement = (round(self.deplacement[0]+self.speed*self.direction[0]),round(self.deplacement[1]+self.speed*self.direction[1]))
def draw(self,surface,offset):
surface.blit(self.img,(offset[0]+self.pos_init[0]+self.deplacement[0]-self.rect[0],offset[1]+self.pos_init[1]+self.deplacement[1]-self.rect[1]))
def is_out(self,surface):
if (self.position[0]<0-self.img.get_width() or
self.position[1]<0-self.img.get_height() or
self.position[0]>surface.get_width()+self.img.get_width() or
self.position[1]>surface.get_height()+self.img.get_height()):
if (self.deplacement[0]+surface.get_width()/2<0-self.img.get_width() or
self.deplacement[1]+surface.get_height()/2<0-self.img.get_height() or
self.deplacement[0]+surface.get_width()/2>surface.get_width()+self.img.get_width() or
self.deplacement[1]+surface.get_height()/2>surface.get_height()+self.img.get_height()):
return True
else:
return False