Game Programming - Episode 52 - Loading Level Files

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

Key Takeaways

Loads level files for a game using Java

Full Transcript

Hey, what's up guys? My name is Aerno. Welcome to episode 52 of game programming. So, yesterday we took a look at um all the different sprites that we created for our randomly generated world. And today we're going to take a look at actually, you know, making a map, I guess. Um so, we're going to start off by first of all, you can see that we've got way too many classes open here. Um, I'm literally just going to right click and hit up close all just to just to clean up our our area here. Okay, cuz um cuz I'm I I like clean things as you might as you might know. Um anyway, so um let's let's just jump in. So what I want here is the level class. That's pretty much all we want to focus on. So right over here, you'll see that we've got uh our level class. And right now what happens is we sort of make a new level I guess uh with a string as well possibly with string uh and then I guess you know we render it we get get the appropriate tile. This is sort of like the the random method of getting tiles. It's not really how we want to get tiles. There are different there are many different ways of actually getting tiles, which is why it's sort of um h I guess you'd say it's really up to you in a sense. But um the way that we're going to make maps in this game is we're literally going to just draw them. Um and then we're going to sort through pixels and determine which color is which sprite, basically which tile. Um so that'll make sense when we actually implement it. But first of all, I want to right over here in level. So, this is our navigator. Obviously, we've got level. I'm just going to right click, hit new class, and we're just going to make a new level called spawn level. Now, spawn level is just going to be like the main um place where cuz this is sort of like an MMO, right? So, it's going to be like in in Real of the Mad God, for example, it's going to be that um that beginning place. I forgotten the name of it cuz I haven't played Real of the Mad in about half a year. Uh but it's um it's like the the the Nexus. I think that's it, right? The Nexus. Yeah. Anyway, it's like that that area where it's like the safe place like the area and then from there you can sort of enter different portals I guess to go to different worlds and different levels from there. But the spawn level is going to be the spawn level. So first of all it extends level. It's the most important thing. Um now obviously it needs to um it must define an explicit constructor. So the constructor that the spawn level is going to actually define will be the one that's string path. All right. Right. So, let's just copy that and paste it here. Right. Except instead of load level pass path, I'm just going to type in super path. Right. And that's it. Simple as that. So, um except obviously we have to change level to spawn level. That should uh should be self-explanatory. If you didn't know that, then I suggest you go back to some kind of I don't know easier series. I don't know. Um, it's hard to measure like your guys understanding of Java cuz I have no idea how experienced you are with Java. I feel that this series has sort of turned into people just taking down code and not understanding what it does. Um, I guess it's because people have asked me to speed up and not explain everything, but I feel that if I do that, I don't know. I don't know. I don't know. I don't know. This series this series hasn't really turned into what I wanted it to turn into. Um, which was literally like a few lines of code per episode teaching you guys like everything. But, um, I don't know. It sort of turned into you guys probably just taking down code and um not really learning anything from it which sucks. But anyway um so the spawn level uh what does the spawn level need to do? First of all um it needs to you know loads the level from path. So we got this private void load level. Let's copy that. Let's paste it here. The fact that it's private is a bit of a problem. Let's make it protected so that it um there you go. All right. Cool. So, protected void load level. Um, well, what do we want to do here? So, this is the part where we actually load um the level from a file. Now, this is really important because um this can be done many different ways. The way that I prefer to do it is I'm just thinking um okay well we can do it several ways. In fact, we probably will do it several ways and then we'll measure performance. So, one of the ways to do it, and this is a bit on the slow side, is just simply make an array of tiles, right? That's it. Um, this is very precise though. Um, it's a bit slower, but hey, it's precise. That's what matters. Um, well, actually, that doesn't really matter if you're if there's no point in it being precise if it's running at 100 frames per second, but it won't be obviously not not this not this method. Um, so we'll make tiles equal to new tile. In fact, we won't do anything here yet. So, load level string path. Um, this is where we actually load the level, right? So, let's try and basically load that image that I was talking about. So, buffet image image equals image io read. And then what's this class called? Spawn level.class do.get resource. And I love making subasses cuz um if this actually fails, we'll know that this happened in spawn level, which is really cool. Uh anyway, so we're we're loading it at path obviously string path, which of course is uh is this thing. So um let's import buffer image and let's catch an Oh, probably just be an IO exception. That should uh that should be good and we'll handle that later. Not now. So, oops. First thing we want to do is get the width and height of the image, right? And we'll set the uh the width and height to int w and int h respectively. So, now that we got the width and height, all we really need to do is kind of put it into an array of pixels. So, let's make private int. Uh, we'll call it, I guess, level pixels. I don't know. I really hate um I really hate spelling level like that, though. So, I spell it properly. Level pixels. Um, so image.get RGB. So, we'll start at 0 0. Uh, width and height will be W and H. RGB array will be level pixels. offset will be zero. Scan size will be W. Simple as that. And that's it. Uh if we crash, you know, I don't think we will, but um um we might actually make an exception class in the future. I don't really need to. Exception um level file could not load level file. All right, that's how I like to handle exceptions. Um there's a number of things we can do. We should probably also um print the stack trace in case there are any other issues that are unseen. Um okay, awesome. So now that we've got a way to actually load level pixels, we need to deal with Okay, so what does this do? I'm I'm sort of not explaining this because we did do the same thing for sprites, but basically what this is doing is it's loading. Um is my audio picking? My audio might be peaking. I hate I hate when that happens cuz you can't really fix that. Oh well, I can make my microphone quieter. I think I'll do that now actually. Make it a bit quieter. All right, there we go. Audio should be less peaky now, but it will also be quieter. So hopefully you guys can still hear me. Anyway, so back to this. Um, what I want to do here is, um, so what happens here is I'm loading an image, right, from a particular path. Um, I'm calculating the width and height of the image and then I'm basically uh converting that image into a into an array of pixels. And that array of pixels tells me which color every pixel of that image is, which is really important because that's how we're going to differentiate between tiles. So over here, you guys can probably tell that we need a method to convert this into this. All right, that's what we're going to do now in level. I'm going to make a protected void method called what shall we call it? In fact, I am going to call it generate level because even though that's in use by um by our random level, you can see here it it is gen it is generally doing the same thing. So, let's just do it. Um so, we're doing it the Whoops. We're doing it the tiles the tile way today and we'll take a look at how that works performance base and then we'll compare it with another way. So, first off, um, let's just run that generate level. So, protected for generate level. And, yep, it is overwriting. Good. I'll just pop it under here, though, cuz I like things to be in chronological order. I guess it needs to load the level first, then generate it. Even though it doesn't actually matter chronologically, I just like reading it chronologically, that's all. Just a just just a personal preference really. Um, so what does generate level need to do? Generate level, right? essentially needs to convert level pixels, this array of integers into this array of tiles, right? Level pixels into tiles. So, how are we going to do that? Well, first off, we're going to have to um basically draw the level, I guess. But, um to actually convert pixels into tiles, um integers into tiles, we obviously need to figure out which pixels we want to be which tiles. And we will talk about that next time. So, later guys, hope you enjoyed the episode of game programming. If you did, that like button is right there. So, please hit the like button. Let's see if we can hit 100 likes. If we can hit if we can hit 100 likes, I'll um I'll do something for you. That sounded really lame. Anyway, later, guys.

Original Description

Lets hit 100 likes! 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

Related Reads

📰
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Learn how to introduce primary school children to computer applications in a fun and safe way, teaching them essential skills for the future.
Dev.to · Ogunkola Adeola
📰
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Create an interactive coding book for kids using Rust, with a focus on storytelling and bilingual support, to teach programming concepts in an engaging way
Dev.to · born1987-ir
📰
How Kids Can Build Fighting Games Stickman in Scratch
Kids can learn coding by building a stickman fighting game in Scratch, developing skills like movement control and collision detection.
Medium · Programming
📰
Coding Platform for Kids: A Simple Guide to Start Learning Programming
Learn how to introduce kids to coding with a simple guide to get them started with programming
Dev.to · Coding Learning Lab
Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →