Déplacements en 2D et collisions de grille

This commit is contained in:
theo@manjaro 2021-06-25 18:29:57 +02:00
parent 92e3ac2a25
commit 8a75f09575
5 changed files with 100 additions and 7 deletions

BIN
gamedata/assets/empty.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 B

View File

@ -2,6 +2,97 @@ from gamedata.objects.base import BaseObject
class Player(BaseObject):
def __init__(self,x,y,game):
self.sprite = game.sprite_lib["icon.png"]
super().__init__(x,y,game,self.sprite.get_width(),self.sprite.get_height())
tileset = game.gameloop.findname("TilesetRenderer")[0]
self.grid = tileset.collisiongrid
self.tilew = tileset.tilew
self.tileh = tileset.tileh
def step(self):
keys = self.game.inputs["keys"]
self.rect[0]+=keys["right"]["pressed"]-keys["left"]["pressed"]
hor=keys["right"]["pressed"]-keys["left"]["pressed"]
ver=keys["down"]["pressed"]-keys["up"]["pressed"]
self.move(hor*300*self.game.dt,ver*300*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"]
return result
def move(self,hor,ver):
if hor>0:
colpoints = ["ur","dr"]
elif hor<0:
colpoints = ["ul","dl"]
hstoped = False
while abs(hor)>0:
if abs(hor)>self.tilew:
diffx = self.tilew*abs(hor)/hor
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:
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
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:
ver-=diffy
self.rect[1]+=diffy
else:
vstoped = True
ver = 0

View File

@ -9,11 +9,12 @@ class TilesetRenderer(BaseObject):
self.tilew = 64
self.tileh = 64
self.sprite = game.sprite_lib["solid.png"]
self.solid = game.sprite_lib["solid.png"]
self.empty = game.sprite_lib["empty.png"]
self.collisiongrid = [
[0,0,0,0,0,0,0],
[0,0,0,0,1,0,0],
[1,0,0,0,1,0,0],
[1,1,0,0,0,0,0],
[1,1,0,0,0,0,0],
[1,1,0,0,0,1,1],
@ -26,5 +27,5 @@ class TilesetRenderer(BaseObject):
for x in range(len(self.collisiongrid[y])):
data = self.collisiongrid[y][x]
if data==1:
self.game.window.blit(self.sprite,(self.rect[0]+x*self.tilew,self.rect[1]+y*self.tileh))
sprites = [self.empty,self.solid]
self.game.window.blit(sprites[data],(self.rect[0]+x*self.tilew,self.rect[1]+y*self.tileh))

View File

@ -19,6 +19,7 @@ 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

View File

@ -16,10 +16,10 @@ def main(game):
def fight(game):
game.gameloop.reinit()
p1 = Player(50,50,game)
tileset = TilesetRenderer(0,0,game)
game.gameloop.summon(p1)
game.gameloop.summon(tileset)
p1 = Player(70,50,game)
game.gameloop.summon(p1)
def options(game):
game.gameloop.reinit()