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-12 06:08:24 +02:00
|
|
|
from math import atan, degrees,radians, cos, sin
|
2020-03-28 20:56:52 +01:00
|
|
|
|
2020-04-04 06:11:34 +02:00
|
|
|
img_path = 'asset/'
|
|
|
|
|
2020-04-11 06:17:49 +02:00
|
|
|
cap_speed = 10
|
2020-04-18 19:34:24 +02:00
|
|
|
acceleration = 2
|
2020-04-06 00:29:38 +02:00
|
|
|
deceleration = 1.1
|
2020-04-04 06:11:34 +02:00
|
|
|
|
2020-04-15 01:07:57 +02:00
|
|
|
|
|
|
|
class Game():
|
|
|
|
def __init__(self,carte,perso):
|
|
|
|
self.carte = carte
|
|
|
|
self.perso = perso
|
|
|
|
|
|
|
|
def draw(self,surface):
|
|
|
|
self.carte.draw(surface,self.perso)
|
|
|
|
self.perso.draw(surface)
|
|
|
|
|
|
|
|
|
2020-04-14 02:43:09 +02:00
|
|
|
class Carte():
|
|
|
|
def __init__(self,texture):
|
|
|
|
self.img = pygame.image.load('maps/'+texture).convert_alpha()
|
2020-04-15 01:07:57 +02:00
|
|
|
self.rect = self.img.get_rect()
|
|
|
|
self.mask = pygame.mask.from_surface(self.img)
|
2020-04-14 02:43:09 +02:00
|
|
|
|
2020-04-15 01:07:57 +02:00
|
|
|
def draw(self,surface,perso):
|
|
|
|
surface.blit(self.img,(perso.posx_screen-perso.posx,perso.posy_screen-perso.posy))
|
2020-04-14 02:43:09 +02:00
|
|
|
|
|
|
|
|
2020-03-29 23:17:04 +02:00
|
|
|
class Perso():
|
2020-03-28 20:56:52 +01:00
|
|
|
|
2020-04-15 01:07:57 +02:00
|
|
|
def __init__(self,name,posx,posy,center_screen,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon,texture_proj):
|
2020-03-28 20:56:52 +01:00
|
|
|
self.name = name
|
|
|
|
self.posx = posx
|
|
|
|
self.posy = posy
|
2020-04-15 01:07:57 +02:00
|
|
|
self.posx_screen = center_screen[0]
|
|
|
|
self.posy_screen = center_screen[1]
|
2020-04-06 00:29:38 +02:00
|
|
|
self.speed = [0,0]
|
2020-04-15 01:07:57 +02:00
|
|
|
|
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-15 01:07:57 +02:00
|
|
|
|
2020-04-13 02:33:10 +02:00
|
|
|
self.img = pygame.image.load(img_path+texture).convert_alpha()
|
2020-04-15 01:07:57 +02:00
|
|
|
self.perso_rect = self.img.get_rect()
|
|
|
|
self.mask = pygame.mask.from_surface(self.img)
|
|
|
|
print(self.mask)
|
|
|
|
|
2020-04-11 06:17:49 +02:00
|
|
|
self.canon = pygame.image.load(img_path+texture_canon).convert_alpha()
|
2020-04-15 01:07:57 +02:00
|
|
|
self.canon_rect = self.canon.get_rect()
|
2020-04-13 02:33:10 +02:00
|
|
|
self.degres_perso = 0
|
|
|
|
self.degres_canon = 0
|
2020-04-04 06:11:34 +02:00
|
|
|
self.texture_proj = texture_proj
|
2020-04-03 04:53:25 +02:00
|
|
|
|
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-04-15 01:07:57 +02:00
|
|
|
def check_keys(self,keystate,screen_width,screen_height,carte):
|
2020-04-03 04:53:25 +02:00
|
|
|
|
2020-04-06 00:29:38 +02:00
|
|
|
# If an interresting key is pressed
|
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]:
|
2020-04-06 00:29:38 +02:00
|
|
|
|
2020-04-03 04:53:25 +02:00
|
|
|
if keystate[self.key_left]:
|
2020-04-18 19:34:24 +02:00
|
|
|
self.speed[0] -= acceleration
|
|
|
|
if self.speed[0] < -cap_speed: self.speed[0] = -cap_speed
|
2020-04-03 04:53:25 +02:00
|
|
|
|
|
|
|
if keystate[self.key_right]:
|
2020-04-18 19:34:24 +02:00
|
|
|
self.speed[0] += acceleration
|
|
|
|
if self.speed[0] > cap_speed: self.speed[0] = cap_speed
|
2020-04-03 04:53:25 +02:00
|
|
|
|
|
|
|
if keystate[self.key_up]:
|
2020-04-18 19:34:24 +02:00
|
|
|
self.speed[1] -= acceleration
|
|
|
|
if self.speed[1] < -cap_speed: self.speed[1] = -cap_speed
|
2020-04-03 04:53:25 +02:00
|
|
|
|
|
|
|
if keystate[self.key_down]:
|
2020-04-18 19:34:24 +02:00
|
|
|
self.speed[1] += acceleration
|
|
|
|
if self.speed[1] > cap_speed: self.speed[1] = cap_speed
|
2020-04-03 04:53:25 +02:00
|
|
|
|
2020-04-06 00:29:38 +02:00
|
|
|
# Begin the deceleration
|
2020-04-18 19:34:24 +02:00
|
|
|
if not (keystate[self.key_left] or keystate[self.key_right]):
|
|
|
|
self.speed[0] = int(self.speed[0]/deceleration)
|
|
|
|
if not (keystate[self.key_up] or keystate[self.key_down]):
|
|
|
|
self.speed[1] = int(self.speed[1]/deceleration)
|
2020-04-06 00:29:38 +02:00
|
|
|
|
|
|
|
# Update the position
|
|
|
|
self.posx = self.posx+self.speed[0]
|
|
|
|
self.posy = self.posy+self.speed[1]
|
|
|
|
|
2020-04-15 01:07:57 +02:00
|
|
|
if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])):#,pygame.sprite.collide_mask):
|
|
|
|
self.posx = self.posx-self.speed[0]
|
|
|
|
self.posy = self.posy-self.speed[1]
|
2020-04-06 00:29:38 +02:00
|
|
|
|
|
|
|
|
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-13 02:33:10 +02:00
|
|
|
#Calculate player rotation:
|
2020-04-15 01:07:57 +02:00
|
|
|
if self.speed[0]!=0:
|
|
|
|
self.degres_perso = -1*degrees(atan(self.speed[1]/self.speed[0]))
|
|
|
|
if self.speed[0] < 0:
|
2020-04-13 02:33:10 +02:00
|
|
|
self.degres_perso = 180+self.degres_perso
|
|
|
|
else:
|
2020-04-15 01:07:57 +02:00
|
|
|
self.degres_perso = ((self.speed[1]>0)*2-1)*-90
|
2020-04-13 02:33:10 +02:00
|
|
|
perso = pygame.transform.rotate(self.img,self.degres_perso)
|
2020-04-11 06:17:49 +02:00
|
|
|
|
|
|
|
#Calculate canon rotation:
|
|
|
|
x_mouse, y_mouse = pygame.mouse.get_pos()
|
2020-04-15 01:07:57 +02:00
|
|
|
if x_mouse==self.posx_screen:
|
2020-04-11 06:17:49 +02:00
|
|
|
x_mouse+=0.1
|
2020-04-15 01:07:57 +02:00
|
|
|
self.degres_canon = -1*degrees(atan((y_mouse-self.posy_screen)/(x_mouse-self.posx_screen)))
|
|
|
|
if x_mouse < self.posx_screen:
|
2020-04-13 02:33:10 +02:00
|
|
|
self.degres_canon = 180+self.degres_canon
|
|
|
|
canon = pygame.transform.rotate(self.canon,self.degres_canon)
|
2020-04-11 06:17:49 +02:00
|
|
|
|
|
|
|
# Get rects
|
2020-04-18 19:34:24 +02:00
|
|
|
self.perso_rect = perso.get_rect()
|
2020-04-15 01:07:57 +02:00
|
|
|
self.canon_rect = canon.get_rect()
|
2020-04-11 06:17:49 +02:00
|
|
|
|
2020-04-13 02:33:10 +02:00
|
|
|
# Blits
|
2020-04-15 01:07:57 +02:00
|
|
|
surface.blit(perso,(self.posx_screen-self.perso_rect.center[0],self.posy_screen-self.perso_rect.center[1]))
|
|
|
|
surface.blit(canon,(self.posx_screen-self.canon_rect.center[0],self.posy_screen-self.canon_rect.center[1]))
|
2020-04-13 02:33:10 +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-04-15 01:07:57 +02:00
|
|
|
new_proj = Projectile(name,self.texture_proj,(self.posx_screen,self.posy_screen),20,self.degres_canon)
|
2020-03-29 23:17:04 +02:00
|
|
|
self.projectiles.append(new_proj)
|
|
|
|
self.last_fire = time()
|
|
|
|
|
|
|
|
|
|
|
|
class Projectile():
|
|
|
|
|
2020-04-13 02:33:10 +02:00
|
|
|
def __init__(self,name,texture,position,speed,angle):
|
2020-03-29 23:17:04 +02:00
|
|
|
self.name = name
|
|
|
|
self.speed = speed
|
|
|
|
self.position = position
|
2020-04-12 06:08:24 +02:00
|
|
|
self.direction = direction = [cos(radians(angle)),-sin(radians(angle))]
|
|
|
|
self.img = pygame.transform.rotate(pygame.image.load(img_path+texture).convert_alpha(),angle)
|
2020-04-13 02:33:10 +02:00
|
|
|
self.rect = self.img.get_rect()
|
2020-03-31 05:03:42 +02:00
|
|
|
|
2020-03-29 23:17:04 +02:00
|
|
|
def move(self):
|
2020-04-13 02:33:10 +02:00
|
|
|
self.position = (round(self.position[0]+self.speed*self.direction[0]),round(self.position[1]+self.speed*self.direction[1]))
|
2020-03-28 20:56:52 +01:00
|
|
|
def draw(self,surface):
|
2020-04-13 02:33:10 +02:00
|
|
|
surface.blit(self.img,(self.position[0]-self.rect[0],self.position[1]-self.rect[1]))
|
2020-03-31 05:03:42 +02:00
|
|
|
|
|
|
|
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
|