Episode 33 - The Void Tile
Key Takeaways
The video demonstrates creating a void tile in Java for game programming, handling null pointer exceptions, and rendering tiles that shouldn't exist. The Cherno creates a void tile class, a sprite for it, and sets its color using a new constructor and a set color method.
Full Transcript
hey what's up guys My Name Is AO welcome to episode 33 of game programming so last time I talked about making a void tile that is a tile that sort of goes into I guess that gets used when there's an error or something because we don't just want to leave it blank we don't just want to have like black thing at the moment we've got return null in our get tile method which means that theoretically because this tile method it returns a tile if that tile is null obviously that will throw a null pointer exception um and the thing with that is you know that could potentially crash our game if we don't handle that and the other thing is we just we never want to be actually returning n we want to actually have a type of tile like an actual valid tile object that gets returned when something doesn't go according to plan or just to fill it up so in other words if someone I don't know if someone gets out of the map for example we don't really want to be drawing nothing like some games actually do um my microphone keeps falling down um we we don't don't we don't want to be doing that what we want to be doing is if someone this is just one example by the way if someone gets out of the map in our game um we want to draw a specific type of title because obviously the map isn't infinite um and there is a boundary to the map if someone manages to go above and beyond that boundary they'll just be present presented with a a void tile quite unquote um so that is a tile that actually has not basically no data but again it will have data just have dummy dotter okay think of it that way so let's create that tile so if we go into our tile class we've got this public static tile grass right again we'll do the same thing but we'll create a void tile out of it so public static tile void tile um obviously you can't call it void cuz that's a keyword in Java we'll just call it void tile and we'll set it equal to new void tile and we'll have to make a Sprite for it which we'll do in a minute um so let's actually go right now and create a Sprite for it so if we hit the Sprite if we open sprite. Java we can uh we can assemble a new uh a new Sprite here and um we don't actually have to draw a Sprite because I'll sort of get to that in a minute but we don't actually have to go into paint.net or any other program Open Up Our Sprite sheet and draw ourselves a void Sprite because again the void Sprite will just be a solid black color at this point um so we can easily do that with code we don't actually have to draw that so public static Sprite void Sprite equals new Sprite and now this current Constructor actually asks for a Sprite sheet meaning that the problem with that is you know that means that it will actually draw a value from a Sprite sheet as you can see it loads it and it does all this we don't want it to do that though right we want to create an alternate Constructor which just simply which simply creates a new Sprite with let's just say just creates a Sprite with a color so if we go over here and type in public whoops public Sprite and then let's just go in size again uh and we'll go in color all right simple as that and over here um we'll again we'll set size equal to size and we'll set pixels equals to new in size * size and then we'll set color I'm spelling everything everything in Australian by the way so there you go color with a u um so yeah so now we'll make a set color method this is actually adding quite quite a lot of lines of code you guys always complain that I don't add enough lines of code so here you are um yeah so set color and set color will actually simply fill our pixel array with a particular color now we actually need to set the color to something so we'll just put color as a parameter here and type in color here so obviously the input that we put into here will flow through to our set color and it will actually be accessible in our set color method um so we'll simply use a for Loop to cycle through everything so for in IAL 0 I is less than the size of our pixels size time size of our pixels so that way it'll cover everything it'll cover every single um it'll cover every single index of our pixels thing because we've set pixels equal to we've set pixels equal to new in size time size um and uh over here we'll simply say pixels I equals our color and that's it that that is literally how simple it is so over here now that we've made this new Constructor which does this set color thing all we'll do over here is over here at the new Sprite we'll um we'll type in we need to specify a size and a color so the size will be 16 and the color will be zero which is black and that that's it all right uh if you want if you want to do white you have to type this all right that is white so that's the hexad decimal for white um if you guys want to set it to a particular color or if you actually just want to make tiles that are specific colors you can actually go to a website called color.com I'll show you that website right over here here it is color.com and what you can do here is you can select any color you want such as this blue for example you can copy the hexa decimal that it gives you this number up here um and then you actually just paste it you just go 0x to to specify that the number that you're about to give as a hexad decimal and then you paste in your number and that will give you a void tile with that particular color so we'll just leave it as that for now but um that is how we create the Sprite for that and now let's go back into our tile and then um over here where where we were creating our void tile we have to specify Sprite of course so we'll type Sprite do void Sprite and then let's actually create this void T sub class so let's create the class it will automatically make uh it accenti so we'll just fix this error by adding The Constructor that we need to add and then finally we can actually just go back to our grass tile and just copy and paste this honestly that's how easy it is so in other words I've added a random method into um I've added a random method into our void to class and it is as simple as just doing that in fact we probably could um put that into the Super class right over here um but we won't do that for now so yeah um and again that will simply render um our we've created a void tile which is just a tile with a particular color that we'll use for um for rendering tiles that basically shouldn't exist so if we go in over here into our screen class sorry into our level class so level. Java instead of returning null we can now return tile dot void tile which is again obviously a valid object whenever we actually for some reason don't satisfy the criteria so yeah so that's how we use void tiles and you'll you'll be able to see them in action in the future but that's the end of this episode so if you did like the video please hit the like button give it a favorite and I'll see you guys next time [Music] goodbye
Original Description
Support this series and get rewards! ► http://www.patreon.com/thecherno
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
3D Game Programming - Episode 1 - Window
The Cherno
3D Game Programming - Episode 2 - Game Loop
The Cherno
3D Game Programming - Episode 3 - Arrays
The Cherno
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
3D Game Programming - Episode 7 - FPS Counter
The Cherno
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
3D Game Programming - Episode 11 - Rotation
The Cherno
3D Game Programming - Episode 12 - User Input
The Cherno
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
3D Game Programming - Episode 15 - Textures + More!
The Cherno
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
Episode 18.5 - Creating an EXE File in Java
The Cherno
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
3D Game Programming - Episode 29 - Launching The Game
The Cherno
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
3D Game Programming - Episode 31 - Sprites!
The Cherno
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
3D Game Programming - Episode 34 - Entities
The Cherno
Genesis - My Game for Ludum Dare 24
The Cherno
Vlog + Ludum Dare Results
The Cherno
Game Programming - Episode 1 - Resolution
The Cherno
Game Programming - Episode 2 - Threads
The Cherno
Game Programming - Episode 3 - Game Loop
The Cherno
Game Programming - Episode 4 - Window
The Cherno
Episode 5 - Buffer Strategy
The Cherno
Game Programming - Episode 6 - Graphics Initialized
The Cherno
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
Game Programming - Episode 8 - The Screen Class
The Cherno
Game Programming - Episode 9 - Rendering Pixels
The Cherno
Game Programming - Episode 10 - Clearing the Screen
The Cherno
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
Game Programming - Episode 12 - Negative Bounds
The Cherno
Game Programming - Episode 13 - Timer
The Cherno
Game Programming - Episode 14 - FPS Counter
The Cherno
Episode 15 - Tiles
The Cherno
Game Programming - Episode 16 - The Map
The Cherno
The Walls 2 - Minecraft PvP Survival Map
The Cherno
Game Programming - Episode 17 - Key Input
The Cherno
Game Programming - Episode 18 - Controlling The Map
The Cherno
Related Reads
📰
📰
📰
📰
In India, It Is Often Easier to Earn Offline Than Online
Medium · Startup
TechCabal’s Road to Moonshot brings Africa’s startup debate to Nairobi
TechCabal
The Provider Credentialing Maze: Are We Protecting Patients Or Slowing Care?
Forbes Innovation
Stop Chasing Efficiency. Start Building Autonomy.
Medium · AI
🎓
Tutor Explanation
DeepCamp AI