From defd4946d91e9a6a7dc4814cfe91a1f879f5a81c Mon Sep 17 00:00:00 2001 From: Arthur 'Grizzly' Grisel-Davy Date: Sun, 12 Apr 2020 00:08:24 -0400 Subject: [PATCH] Projectile follow direction mouse --- asset/asset.svg | 28 ++++++++++++++++++---------- main.py | 4 ++-- models.py | 23 +++++++++++------------ 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/asset/asset.svg b/asset/asset.svg index 0f8ca51..86b73c4 100644 --- a/asset/asset.svg +++ b/asset/asset.svg @@ -25,9 +25,9 @@ borderopacity="1.0" inkscape:pageopacity="0.0" inkscape:pageshadow="2" - inkscape:zoom="3.959798" - inkscape:cx="-1038.4591" - inkscape:cy="251.12477" + inkscape:zoom="5.6" + inkscape:cx="-978.35637" + inkscape:cy="195.0211" inkscape:document-units="px" inkscape:current-layer="layer1" showgrid="true" @@ -57,6 +57,13 @@ inkscape:label="Layer 1" inkscape:groupmode="layer" id="layer1"> + + x="221.85834" + y="274.90207" + inkscape:export-xdpi="133.33333" + inkscape:export-ydpi="133.33333" + transform="rotate(90)" /> + inkscape:export-xdpi="133.33333" + inkscape:export-ydpi="133.33333" /> diff --git a/main.py b/main.py index d9af084..7b58bed 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') +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') clock = pygame.time.Clock() @@ -41,5 +41,5 @@ while not done: pygame.display.flip() - clock.tick(30) + clock.tick(60) \ No newline at end of file diff --git a/models.py b/models.py index f4dd650..fb1fcd7 100644 --- a/models.py +++ b/models.py @@ -7,7 +7,7 @@ import pygame from time import time import numpy as np from numpy import sqrt -from math import atan, degrees +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', @@ -48,6 +48,7 @@ class Perso(): pygame.image.load(img_path+texture+'_down_right.png').convert_alpha(),] self.img = self.imgs[0] self.canon = pygame.image.load(img_path+texture_canon).convert_alpha() + self.degres = 0 self.texture_proj = texture_proj self.size = self.img.get_size() @@ -129,13 +130,13 @@ class Perso(): x_mouse, y_mouse = pygame.mouse.get_pos() if x_mouse==self.posx: x_mouse+=0.1 - degres = degrees(abs(atan((y_mouse-self.posy)/(x_mouse-self.posx)))) + self.degres = degrees(abs(atan((y_mouse-self.posy)/(x_mouse-self.posx)))) if y_mouse > self.posy: - degres = -degres + self.degres = -self.degres if x_mouse < self.posx: - degres = 180-degres + self.degres = 180-self.degres - canon = pygame.transform.rotate(self.canon,degres) + canon = pygame.transform.rotate(self.canon,self.degres) # Get rects perso_rect = self.img.get_rect() @@ -146,7 +147,7 @@ class Perso(): def fire(self,name,texture): if (time()-self.last_fire> 0.2): - new_proj = Projectile(name,self.texture_proj,3,(self.posx+int(self.img.get_height()/2),self.posy+int(self.img.get_width()/2)),[self.direction[0],self.direction[1]]) + 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) self.projectiles.append(new_proj) self.last_fire = time() @@ -154,17 +155,15 @@ class Perso(): class Projectile(): - def __init__(self,name,texture,speed,position,direction): + def __init__(self,name,texture,speed,position,angle): self.name = name self.speed = speed self.position = position - self.direction = direction - - index = decision_matrix[self.direction[1]+1,self.direction[0]+1] - self.img = pygame.image.load(img_path+texture+suffix_matrix[index]).convert_alpha() + self.direction = direction = [cos(radians(angle)),-sin(radians(angle))] + self.img = pygame.transform.rotate(pygame.image.load(img_path+texture).convert_alpha(),angle) def move(self): - self.position = (self.position[0]+self.speed*self.direction[0],self.position[1]+self.speed*self.direction[1]) + self.position = (int(self.position[0]+self.speed*self.direction[0]),int(self.position[1]+self.speed*self.direction[1])) def draw(self,surface): surface.blit(self.img,self.position)