# -*- coding: UTF-8 -*- ### Tangled Mind ### Author: Arthur 'Grizzly' Grisel-Davy 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', '_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 deceleration = 1.1 class Perso(): 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 self.speed = [0,0] self.direction = [0,-1] self.key_up = key_up self.key_down = key_down 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.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): # 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]: if keystate[self.key_left]: self.speed[0] = -1*cap_speed if keystate[self.key_right]: self.speed[0] = cap_speed if keystate[self.key_up]: self.speed[1] = -1*cap_speed if keystate[self.key_down]: self.speed[1] = cap_speed if not keystate[self.key_left] and not keystate[self.key_right]: self.direction[0] = 0 if not keystate[self.key_up] and not keystate[self.key_down]: self.direction[1] = 0 # Cap the speed if abs(self.speed[0])>cap_speed: self.speed[0] = self.speed[0]/abs(self.speed[0])*cap_speed if abs(self.speed[1])>cap_speed: self.speed[1] = self.speed[1]/abs(self.speed[1])*cap_speed # Begin the deceleration self.speed[0] = int(self.speed[0]/deceleration) self.speed[1] = int(self.speed[1]/deceleration) # Update the position self.posx = self.posx+self.speed[0] self.posy = self.posy+self.speed[1] # Select the direction based on the closest cardinals+bissectrices vectors if self.speed != [0,0]: direction=[] min_dist = 1000 for a in [-1,0,1]: for b in [-1,0,1]: dist = sqrt((self.speed[0]-a)**2+(self.speed[1]-b)**2) if dist < min_dist: min_dist = dist direction = [a,b] self.direction = direction if keystate[self.key_fire]: self.fire('fireball','asset/projectile') def draw(self,surface): # Make all projectiles move or disepear and draw them to_remove = [] for k,proj in enumerate(self.projectiles): if not proj.is_out(surface): proj.move() proj.draw(surface) else: to_remove.append(k) if to_remove != []: for k in to_remove[::-1]: del self.projectiles[k] # select img index = decision_matrix[self.direction[1]+1,self.direction[0]+1] self.img = self.imgs[index] #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): 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]]) self.projectiles.append(new_proj) self.last_fire = time() class Projectile(): def __init__(self,name,texture,speed,position,direction): 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() def move(self): self.position = (self.position[0]+self.speed*self.direction[0],self.position[1]+self.speed*self.direction[1]) def draw(self,surface): surface.blit(self.img,self.position) def is_out(self,surface): if (self.position[0]<0-self.img.get_width() or self.position[1]<0-self.img.get_height() or self.position[0]>surface.get_width()+self.img.get_width() or self.position[1]>surface.get_height()+self.img.get_height()): return True else: return False