This commit is contained in:
theo@manjaro 2021-07-08 16:20:22 +02:00
parent 5f4d490f07
commit b8285755ae
4 changed files with 178 additions and 115 deletions

View File

@ -97,6 +97,8 @@ class Timer(): # Mannière de gerer les timers pour des espaces de temps flottan
return self.maxcount return self.maxcount
def getratio(self): def getratio(self):
return max(min(1,self.timer/self.maxcount),0) return max(min(1,self.timer/self.maxcount),0)
def reset(self):
self.timer = self.maxcount
def add(self,amount): def add(self,amount):
self.timer+=amount self.timer+=amount
self.maxcount+=amount self.maxcount+=amount

View File

@ -139,36 +139,43 @@ class Game():
self.inputs["keys"] = { self.inputs["keys"] = {
"escape":{ "escape":{
"timer" : 0, # Timer de la touche "timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False, "pressed" : False,
"keycode" : pygame.K_ESCAPE # Code pygame de la touche en question "keycode" : pygame.K_ESCAPE # Code pygame de la touche en question
}, },
"backspace":{ "backspace":{
"timer" : 0, # Timer de la touche "timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False, "pressed" : False,
"keycode" : pygame.K_BACKSPACE # Code pygame de la touche en question "keycode" : pygame.K_BACKSPACE # Code pygame de la touche en question
}, },
"enter":{ "enter":{
"timer" : 0, # Timer de la touche "timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False, "pressed" : False,
"keycode" : pygame.K_RETURN # Code pygame de la touche en question "keycode" : pygame.K_RETURN # Code pygame de la touche en question
}, },
"left":{ "left":{
"timer" : 0, # Timer de la touche "timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False, "pressed" : False,
"keycode" : pygame.K_q # Code pygame de la touche en question "keycode" : pygame.K_q # Code pygame de la touche en question
}, },
"right":{ "right":{
"timer" : 0, # Timer de la touche "timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False, "pressed" : False,
"keycode" : pygame.K_d # Code pygame de la touche en question "keycode" : pygame.K_d # Code pygame de la touche en question
}, },
"up":{ "up":{
"timer" : 0, # Timer de la touche "timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False, "pressed" : False,
"keycode" : pygame.K_z # Code pygame de la touche en question "keycode" : pygame.K_z # Code pygame de la touche en question
}, },
"down":{ "down":{
"timer" : 0, # Timer de la touche "timer" : 0, # Timer de la touche
"realtime" : 0, # Temps réel préssé
"pressed" : False, "pressed" : False,
"keycode" : pygame.K_s # Code pygame de la touche en question "keycode" : pygame.K_s # Code pygame de la touche en question
} }
@ -202,8 +209,10 @@ class Game():
for i in self.inputs["keys"].keys(): for i in self.inputs["keys"].keys():
if self.inputs["keys"][i]["pressed"]: if self.inputs["keys"][i]["pressed"]:
self.inputs["keys"][i]["timer"]+=1 self.inputs["keys"][i]["timer"]+=1
self.inputs["keys"][i]["realtime"]+=self.dt
else: else:
self.inputs["keys"][i]["timer"]=0 self.inputs["keys"][i]["timer"]=0
self.inputs["keys"][i]["realtime"]=0
# Timer du click # Timer du click
boutons = pygame.mouse.get_pressed() boutons = pygame.mouse.get_pressed()
if boutons[0]: # Si click gauche if boutons[0]: # Si click gauche

View File

@ -0,0 +1,114 @@
from gamedata.objects.base import BaseObject
class Movable(BaseObject):
def __init__(self,game,x,y):
self.sprite = game.sprite_lib["icon.png"]
self.tileset = game.gameloop.findname("TilesetRenderer")[0]
super().__init__(x,y,game,self.sprite.get_width(),self.sprite.get_height())
self.collisionrects = self.tileset.rects
self.semirects = self.tileset.semirects
self.tilew = self.tileset.solidtilew
self.tileh = self.tileset.solidtileh
self.horspd = 0
self.verspd = 0
self.currentspdh = 0
self.currentspdv = 0
self.hrest = 0
self.vrest = 0
self.dechorentropy = 10
self.acchorentropy = 15
self.decverentropy = 100
self.accverentropy = 25
def step(self):
self.entropy()
self.move(self.currentspdh,self.currentspdv)
def entropy(self):
# Horizontal
if abs(self.currentspdh)>abs(self.horspd): # Décelleration
self.currentspdh += (self.horspd - self.currentspdh)*self.game.dt*self.dechorentropy
else:
self.currentspdh += (self.horspd - self.currentspdh)*self.game.dt*self.acchorentropy
# Vertical
if abs(self.currentspdv)>abs(self.verspd): # Décelleration
self.currentspdv += (self.verspd - self.currentspdv)*self.game.dt*self.decverentropy
else: # Acceleration
self.currentspdv += (self.verspd - self.currentspdv)*self.game.dt*self.accverentropy
def move(self,movex,movey):
hstoped = False
hor = int(movex+self.hrest)
self.hrest = movex+self.hrest-hor
while abs(hor)>0:
if hor>=self.tilew:
diffx = self.tilew
elif hor<=-self.tilew:
diffx = -self.tilew
else:
diffx = hor
rect = self.checkcollisions(diffx,0)
if rect:
hor = 0
self.hrest = 0
hstopped = True
if diffx>0:
self.rect.right = rect.left
else:
self.rect.left = rect.right
else:
hor-=diffx
self.rect[0]+=diffx
vstoped = False
ver = int(movey+self.vrest)
self.vrest = movey+self.vrest-ver
while abs(ver)>0:
if abs(ver)>self.tileh:
diffy = self.tileh*abs(ver)/ver
else:
diffy = ver
rect = self.checkcollisions(0,diffy)
if rect:
vstopped = True
ver = 0
self.vrest = 0
if diffy>0:
self.rect.bottom = rect.top
else:
self.rect.top = rect.bottom
else:
ver-=diffy
self.rect[1]+=diffy
def checkcollisions(self,offx,offy,classic=True,semi=True):
temprect = self.rect.copy()
temprect[0]+=offx
temprect[1]+=offy
result = False
if classic:
for rect in self.collisionrects:
if temprect.colliderect(rect):
result = rect
break
if not result and semi:
for rect in self.semirects:
if self.rect.bottom<=rect.top:
if temprect.colliderect(rect):
result = rect
break
return result
def draw(self):
self.game.window.blit(self.sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])

View File

@ -1,136 +1,74 @@
from gamedata.objects.base import BaseObject from gamedata.objects.combat.movable import Movable
class Player(BaseObject): class Player(Movable):
def __init__(self,game): def __init__(self,game):
super().__init__(game,0,0)
spawnpoint = game.lib.choice(self.tileset.spawns)
self.rect.move_ip(spawnpoint.center)
self.sprite = game.sprite_lib["icon.png"] self.sprite = game.sprite_lib["icon.png"]
tileset = game.gameloop.findname("TilesetRenderer")[0] self.speed = 200
spawnpoint = game.lib.choice(tileset.spawns) self.fastfallmargin = 2
x,y = spawnpoint.center
super().__init__(x,y,game,self.sprite.get_width(),self.sprite.get_height()) self.hitpose = False
self.hitposeduration = 0.5
self.collisionrects = tileset.rects self.hitposetimer = game.lib.Timer(self.hitposeduration)
self.semirects = tileset.semirects
self.tilew = tileset.solidtilew
self.tileh = tileset.solidtileh
self.speed = 300
self.stepsize = 20 self.stepsize = 20
self.gravity = 1500 self.gravity = 15
self.maxgravity = 800 self.maxgravity = 80
self.jump = -600 self.jump = -5
self.vertical = self.maxgravity self.verspd = self.maxgravity
self.hrest = 0
self.vrest = 0
def step(self): def step(self):
if not self.hitpose:
keys = self.game.inputs["keys"] keys = self.game.inputs["keys"]
hor=keys["right"]["pressed"]-keys["left"]["pressed"] self.horspd=(keys["right"]["pressed"]-keys["left"]["pressed"])*self.game.dt*self.speed
# Si je suis sur le sol # Si je suis sur le sol
self.onground = False self.onground = False
if self.checkcollisions(0,1): if self.checkcollisions(0,1):
self.onground = True self.onground = True
self.vertical=min(0,self.vertical) self.verspd=min(0,self.verspd)
else: else:
self.vertical+=self.gravity*self.game.dt self.verspd+=self.gravity*self.game.dt
self.vertical = min(self.maxgravity,self.vertical) self.verspd= min(self.maxgravity,self.verspd)
if self.checkcollisions(0,-1): if self.checkcollisions(0,-1):
self.vertical = max(0,self.vertical) # Se cogne au plafond self.verspd= max(0,self.verspd) # Se cogne au plafond BONK
self.jumped = False self.jumped = False
if self.onground: if self.onground:
if 0<keys["up"]["timer"]<=3: if 0<keys["up"]["timer"]<=3:
self.vertical = self.jump self.verspd= self.jump
self.jumped = True self.jumped = True
if self.verspd==0:
if self.vertical==0:
self.move(0,-self.stepsize) self.move(0,-self.stepsize)
self.move(hor*self.speed*self.game.dt,self.vertical*self.game.dt) super().step()
self.move(0,self.stepsize) self.move(0,self.stepsize)
else: else:
self.move(hor*self.speed*self.game.dt,self.vertical*self.game.dt) super().step()
# Je passe en dessous des semi-plateformes # Je passe en dessous des semi-plateformes
if keys["down"]["timer"]==1:
if not self.checkcollisions(0,1,semi=False): if not self.checkcollisions(0,1,semi=False):
semi = self.checkcollisions(0,1,classic=False) semi = self.checkcollisions(0,1,classic=False)
if semi!=self.checkcollisions(0,0,classic=False) and semi: if semi!=self.checkcollisions(0,0,classic=False) and semi:
if keys["down"]["timer"]==1:
self.rect.bottom = semi.top+1 self.rect.bottom = semi.top+1
if 0<self.verspd <= self.fastfallmargin or (abs(self.verspd)<abs(self.currentspdv) and abs(self.verspd)<=self.fastfallmargin/1.8):
self.verspd = self.maxgravity/4
else:
if self.hitposetimer.tick(self.game.dt):
self.hitpose = False
self.game.globals["camerax"] = self.rect.center[0]-self.game.globals["cameraw"]/2 self.game.globals["camerax"] = self.rect.center[0]-self.game.globals["cameraw"]/2
self.game.globals["cameray"] = self.rect.center[1]-self.game.globals["camerah"]/2 self.game.globals["cameray"] = self.rect.center[1]-self.game.globals["camerah"]/2
def move(self,movex,movey):
hstoped = False
hor = int(movex+self.hrest)
self.hrest = movex+self.hrest-hor
while abs(hor)>0:
if hor>=self.tilew:
diffx = self.tilew
elif hor<=-self.tilew:
diffx = -self.tilew
else:
diffx = hor
rect = self.checkcollisions(diffx,0)
if rect:
hor = 0
self.hrest = 0
hstopped = True
if diffx>0:
self.rect.right = rect.left
else:
self.rect.left = rect.right
else:
hor-=diffx
self.rect[0]+=diffx
vstoped = False
ver = int(movey+self.vrest)
self.vrest = movey+self.vrest-ver
while abs(ver)>0:
if abs(ver)>self.tileh:
diffy = self.tileh*abs(ver)/ver
else:
diffy = ver
rect = self.checkcollisions(0,diffy)
if rect:
vstopped = True
ver = 0
self.vrest = 0
if diffy>0:
self.rect.bottom = rect.top
else:
self.rect.top = rect.bottom
else:
ver-=diffy
self.rect[1]+=diffy
def checkcollisions(self,offx,offy,classic=True,semi=True):
temprect = self.rect.copy()
temprect[0]+=offx
temprect[1]+=offy
result = False
if classic:
for rect in self.collisionrects:
if temprect.colliderect(rect):
result = rect
break
if not result and semi:
for rect in self.semirects:
if self.rect.bottom<=rect.top:
if temprect.colliderect(rect):
result = rect
break
return result
def draw(self): def draw(self):
self.game.window.blit(self.sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]]) self.game.window.blit(self.sprite,[self.rect[0]-self.game.globals["camerax"],self.rect[1]-self.game.globals["cameray"]])