Refonte des collisions

This commit is contained in:
theo@manjaro 2021-07-01 13:54:56 +02:00
parent b76e3b45d6
commit 4d89036f66
3 changed files with 69 additions and 96 deletions

View File

@ -2,7 +2,7 @@ import pygame
class BaseObject():
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.game = game
self.sprite = game.sprite_lib["icon.png"]

View File

@ -15,125 +15,99 @@ class Player(BaseObject):
self.speed = 300
self.gravity = 800
self.maxgrav = 400
self.jump = -400
self.vertical = self.maxgrav
self.gravity = 1500
self.maxgravity = 800
self.jump = -600
self.vertical = self.maxgravity
self.collisionrects = self.getrects()
self.hrest = 0
self.vrest = 0
def step(self):
keys = self.game.inputs["keys"]
hor=keys["right"]["pressed"]-keys["left"]["pressed"]
# Check if on ground
# Si je suis sur le sol
self.onground = False
targetpos = self.getgridpos(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
break
# 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 = min(self.maxgrav,self.vertical)
if self.checkcollisions(0,1):
self.onground = True
self.vertical=0
else:
if keys["up"]["timer"]==1:
self.vertical = self.jump
else:
self.vertical = 0
self.vertical+=self.gravity*self.game.dt
self.vertical = min(self.maxgravity,self.vertical)
if self.onceiling and self.vertical < 0:
self.vertical = 0
self.jumped = False
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)
if self.jumped:
print(self.vertical*self.game.dt)
def getgridpos(self,modx=0,mody=0):
result = {}
result["ul"] = {}
result["ul"]["y"] = int((self.rect[1]+mody)/self.tileh)
result["ul"]["x"] = int((self.rect[0]+modx)/self.tilew)
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"]
def getrects(self):
result = []
for y in range(len(self.grid)):
for x in range(len(self.grid[y])):
if self.grid[y][x]>0:
result.append(self.game.pygame.Rect([x*self.tilew,y*self.tileh,self.tilew,self.tileh]))
return result
def move(self,hor,ver):
if hor>0:
colpoints = ["ur","dr"]
elif hor<0:
colpoints = ["ul","dl"]
def move(self,movex,movey):
hstoped = False
hor = int(movex+self.hrest)
self.hrest = movex+self.hrest-hor
while abs(hor)>0:
if abs(hor)>self.tilew:
diffx = self.tilew*abs(hor)/hor
if hor>=self.tilew:
diffx = self.tilew
elif hor<=-self.tilew:
diffx = -self.tilew
else:
diffx = hor
newpos = self.getgridpos(diffx,0) # Future position
canmove = True
upy = newpos[colpoints[0]]["y"]
downy = newpos[colpoints[1]]["y"]
for posy in range(upy,downy+1): # Je vérifie qu'elle n'est pas en colision
x = newpos[colpoints[0]]["x"]
if posy>=0 and posy < len(self.grid):
if x>=0 and x < len(self.grid[posy]):
if self.grid[posy][x]==1:
canmove = False
break
if canmove:
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
else:
hstoped = True
hor = 0
if ver>0:
colpoints = ["dl","dr"]
elif ver<0:
colpoints = ["ul","ur"]
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
newpos = self.getgridpos(0,diffy) # Future position
canmove = True
leftx = newpos[colpoints[0]]["x"]
rightx = newpos[colpoints[1]]["x"]
for posx in range(leftx,rightx+1): # Je vérifie qu'elle n'est pas en colision
y = newpos[colpoints[0]]["y"]
if y>=0 and y<len(self.grid):
if posx>=0 and posx<len(self.grid[y]):
if self.grid[y][posx]==1:
canmove = False
break
if canmove:
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
else:
vstoped = True
ver = 0
def checkcollisions(self,offx,offy):
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

View File

@ -19,7 +19,6 @@ class GameLoop():
# Retournes tout les objets portants ce nom
result = []
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
result.append(i)
return result