#Snake Movement Grid

1 messages · Page 1 of 1 (latest)

uncut remnant
#

@stable oracle this the right one?

#

i dont think i can find my older code.. but i can def fix up an example real quick.. as i said earlier im really really bad with grids and creating them via forloops

#

the logic gets me everytime once it gets more advanced than actually spawning the gridtiles

uncut remnant
#

shoot

fierce echo
#

You keep the highest/lowest x value for the snake

#

Vice versa for y

uncut remnant
#

ohhh

#

i think i know what ur saying

fierce echo
#

And then every time the snake moves you have a script that spawns/kills the tiles

uncut remnant
#

like this?

#

but with the Y also

fierce echo
#

Yes

uncut remnant
#

ahh thats pretty clever

stable oracle
#

that wouldn't work i don't think

#

it doesn't store the full state of the snake

fierce echo
#

Oh yea?

uncut remnant
#

ya, thast what i kiinda thought at first.. what if it zig zagged?

fierce echo
#

Must go script for fake internet points... brb

uncut remnant
stable oracle
#

the values { length = 3, minX = 1, maxX = 3, minY = 1, minY = 3 } has 8 possible states

uncut remnant
#

like if the snake did this ^

stable oracle
#

or a spiral

uncut remnant
#

i seem to believe it would be overly complicated for to know using a single x,y vec2

stable oracle
#

if you only consider the boundaries, a snake spiraling into itself wouldn't even store the head

fierce echo
#

Oh no i was just talking about the tile spawn system

stable oracle
#

using a queue is just very simple and straightforward

uncut remnant
#

i couldnt find that project chris.. i think i deleted it.. imma start a new one here in a minute

uncut remnant
#

altho ive never used a queue other than for task management

stable oracle
stable oracle
fierce echo
uncut remnant
#

i just bought some graphing paper! what a wonderful suprise.. i can use that today in my research

#

wasn't even sure what i was gonna do with it 😅

fierce echo
#

Do people besides me actually use paper for coding?

uncut remnant
#

i use paper all the time

stable oracle
#

only when i have it on hand

uncut remnant
stable oracle
#

i drew so many scenarios for figuring out background tiling lol

uncut remnant
#

its also common for me to sketch out ideas

#

to feed to AI.. to get a better image.. that i can use as reference to model

fierce echo
#

Ai did not do justice lol

uncut remnant
#

lol.. ya i know 😦 it was a bit disappointing

#

but the best i could get it to do

#

the first iteration was even worse

#

i mean.. its a decent thumbnail i guess..

fierce echo
#

alright lemme write up a script

stable oracle
#

weird path though

uncut remnant
#

but if i feed the AI my own sketches.. i kinda eliminate some of the chances that i get regurgitated artwork from someone else

fierce echo
#

altho idk what queues are so im gonna use arrays

uncut remnant
#

its basically that ^

fierce echo
#

(the jank strikes again)

uncut remnant
#

but something about first in first out

#

or last in last out

stable oracle
#

same thing

fierce echo
#

oh i see

stable oracle
#

it's a layer right above a list

uncut remnant
#

lmao.. great example i just found of a queue

stable oracle
#

array - sequence of elements
list/vector - sequence of elements with dynamic length
queue/stack - sequence of elements with dynamic length and specified operations

uncut remnant
#

what namespace does queue use again?

stable oracle
#

the same as list

uncut remnant
#

is it the same

#

ok

#

alrighty!! nose to the grindstone!!

#

i shall update with my first attempt

#

thanks for the 1on1 guys

#

and for movement i probably also need like a stepper..
move once / so and so frames

#

so i get that one step at a time kinda feeling

fierce echo
stable oracle
#

it's not a unity thing, it's a c# thing

stable oracle
#

a list (at the very basic level) defines retrieving, setting, adding, and removing at a certain index
a stack defines adding, retreiving, and removing only at the end
a queue defines adding only at the end, and retrieving and removing only at the start

uncut remnant
#

heres a doc page about queues

fierce echo
#

thats what i ment sorry

uncut remnant
stable oracle
uncut remnant
#

ya im on it

stable oracle
uncut remnant
stable oracle
#

Represents a first-in, first-out collection of objects.
this is, at its core, what a queue is

  • it's a collection
  • it holds objects
  • it defines in and out operations
  • the first thing you in is the first thing you get from out
uncut remnant
#

snake movement has been on my todo list for years now.. it'd be great to finally prototype it out

#

oh a question for u chris..

#

when u do grids are they power of 2?

fierce echo
uncut remnant
#

or do u do odd number to have a center row

stable oracle
#

i.. didn't put any thought into it

uncut remnant
#

ive tried both and i honestly cant tell which is the preferred method

stable oracle
#

i just did 10x10 and called it a day

uncut remnant
#

okay.. sounds good to me 👍

stable oracle
#

it doesn't really matter for a for-fun project anyways

uncut remnant
#

true true

#

i just like to keep my grids centered..

#

pet peeve of mine

stable oracle
#

also this is kinda funny...

#

very disappointed my search filter extension stopped working

uncut remnant
stable oracle
#

google search engine

uncut remnant
#

ohh.. super minimilistic

stable oracle
#

what browser doesn't look like this

uncut remnant
#

alright..
Step1. Create a Grid (i shall return)

stable oracle
#

-# narrator: he never returned

uncut remnant
#

nah, wont take me 10 minutes to make a grid

stable oracle
#

-# little did he know, he was right - it took him 10 hours instead

uncut remnant
#

i knew it 🤣

stable oracle
#

lmao

uncut remnant
#

-# predictability++

fierce echo
#

-# and he was never seen again

uncut remnant
#

no faith

uncut remnant
#

soo i guess next i should make the head of the snake.. and center it.. once we press a directional key.. the head begins to travel and the initial segments appear until the head moves (segment number) from the origin.. then the tail will begin to move..

uncut remnant
#

phew... getting the snake to spawn in slowly until it reaches its full starting length
and then making it work from the queue from then on was a pita

stable oracle
#

ooh nice

uncut remnant
#

i kept getting gaps and stuff on the 3rd move.. (finally i realized i had to keep track of every position of every segment)

#

atleast to grow it the way i wanted.. and then i realized i could give quick inputs between steps to get it to reverse on itself

#

so i added a little input buffer.. (just 1 input per step)

#

and that got it where it wont reverse on itself 💪

#

now.. i gotta add apples and see if it grows correctly

#

the entire "Input buffer b/c of the step movement" is a good learning lesson ngl

fierce echo
#

Hell ye dude

#

(Im stealing ur idea)

stable oracle
#

i just made it ignore going the opposite way of the current direction lol

fierce echo
uncut remnant
#

i actually couldnt get it to work the way i wanted to with a queue.. i did orignally but to build the snake i wanted it to start with 1 piece. then 2 piece, then 3 piece.. the queue method i was using needed all pieces to be there at teh get go.. so instead

** i used two lists ** - one for position history and one for snake segments

im just keeping track of each pieces position..
only the head is actually moving
and then the rest of the pieces follow

basically like here's the snakes path -- now every piece follow the guy in front

#
        private List<GameObject> snakePieces = new List<GameObject>();
        private List<Vector2Int> positionHistory = new List<Vector2Int>();```

```cs
for (int i = 0; i < snakePieces.Count; i++)
{
    if (i < positionHistory.Count && i < startingLength)
    {
        // Move piece to the position in history
        Vector3 worldPos = grid.GetWorldPosition(positionHistory[i].x, positionHistory[i].y);
        snakePieces[i].transform.position = worldPos;
        snakePieces[i].SetActive(true);
    }
    else
    {
        snakePieces[i].SetActive(false); // Hide unused pieces
    }
}```

i first check if the head is moving to a valid position.. if it is i move the head.. and then the next segment (2nd) piece then goes to where the head was, and the (3rd) piece goes where that piece was
#

check out the inspector..segment0 is the head.. so it goes to element 0 in the history (most recent move).. then 1-1, 2-2, 3-3 etc.. the reason i have 10 positions is because my snake can grow to 10 segments... so i need to track all 10 recent locations to keep the snake updated properly

#

imma work out the queue thing a bit later.. (its probably the proper way to do it)