tangledmind/models.py

334 lines
12 KiB
Python
Raw Normal View History

# -*- 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-05-04 04:08:10 +02:00
from utils import *
2020-04-04 06:11:34 +02:00
img_path = 'asset/'
2020-04-28 22:40:40 +02:00
with open(r'./config.yaml') as file:
data = yaml.load(file,Loader=yaml.FullLoader)
max_fire_rate = data['max_fire_rate'] # in fires per seconds
cap_speed = data['cap_speed']
acceleration = data['acceleration']
deceleration = data['deceleration']
2020-04-04 06:11:34 +02:00
2020-04-15 01:07:57 +02:00
2020-04-28 22:40:40 +02:00
2020-04-15 01:07:57 +02:00
class Game():
2020-05-02 05:34:46 +02:00
def __init__(self,carte,player,camera,enemies,hud):
2020-04-15 01:07:57 +02:00
self.carte = carte
2020-05-02 05:34:46 +02:00
self.player = player
2020-04-28 22:40:40 +02:00
self.camera = camera
2020-05-01 03:28:51 +02:00
self.enemies = enemies
2020-05-02 05:34:46 +02:00
self.hud = hud
2020-04-15 01:07:57 +02:00
def draw(self,surface):
2020-04-19 05:03:48 +02:00
# draw the map
2020-04-28 22:40:40 +02:00
self.carte.draw(surface,self.camera)
2020-04-19 05:03:48 +02:00
# draw the projectiles and remove them if needed
to_remove = []
2020-05-02 05:34:46 +02:00
for k,proj in enumerate(self.player.projectiles):
2020-05-01 03:28:51 +02:00
if not proj.is_out(self.carte,self):
2020-04-19 05:03:48 +02:00
proj.move()
2020-04-29 16:34:28 +02:00
proj.draw(surface,self.camera)
2020-04-19 05:03:48 +02:00
else:
to_remove.append(k)
if to_remove != []:
for k in to_remove[::-1]:
2020-05-02 05:34:46 +02:00
del self.player.projectiles[k]
2020-04-19 05:03:48 +02:00
2020-05-01 03:28:51 +02:00
for enemy in self.enemies:
enemy.draw(surface,self.camera)
2020-05-02 05:34:46 +02:00
self.player.draw(surface,self.camera)
2020-04-28 22:40:40 +02:00
self.camera.draw(surface)
2020-05-02 05:34:46 +02:00
self.hud.draw(surface)
2020-04-28 22:40:40 +02:00
def get_offset(self):
#Return the position of the elements on the screen (camera)
2020-05-02 05:34:46 +02:00
return (self.player.posx-self.camera.posx,self.player.posy-self.camera.posy)
2020-04-28 22:40:40 +02:00
2020-05-02 05:34:46 +02:00
class Hud():
def __init__(self,player):
self.player = player
self.ratio = 1
2020-05-02 06:26:30 +02:00
self.font = pygame.font.SysFont('GlacialIndifference-Regular', 72)
2020-05-02 05:34:46 +02:00
def draw(self,surface):
# draw the life bar
pygame.draw.rect(surface,(255,0,0),(10,10,round(self.player.life/100*surface.get_width()/3),30),0)
pygame.draw.rect(surface,(255,255,255),(10,10,int(surface.get_width()/3),30),3)
2020-04-15 01:07:57 +02:00
2020-05-02 06:26:30 +02:00
text = self.font.render(str(self.player.ammo), True, (255, 255, 255))
surface.blit(text, (int(surface.get_width()-1.5*text.get_width()),int(surface.get_height()-1.5*text.get_height())))
2020-04-14 02:43:09 +02:00
class Carte():
def __init__(self,n):
self.carte, self.player_start_pos, self.back = map_generator(n)
2020-04-28 22:40:40 +02:00
def draw(self,surface,camera):
offsetx,offsety = camera.get_offset()
2020-05-04 04:08:10 +02:00
for room in self.carte:
surface.blit(self.back,(room[2][0]-offsetx,room[2][1]-offsety))
2020-05-04 04:08:10 +02:00
surface.blit(room[0],(room[2][0]-offsetx,room[2][1]-offsety))
def collision(self,thing):
2020-05-05 01:29:22 +02:00
#print(f"player in position {thing.posx,thing.posy}")
2020-05-04 04:08:10 +02:00
for room in self.carte:
if room[1].overlap(thing.mask, (thing.posx-thing.rect.center[0]-room[2][0],thing.posy-thing.rect.center[1]-room[2][1])):
2020-05-04 04:08:10 +02:00
return True
2020-05-05 01:29:22 +02:00
2020-05-04 04:08:10 +02:00
return False
2020-04-14 02:43:09 +02:00
2020-04-28 22:40:40 +02:00
class Camera():
2020-05-04 04:08:10 +02:00
def __init__(self,start_pos,screen_width,screen_height,box_ratio):
self.posx = start_pos[0]
self.posy = start_pos[1]
2020-04-28 22:40:40 +02:00
self.screen_height = screen_height
self.screen_width = screen_width
self.box_ratio = box_ratio
self.box = pygame.Rect(int(screen_width*box_ratio),
int(screen_height*box_ratio),
int(screen_width*(1-2*box_ratio)),
int(screen_height*(1-2*box_ratio)))
def draw(self,surface):
pass
# pygame.draw.line(surface, (255,0,0), (int(surface.get_width()/2-20),int(surface.get_height()/2)), (int(surface.get_width()/2+20),int(surface.get_height()/2)), 3)
# pygame.draw.line(surface, (255,0,0), (int(surface.get_width()/2),int(surface.get_height()/2-20)), (int(surface.get_width()/2),int(surface.get_height()/2+20)), 3)
# pygame.draw.rect(surface, (0,255,0), self.box, 3)
def get_offset(self):
return (int(self.posx-self.screen_width/2),int(self.posy-self.screen_height/2))
2020-05-02 05:34:46 +02:00
class Player():
2020-05-04 04:08:10 +02:00
def __init__(self,name,start_pos,center_screen,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon,texture_proj):
self.name = name
2020-05-04 04:08:10 +02:00
self.posx = start_pos[0]
self.posy = start_pos[1]
2020-04-06 00:29:38 +02:00
self.speed = [0,0]
2020-04-15 01:07:57 +02: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-05-02 05:34:46 +02:00
self.img_player = pygame.image.load(img_path+texture).convert_alpha()
self.player = self.img_player
self.rect = self.player.get_rect()
2020-05-02 05:34:46 +02:00
self.mask = pygame.mask.from_surface(self.img_player)
2020-04-15 01:07:57 +02:00
2020-04-20 05:37:28 +02:00
self.img_canon = pygame.image.load(img_path+texture_canon).convert_alpha()
self.canon = self.img_canon
2020-04-15 01:07:57 +02:00
self.canon_rect = self.canon.get_rect()
2020-05-02 05:34:46 +02:00
self.degres_player = 0
2020-04-13 02:33:10 +02:00
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 23:17:04 +02:00
self.projectiles = []
self.last_fire = time()
2020-05-02 05:34:46 +02:00
# RP attributes:
self.life = 100
self.ammo = 50
2020-04-28 22:40:40 +02:00
def check_keys(self,keystate,screen_width,screen_height,carte,camera):
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]:
self.speed[0] -= acceleration
2020-05-05 06:13:57 +02:00
if self.speed[0] < -cap_speed: self.speed[0] = -cap_speed
2020-04-03 04:53:25 +02:00
if keystate[self.key_right]:
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]:
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]:
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
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
2020-04-28 22:40:40 +02:00
# get pos of player on screen to check collision with the movement box
offsetx,offsety = camera.get_offset()
posx_screen = self.posx-offsetx
posy_screen = self.posy-offsety
2020-05-02 05:34:46 +02:00
# player in the box:
2020-04-28 22:40:40 +02:00
if camera.box.collidepoint(posx_screen,posy_screen):
# X AXIS
temp_pos = self.posx
self.posx = self.posx+self.speed[0]
2020-05-04 04:08:10 +02:00
if carte.collision(self):
2020-04-28 22:40:40 +02:00
self.posx = temp_pos
self.speed[0] = 0
# Recalculate position on screen to detect an exit of the box
posx_screen = self.posx-offsetx
if not camera.box.collidepoint(posx_screen,posy_screen):
# Exit of the box, move the box so that the player stays in it
camera.posx = camera.posx + self.speed[0]
# Y AXIS
temp_pos = self.posy
self.posy = self.posy+self.speed[1]
2020-05-04 04:08:10 +02:00
if carte.collision(self):
2020-04-28 22:40:40 +02:00
self.posy = temp_pos
self.speed[1] = 0
# Recalculate position on screen to detect an exit of the box
posy_screen = self.posy-offsety
if not camera.box.collidepoint(posx_screen,posy_screen):
# Exit of the box, move the box so that the player stays in it
camera.posy = camera.posy + self.speed[1]
2020-05-02 05:34:46 +02:00
# player not in the box
2020-04-28 22:40:40 +02:00
else:
# X AXIS
temp_pos = self.posx
self.posx = self.posx+self.speed[0]
2020-05-04 04:08:10 +02:00
if carte.collision(self):
2020-04-28 22:40:40 +02:00
self.posx = temp_pos
self.speed[0] = 0
2020-05-02 05:34:46 +02:00
# If the player really move, camera follow
2020-04-28 22:40:40 +02:00
else:
camera.posx = camera.posx + self.posx-temp_pos
# Y AXIS
temp_pos = self.posy
self.posy = self.posy+self.speed[1]
2020-05-04 04:08:10 +02:00
if carte.collision(self):
2020-04-28 22:40:40 +02:00
self.posy = temp_pos
self.speed[1] = 0
2020-05-02 05:34:46 +02:00
# If the player really move, camera follow
2020-04-28 22:40:40 +02:00
else:
camera.posy = camera.posy + self.posy-temp_pos
2020-04-06 00:29:38 +02:00
2020-03-29 23:17:04 +02:00
if keystate[self.key_fire]:
self.fire('bullet','asset/projectile')
2020-03-29 23:17:04 +02:00
2020-04-28 22:40:40 +02:00
def draw(self,surface,camera):
#Get offset and compute player position in screen
offsetx,offsety = camera.get_offset()
posx_screen = self.posx-offsetx
posy_screen = self.posy-offsety
2020-04-11 06:17:49 +02:00
#Calculate canon rotation:
x_mouse, y_mouse = pygame.mouse.get_pos()
2020-04-28 22:40:40 +02:00
if x_mouse==posx_screen:
2020-04-11 06:17:49 +02:00
x_mouse+=0.1
2020-04-28 22:40:40 +02:00
self.degres_canon = -1*degrees(atan((y_mouse-posy_screen)/(x_mouse-posx_screen)))
if x_mouse < posx_screen:
2020-04-13 02:33:10 +02:00
self.degres_canon = 180+self.degres_canon
2020-04-20 05:37:28 +02:00
self.canon = pygame.transform.rotate(self.img_canon,self.degres_canon)
2020-04-11 06:17:49 +02:00
# Get rects
self.rect = self.player.get_rect()
2020-04-20 05:37:28 +02:00
self.canon_rect = self.canon.get_rect()
2020-04-11 06:17:49 +02:00
2020-04-28 22:40:40 +02:00
# blit player and canon
surface.blit(self.player,(posx_screen-self.rect.center[0],posy_screen-self.rect.center[1]))
2020-04-28 22:40:40 +02:00
surface.blit(self.canon,(posx_screen-self.canon_rect.center[0],posy_screen-self.canon_rect.center[1]))
#pygame.draw.rect(surface, (255,0,0), (int(move_box_ratio*scwidth),int(move_box_ratio*scheight),int(scwidth*(1-2*move_box_ratio)),int(scheight*(1-2*move_box_ratio))), 2)
2020-04-13 02:33:10 +02:00
2020-03-31 05:03:42 +02:00
def fire(self,name,texture):
2020-04-28 22:40:40 +02:00
if (time()-self.last_fire> 1/max_fire_rate):
2020-04-19 05:03:48 +02:00
new_proj = Projectile(name,self.texture_proj,(self.posx,self.posy),20,self.degres_canon)
2020-03-29 23:17:04 +02:00
self.projectiles.append(new_proj)
self.last_fire = time()
2020-05-02 06:26:30 +02:00
self.ammo = self.ammo -1
2020-03-29 23:17:04 +02:00
2020-05-01 03:28:51 +02:00
class Enemy():
def __init__(self,name,position,texture):
self.name = name
self.posx = position[0]
self.posy = position[1]
self.img = pygame.image.load(img_path+texture).convert_alpha()
self.rect = self.img.get_rect()
self.mask = pygame.mask.from_surface(self.img)
def draw(self,surface,camera):
offsetx,offsety = camera.get_offset()
posx_screen = self.posx-offsetx
posy_screen = self.posy-offsety
surface.blit(self.img,(posx_screen-self.rect.center[0],posy_screen-self.rect.center[1]))
def die(self,game,index):
print(f"{self.name} died with honor.")
del game.enemies[index]
class Projectile():
2020-03-29 23:17:04 +02:00
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
2020-04-29 16:34:28 +02:00
self.posx = position[0]
self.posy = position[1]
#self.deplacement = [0,0]
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()
self.mask = pygame.mask.from_surface(self.img)
2020-03-31 05:03:42 +02:00
2020-03-29 23:17:04 +02:00
def move(self):
2020-04-29 16:34:28 +02:00
self.posx = round(self.posx+self.speed*self.direction[0])
self.posy = round(self.posy+self.speed*self.direction[1])
#self.deplacement = (round(self.deplacement[0]+self.speed*self.direction[0]),round(self.deplacement[1]+self.speed*self.direction[1]))
2020-04-29 16:34:28 +02:00
def draw(self,surface,camera):
# Get position on screen
offsetx,offsety = camera.get_offset()
posx_screen = self.posx-offsetx
posy_screen = self.posy-offsety
surface.blit(self.img,(posx_screen-self.rect[0],posy_screen-self.rect[1]))
2020-03-31 05:03:42 +02:00
2020-05-01 03:28:51 +02:00
def is_out(self,carte,game):
# A projectile is out if it touches a wall or an enemy. Otherwise it continues.
impact = False
# Touchs a wall
if carte.collision(self):
2020-05-01 03:28:51 +02:00
impact = True
# Touchs an enemy
for k,enemy in enumerate(game.enemies):
if enemy.mask.overlap(self.mask, (enemy.posx+enemy.rect.center[0]-self.posx-self.rect.center[0],enemy.posy+enemy.rect.center[1]-self.posy-self.rect.center[1])):
print("Impact on enemy {} at position {}".format(enemy.name,(enemy.posx,enemy.posy)))
enemy.die(game,k)
impact = True
return impact