From 244359fb9c945e7d330f475bb5f7c6855a6ab8a7 Mon Sep 17 00:00:00 2001 From: Arthur 'Grizzly' Grisel-Davy Date: Sun, 19 Apr 2020 23:37:28 -0400 Subject: [PATCH] player rotation remain when stoped --- models.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/models.py b/models.py index 7c2cb92..43254ca 100644 --- a/models.py +++ b/models.py @@ -64,17 +64,18 @@ class Perso(): self.key_right = key_right self.key_fire = key_fire - 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) + 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.canon = pygame.image.load(img_path+texture_canon).convert_alpha() + 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_canon = 0 self.texture_proj = texture_proj - self.size = self.img.get_size() self.projectiles = [] self.last_fire = time() @@ -122,13 +123,14 @@ class Perso(): def draw(self,surface): #Calculate player rotation: - if self.speed[0]!=0: - self.degres_perso = -1*degrees(atan(self.speed[1]/self.speed[0])) - if self.speed[0] < 0: - self.degres_perso = 180+self.degres_perso - else: - self.degres_perso = ((self.speed[1]>0)*2-1)*-90 - perso = pygame.transform.rotate(self.img,self.degres_perso) + if self.speed != [0,0]: + if self.speed[0]!=0: + self.degres_perso = -1*degrees(atan(self.speed[1]/self.speed[0])) + if self.speed[0] < 0: + self.degres_perso = 180+self.degres_perso + else: + self.degres_perso = ((self.speed[1]>0)*2-1)*-90 + self.perso = pygame.transform.rotate(self.img_perso,self.degres_perso) #Calculate canon rotation: x_mouse, y_mouse = pygame.mouse.get_pos() @@ -137,17 +139,17 @@ class Perso(): self.degres_canon = -1*degrees(atan((y_mouse-self.posy_screen)/(x_mouse-self.posx_screen))) if x_mouse < self.posx_screen: self.degres_canon = 180+self.degres_canon - canon = pygame.transform.rotate(self.canon,self.degres_canon) + self.canon = pygame.transform.rotate(self.img_canon,self.degres_canon) # Get rects - self.perso_rect = perso.get_rect() - self.canon_rect = canon.get_rect() + self.perso_rect = self.perso.get_rect() + self.canon_rect = self.canon.get_rect() # 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]) + surface.blit(self.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])) + surface.blit(self.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): @@ -160,7 +162,7 @@ class Perso(): return((self.posx_screen-self.posx,self.posy_screen-self.posy)) -class Projectile(): +class Projectile(pygame.sprite.Sprite): def __init__(self,name,texture,position,speed,angle): self.name = name @@ -183,4 +185,4 @@ class Projectile(): self.deplacement[1]+surface.get_height()/2>surface.get_height()+self.img.get_height()): return True else: - return False + return False \ No newline at end of file