2020-03-28 20:56:52 +01:00
|
|
|
# -*- coding: UTF-8 -*-
|
|
|
|
|
|
|
|
### Tangled Mind
|
|
|
|
### Author: Arthur 'Grizzly' Grisel-Davy
|
|
|
|
|
|
|
|
import pygame
|
2020-03-29 23:17:04 +02:00
|
|
|
from time import time
|
2020-04-03 04:53:25 +02:00
|
|
|
import numpy as np
|
2020-03-28 20:56:52 +01:00
|
|
|
|
2020-03-29 23:17:04 +02:00
|
|
|
class Perso():
|
2020-03-28 20:56:52 +01:00
|
|
|
|
2020-03-29 23:17:04 +02:00
|
|
|
def __init__(self,name,posx,posy,key_up,key_down,key_left,key_right,key_fire,texture):
|
2020-03-28 20:56:52 +01:00
|
|
|
self.name = name
|
|
|
|
self.posx = posx
|
|
|
|
self.posy = posy
|
2020-04-03 04:53:25 +02:00
|
|
|
self.direction = [0,-1]
|
2020-03-28 20:56:52 +01:00
|
|
|
self.key_up = key_up
|
|
|
|
self.key_down = key_down
|
|
|
|
self.key_left = key_left
|
|
|
|
self.key_right = key_right
|
2020-03-29 23:17:04 +02:00
|
|
|
self.key_fire = key_fire
|
2020-04-03 04:53:25 +02:00
|
|
|
self.imgs = [pygame.image.load(texture+'_up_left.png'),
|
|
|
|
pygame.image.load(texture+'_up.png'),
|
|
|
|
pygame.image.load(texture+'_up_right.png'),
|
|
|
|
pygame.image.load(texture+'_left.png'),
|
|
|
|
pygame.image.load(texture+'_up.png'),
|
|
|
|
pygame.image.load(texture+'_right.png'),
|
|
|
|
pygame.image.load(texture+'_down_left.png'),
|
|
|
|
pygame.image.load(texture+'_down.png'),
|
|
|
|
pygame.image.load(texture+'_down_right.png'),]
|
|
|
|
self.img_matrix = np.array([[0,1,2],[3,4,5],[6,7,8]])
|
|
|
|
self.img = self.imgs[0]
|
|
|
|
|
2020-03-29 04:38:20 +02:00
|
|
|
self.size = self.img.get_size()
|
2020-03-29 23:17:04 +02:00
|
|
|
self.projectiles = []
|
|
|
|
self.last_fire = time()
|
2020-03-28 20:56:52 +01:00
|
|
|
|
2020-03-29 23:17:04 +02:00
|
|
|
def check_keys(self,keystate,screen_width,screen_height,speed):
|
2020-04-03 04:53:25 +02:00
|
|
|
|
|
|
|
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.posx -= speed
|
|
|
|
if self.posx <0:
|
|
|
|
self.posx=0
|
|
|
|
self.direction[0] = -1
|
|
|
|
|
|
|
|
if keystate[self.key_right]:
|
|
|
|
self.posx += speed
|
|
|
|
if self.posx+self.size[0] > screen_width:
|
|
|
|
self.posx = screen_width-self.size[0]
|
|
|
|
self.direction[0] = 1
|
|
|
|
|
|
|
|
if keystate[self.key_up]:
|
|
|
|
self.posy -= speed
|
|
|
|
if self.posy < 0:
|
|
|
|
self.posy = 0
|
|
|
|
self.direction[1] = -1
|
|
|
|
|
|
|
|
if keystate[self.key_down]:
|
|
|
|
self.posy += speed
|
|
|
|
if self.posy+self.size[1] > screen_height:
|
|
|
|
self.posy = screen_height-self.size[1]
|
|
|
|
self.direction[1] = 1
|
|
|
|
|
|
|
|
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
|
2020-03-29 23:17:04 +02:00
|
|
|
|
|
|
|
if keystate[self.key_fire]:
|
2020-03-31 05:03:42 +02:00
|
|
|
self.fire('fireball','asset/projectile')
|
2020-03-29 23:17:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
def draw(self,surface):
|
2020-04-03 04:53:25 +02:00
|
|
|
# Make all projectiles move or disepear and draw them
|
2020-03-31 05:03:42 +02:00
|
|
|
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]
|
|
|
|
|
2020-04-03 04:53:25 +02:00
|
|
|
# select img
|
|
|
|
index = self.img_matrix[self.direction[1]+1,self.direction[0]+1]
|
|
|
|
self.img = self.imgs[index]
|
2020-03-29 23:17:04 +02:00
|
|
|
surface.blit(self.img,(self.posx,self.posy))
|
2020-04-03 04:53:25 +02:00
|
|
|
#print("Projectiles for {}: {}".format(self.name,len(self.projectiles)))
|
2020-03-29 23:17:04 +02:00
|
|
|
|
2020-03-31 05:03:42 +02:00
|
|
|
def fire(self,name,texture):
|
2020-03-29 23:17:04 +02:00
|
|
|
if (time()-self.last_fire> 0.2):
|
2020-03-31 05:03:42 +02:00
|
|
|
new_proj = Projectile(name,texture,2,(self.posx+int(self.img.get_height()/2),self.posy+int(self.img.get_width()/2)),self.direction)
|
2020-03-29 23:17:04 +02:00
|
|
|
self.projectiles.append(new_proj)
|
|
|
|
self.last_fire = time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Projectile():
|
|
|
|
|
2020-03-31 05:03:42 +02:00
|
|
|
def __init__(self,name,texture,speed,position,direction):
|
2020-03-29 23:17:04 +02:00
|
|
|
self.name = name
|
|
|
|
self.speed = speed
|
|
|
|
self.position = position
|
|
|
|
self.direction = direction
|
|
|
|
|
2020-03-31 05:03:42 +02:00
|
|
|
if direction[0]!=0:
|
|
|
|
if direction[0]>0:
|
|
|
|
self.img = pygame.image.load(texture+'_right.png')
|
|
|
|
else:
|
|
|
|
self.img = pygame.image.load(texture+'_left.png')
|
|
|
|
else:
|
|
|
|
if direction[1]>0:
|
|
|
|
self.img = pygame.image.load(texture+'_down.png')
|
|
|
|
else:
|
|
|
|
self.img = pygame.image.load(texture+'_up.png')
|
|
|
|
|
2020-03-29 23:17:04 +02:00
|
|
|
def move(self):
|
|
|
|
self.position = (self.position[0]+self.speed*self.direction[0],self.position[1]+self.speed*self.direction[1])
|
2020-03-28 20:56:52 +01:00
|
|
|
|
|
|
|
def draw(self,surface):
|
2020-03-31 05:03:42 +02:00
|
|
|
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
|