07-21-2024, 05:39 PM
I don't understand the code very well, so I have prepared a small example to use as a basis for testing.
In the example I have the player who is the head of the snake and I have the body which is the tail, the player moves with the keys and the body I have added to a list and I show three bodies at the moment. How do I make the body is in the player's position and moves with him like the tail of the snake.
In the example I have the player who is the head of the snake and I have the body which is the tail, the player moves with the keys and the body I have added to a list and I show three bodies at the moment. How do I make the body is in the player's position and moves with him like the tail of the snake.
Code:
'prueba de cola de serpiente
'variables-----------------------
visible player,list_body
set window "serpiente",640,480,false
set redraw off
'player-----------------------------
function Player()
obj = []
obj.radius = 14
obj.x = 320 - obj.radius/2
obj.y = 240 - obj.radius/2
obj.speedX = 0
obj.speedY = 0
obj.Active = true
obj.update = function()
if this.Active = true
this.move()
this.Draw()
endif
endfunc
obj.Draw = function()
set color 255,204,0
draw ellipse this.x,this.y,this.radius,this.radius,true
endfunc
obj.move = function()
if keydown(KEY_RIGHT,false)
this.speedX = 2
this.speedY = 0
endif
if keydown(KEY_LEFT,false)
this.speedX = -2
this.speedY = 0
endif
if keydown(KEY_UP,false)
this.speedX = 0
this.speedY = -2
endif
if keydown(KEY_DOWN,false)
this.speedX = 0
this.speedY = 2
endif
this.x = this.x + this.speedX
this.y = this.y + this.speedY
endfunc
return obj
endfunc
'end player--------------------------
'body--------------------------------
function Body(x,y)
obj = []
obj.radius = 10
obj.x = x - obj.radius/2
obj.y = y - obj.radius/2
obj.update = function()
this.Draw()
endfunc
obj.Draw = function()
set color 255,0,204
draw ellipse this.x,this.y,this.radius,this.radius,true
endfunc
return obj
endfunc
'end body----------------------------
'iniciar variables-------------------
player = Player()
list_body = []
list_body[sizeof(list_body)] = Body(320,64)
list_body[sizeof(list_body)] = Body(320-32,64)
list_body[sizeof(list_body)] = Body(320-64,64)
while not keydown(KEY_ESCAPE,true)
set color 0,0,0
cls
'actualizar objetos del juego-----------
player.update()
if sizeof(list_body)
for i = 0 to sizeof(list_body)-1
list_body[i].update()
next
endif
'---------------------------------------
fwait 60
redraw
wend