Projectile follow direction mouse

master
grisel-davy 4 years ago
parent dd103744d6
commit defd4946d9

@ -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">
<rect
style="opacity:1;fill:#ffffff;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:0.38599998;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect907"
width="114.71026"
height="84.800301"
x="-297.65625"
y="212.33334" />
<rect
style="opacity:1;fill:none;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:0.04628637;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
id="rect815"
@ -721,17 +728,18 @@
id="rect101"
width="2.6458335"
height="3.9687505"
x="-277.8125"
y="221.59375"
inkscape:export-xdpi="32.161598"
inkscape:export-ydpi="32.161598" />
x="221.85834"
y="274.90207"
inkscape:export-xdpi="133.33333"
inkscape:export-ydpi="133.33333"
transform="rotate(90)" />
<path
style="fill:#333333;stroke:none;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="M -277.28333,221.59375 V 220.8 c 0,0 0.51485,0.26458 0.79375,0.26458 0.27889,0 0.79375,-0.26458 0.79375,-0.26458 v 0.79375 h -1.5875"
d="m -274.90208,222.3875 h 0.79375 c 0,0 -0.26458,0.51485 -0.26458,0.79375 0,0.27889 0.26458,0.79375 0.26458,0.79375 h -0.79375 v -1.5875"
id="path103"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ccsccc"
inkscape:export-xdpi="32.161598"
inkscape:export-ydpi="32.161598" />
inkscape:export-xdpi="133.33333"
inkscape:export-ydpi="133.33333" />
</g>
</svg>

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

@ -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)

@ -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)

Loading…
Cancel
Save