From a93f0dc9bd3763d772ace07df5f737453ee8870f Mon Sep 17 00:00:00 2001 From: Arthur 'Grizzly' Grisel-Davy Date: Sun, 12 Apr 2020 20:33:10 -0400 Subject: [PATCH] align all sprites --- main.py | 2 +- models.py | 59 ++++++++++++++++++++++--------------------------------- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/main.py b/main.py index 7b58bed..65b5893 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ WHITE=(255,255,255) BLUE=(0,0,255) RED=(255,0,0) -perso = Perso('Alice',200,200,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,pygame.K_SPACE,'perso1','canon.png','projectile1_right.png') +perso = Perso('Alice',200,200,pygame.K_z,pygame.K_s,pygame.K_q,pygame.K_d,pygame.K_SPACE,'perso1_right.png','canon.png','projectile1_right.png') clock = pygame.time.Clock() diff --git a/models.py b/models.py index fb1fcd7..09690af 100644 --- a/models.py +++ b/models.py @@ -9,16 +9,6 @@ import numpy as np from numpy import sqrt from math import atan, degrees,radians, cos, sin -decision_matrix = np.array([[0,1,2],[3,4,5],[6,7,8]]) -suffix_matrix = ['_up_left.png', - '_up.png', - '_up_right.png', - '_left.png', - '_up.png', - '_right.png', - '_down_left.png', - '_down.png', - '_down_right.png',] img_path = 'asset/' cap_speed = 10 @@ -37,18 +27,10 @@ class Perso(): self.key_left = key_left self.key_right = key_right self.key_fire = key_fire - self.imgs = [pygame.image.load(img_path+texture+'_up_left.png').convert_alpha(), - pygame.image.load(img_path+texture+'_up.png').convert_alpha(), - pygame.image.load(img_path+texture+'_up_right.png').convert_alpha(), - pygame.image.load(img_path+texture+'_left.png').convert_alpha(), - pygame.image.load(img_path+texture+'_down.png').convert_alpha(), - pygame.image.load(img_path+texture+'_right.png').convert_alpha(), - pygame.image.load(img_path+texture+'_down_left.png').convert_alpha(), - pygame.image.load(img_path+texture+'_down.png').convert_alpha(), - pygame.image.load(img_path+texture+'_down_right.png').convert_alpha(),] - self.img = self.imgs[0] + self.img = pygame.image.load(img_path+texture).convert_alpha() self.canon = pygame.image.load(img_path+texture_canon).convert_alpha() - self.degres = 0 + self.degres_perso = 0 + self.degres_canon = 0 self.texture_proj = texture_proj self.size = self.img.get_size() @@ -123,31 +105,38 @@ class Perso(): del self.projectiles[k] # select img - index = decision_matrix[self.direction[1]+1,self.direction[0]+1] - self.img = self.imgs[index] + + #Calculate player rotation: + if self.direction[0]!=0: + self.degres_perso = -1*degrees(atan(self.direction[1]/self.direction[0])) + if self.direction[0] < 0: + self.degres_perso = 180+self.degres_perso + else: + self.degres_perso = ((self.direction[1]>0)*2-1)*-90 + perso = pygame.transform.rotate(self.img,self.degres_perso) #Calculate canon rotation: x_mouse, y_mouse = pygame.mouse.get_pos() if x_mouse==self.posx: x_mouse+=0.1 - self.degres = degrees(abs(atan((y_mouse-self.posy)/(x_mouse-self.posx)))) - if y_mouse > self.posy: - self.degres = -self.degres + self.degres_canon = -1*degrees(atan((y_mouse-self.posy)/(x_mouse-self.posx))) if x_mouse < self.posx: - self.degres = 180-self.degres - - canon = pygame.transform.rotate(self.canon,self.degres) + self.degres_canon = 180+self.degres_canon + canon = pygame.transform.rotate(self.canon,self.degres_canon) # Get rects perso_rect = self.img.get_rect() canon_rect = canon.get_rect() - surface.blit(self.img,(self.posx-perso_rect.center[0],self.posy-perso_rect.center[1])) + # Blits + surface.blit(perso,(self.posx-perso_rect.center[0],self.posy-perso_rect.center[1])) surface.blit(canon,(self.posx-canon_rect.center[0],self.posy-canon_rect.center[1])) + #pygame.draw.circle(surface, (200,0,0), (self.posx,self.posy), 10) + def fire(self,name,texture): if (time()-self.last_fire> 0.2): - new_proj = Projectile(name,self.texture_proj,10,(self.posx+int(self.img.get_height()/2),self.posy+int(self.img.get_width()/2)),self.degres) + new_proj = Projectile(name,self.texture_proj,(self.posx,self.posy),20,self.degres_canon) self.projectiles.append(new_proj) self.last_fire = time() @@ -155,18 +144,18 @@ class Perso(): class Projectile(): - def __init__(self,name,texture,speed,position,angle): + def __init__(self,name,texture,position,speed,angle): self.name = name self.speed = speed self.position = position 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 = (int(self.position[0]+self.speed*self.direction[0]),int(self.position[1]+self.speed*self.direction[1])) - + 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) + surface.blit(self.img,(self.position[0]-self.rect[0],self.position[1]-self.rect[1])) def is_out(self,surface): if (self.position[0]<0-self.img.get_width() or