Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about snake game
#8
(07-21-2024, 05:39 PM)aliensoldier Wrote: 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.

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

Sorry, I totally forgot about your question. I'll have a look at it right away!
Reply


Messages In This Thread
Question about snake game - by aliensoldier - 07-19-2024, 05:48 PM
RE: Question about snake game - by Marcus - 07-20-2024, 09:23 AM
RE: Question about snake game - by aliensoldier - 07-20-2024, 06:50 PM
RE: Question about snake game - by Marcus - 07-20-2024, 08:16 PM
RE: Question about snake game - by johnno56 - 07-20-2024, 10:46 AM
RE: Question about snake game - by Marcus - 07-20-2024, 12:42 PM
RE: Question about snake game - by aliensoldier - 07-21-2024, 05:39 PM
RE: Question about snake game - by Marcus - 08-08-2024, 01:53 PM
RE: Question about snake game - by Marcus - 08-08-2024, 02:56 PM
RE: Question about snake game - by aliensoldier - 08-10-2024, 12:06 PM
RE: Question about snake game - by Marcus - 09-19-2024, 02:47 PM
RE: Question about snake game - by johnno56 - 09-19-2024, 07:58 PM

Forum Jump:


Users browsing this thread: 4 Guest(s)