Refonte des collisions
This commit is contained in:
parent
b76e3b45d6
commit
4d89036f66
@ -2,7 +2,7 @@ import pygame
|
|||||||
|
|
||||||
class BaseObject():
|
class BaseObject():
|
||||||
def __init__(self,x,y,game,w=10,h=10):
|
def __init__(self,x,y,game,w=10,h=10):
|
||||||
self.rect = [x,y,w,h]
|
self.rect = pygame.Rect([x,y,w,h])
|
||||||
self.baserect = self.rect.copy()
|
self.baserect = self.rect.copy()
|
||||||
self.game = game
|
self.game = game
|
||||||
self.sprite = game.sprite_lib["icon.png"]
|
self.sprite = game.sprite_lib["icon.png"]
|
||||||
|
@ -15,125 +15,99 @@ class Player(BaseObject):
|
|||||||
|
|
||||||
self.speed = 300
|
self.speed = 300
|
||||||
|
|
||||||
self.gravity = 800
|
self.gravity = 1500
|
||||||
self.maxgrav = 400
|
self.maxgravity = 800
|
||||||
self.jump = -400
|
self.jump = -600
|
||||||
self.vertical = self.maxgrav
|
self.vertical = self.maxgravity
|
||||||
|
|
||||||
|
self.collisionrects = self.getrects()
|
||||||
|
|
||||||
|
self.hrest = 0
|
||||||
|
self.vrest = 0
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
keys = self.game.inputs["keys"]
|
keys = self.game.inputs["keys"]
|
||||||
hor=keys["right"]["pressed"]-keys["left"]["pressed"]
|
hor=keys["right"]["pressed"]-keys["left"]["pressed"]
|
||||||
|
|
||||||
# Check if on ground
|
# Si je suis sur le sol
|
||||||
self.onground = False
|
self.onground = False
|
||||||
targetpos = self.getgridpos(0,1)
|
if self.checkcollisions(0,1):
|
||||||
pos = [targetpos["dl"],targetpos["dr"]]
|
|
||||||
for x in range(pos[0]["x"],pos[1]["x"]+1):
|
|
||||||
if pos[0]["y"]>=0 and pos[0]["y"]<len(self.grid):
|
|
||||||
if x>=0 and x<len(self.grid[pos[0]["y"]]):
|
|
||||||
if self.grid[pos[0]["y"]][x]==1:
|
|
||||||
self.onground = True
|
self.onground = True
|
||||||
break
|
self.vertical=0
|
||||||
|
else:
|
||||||
# Check if under ceiling
|
|
||||||
self.onceiling = False
|
|
||||||
targetpos = self.getgridpos(0,-1)
|
|
||||||
pos = [targetpos["ul"],targetpos["ur"]]
|
|
||||||
for x in range(pos[0]["x"],pos[1]["x"]+1):
|
|
||||||
if pos[0]["y"]>=0 and pos[0]["y"]<len(self.grid):
|
|
||||||
if x>=0 and x<len(self.grid[pos[0]["y"]]):
|
|
||||||
if self.grid[pos[0]["y"]][x]==1:
|
|
||||||
self.onceiling= True
|
|
||||||
break
|
|
||||||
|
|
||||||
if not self.onground:
|
|
||||||
self.vertical+=self.gravity*self.game.dt
|
self.vertical+=self.gravity*self.game.dt
|
||||||
self.vertical = min(self.maxgrav,self.vertical)
|
self.vertical = min(self.maxgravity,self.vertical)
|
||||||
else:
|
|
||||||
if keys["up"]["timer"]==1:
|
|
||||||
self.vertical = self.jump
|
|
||||||
else:
|
|
||||||
self.vertical = 0
|
|
||||||
|
|
||||||
if self.onceiling and self.vertical < 0:
|
self.jumped = False
|
||||||
self.vertical = 0
|
if self.onground:
|
||||||
|
if 0<keys["up"]["timer"]<=3:
|
||||||
|
self.vertical = self.jump
|
||||||
|
self.jumped = True
|
||||||
|
|
||||||
self.move(hor*self.speed*self.game.dt,self.vertical*self.game.dt)
|
self.move(hor*self.speed*self.game.dt,self.vertical*self.game.dt)
|
||||||
|
if self.jumped:
|
||||||
|
print(self.vertical*self.game.dt)
|
||||||
|
|
||||||
def getgridpos(self,modx=0,mody=0):
|
def getrects(self):
|
||||||
result = {}
|
result = []
|
||||||
|
for y in range(len(self.grid)):
|
||||||
result["ul"] = {}
|
for x in range(len(self.grid[y])):
|
||||||
result["ul"]["y"] = int((self.rect[1]+mody)/self.tileh)
|
if self.grid[y][x]>0:
|
||||||
result["ul"]["x"] = int((self.rect[0]+modx)/self.tilew)
|
result.append(self.game.pygame.Rect([x*self.tilew,y*self.tileh,self.tilew,self.tileh]))
|
||||||
|
|
||||||
result["ur"] = {}
|
|
||||||
result["ur"]["y"] = result["ul"]["y"]
|
|
||||||
result["ur"]["x"] = int((self.rect[0]+self.rect[2]+modx)/self.tilew)
|
|
||||||
|
|
||||||
result["dl"] = {}
|
|
||||||
result["dl"]["y"] = int((self.rect[1]+self.rect[3]+mody)/self.tileh)
|
|
||||||
result["dl"]["x"] = result["ul"]["x"]
|
|
||||||
|
|
||||||
result["dr"] = {}
|
|
||||||
result["dr"]["y"] = result["dl"]["y"]
|
|
||||||
result["dr"]["x"] = result["ur"]["x"]
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def move(self,hor,ver):
|
def move(self,movex,movey):
|
||||||
if hor>0:
|
|
||||||
colpoints = ["ur","dr"]
|
|
||||||
elif hor<0:
|
|
||||||
colpoints = ["ul","dl"]
|
|
||||||
hstoped = False
|
hstoped = False
|
||||||
|
hor = int(movex+self.hrest)
|
||||||
|
self.hrest = movex+self.hrest-hor
|
||||||
while abs(hor)>0:
|
while abs(hor)>0:
|
||||||
if abs(hor)>self.tilew:
|
if hor>=self.tilew:
|
||||||
diffx = self.tilew*abs(hor)/hor
|
diffx = self.tilew
|
||||||
|
elif hor<=-self.tilew:
|
||||||
|
diffx = -self.tilew
|
||||||
else:
|
else:
|
||||||
diffx = hor
|
diffx = hor
|
||||||
newpos = self.getgridpos(diffx,0) # Future position
|
rect = self.checkcollisions(diffx,0)
|
||||||
canmove = True
|
if rect:
|
||||||
upy = newpos[colpoints[0]]["y"]
|
hor = 0
|
||||||
downy = newpos[colpoints[1]]["y"]
|
self.hrest = 0
|
||||||
for posy in range(upy,downy+1): # Je vérifie qu'elle n'est pas en colision
|
hstopped = True
|
||||||
x = newpos[colpoints[0]]["x"]
|
if diffx>0:
|
||||||
if posy>=0 and posy < len(self.grid):
|
self.rect.right = rect.left
|
||||||
if x>=0 and x < len(self.grid[posy]):
|
else:
|
||||||
if self.grid[posy][x]==1:
|
self.rect.left = rect.right
|
||||||
canmove = False
|
else:
|
||||||
break
|
|
||||||
if canmove:
|
|
||||||
hor-=diffx
|
hor-=diffx
|
||||||
self.rect[0]+=diffx
|
self.rect[0]+=diffx
|
||||||
else:
|
|
||||||
hstoped = True
|
|
||||||
hor = 0
|
|
||||||
|
|
||||||
if ver>0:
|
|
||||||
colpoints = ["dl","dr"]
|
|
||||||
elif ver<0:
|
|
||||||
colpoints = ["ul","ur"]
|
|
||||||
vstoped = False
|
vstoped = False
|
||||||
|
ver = int(movey+self.vrest)
|
||||||
|
self.vrest = movey+self.vrest-ver
|
||||||
while abs(ver)>0:
|
while abs(ver)>0:
|
||||||
if abs(ver)>self.tileh:
|
if abs(ver)>self.tileh:
|
||||||
diffy = self.tileh*abs(ver)/ver
|
diffy = self.tileh*abs(ver)/ver
|
||||||
else:
|
else:
|
||||||
diffy = ver
|
diffy = ver
|
||||||
newpos = self.getgridpos(0,diffy) # Future position
|
rect = self.checkcollisions(0,diffy)
|
||||||
canmove = True
|
if rect:
|
||||||
leftx = newpos[colpoints[0]]["x"]
|
vstopped = True
|
||||||
rightx = newpos[colpoints[1]]["x"]
|
ver = 0
|
||||||
for posx in range(leftx,rightx+1): # Je vérifie qu'elle n'est pas en colision
|
self.vrest = 0
|
||||||
y = newpos[colpoints[0]]["y"]
|
if diffy>0:
|
||||||
if y>=0 and y<len(self.grid):
|
self.rect.bottom = rect.top
|
||||||
if posx>=0 and posx<len(self.grid[y]):
|
else:
|
||||||
if self.grid[y][posx]==1:
|
self.rect.top = rect.bottom
|
||||||
canmove = False
|
else:
|
||||||
break
|
|
||||||
if canmove:
|
|
||||||
ver-=diffy
|
ver-=diffy
|
||||||
self.rect[1]+=diffy
|
self.rect[1]+=diffy
|
||||||
else:
|
|
||||||
vstoped = True
|
def checkcollisions(self,offx,offy):
|
||||||
ver = 0
|
temprect = self.rect.copy()
|
||||||
|
temprect[0]+=offx
|
||||||
|
temprect[1]+=offy
|
||||||
|
result = False
|
||||||
|
for rect in self.collisionrects:
|
||||||
|
if temprect.colliderect(rect):
|
||||||
|
result = rect
|
||||||
|
break
|
||||||
|
return result
|
||||||
|
@ -19,7 +19,6 @@ class GameLoop():
|
|||||||
# Retournes tout les objets portants ce nom
|
# Retournes tout les objets portants ce nom
|
||||||
result = []
|
result = []
|
||||||
for i in self.objects.values():
|
for i in self.objects.values():
|
||||||
print(type(i).__name__)
|
|
||||||
if type(i).__name__==name: # Si le nom de la classe correspond au nom donné, je retourne la classe
|
if type(i).__name__==name: # Si le nom de la classe correspond au nom donné, je retourne la classe
|
||||||
result.append(i)
|
result.append(i)
|
||||||
return result
|
return result
|
||||||
|
Loading…
Reference in New Issue
Block a user