Game Programming - Episode 57 - Player Spawns

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

Key Takeaways

The Cherno discusses game programming and player spawns in Java, covering error checking, Constructor review, and customization of player spawn locations using tools like Visual Studio and Paint.NET

Full Transcript

episode 57 of game programming that's what's up my name is AO and today we're going to talk about well we're going to talk about game programming that's what we're going to talk about today big surprise eh um so last episode I left you guys on a cliffhanger and um I don't know just to mix it up a bit you know cuz it's fun to it's fun to do that it's fun to make you guys suffer no it's it's more or less I'm being serious here by the way it's more or L it's fun to to actually solve things yourselves which is why I like kind of I might do that more often not like in the crude cruel sort of fashion that I did last time where well the the game didn't even function properly but I I I I do like the idea of letting you guys solve things yourselves because that that just involves the community a lot more and it is entertaining so um mxx Z not sure what kind of username that is but literally mxx Z in the comments section of episode 56 actually pretty much solved the cliff now um so shout out to him or her actually mxx that it's not really a male or female name but um for solving that uh that problem now we're going to implement it a bit differently than what he or she wrote we're actually going to um well first of all we're going to talk about it cuz you guys probably have no idea what I'm talking about so basically we had this problem where we opened up the game and we just went on the map and why we went on the map why okay let's talk about that now so what what you might notice right is that we uh we actually return if we if we go over here right in fact if we just don't go over here what's going to happen right is if this if this statement isn't true right so if this statement is not true right the compile well the game is going to go onto the next lineer code which happens to be this one and run it because it's not returning this return obviously means that it actually exits the method in this case it returns this data and then it does not run this that code is not ran or run that code is not actually executed if the statement is true right because it returns so in other words what we should put here is let's just say good right so in other words if good is actually printed that means that at some point in this thing this like this code and lower has actually you know been executed so in other words if good is printed out here in our console that means that for one of the tiles it has not returned a void tile so other words let's let's take a look here and you can see here in the console that that's just not the case good is never printed so what does that mean that means that this statement is always true so in other words that tells us as programmers to actually take a look at this statement and try and make sure and just check it for errors so you can't really go wrong with X and Y because that's the only way to input it and we actually did do that earlier that worked fine right um less than zero zero is a static number right it's not like a variable uh it's just a static number it's a constant so there's nothing really to check there however we've got width and height and they are our variables so there's most likely something wrong with them so if we come over here we see oh okay here we go check check this out this is our old Constructor that we used when we actually made made randomly generated level so apparently we actually entered a width and height and we set this do width equal to that width so in other words if we wanted a randomly generated a level that was 64x 64 tiles big then we put 64 here and 64 here and that that worked beautifully but now we've got a 16x 16 size image that we actually load right so is that actually like being applied here at all and it looks like this is our other Constructor not really right width and height does do not actually equal that so let's go into our spawn level since that's the level where we where we load this thing and over here you'll see that yeah we do actually Define the width and height and remember the width and height of our image is equal to the width and height of our map in tiles so in other words pixel one pixel it's a one: one scale here where one pixel in the image equals one tile in the game so all we need to do here is say that part that in W because remember we use that here is equal to to width right and image. get width so what this is going to do is is basically going to make a new variable called W which is and and W is going to equal width which is going to equal image. get width so in other words this line of code in case you guys are having trouble understanding is literally the same as this line of code all right these lines of code this line these lines of code are exactly the same as this one line of code all right cool and now if we run our game here you can see that we actually spawn in the top left corner because the top left corner is 0 0 and you can see how we've got this map now and it's just one map awesome so that is basically how the map gets loaded um I know you guys are going to hate me unless I give you guys some more content in this episode so we're going to take a look at one more thing and that is actually customizing the player spawn location I saw that came up on a Reddit thread on my subreddit so I thought I might answer that I don't know someone probably answered it there but um cuz obviously not all you guys catch that I'll um I'll talk about it here so you'll notice it right now we actually spawn in the top left corner now why is that and the reason that actually happens and bear with me for a minute cuz I got to open up my image to show you guys guys um why that actually happens so here we go um if we open our if we open our level file in paint.net you'll actually see that if you if you Mouse over that area which is right over here you'll actually see that over here right it says 0 0 when I Mouse over there yep you can see there 0 0 that is the coordinate of that level that's why we spawn there because our players position is equal to 0 0 and that and that part of the map is z 0 as far as the as far as the map is concerned so that's that's why we spawned there now in the player class by the way I saw a comment about this I I press control shift T to actually open type which means I can just pretty much search for glasses and open them all right so control shift T is the shortcut for that you can see here that we've got in X and in y right so what we can do here is actually in game so we made this Constructor a while back but what it does is it actually changes the location of our entity of our player entity to be you know whatever we specify here in the parameters so if I actually go back to our game class where we instantiate this player um I can just add I don't know like 30 comma 30 all right and run this and let's see what happens oh look at that we're not in the top left corner anymore we're over here which is awesome so in other words I can totally customize how all that works so I can maybe set this to like 200 this one to maybe like I don't know um 250 220 something like that and if I run it you'll see that I'll actually spawn there now that's all cool but what if I want to spawn in the middle like what if I want to spawn in the middle of a particular tile so in other words here you see that where actually if I just clear these and set them to 0 0 you can see that we sort of Spawn there what if I want to spawn on a particular tile well all you all you want to do there is you know the tiles are 16 size big so if you want to spawn let should say let's just open up our level again if I want to spawn right on this rock here this is actually going to be really hard to do because um it doesn't have coordinates so let me just open it and pt. net again all right cool so if I want to spawn on this like Little Rock here we can see that that rock has coordinat 64 so um because we know so 6 comma 4 right yep so 6 x and 4 Y and because we know that the tiles are 16 like 16 pixels large we can simply go 6 * 16 for x and four time 16 for y this is very basic math but um some of you guys might not be aware and if we launch this you can see that we spawned right on that rock so that is sort of how you adjust the player spawn locations again that'll be very very like important and crucial to our game in the future but um that's also basically how we're going to be loading maps and tiles from now on so what I need now is I actually asked you guys a while back to send me tiles to send me Sprite shets and a lot of you guys did um which is awesome uh but if if there's anyone out there who thinks they can make a tile sheet that will contain all sorts of tiles for a real the M God type game one thing I didn't actually check that I I did get some really high quality ones but guys if you send me ones that you make or you find make sure that they are actually like copyright free so in other words make sure that I have permission to use them all right that's very important cuz I don't want to get like I don't want to get in trouble so yeah but otherwise we'll start on actually making our spawn level probably next time and then we'll talk about things like collisions and that sort of thing and maybe some lighting as well but um hope you enjoyed this episode of game programming if you did hit that like button let's get 200 likes guys I'm serious punch that like button let's get to 200 likes if you guys are enjoying that series I appreciate it a lot if you guys hit the like button and I'll see you guys next time later [Music] guys [Music]

Original Description

Lets go for 200 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

This video teaches game programming concepts, specifically player spawns, in Java, covering error checking, Constructor review, and customization of player spawn locations. It provides practical steps for implementing player spawn mechanics and customizing spawn locations. The video is useful for game developers and programmers who want to learn about game programming in Java.

Key Takeaways
  1. Run the game to see the player spawn in the top left corner
  2. Customize the player spawn location by changing the x and y coordinates in the player class
  3. Use the Control Shift T shortcut to open the type window in Visual Studio
  4. Calculate coordinates based on tile size and position to spawn player on a specific tile
  5. Load maps and tiles from external sources
💡 Customizing player spawn locations requires modifying the x and y coordinates in the player class and using tile size and position calculations to spawn players on specific tiles.

Related Reads

📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Learn how creative characters like Sprunki inspire kids to learn coding through interactive games and animations
Medium · Programming
📰
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
Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →