You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
14 KiB
Python

# -*- coding: UTF-8 -*-
### Tangled Mind
### Author: Arthur 'Grizzly' Grisel-Davy
import pygame
from time import time
from math import atan, degrees,radians, cos, sin
from utils import *
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']
img_path = data['texture_path']
font_large_size = data['font_large']
font_small_size = data['font_small']
health_width = data['health_width']
font_name = data['font_name']
class Game():
def __init__(self,carte,player,camera,enemies,hud):
self.pause = False
self.carte = carte
self.player = player
self.camera = camera
self.enemies = enemies
self.hud = hud
def draw(self,surface):
# draw the map
self.carte.draw(surface,self.camera)
# draw the projectiles and remove them if needed
to_remove = []
for k,proj in enumerate(self.player.projectiles):
if not proj.is_out(self.carte,self):
if not self.pause:
proj.move()
proj.draw(surface,self.camera)
else:
to_remove.append(k)
if to_remove != []:
for k in to_remove[::-1]:
del self.player.projectiles[k]
for enemy in self.enemies:
enemy.draw(surface,self.camera)
self.player.draw(surface,self.camera,self)
self.camera.draw(surface)
self.hud.draw(surface)
def draw_pause(self,surface):
pause_screen = pygame.Surface((surface.get_width(),surface.get_height()))
pause_screen.set_alpha(128)
pause_screen.fill((255,255,255))
surface.blit(pause_screen, (0,0))
text = self.hud.font_large.render(str('...PAUSED...'), True, (255, 255, 255))
surface.blit(text, (int(surface.get_width()/2-text.get_width()/2),int(0.3*surface.get_height()-text.get_height()/2)))
def get_offset(self):
#Return the position of the elements on the screen (camera)
return (self.player.posx-self.camera.posx,self.player.posy-self.camera.posy)
class Hud():
def __init__(self,player):
self.player = player
self.ratio = 1
self.font_large = pygame.font.SysFont(font_name, font_large_size)
self.font_small = pygame.font.SysFont(font_name, font_small_size)
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),health_width),0)
pygame.draw.rect(surface,(255,255,255),(10,10,int(surface.get_width()/3),health_width),3)
text = self.font_large.render(str(self.player.equipped_weapon.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())))
def write_fps(self,surface,time_mili):
text = self.font_small.render(str(int(1/time_mili)), True, (255, 255, 255))
surface.blit(text, (int(surface.get_width()-1.5*text.get_width()),int(0.5*text.get_height())))
class Carte():
def __init__(self,n):
self.carte, self.player_start_pos = map_generator(n)
def draw(self,surface,camera):
offsetx,offsety = camera.get_offset()
for k,room in enumerate(self.carte):
surface.blit(room[0],(room[3][0]-offsetx,room[3][1]-offsety))
surface.blit(room[1],(room[3][0]-offsetx,room[3][1]-offsety))
def collision(self,thing):
#print(f"player in position {thing.posx,thing.posy}")
for room in self.carte:
if room[2].overlap(thing.mask, (thing.posx-thing.rect.center[0]-room[3][0],thing.posy-thing.rect.center[1]-room[3][1])):
return True
return False
class Camera():
def __init__(self,start_pos,screen_width,screen_height,box_ratio):
self.posx = start_pos[0]
self.posy = start_pos[1]
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))
class Player():
def __init__(self,name,start_pos,center_screen,key_up,key_down,key_left,key_right,key_fire,texture,texture_canon):
self.name = name
self.posx = start_pos[0]
self.posy = start_pos[1]
self.speed = [0,0]
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.img_player = pygame.image.load(img_path+texture).convert_alpha()
self.player = self.img_player
self.rect = self.player.get_rect()
self.mask = pygame.mask.from_surface(self.img_player)
self.img_canon = pygame.image.load(img_path+texture_canon).convert_alpha()
self.canon = self.img_canon
self.canon_rect = self.canon.get_rect()
self.degres_player = 0
self.degres_canon = 0
self.projectiles = []
self.last_fire = time()
# RP attributes:
self.life = 100
self.weapons = []
self.equipped_weapon = None
def check_keys(self,keystate,screen_width,screen_height,carte,camera):
# 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] -= acceleration
if self.speed[0] < -cap_speed: self.speed[0] = -cap_speed
if keystate[self.key_right]:
self.speed[0] += acceleration
if self.speed[0] > cap_speed: self.speed[0] = cap_speed
if keystate[self.key_up]:
self.speed[1] -= acceleration
if self.speed[1] < -cap_speed: self.speed[1] = -cap_speed
if keystate[self.key_down]:
self.speed[1] += acceleration
if self.speed[1] > cap_speed: self.speed[1] = cap_speed
# 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)
# 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
# player in the box:
if camera.box.collidepoint(posx_screen,posy_screen):
# X AXIS
temp_pos = self.posx
self.posx = self.posx+self.speed[0]
if carte.collision(self):
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]
if carte.collision(self):
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]
# player not in the box
else:
# X AXIS
temp_pos = self.posx
self.posx = self.posx+self.speed[0]
if carte.collision(self):
self.posx = temp_pos
self.speed[0] = 0
# If the player really move, camera follow
else:
camera.posx = camera.posx + self.posx-temp_pos
# Y AXIS
temp_pos = self.posy
self.posy = self.posy+self.speed[1]
if carte.collision(self):
self.posy = temp_pos
self.speed[1] = 0
# If the player really move, camera follow
else:
camera.posy = camera.posy + self.posy-temp_pos
if keystate[self.key_fire]:
self.fire('bullet','asset/projectile')
def draw(self,surface,camera,game):
#Get offset and compute player position in screen
offsetx,offsety = camera.get_offset()
posx_screen = self.posx-offsetx
posy_screen = self.posy-offsety
#Calculate canon rotation:
if not game.pause:
x_mouse, y_mouse = pygame.mouse.get_pos()
if x_mouse==posx_screen:
x_mouse+=0.1
self.degres_canon = -1*degrees(atan((y_mouse-posy_screen)/(x_mouse-posx_screen)))
if x_mouse < posx_screen:
self.degres_canon = 180+self.degres_canon
self.canon = pygame.transform.rotate(self.img_canon,self.degres_canon)
# Get rects
self.rect = self.player.get_rect()
self.canon_rect = self.canon.get_rect()
# blit player and canon
surface.blit(self.player,(posx_screen-self.rect.center[0],posy_screen-self.rect.center[1]))
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)
def fire(self,name,texture):
if self.equipped_weapon and (time()-self.last_fire> 1/max_fire_rate):
self.equipped_weapon.fire(self)
self.last_fire = time()
def add_weapon(self,weapon):
self.weapons.append(weapon)
if len(self.weapons) == 1:
self.equipped_weapon = self.weapons[0]
def change_weapon(self,index):
self.equipped_weapon = self.weapons[index]
class Enemy():
def __init__(self,name,position,texture):
self.life = 100
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 touched(self, proj):
self.life -= proj.damage
print(f"life down to {self.life}")
def die(self,game,index):
print(f"{self.name} died with honor.")
del game.enemies[index]
class Weapon():
""" Base model for a weapon. All weapons will child class of this one."""
def __init__(self, name, damages, spray, ammo, texture_proj):
self.ammo = ammo
self.name = name
self.damages = damages
self.spray = spray
self.texture_proj = texture_proj
def fire(self,player):
if self.ammo > 0:
player.projectiles.append(Projectile('plop',self.texture_proj,(player.posx,player.posy),20,player.degres_canon,50))
self.ammo = self.ammo -1
class Projectile():
def __init__(self,name,texture,position,speed,angle,damage):
self.damage = damage
self.name = name
self.speed = speed
self.posx = position[0]
self.posy = position[1]
#self.deplacement = [0,0]
self.direction = direction = [cos(radians(angle)),-sin(radians(angle))]
self.img = pygame.transform.rotate(pygame.image.load(img_path+texture).convert_alpha(),angle)
self.rect = self.img.get_rect()
self.mask = pygame.mask.from_surface(self.img)
def move(self):
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]))
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]))
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):
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.touched(self)
if enemy.life <= 0:
enemy.die(game,k)
impact = True
return impact