PinmikPanik/gamedata/definitions.py
2021-09-19 12:20:43 +02:00

146 lines
4.6 KiB
Python

from random import *
import sys,os,configparser
def drawcenter(game,surface,posx,posy):
# Draw a surface in its center
game.window.blit(surface,(round(posx-surface.get_width()/2),round(posy-surface.get_height()/2)))
def blit_text(pygame,surface, text, pos, font, color=[255]*4,center=False): # Blitting text with linebreaks
words = [word.split(' ') for word in text.splitlines()] # Liste qui contient tout les mots
space = font.size(' ')[0] # La largeur d'un espace
max_width, max_height = surface.get_size()
x, y = pos
linebreak = False
linesurf = pygame.Surface(surface.get_size(),pygame.SRCALPHA)
for line in words:
if linebreak:
px = 0
if center:
px = (surface.get_width()-x+space)//2
surface.blit(linesurf,(px,0))
linesurf.fill([0]*4)
x=pos[0]
y+=font.size(' ')[1]
linebreak = False
for word in line:
word_surface = font.render(word,False,color)
word_width = word_surface.get_width()
if x+word_width>surface.get_width():
px = 0
if center:
px = (surface.get_width()-x+space)//2
surface.blit(linesurf,(px,0))
linesurf.fill([0]*4)
x=pos[0]
y+=font.size(' ')[1]
linesurf.blit(word_surface,(x,y))
x+=word_width+space
linebreak = True
px = 0
if center:
px = (surface.get_width()-x+space)//2
surface.blit(linesurf,(px,0))
linesurf.fill([0]*4)
x=pos[0]
y+=font.size(' ')[1]
class Timer():
def __init__(self,maxcount):
self.maxcount = maxcount
self.timer = maxcount
self.loops = 0
def tick(self,avancement):
result = self.timer<=0
self.timer-=avancement
if result:
self.timer+=self.maxcount
self.loops+=1
return result
def getloops(self):
return self.loops
def getmax(self):
return self.maxcount
def getratio(self):
return max(min(1,self.timer/self.maxcount),0)
def reset(self):
self.timer = self.maxcount
def add(self,amount):
self.timer+=amount
self.maxcount+=amount
def get_save_dir(dirname):
path = ""
p = sys.platform
if p=="linux":
path = os.environ["HOME"]+"/.local/share/"+dirname
if p=="win32":
path = os.getenv('APPDATA')+"\\"+dirname
# Create folder if non-existant
try:
os.mkdir(path)
except:
pass
return path
def loadscore(dirname,filename="highscore.ini"):
filepath = os.path.join(dirname,filename)
highscore = 0
try:
config = configparser.ConfigParser()
config.read(filepath)
if "highscore" in config.sections():
highscore = int(config["highscore"].get("score",0))
except:
# Save file corrupted, reset it
os.remove(filepath)
return highscore
def loadsettings(dirname,filename="settings.ini"):
filepath = os.path.join(dirname,filename)
sfx = 100
bgm = 100
try:
config = configparser.ConfigParser()
config.read(filepath)
if "settings" in config.sections():
bgm = int(config["settings"].get("bgm",100))
sfx = int(config["settings"].get("sfx",100))
except:
# Save file corrupted, reset it
os.remove(filepath)
return bgm,sfx
def savescore(dirname,highscore=0,filename="highscore.ini"):
filepath = os.path.join(dirname,filename)
config = configparser.ConfigParser()
config["highscore"] = {"score":highscore}
with open(filepath,'w') as configfile:
config.write(configfile)
def savesettings(dirname,bgm=1,sfx=1,filename="settings.ini"):
filepath = os.path.join(dirname,filename)
config = configparser.ConfigParser()
bgm,sfx = round(bgm*100),round(sfx*100)
config["settings"] = {"bgm":bgm,"sfx":sfx}
with open(filepath,'w') as configfile:
config.write(configfile)
def getunlocks(highscore):
unlocks = {"normal":["base"],"specials":[]}
if highscore>=50:
unlocks["normal"].append("green")
if highscore>=250:
unlocks["normal"].append("blue")
if highscore>=400:
unlocks["specials"].append("gold")
if highscore>=500:
unlocks["specials"].append("monster")
if highscore>=750:
unlocks["specials"].append("platinum")
return unlocks