Wrapping the Player: Godot 4 Snake Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hey Sean here from bacon and games this is the second video in our snake coatal along Series in the first video we did some basic project set up and got our player moving so if you haven't checked that out yet the link will be in the description Down Below in today's video we're going to get our snake wrapping from one side of the screen to the other so let's get started when we left off we got our character moving but once it goes off the screen it's gone forever in order to make the snake wrap from the left side of the screen back to the right we're going to build a little helper node that's going to keep track of where our boundaries are and we're going to be able to Ping it to find out whether we've gone outside of them so let's start by adding another node 2D we're going to call this bounce and to that we're going to add two marker 2DS a marker 2D is just going to give you a Crosshair Gizmo that you can drop into your game scene to visually Mark something in X and Y coordinates I'm going to name this one upper left and I'm going to duplicate it with command D we're going to make another one called lower right if I select these and go to our move tool you can see we actually have two little gizmos that we can drag around here's one here's the other and for right now I'm going to position this as close to the bottom right hand corner as I can and that's going to give us the boundaries of our screen this is our upper leftmost position this is the bottom right and between them we form an entire rectangle let's add a script to our bounds node give it a class name of bounds and let's get references to our two boundary markers let's create four variables that we can use to store the boundaries of our game in the onready function we can access the positions of our two markers and store those values as our minimum and maximum positions this isn't strictly necessary because we could access these positions directly however these short variables are easier to type and they read a little bit more like what they actually are R the next thing we're going to do is write a function called wrap Vector which is going to take a vector 2 and it's going to return a vector 2 now there's not very much to this function we're going to use this wrap Vector function to pass in the position we'd like to move the player to and it's either going to return the vector that we gave it if it's within bounds or it's going to return the leftmost x coordinate if we've gone too far to the right the rightmost if we've gone too far to the left and so on and so forth so let's see how we Implement that first we're going to need to grab a reference to our bounds so let's drag that in here release with a control click and let's force it to type bounds so that we get our code completion now down here before we set the position let's check it against our wrap Vector so we can feed it new position if it's within bounds it'll just return the same position we gave it and if it's out of bounds it'll return the wrap position so now if we test our game our player wraps nicely top to the bottom left left to the right and so forth the next thing I'd like to do because this window is a little bit gigantic let's change the size of our game so flipping back over to 2D the other problem this is going to solve is if you take a look at our marker our game size is not exactly in 32x 32 Dimensions so we're going to go up to project project settings window and right now we've got this giant window that is n necessarily in 32x 32 increments and I don't know the exact number we want but I do know the ratio so I can actually use a shortcut by multiplying 25 by our grid size and hit enter and 14 by 32 to get our height so we're going to have a window of 800 by 448 now when I come back you can see here we've got a smaller window size that fits directly ly onto the grid and now all I have to do is move this marker into the bottom corner and if I run it again everything still works exactly as we wanted it to since we've got a little bit of time left let's go ahead and add a tile map so we can put a very basic background Grid in place in snake especially when you're wrapping from one side to the other it helps to have a visual guide to show you what row or column you're in when you're lining up your moves we can select gaml search for tile map and we're going to want to drag this up to the top because we want it to be bind everything whoops here we go and tile naap is going to require a tile set so with our tile map selected we can click on this empty dropdown and select new tile set we can click on that to change some of the settings now we know our tiles are 32x 32 so I'm going to change the default from from 16 to 32 now of course our tile set needs some actual tiles so let's grab in this Grid it's going to ask if we want to automatically create tiles in the atlas for this very basic example we can just click yes now I can click over to my tile map select my tile select the rectangle angular paint tool and you'll actually need the selection tool up here selected for this to work and now I can paint my tiles in to fit the size of our window and let me click off of that so we don't see the orange anymore now when we play this you can see we'll have a grid that gives us a visual guideline where will'll come out when we wrap around in the next video we're going to build our spawner class to randomly drop food and we'll Implement our tail class so that our snake can grow each time it eats if you learn something or just want to be nice please like or subscribe to help me reach more lovely makers like yourself also feel free to drop a comment down below if you have questions about anything we've covered so far until next time be kind to yourself and be kind to others
Info
Channel: Bacon and Games
Views: 671
Rating: undefined out of 5
Keywords: godot, godot4, tutorial, gaming, programming, snake, retro, learn, video games, boundaries, window size, godot tutorial, godot 4, gdscript, gdscript tutorial, gdscript full course, gds, godot 4 tutorial, godot engine, godot engine tutorial
Id: BUHuu9l5lnM
Channel Id: undefined
Length: 7min 24sec (444 seconds)
Published: Thu Nov 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.