Overflown/gamedata/game.py

328 lines
14 KiB
Python
Raw Normal View History

2021-06-25 14:42:35 +02:00
import pygame, os, math, time
import gamedata.definitions as lib
import gamedata.scenes as scenes
import gamedata.objects.gameloop as gameloop
from gamedata.objects.particles import Particle
class Game():
def __init__(self):
self.datadir = lib.get_save_dir("slime")
self.DISPLAY_WIDTH, self.DISPLAY_HEIGHT = 1280, 720
2021-07-02 21:06:34 +02:00
self.window = pygame.Surface((self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT))
self.realwindow = pygame.display.set_mode((self.DISPLAY_WIDTH,self.DISPLAY_HEIGHT))
2021-11-21 12:28:27 +01:00
pygame.display.set_caption("Overflown")
2021-06-25 14:42:35 +02:00
pygame.init()
pygame.mixer.init()
2021-11-22 16:30:37 +01:00
self.fps = 60
self.logs = []
2021-06-25 14:42:35 +02:00
self.running = True
self.gameloop = gameloop.GameLoop() # Je crée une boucle de jeu
2021-09-05 21:12:47 +02:00
def load_image(filename):
return pygame.image.load(filename).convert_alpha()
self.sprite_lib = self.init_assets("gamedata/assets/",load_image) # Dico qui contient chaques images du dossier assets
2021-06-25 14:42:35 +02:00
pygame.display.set_icon(self.sprite_lib["icon.png"])
self.sound_lib = self.init_assets("gamedata/sounds/",pygame.mixer.Sound) # Pareil, mais pour les musiques / sons
2021-11-22 09:56:55 +01:00
self.fontmap = self.sprite_lib["fontmap.png"]
2021-06-25 14:42:35 +02:00
# Chargement des niveaux
def loadlvldata(mapfolder):
mapdico = {}
mapdico["name"] = mapfolder.split(os.sep)[-1]
mapdico["cover"] = None
2021-11-20 21:23:05 +01:00
mapdico["backgrounds"] = []
mapdico["data"] = None
2021-11-20 21:23:05 +01:00
mapdico["filler"] = None
mapdico["tilesets"] = {}
scanner = os.scandir(path=mapfolder)
for i in scanner: # Je check tout les fichiers du dossier
name = i.name
try:
if name.endswith(".png"):
if name=="cover.png":
mapdico["cover"] = pygame.image.load(i.path)
2021-11-20 21:23:05 +01:00
elif name.startswith("background"):
mapdico["backgrounds"].append({"sprite":pygame.image.load(i.path).convert_alpha(),"name":i.path})
elif name=="filler.png":
mapdico["filler"] = pygame.image.load(i.path).convert_alpha()
else:
2021-11-20 21:23:05 +01:00
mapdico["tilesets"][i.name] = pygame.image.load(i.path).convert_alpha()
except:
self.log("Erreur",mapfolder,name,"Fichier invalide")
if name=="map.json":
try:
with open(i.path,"r") as jsonfile:
mapdico["data"] = lib.json.loads(jsonfile.read())
except:
self.log("Erreur",mapfolder,name)
if mapdico["data"]:
2021-11-20 21:23:05 +01:00
mapdico["backgrounds"].sort(key=lambda x: x["name"])
return mapdico
return None
self.levels_lib = self.init_assets("gamedata/maps/",loadlvldata,recursive=False) # Je charge le dossier de maps
2021-06-25 14:42:35 +02:00
self.sound_volumes = {}
for i in self.sound_lib.keys():
self.sound_volumes[i] = 1
self.spriteLists = {} # Tiendra des listes préarrangées
self.scenes = scenes # Je stoque mes modules à l'intérieur de ma classe pour y avoir accès partout
self.scene = None
self.lib = lib
self.pygame = pygame
self.math = math
2021-11-21 18:02:46 +01:00
self.clock = pygame.time.Clock()
2021-06-25 14:42:35 +02:00
2021-08-08 19:55:21 +02:00
self.elapsedtime = 0
2021-06-25 14:42:35 +02:00
self.globals = {} # Un dico pour ranger toute les valeurs globales, pour communiquer entre objets par exemples
self.globals["camerax"] = 0
self.globals["cameray"] = 0
2021-11-22 22:14:09 +01:00
self.globals["scamerax"] = 0
2021-06-25 14:42:35 +02:00
self.globals["scameray"] = 0
2021-08-08 19:55:21 +02:00
self.globals["players"] = []
2021-11-26 16:10:58 +01:00
self.globals["AZERTY"] = True
2021-11-25 13:41:38 +01:00
self.globals["pause"] = False
2021-11-22 22:14:09 +01:00
self.globals["finishedlevels"] = [] # Levels where the player went to the end
self.globals["completedlevels"] = [] # Levels where the player kicked all non-respawnable ennemies
2021-11-24 15:47:52 +01:00
self.globals["speedrunlevels"] = [] # Levels where the player finished fast enough
2021-11-28 11:33:00 +01:00
self.globals["overworld"] = True
self.globals["nblevels"] = 0
self.globals["levelname"] = None
2021-11-24 07:41:30 +01:00
self.globals["allunlocked"] = True
2021-11-27 19:27:14 +01:00
self.globals["bgms"] = ["city.ogg","docks.ogg","isle.ogg","isle.ogg","docks.ogg","city.ogg"]
2021-11-26 16:10:58 +01:00
self.init_inputs()
2021-07-02 21:06:34 +02:00
self.scaleCamera()
2021-06-25 14:42:35 +02:00
2021-11-25 08:11:08 +01:00
settings = {"sfx":1,"bgm":0.2}
2021-06-25 14:42:35 +02:00
self.globals["bgmvolume"] = settings["bgm"]
self.globals["sfxvolume"] = settings["sfx"]
self.reinit_volumes()
self.pasttime = time.time()
# Je charge la scene de base
2021-11-27 20:24:50 +01:00
scenes.boot(self)
2021-06-25 14:42:35 +02:00
def set_camera(self,posx,posy):
self.globals["camerax"], self.globals["cameray"] = posx,posy
def log(*args):
args[0].logs.append(" ".join(args[1:]))
print(" ".join(args[1:]))
2021-06-25 14:42:35 +02:00
def reinit_volumes(self):
for i in self.sound_lib.keys(): # J'applique de base les volumes
if i.startswith("sfx/"):
self.sound_lib[i].set_volume(self.globals["sfxvolume"]*self.sound_volumes[i])
if i.startswith("bgm/"):
self.sound_lib[i].set_volume(self.globals["bgmvolume"]*self.sound_volumes[i])
def set_volume(self,sound,newvolume):
self.sound_volumes[sound] = newvolume
self.reinit_volumes()
def game_loop(self):
2021-11-22 16:30:37 +01:00
self.dt = 1/self.fps
2021-08-08 19:55:21 +02:00
self.elapsedtime += self.dt
2021-06-25 14:42:35 +02:00
self.pasttime = time.time()
self.check_events() # Détecte les entrées clavier
self.window.fill((0)*3) # Remplis l'écran de noir
if self.gameloop: # Si j'ai une boucle de jeu, la lancer
self.gameloop.step(self) # La logique de la boucle
self.gameloop.draw(self) # L'affichage de la boucle
if self.scene :
self.scene(self)
self.scene = False
pygame.display.update() # Mettre à jour l'affichage
2021-11-23 17:32:14 +01:00
self.clock.tick(self.fps)
2021-06-25 14:42:35 +02:00
def init_inputs(self):
self.inputs = {}
self.inputs["keys"] = {
"escape":{
2021-11-26 16:10:58 +01:00
"keycode" : [pygame.K_ESCAPE,pygame.K_RETURN] # Code pygame de la touche en question
2021-06-25 14:42:35 +02:00
},
"left":{
2021-11-26 16:10:58 +01:00
"keycode" : [pygame.K_LEFT] # Code pygame de la touche en question
2021-06-25 14:42:35 +02:00
},
"right":{
2021-11-26 16:10:58 +01:00
"keycode" : [pygame.K_RIGHT] # Code pygame de la touche en question
2021-06-25 14:42:35 +02:00
},
"up":{
2021-11-26 16:10:58 +01:00
"keycode" : [pygame.K_SPACE,pygame.K_UP] # Code pygame de la touche en question
2021-06-25 14:42:35 +02:00
},
"down":{
2021-11-26 16:10:58 +01:00
"keycode" : [pygame.K_DOWN] # Code pygame de la touche en question
2021-06-25 14:42:35 +02:00
}
}
2021-11-26 16:10:58 +01:00
if self.globals["AZERTY"]: # Azerty controls
self.inputs["keys"]["left"]["keycode"].append(pygame.K_q)
self.inputs["keys"]["right"]["keycode"].append(pygame.K_d)
self.inputs["keys"]["up"]["keycode"].append(pygame.K_z)
self.inputs["keys"]["down"]["keycode"].append(pygame.K_s)
else: # Qwerty controls
self.inputs["keys"]["left"]["keycode"].append(pygame.K_a)
self.inputs["keys"]["right"]["keycode"].append(pygame.K_d)
self.inputs["keys"]["up"]["keycode"].append(pygame.K_w)
self.inputs["keys"]["down"]["keycode"].append(pygame.K_s)
for key in self.inputs["keys"].keys():
self.inputs["keys"][key]["timer"] = 0
self.inputs["keys"][key]["realtime"] = 0
self.inputs["keys"][key]["pressed"] = False
self.inputs["keys"][key]["counted"] = False
2021-06-25 14:42:35 +02:00
2021-11-26 15:02:44 +01:00
self.inputs["joysticks"] = {}
2021-06-25 14:42:35 +02:00
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
if event.type == pygame.KEYDOWN:
for i in self.inputs["keys"].keys():
2021-11-26 16:10:58 +01:00
if event.key in self.inputs["keys"][i]["keycode"]: # Vérifie si une des touches du dico est préssée
2021-06-25 14:42:35 +02:00
self.inputs["keys"][i]["pressed"] = True
if event.type == pygame.KEYUP:
for i in self.inputs["keys"].keys():
2021-11-26 16:10:58 +01:00
if event.key in self.inputs["keys"][i]["keycode"]: # Vérifie si une des touches du dico est préssée
self.inputs["keys"][i]["pressed"] = False
2021-11-26 15:02:44 +01:00
if event.type == pygame.JOYDEVICEADDED:
index = event.device_index
self.inputs["joysticks"][index] = pygame.joystick.Joystick(index)
if event.type == pygame.JOYDEVICEREMOVED:
index = event.instance_id
del(self.inputs["joysticks"][index])
# Check for controller input
for i in self.inputs["keys"].keys():
self.inputs["keys"][i]["counted"] = False
for joy in self.inputs["joysticks"].values():
nbhats = joy.get_numhats()
for i in range(nbhats):
x,y = joy.get_hat(i)
if x==-1:
self.inputs["keys"]["left"]["counted"] = True
if y==1:
self.inputs["keys"]["up"]["counted"] = True
if x==1:
self.inputs["keys"]["right"]["counted"] = True
if y==-1:
self.inputs["keys"]["down"]["counted"] = True
nbaxis = joy.get_numaxes()
if nbaxis>=2:
x = joy.get_axis(0)
y = joy.get_axis(1)
deadzone = 0.2
if x<-deadzone:
self.inputs["keys"]["left"]["counted"] = True
if x>deadzone:
self.inputs["keys"]["right"]["counted"] = True
if y>deadzone*3:
self.inputs["keys"]["down"]["counted"] = True
nbbuttons = joy.get_numbuttons()
for i in range(8):
if nbbuttons>=i and joy.get_button(i)==1:
if i==0: # A button
self.inputs["keys"]["up"]["counted"] = True
if i>0 and i<=3: # B,X, and Y buttons
self.inputs["keys"]["down"]["counted"] = True
if i>5: # Start and Select
self.inputs["keys"]["escape"]["counted"] = True
2021-06-25 14:42:35 +02:00
2021-11-26 15:02:44 +01:00
for i in self.inputs["keys"].keys():
self.inputs["keys"][i]["counted"] = self.inputs["keys"][i]["counted"] or self.inputs["keys"][i]["pressed"]
2021-06-25 14:42:35 +02:00
# Augmente le timer si la touche est préssée, le reset sinon
for i in self.inputs["keys"].keys():
2021-11-26 15:02:44 +01:00
if self.inputs["keys"][i]["counted"]:
2021-06-25 14:42:35 +02:00
self.inputs["keys"][i]["timer"]+=1
2021-07-08 16:20:22 +02:00
self.inputs["keys"][i]["realtime"]+=self.dt
2021-06-25 14:42:35 +02:00
else:
self.inputs["keys"][i]["timer"]=0
2021-07-08 16:20:22 +02:00
self.inputs["keys"][i]["realtime"]=0
2021-06-25 14:42:35 +02:00
def init_assets(self,path,function,recursive=True):
2021-06-25 14:42:35 +02:00
dico = {}
self.scan_dir(path,path,dico,function,recursive)
2021-06-25 14:42:35 +02:00
return dico
def scan_dir(self,dirpath,origin,dico,function,recursive=True):
2021-06-25 14:42:35 +02:00
scanner = os.scandir(path=dirpath)
for i in scanner: # Je passe à travers toutes les données d'un dossier, fichiers et sous dossiers compris
# i.path est le chemin du fichier, par exemple
# Si c'est une image, je l'importe et l'ajoute à la librairie
finalpath = i.path.replace("\\","/")
if i.is_file() or not recursive:
2021-06-25 14:42:35 +02:00
finalpath = finalpath.replace(origin,'') # J'enleve l'origine (dans ce cas 'assets/' car c'est redontant, tout les sprites sont dedans
dico[finalpath] = function(i.path)
# Si c'est un dossier, je répete l'opération mais à l'intérieur de celui ci
if i.is_dir() and recursive:
self.scan_dir(i.path,origin,dico,function,recursive)
2021-06-25 14:42:35 +02:00
scanner.close()
def getSpriteDir(self,directory,ext=".png",assetdir="sprite_lib"):
keys = (directory,ext,assetdir)
assetdir = getattr(self,assetdir) # Si je ne précise pas quel type de ressource, je cherche dans mes sprites
if keys in self.spriteLists.keys():
return self.spriteLists[keys]
else:
# Organise les sprites
sprite_list = []
index = 0
while directory+str(index)+ext in assetdir.keys():
sprite_list.append(assetdir[directory+str(index)+ext])
index+=1
# Le stoque pour éviter de les réorganiser à chaque fois
self.spriteLists[keys] = sprite_list
return sprite_list
2021-11-25 09:21:42 +01:00
def addParticle(self,sprites,posx,posy,velx=0,vely=0,modvelx=0,modvely=0,flipx=False,flipy=False,fps=15,depth=0):
2021-06-25 14:42:35 +02:00
p = Particle(self,sprites,posx,posy,velx,vely,modvelx,modvely,flipx,flipy,fps)
2021-11-25 09:21:42 +01:00
p.depth = depth
2021-06-25 14:42:35 +02:00
self.gameloop.summon(p)
2021-07-02 21:06:34 +02:00
def scaleCamera(self,neww=None,newh=None):
if not neww:
neww=self.DISPLAY_WIDTH
if not newh:
newh=self.DISPLAY_HEIGHT
self.globals["cameraw"] = neww
self.globals["camerah"] = newh
2021-06-25 14:42:35 +02:00
2021-07-02 21:06:34 +02:00
self.globals["tempsubsurface"] = pygame.Surface((neww,newh))
2021-11-22 09:56:55 +01:00
def getchar(self,char,width=9,height=9):
2021-11-24 15:47:52 +01:00
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@\"#&()*:;?!abcdefghijklmnopqrstuvwxyz/"
2021-11-22 09:56:55 +01:00
charsperline = self.fontmap.get_width()//width
result = None
if char in chars:
index = chars.index(char)
line = index//charsperline
column = index%charsperline
result = self.pygame.Surface((width,height),flags=self.pygame.SRCALPHA)
result.blit(self.fontmap,[-width*column,-height*line])
return result
def getchars(self,chars):
result = []
for i in chars:
result.append(self.getchar(i))
2021-11-23 18:53:48 +01:00
surface = pygame.Surface((len(result)*9,9),flags=pygame.SRCALPHA)
for i in range(len(result)):
if result[i]:
surface.blit(result[i],(i*9,0))
return surface