Game Programming - Episode 26 - The Tile Class
Key Takeaways
Implements the Tile Class in Java for game programming
Full Transcript
hey what's up guys My Name Is AO welcome to episode 26 of game programming let's take a look at the tile class now so last time we created yesterday we actually created a random level class and the day before we created an actual level class sort of template um and someone actually mentioned that I should make this abstract yeah that's not a bad idea actually making it a public abstract class uh wouldn't wouldn't be a bad idea that would be strictly correct but you know I don't want to do that too early because in in the devel cycle just cuz I might actually use this class for something so I don't really want to make it abstract just yet um and half of you probably more than half of you probably don't even know what abstract means so just we're not getting into complex stuff like that just yet uh we probably will just to you know just cuz it's good practice but um for the minute we actually need to create a tile class now yesterday I actually said that we'd be rendering tiles today in order to render tiles we need to actually make a tile class because each individual tile is going to have its own rendering method and that might sound inefficient but it's actually more efficient and I'll I'll explain all this in a minute so right over here in the level class let's just right click on it hit new and class and we're just going to type tile right and here in the package we'll change it to level do tile so in other words we're creating a folder in level called tile because there's going to be all different types of tiles like just the normal tile class then we've got stuff like you know like the grass tiles Stone water ice I don't know trees whatever there's all different types of tiles so I sort of want to group them in a folder just to organize my game a bit more let's head finish so public class tile let's talk about the tile class let's just take an overview of the tile class here so what is a tile first of all if we actually just launch our game did I hit the button yeah I did if we actually just launch our game you'll see a bunch of well you know tiles um there's a bunch of grass tiles here and each one of these right each each little like section of the screen that is 16 by 16 that is one texture um and I probably shouldn't explain like that cuz we might have textures that are larger than 16x 16 but one like one little block here that's called a tile okay so what does the tile class have to do right each tile should have a position each tile should have a Sprite because even if that Sprite is null or like a void Sprite it should still have a Sprite because we want it to be if if we want something to be displayable in some way we want to ATT we actually want attach a Sprite to it that way you know it's if we if we have a tile like without a Sprite it if something crashes in the future might be a lot harder to actually fix or you know diagnose just because it doesn't have a Sprite to it and it should have a Sprite to it because a tile is something that we need to display at all times okay and by all times I mean if if a specific tile is actually on the screen it should be rendered that's how tiles work or at least in this game anyway um something like an entity might not necessarily have a Sprite because we might have an entity see that's just like a I don't know like a just just a level timer it's just like some some background logic in our level that doesn't necessarily have to be displayed on the screen but still has to be computed and calculated all right but the tile tiles do need to have a Sprite um other things that tiles need off the top of my head well they need to be rendered first of all and also they probably need some way to recognize if it's actually solid or not so in other words if it actually should stop like mobs passing through in other words is it collidable or not is it a solid is it a solid object so to define the position we're just going to put public int X and Y all right again the x coordinate is going to be the x coordinate on the screen Y is going to be y self-explanatory now to attach a Sprite to it we simply type public Sprite Sprite all right simple as that and of course we will import the Sprite class okay so now we actually need to make a Constructor and every time we make a tile this specific Constructor has to actually happen it has to be run and we'll simply type public tile and again Sprite Sprite few of you are already going to guess that I'm going to type this. Sprite equals Sprite all right so first of all this is not the default Constructor right because it's got a parameter in it it's actually got a parameter that is a Sprite object in it okay that means that anytime we actually want to create new tile in any in any way we set something equal something equal to new tile or new whether it be new grass tile grass tile extends tile anything that is tile or extends tile if we want to set it equal to new something we're going to have to put a Sprite here otherwise it won't work so this is an example for you right here if we try not to do that it's going to it's not going to work all right again most of you probably understand that but by doing this we're requiring that each tile we actually create is going to have a Sprite the other thing we need to do is render and again what is rendering right what do we actually what what what um Properties or what uh what information do we need what data do we need to actually render a tile well firstly we actually need the position of where on the screen we want to render it so in X and in y and then we also need what's rendering it we need something to actually render it which is a screen class which is going to manage all kinds of rendering because the the screen class is what puts stuff onto our screen okay so that's rendering taken care of is again I'm not actually filling out anything here because we'll do that in sub classes kind of like we did with this generate level method and right here as well and then finally we're just going to type public bullying solid all right now you'll see it's actually a bulling so it's not void it returns a bulling which is a true or false value and then you know by default just in case we want to just return false because again it gives us an error if we don't return anything so let's just return false okay so in other words by default if we don't actually override this this method in a subass then by default the tile is not going to be solid so we can pass through it all right simple as that so that is the basic overview of the tile class we probably won't even add anything else into this specific code just in into this tile class it's pretty much ready to go again we will define public static tiles kind of like we did with public static Sprites same way absolutely the same way because tiles are static so if we create a new grass tile that grass tile is not going to change it's going to be the same it's going to be one it's going to manage one tile that's it so yeah but that is how the that is how the tile class sorry works and um yeah I hope you enjoyed this episode of game programming if you did please hit that like button and leave a comment let me know how you guys are going and um yeah say I'll see you guys tomorrow where we'll hopefully move on to actually creating the grass tile object and move on to eventually you know rendering uh a random level on toas crin but until then guys 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
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
I Analyzed 100+ Startup Directories So Founders Don’t Have To
Medium · Startup
Building LeeX: Why We're Starting With Data Before Users
Dev.to · Oscar Awowari
Ford and Geely will build EVs together at Ford’s plant in Spain. Ford owns 66%, Geely 34%.
The Next Web AI
Gamma scaled like a bootstrapper, and indie hackers should steal the playbook
Medium · Startup
🎓
Tutor Explanation
DeepCamp AI