How do I create a Snake like trail without Arrays

0 favourites
  • 7 posts
From the Asset Store
In this template the music plays without looping in your game
  • I'm working on a game that has a function similar to that of the classic game snake.

    The players tail grows as their level increases and follows them around (player movement is in 4 directions).

    I'm not sure how to get the location of the players old coordinates - as in where the player "was" 0.2 seconds ago and set the object to follow in that same pattern of movement. The tail does not need to be made up of parts but instead can be one object that gets stretched as the players level increases.

    Does anyone have any ideas? I'd like to not use arrays if possible - I'd like to understand it without.

  • I haven't made a snake game but my thinking is that you might want to try looking into setting an image point outside of your snake head, and additional appendages

    You can have a global variable equaling he UID of each appendage as it is created and use that to give a reference point for spawning new appendages (Pick Appendige by UID=global variable).

    Each appendage sprite could have its own variable for UID that could be set the the UID of the *spawning* appendage and used to set it's position to the aforementioned image point.

    This is all just me thinking off the top of my head so let me know if this is helpful haha.

    Otherwise, I think that if you've found a tutorial that uses arrays, I'd really invest time into getting a handle on it. Arrays are tricky but very useful.

  • ultrafop I appreciate the help - but the tail wouldn't leave a trail that way.. the tail would change instantly depending on the rotation of the player.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hmmm I suppose you could have a for each condition that lerps the current x,y to the previous UID's X Y in the chain? but either way, I'm rereading your original post it it looks like you want a solid body. I'd experiment with having one appendage that spawns a duplicate sprite that has a fade effect on t. As the number of appendages increases you can increase the number of instances being spawned/adjust the fade time and see if that doesn't get you what you're looking for.

  • You could do the following. Store the x,y position of the sprites into private variables. Then move the head or first sprite in whatever direction you like. And finally just set the other sprite's position to the saved position of the previous one.

    every tick

    --- sprite: set oldx to self.x

    --- sprite: set oldy to self.y

    pick sprite instance 0

    --- sprite: move forward 32 pixels

    pick sprite by comparison: sprite.iid>0

    --- sprite: set position to (sprite(self.iid-1).oldx, sprite(self.iid-1).oldy)

  • For the record, there should be a thread consisting purely of R0J0hound 's posts.

  • Since the op mentioned wanting multiple snakes the above would only work if each snake were a different object type. Here's a go at using a for loop to do it. In a way it looks a lot cleaner to me.

    https://dl.dropboxusercontent.com/u/542 ... nakes.capx

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)