Hi, so to make the player blocked by the wall you need:
1- a collision function. I suggest you use mine; it works. Add it to your code. You can use it by saying checkCollision(obj1,obj2)
whith the two object you want to collide with. Here it is.
return o1.x - o1.w/2 < o2.x + o2.w/2 and
o1.x + o1.w/2 > o2.x - o2.w/2 and
o1.y - o1.h/2 < o2.y + o2.h/2 and
o1.y + o1.h/2 > o2.y - o2.h/2
end
2- Secondly, you need logic that blocks the player when they collide with another player. Hmm... looking at your code, I suggest creating variables that save the player's previous position to block them if there's a collision. Let's see...
Ah, I've found the perfect logic for you.
Here I'm using an object, but if you want more options, use a class. Here's an example based on your code.
//collide function
checkCollision = function(o1, o2)
return o1.x - o1.w/2 < o2.x + o2.w/2 and
o1.x + o1.w/2 > o2.x - o2.w/2 and
o1.y - o1.h/2 < o2.y + o2.h/2 and
o1.y + o1.h/2 > o2.y - o2.h/2
end
init = function()
player = object
x = 0
y = 0
w = 30
h = 30 /*
You need to have size variables for my function to work.
like w (for the width)
and
h(for height)
*/
oldX = 0
oldY = 0
color = "rgb(255,170,0)"
color2 ="rgb(147,98,0)"
speed = 3
collide = false
end
wall = object
x = -100
y = -20
w = 70
h = 70
/* The same goes for your wall; it must also have size variables.
*/
end
end
update = function()
/*We will connect the oldX to the player.x.
And the same for y; they will be like variables that save the previous position of your player.
*/
player.oldX = player.x
player.oldY = player.y
//mouvement logique
if keyboard.UP then
player.y += player.speed
elsif keyboard.DOWN then
player.y -= player.speed
elsif keyboard.LEFT then
player.x -= player.speed
elsif keyboard.RIGHT then
player.x += player.speed
end
//the function of the solid collision with the wall
if checkCollision(player,wall) then
player.collide = true
else
player.collide = false
end
if player.collide then
player.x = player.oldX
player.y = player.oldY
end
end
draw = function()
screen.clear("rgb(0,170,255)")
// Wall
screen.fillRect(wall.x,wall.y,wall.w,wall.h,"rgb(141,141,141)")
screen.drawRect(wall.x,wall.y,wall.w,wall.h,"rgb(97,97,97)")
// Player
screen.fillRect(player.x,player.y,player.w,player.h,player.color)
screen.drawRect(player.x,player.y,player.w,player.h,player.color2)
end
I think it should work. If you have any other questions, let me know. Good luck with your horror game! ^o^