|
|
|
@ -7,6 +7,7 @@ import pygame
|
|
|
|
|
from time import time
|
|
|
|
|
import numpy as np
|
|
|
|
|
from numpy import sqrt
|
|
|
|
|
from math import atan, degrees
|
|
|
|
|
|
|
|
|
|
decision_matrix = np.array([[0,1,2],[3,4,5],[6,7,8]])
|
|
|
|
|
suffix_matrix = ['_up_left.png',
|
|
|
|
@ -20,12 +21,12 @@ suffix_matrix = ['_up_left.png',
|
|
|
|
|
'_down_right.png',]
|
|
|
|
|
img_path = 'asset/'
|
|
|
|
|
|
|
|
|
|
cap_speed = 2
|
|
|
|
|
cap_speed = 10
|
|
|
|
|
deceleration = 1.1
|
|
|
|
|
|
|
|
|
|
class Perso():
|
|
|
|
|
|
|
|
|
|
def __init__(self,name,posx,posy,key_up,key_down,key_left,key_right,key_fire,texture,texture_proj):
|
|
|
|
|
def __init__(self,name,posx,posy,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon,texture_proj):
|
|
|
|
|
self.name = name
|
|
|
|
|
self.posx = posx
|
|
|
|
|
self.posy = posy
|
|
|
|
@ -36,23 +37,24 @@ 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'),
|
|
|
|
|
pygame.image.load(img_path+texture+'_up.png'),
|
|
|
|
|
pygame.image.load(img_path+texture+'_up_right.png'),
|
|
|
|
|
pygame.image.load(img_path+texture+'_left.png'),
|
|
|
|
|
pygame.image.load(img_path+texture+'_down.png'),
|
|
|
|
|
pygame.image.load(img_path+texture+'_right.png'),
|
|
|
|
|
pygame.image.load(img_path+texture+'_down_left.png'),
|
|
|
|
|
pygame.image.load(img_path+texture+'_down.png'),
|
|
|
|
|
pygame.image.load(img_path+texture+'_down_right.png'),]
|
|
|
|
|
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.canon = pygame.image.load(img_path+texture_canon).convert_alpha()
|
|
|
|
|
self.texture_proj = texture_proj
|
|
|
|
|
|
|
|
|
|
self.size = self.img.get_size()
|
|
|
|
|
self.projectiles = []
|
|
|
|
|
self.last_fire = time()
|
|
|
|
|
|
|
|
|
|
def check_keys(self,keystate,screen_width,screen_height,speed):
|
|
|
|
|
def check_keys(self,keystate,screen_width,screen_height):
|
|
|
|
|
|
|
|
|
|
# If an interresting key is pressed
|
|
|
|
|
if keystate[self.key_left] or keystate[self.key_right] or keystate[self.key_up] or keystate[self.key_down]:
|
|
|
|
@ -122,8 +124,25 @@ class Perso():
|
|
|
|
|
# select img
|
|
|
|
|
index = decision_matrix[self.direction[1]+1,self.direction[0]+1]
|
|
|
|
|
self.img = self.imgs[index]
|
|
|
|
|
surface.blit(self.img,(self.posx,self.posy))
|
|
|
|
|
#print("Projectiles for {}: {}".format(self.name,len(self.projectiles)))
|
|
|
|
|
|
|
|
|
|
#Calculate canon rotation:
|
|
|
|
|
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))))
|
|
|
|
|
if y_mouse > self.posy:
|
|
|
|
|
degres = -degres
|
|
|
|
|
if x_mouse < self.posx:
|
|
|
|
|
degres = 180-degres
|
|
|
|
|
|
|
|
|
|
canon = pygame.transform.rotate(self.canon,degres)
|
|
|
|
|
|
|
|
|
|
# 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]))
|
|
|
|
|
surface.blit(canon,(self.posx-canon_rect.center[0],self.posy-canon_rect.center[1]))
|
|
|
|
|
|
|
|
|
|
def fire(self,name,texture):
|
|
|
|
|
if (time()-self.last_fire> 0.2):
|
|
|
|
@ -142,7 +161,7 @@ class Projectile():
|
|
|
|
|
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])
|
|
|
|
|
self.img = pygame.image.load(img_path+texture+suffix_matrix[index]).convert_alpha()
|
|
|
|
|
|
|
|
|
|
def move(self):
|
|
|
|
|
self.position = (self.position[0]+self.speed*self.direction[0],self.position[1]+self.speed*self.direction[1])
|
|
|
|
|