269 lines
No EOL
10 KiB
Python
269 lines
No EOL
10 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
|
|
|
|
img_path = 'asset/'
|
|
|
|
max_fire_rate = 10 # in fires per seconds
|
|
|
|
cap_speed = 10
|
|
acceleration = 2
|
|
deceleration = 1.1
|
|
|
|
|
|
|
|
class Game():
|
|
def __init__(self,carte,perso,camera):
|
|
self.carte = carte
|
|
self.perso = perso
|
|
self.camera = camera
|
|
|
|
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.perso.projectiles):
|
|
if not proj.is_out(self.carte):
|
|
proj.move()
|
|
proj.draw(surface,self.camera)
|
|
else:
|
|
to_remove.append(k)
|
|
if to_remove != []:
|
|
for k in to_remove[::-1]:
|
|
del self.perso.projectiles[k]
|
|
|
|
self.perso.draw(surface,self.camera)
|
|
self.camera.draw(surface)
|
|
|
|
def get_offset(self):
|
|
#Return the position of the elements on the screen (camera)
|
|
return (self.perso.posx-self.camera.posx,self.perso.posy-self.camera.posy)
|
|
|
|
|
|
|
|
class Carte():
|
|
def __init__(self,texture):
|
|
self.posx = 0
|
|
self.posy = 0
|
|
self.img = pygame.image.load('maps/'+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()
|
|
surface.blit(self.img,(self.posx-offsetx,self.posy-offsety))
|
|
|
|
|
|
|
|
class Camera():
|
|
def __init__(self,start_posx,start_posy,screen_width,screen_height,box_ratio):
|
|
self.posx = start_posx
|
|
self.posy = start_posy
|
|
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 Perso():
|
|
def __init__(self,name,posx,posy,center_screen,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.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_perso = pygame.image.load(img_path+texture).convert_alpha()
|
|
self.perso = self.img_perso
|
|
self.perso_rect = self.perso.get_rect()
|
|
self.mask = pygame.mask.from_surface(self.img_perso)
|
|
|
|
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_perso = 0
|
|
self.degres_canon = 0
|
|
self.texture_proj = texture_proj
|
|
|
|
self.projectiles = []
|
|
self.last_fire = time()
|
|
|
|
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
|
|
|
|
# perso 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.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])):
|
|
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.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])):
|
|
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]
|
|
|
|
|
|
# perso not in the box
|
|
else:
|
|
# X AXIS
|
|
temp_pos = self.posx
|
|
self.posx = self.posx+self.speed[0]
|
|
if carte.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])):
|
|
self.posx = temp_pos
|
|
self.speed[0] = 0
|
|
# If the perso 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.mask.overlap(self.mask, (self.posx-self.perso_rect.center[0],self.posy-self.perso_rect.center[1])):
|
|
self.posy = temp_pos
|
|
self.speed[1] = 0
|
|
# If the perso 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):
|
|
|
|
#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:
|
|
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.perso_rect = self.perso.get_rect()
|
|
self.canon_rect = self.canon.get_rect()
|
|
|
|
# blit player and canon
|
|
surface.blit(self.perso,(posx_screen-self.perso_rect.center[0],posy_screen-self.perso_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 (time()-self.last_fire> 1/max_fire_rate):
|
|
new_proj = Projectile(name,self.texture_proj,(self.posx,self.posy),20,self.degres_canon)
|
|
self.projectiles.append(new_proj)
|
|
self.last_fire = time()
|
|
|
|
class Projectile(pygame.sprite.Sprite):
|
|
|
|
def __init__(self,name,texture,position,speed,angle):
|
|
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):
|
|
if carte.mask.overlap(self.mask, (self.posx-self.rect.center[0],self.posy-self.rect.center[1])):
|
|
# if (self.deplacement[0]+surface.get_width()/2<0-self.img.get_width() or
|
|
# self.deplacement[1]+surface.get_height()/2<0-self.img.get_height() or
|
|
# self.deplacement[0]+surface.get_width()/2>surface.get_width()+self.img.get_width() or
|
|
# self.deplacement[1]+surface.get_height()/2>surface.get_height()+self.img.get_height()):
|
|
return True
|
|
else:
|
|
return False |