PinmikPanik/gamedata/definitions.py

71 lines
2.3 KiB
Python

from random import *
import sys,os,math,shutil,json
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