Overflown/gamedata/definitions.py

119 lines
3.8 KiB
Python

from random import *
import sys,os,math,shutil,json
def ex_sql(db_name,request,replace=""):
conn = sqlite3.connect(db_name)
conn.row_factory=sqlite3.Row # La fonction retourne des dicos et pas des listes
cur = conn.cursor()
if not replace=="":
cur.execute(request,replace)
else:
cur.execute(request)
conn.commit()
a = None
if "SELECT" in request.upper():
a = cur.fetchall()
for i in range(len(a)):
a[i] = dict(a[i]) # Convertir les dicos sqlite3 en dico classiques python
cur.close()
conn.close()
return a
def drawcenter(game,surface,posx,posy):
# Dessine une surface depuis son centre, et non son coin en haut à gauche
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):
words = [word.split(' ') for word in text.splitlines()] # Liste qui contient tout les mots
colors = {"[orange]":[202,67,0],"[violet]":[202,0,202],
"[vert]":[33,202,0],"[rouge]":[246,0,0],"[bleu]":[0,0,255],
"[cyan]":[0,200,200]
}
''' contact : Orange
distance : Violet
taunt : Bleu
soin : Vert
attaque : Rouge
joueur : Cyan
'''
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:
col = color
for j in colors.keys():
if word.find(j)==0:
col = colors[j]
word = word[word.find(j)+len(j):]
word_surface = font.render(word,False,col)
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(): # Mannière de gerer les timers pour des espaces de temps flottants
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 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
# Je crée le dossier s'il n'existe pas
try:
os.mkdir(path)
except:
pass
return path