3D Game Programming - Episode 2 - Game Loop

The Cherno · Advanced ·🧒 Coding for Kids ·14y ago

Key Takeaways

This video series covers creating a 3D game from scratch using Java 1.6, focusing on the game loop and multithreading with the Thread class and Runnable interface in Java and Eclipse

Full Transcript

what's up guys welcome back to episode two of my 3D Java game development tutorials uh this is exactly where we left off haven't changed anything we're just we we are ready to keep on working on this uh on this epic game um first thing I want to do is uh say that we need to do some really important things today to probably the most important things um early on early on in development and the first thing is we need to talk about threads so first thing I want to do is create a thread okay so I'll explain this code right now actually private is the visibility modifier basically private says that only this class can can use threads okay only display. Java can use this public means that anything in the whole freaking world can use it um can see it basically so private means that yeah only only display. Java can see it thread is um basically the the way to explain thread is it's it enables you to do multiple tasks simultaneously so you know at the same time um which is as you can imagine really important for games cuz we we we want to be able to render multiple things at once all all that good stuff I'm not going to go too far into it and you as you know in Java to create a thread to create an object we need to create okay hang on to use it we need to create an object and we need to give that object a name so I've just called it thread cuz that's easy to remember um and the other thing I want to create is a bullying again private um and a bullying is something that's true or false okay two possible values true or false um and I'm going to call it running and I want to set it to false because it's not running at the moment um our game isn't running at the moment not yet that is um yeah so the next thing we need to do is we need to actually initialize This Thread um and the way we do that is I'm going to create a um a a method called start um and in that method I'm going to just simply check um for something really quick I'm going to go if running return and uh this might be a bit difficult to understand so I'll explain it um this is sort of the way that I want to do this tutorials I don't want to add too much code at once I'd rather have shorter videos where I add you know like 10 20 lines of code but I explain everything for all of you guys who don't understand what this means um you know I'll explain it for you guys so that you you can't understand what it means cuz that's sort of that's the point of of a tutorial if you want to copy code from someone just you know go just download Like An Open Source game or something which you know down download the source code to that and you will be fine um so yeah so basically this is an if statement and it's basically saying if running equals true then return now what return means is it's going to exit out of the me out of the method because the reason that we type this code is because think about this if it's all already running so if running equals true we don't want to initialize the game again because it's already running so ignore that um but if it's not running then don't return in other words don't get out of this method keep doing whatever's below um and by the way if if you hit control shift F on your keyboard that's going to format your code so um in other words if you're going like 600 + 23 I don't know divid by 6 * 8 yeah control sh F and it'll set it out really nicely so yeah that's control shift f um we could put this into curly braces I usually like putting if statements and curly braces braces um but you don't need to and because it's one word return kind of looks nicer if you don't so yeah okay um so if it's in other words if it's not running then set running to equal true because we want it to start running um and the most important thing probably is to actually initialize the thread and the way we do this is we go thread equals new thread this um and again it's undefined because we need to implement runnable um and yeah no more errors it's it's giving us an error here because you know we need to we actually need to run we actually have to make a method here called run public void run and there we go no errors yay um okay so that's pretty simple and the other thing what you want to do is actually start the thread so that's how we initialize a thread um and the other thing that I uh the other thing that I want to add at this point is actually a stop method as well this isn't really necessary but because I want this to uh to run as an applet later um and basically what an applet is it's it enables um this to be run inside a we browser so the the I reckon the most popular example is if you think about Minecraft if you go to minecraft.net you can actually play the game inside the web browser that's because it's an applet so I you know it's always good to be able to do that in the future so that's what I want to do right now so I'm making a stop method so this is basically the reverse of the start method um the exclanation mark means not so if it's not running then don't worry about this method get out of this method if it's not running because if it's not running then we've already F we've already fulfilled our duty um so if it's not running then set running sorry if it if it is running and we want to stop it then set running take false and thread. join um and we actually need to we need actually need to try do that you can see that it says um you know surround with try and catch I'm just going to type it out so try to join this thread and if that fails then simply catch an exception and this is this is just normal Java syntax and then we want to exit it obviously yeah so that's pretty much that's our start and stop method how much time are we at 6 minutes brilliant um quickly explain this uh so was going to try and join uh and join is it's going to wait it's going to wait for the thread to die so it's going to try and end the thread if that does not work then it's going to generate an exception and and again we're creating the exception object we've called it a um what it's going to do is it's going to print St stack Trace which is really hard to explain and then it's going to actually exit the program so yeah um that's our start and stop thing um and finally I also want to create the main game Loop game Loop so how are we going to do this we'll make a while loop so while while running equals true do whatever's there and we'll we'll talk about that next time um the other thing I want to talk about is actually um I I really like to check if this code is currently operational in other words if it's working because if i' if I code for an hour and then I go back and then I'm like hang on this this this isn't this doesn't even work you know and obviously it's not going to it's not going to throw us an error because the syntax is correct um so say if we had an error like that so if it's not running blah blah blah um then if we run it you know we're not going to see the difference it's running it's fine running is here by the way um so the way we um the way that I like to check it is simply by going sis oh hang on I should probably um actually initialize that method um so as you can see we're not actually using a stop yet we'll we'll do that later when when we go on applet um yeah so if it's if it's okay so what this code does is the same game this is the good thing about Eclipse as well if you hit your um if you put the the typy thingy on a word it's going to highlight all the words of the same name so we can see that game is the object of this display class so it's going to it's going to it's going to search this class for the method called start which is right here and it's going to obviously you know run that method so that's really important and what we need to do is we need to to check the code best way to do it is just print print a line and I don't know working working and what that's going to do is if we run it as you can see I've made an error here on purpose we going be like hm it's running but it's not working why is that then you go back and you're like oh right of course I put this here run it and T yay it's working um so that's that's sort of like a way to test it we don't we obviously don't need this um but yeah so that's that's the way I like to do it um and that's probably the end for this tutorial um next time I think we'll get started on some graphics stuff I'm not sure H yeah we probably will so make sure you watch the next tutorial which will come out tomorrow tomorrow I'm going to try and make one of these every day um and yeah so tomorrow we're going to try and let me just format this um tomorrow we're going to try and Implement some graphics things maybe actually draw some pixels to the screen so that'll be exciting and yeah that's it so I'll see you guys later bye [Music] he

Original Description

Episode 1: http://www.youtube.com/watch?v=iH1xpfOBN6M Welcome to a series of videos where we will create a 3D game (probably a first person shooter) from scratch, using just the included libraries in Java 1.6. If you have any questions or problems, leave a comment or send me a message, or tweet at me. Tell me if you want more! Twitter: http://twitter.com/#!/thecherno Livestream: http://www.twitch.tv/thecherno Eclipse: http://www.eclipse.org/downloads/ (Java IDE for Java Developers).
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 2 of 60

1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
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 how to create a game loop and use multithreading in Java 1.6 for 3D game programming, using the Thread class and Runnable interface in Eclipse. The game loop is a crucial component of any game, and multithreading allows for more efficient and responsive game development.

Key Takeaways
  1. Create a thread object
  2. Set a boolean variable to track the state of the thread
  3. Implement the Runnable interface to initialize the thread
  4. Use an if statement to check if the thread is already running before initializing it
  5. Create a start method to initialize the thread and a stop method to stop the thread
  6. Use a while loop to create the main game loop
💡 Multithreading is essential for creating a responsive and efficient game loop in 3D game programming

Related AI Lessons

Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →