Game Programming - Episode 35 - Level Render

The Cherno · Intermediate ·🧒 Coding for Kids ·13y ago

Key Takeaways

The video demonstrates how to render tiles in a game using Java, specifically by using nested for loops to cycle through every pixel on the screen and rendering the appropriate tile at each location. The code uses a getTile method to retrieve the correct tile to render, and then calls the render method on that tile to display it on the screen.

Full Transcript

hey what's up guys my name is aern welcome to episode 35 of game programming so last time we took a look at this screen do set offset method that we made here and how that how that actually controlled the offsets of our tiles on our screens now what we're actually going to do today is render those tiles so if I go over here to level we've got these four corner pins that we actually talked about a few episodes back and we actually need to um apply them so in other words if I open if I open my um my game you notice that they're the four corner pins right so in other words what what we sort of want to do is render from this side of the screen to this side of the screen for x and from that from this side of the screen to this uh part of the screen um for y so obviously that's going to be just a full loop so let's talk about this now so four and we'll we'll manage y first four in y equals now before you type zero we're not actually going to be we're not actually going to type zero because that's you know not necessarily going to be zero we're going to type y0 because y0 is the top part of the screen so for y = y0 y is less than the bottom part of the screen y1 Y ++ and we're going to do the same thing for X so for in and remember this is inside whoops that this this x that that I'm about to type right here it is inside the Y um the wide Loop so it's a nested for Loop for in x = x0 x is less than X1 x++ now I'm going to explain this code real quick um if I open up our window we know that y0 is up here y1 is down here right x0 is this left side and X1 is this whoops and X1 is this right side here so what I'm saying here is I'm saying that we want to render from the top part of the screen to the bottom part of the screen so every number in between this very top pixel up here to this very bottom pixel over here and I also want to render every every single um pixel from this this left most pixel to the right most pixel all right that is what that code is doing it's cycling through every number between the top and bottom and the right and left or the left and right technically um okay sweet so what I want to do now is actually you know get the right tile to render so we can do that very simply simply by calling this get tile method and then accessing if I go back into our tile class you'll see that we've got this render method in our tile and if I go into the grass Tile For example you'll see that what that render method actually does is it renders it CLA the screens render tile method and actually renders a tile onto the screen that's what we want to do right we want to render a tile onto the screen so we'll simply call this get tile method by typing get tile now this is important because we want to be able to get the right tile to render um now it's not actually very hard all we do is we Type X comma Y and that's it because we need to input the X position of the tile and the Y position of the tile but remember this is these are those positions all right we're dividing remember we're dividing everything by 16 so we're getting it into tile precision and I talked a bit about pixel pixel precision versus tile Precision a few episodes ago and so what we're doing here is by dividing this x scl value by 16 we're actually you know being like all right every 16 pixels equals one tile so in other words when we actually look up this in this array here um on a specific index you know we're saying that at at these two coordinates what tile is there and then we find out that if in that array the tile at these this x comma y coordinate equals zero then yeah we're returning a grass tile otherwise will just return aoid tile so that is all we need to do because remember these values are getting getting their this y here in this x they're getting their values from these Corner pins and that those Corner pins are intile Precision so that's it that's it's as simple as that you know we've we've managed to get every pixel on the screen into tile Precision so that our program actually knows what tile is where and which tile ends where and starts where so in other words it knows that the first 16 pixel for example is one tile so now that we've called the get tile method which is brilliant what we need to do now is actually render them and rendering them is very simple because you know normally when you're calling a method you just end it like that and that's all fine but remember this method isn't just a void method it actually returns a tile you can see here that it returns a tile and so we can actually use it to access our tile class um like this tile. render class um as if as if it was just you know as if as if it was just tile do whatever so let's get rid of the semicolon and we'll simply type get tile XY which will return us some kind of tile object and then simply do render and now we need to actually fill out these details which are extremely easy as well they're actually all there on the screen for us already so we're going to render them at these positions and that's it that is actually going to render a tile onto the screen um now one thing I want to quickly mention here is why we can do this now you'll notice here that in the tile class which is right over here um we've got this random method and it's not actually doing anything you can see there it just sort of exists there but there's no code inside it and you guys might be like you know why is it there then it doesn't really do anything why is it there because the thing is if we actually you know do this so if we remove it I'm just commenting it out which is the same as removing it really you'll see that we're actually getting error here and you guys might be like but hang on a minute I'm trying to render a grass tile if I go to my grass tile the render method exists but the thing is we're returning a tile we're not returning a grass tile we're returning a grass tile yeah that method will work but we're returning a tile so in other words even if it's returning a grass tile object the actual method needs to be in the tile object because the thing is Java can't just assume that we're going to return a grass tile every time what if we're returning a void tile and what if void tile doesn't actually have a render method because for example I don't know it might not render like it does in this case but if it didn't have a if it didn't have a random method and we returned it it would crash because we just get method not found so Java actually says that all right if you're if you're using classes that extend other classes the actual super class which is in this case tile it has to have a random method it has to have a method it has to have that base method so that you know worst comes to worst I'll just run nothing I'll just run what's in this code if I don't find it in our sub classes so I hope that makes sense um it's not actually uh very easy to understand but you know if you think about it a lot I think you will be able to understand it but um yeah so this line of code over here get tile XY render it's going to get the um the appropriate tile at that particular location and it's going to render it also at this location on the screen and that is it so I hope you enjoyed this episode of game programming guys and I will see you guys next time [Music] goodbye

Original Description

Welcome to Game Programming, a series in which we take an in depth look at how to make a game from scratch, in Java. ---------------------------------------------------------------------------------------------------------------- Website: http://www.thecherno.com Twitter: http://www.twitter.com/thecherno Facebook: http://www.facebook.com/thecherno Subreddit: http://www.reddit.com/r/thecherno/ Steam Group: http://www.steamcommunity.com/groups/thecherno ---------------------------------------------------------------------------------------------------------------- Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 0 of 60

← Previous Next →
1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches how to render tiles in a game using Java, covering topics such as nested for loops, tile precision, and the getTile and render methods. By following along, viewers can learn how to implement tile rendering in their own game projects.

Key Takeaways
  1. Create a nested for loop to cycle through every pixel on the screen
  2. Use the getTile method to retrieve the correct tile to render at each location
  3. Call the render method on the retrieved tile to display it on the screen
  4. Understand the difference between tile precision and pixel precision
  5. Apply system design principles to game development
💡 The getTile method must return a tile object, and the tile class must have a render method, in order for the code to work correctly.

Related AI Lessons

Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →