Episode 33 - The Void Tile

The Cherno · Intermediate ·🚀 Entrepreneurship & Startups ·13y ago

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 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

The video teaches how to create a void tile in Java for game programming, handle null pointer exceptions, and render tiles that shouldn't exist. The Cherno demonstrates creating a void tile class, a sprite for it, and setting its color using a new constructor and a set color method.

Key Takeaways
  1. Create a void tile class in Java
  2. Create a sprite for the void tile
  3. Set the color of the void tile using a new constructor and a set color method
  4. Handle null pointer exceptions by returning a valid void tile object
  5. Render tiles that shouldn't exist using the void tile
💡 Creating a void tile in Java can help handle null pointer exceptions and render tiles that shouldn't exist, improving the overall game development process.

Related Reads

Up next
Watch this before applying for jobs as a developer.
Tech With Tim
Watch →