Thanks to Actts for his patience and help (in one of my biggest problems)
Thank you so much, Actts, for helping me with the function class. I was able to create code that will allow me to finish my game.
Thank you so much, Actts, for helping me with the function class. I was able to create code that will allow me to finish my game.
Wall = class constructor = function(x, y, lar, lon) this.x = x this.y = y this.lar = lar this.lon = lon end end
checkCollision = function(r1, r2) return r1.x < r2.x + r2.lar and r1.x + r1.lar > r2.x and r1.y < r2.y + r2.lon and r1.y + r1.lon > r2.y end
init = function() player = object x = 0 y = 0 lon = 50 lar = 50 vitesse = 3 end
walls = []
walls.push(new Wall(100, 0, 50, 50))
walls.push(new Wall(-100, 50, 50, 80))
end
// --- LOGIQUE --- update = function() local nextX = player.x local nextY = player.y
if keyboard.UP then nextY += player.vitesse end if keyboard.DOWN then nextY -= player.vitesse end if keyboard.LEFT then nextX -= player.vitesse end if keyboard.RIGHT then nextX += player.vitesse end
local potentialPlayer = object x = nextX y = nextY lar = player.lar lon = player.lon end
local canMove = true
for w in walls if checkCollision(potentialPlayer, w) then canMove = false break end end
if canMove then player.x = nextX player.y = nextY end end
// --- DESSIN --- draw = function() screen.clear("rgb(142,255,217)")
for w in walls screen.drawSprite("icon", w.x, w.y, w.lar, w.lon) end
screen.drawSprite("incon", player.x, player.y, player.lar, player.lon) end