OOP with Java: 13. Tetris Game with Java Swing (Part III. Spawning new blocks)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome back in this video we'll continue working on the tetris game that we started a couple videos ago currently our program can drop a tetris block at a certain time interval and when the block touches the bottom edge of the play area it stops in this video we'll make our program spawn a new block when the current one stops so let's get started shall we we already have spawn block method in the game area class we currently call it inside the game area constructor which means that the method gets called only once at the start of the game which isn't what we need so the question is where should we call the spawnblock method instead since we need to call the spawn block method multiple times during the game the game thread class seems to be a good choice so let's remove the spawn block method call from the game area constructor switch over to the game thread class and think about where in code we should call it if you think we should add a spawn block method call inside the run method you're absolutely right we're exactly inside the run method though inside or outside the while loop i didn't really mention this explicitly but this while loop is our main game loop it runs as long as we keep playing the game and yes you got it we should call the spawn block method inside the while loop but we only want to spawn a new block when the current block stops moving down how do we check whether the current block can or cannot continue moving down any ideas well i know that you know that i will answer this question in a few seconds but instead of waiting for me to answer why don't you pause this video and try to find the answer on your own come on don't be lazy so what we can do is we can modify the move block down method so that it returns true if the current block was successfully moved down and false if that did not happen before we actually modify the move block down method let's pretend that we have already done that and the method returns a boolean value so what we can do is we can take this move block down method call out of the try block add another while loop which will run until the current block can move down and while that is happening we will have the game thread object wait let's now switch over to the game area class and make the necessary changes to the move block down method so if the current block cannot be moved down let's have the move block down method return false and otherwise let's have it return true if we now switch back to the game thread class we will see that netbeans is no longer yelling at us we can also shorten the while loop condition like this alright let's run our game to see if it works well now when our blue block hits the bottom edge of the game area the game doesn't stop as it did before but the problem is that the block doesn't stay at the bottom of the game area and sort of teleports back to the spawn position to be more precise the blue block that hits the bottom edge of the game area and the blue block that appears at the top right after are not the same tetris block objects how do we know that well if we look at the spawn block method we will see that every time the method is called it creates a new tetrablock instance and assigns it to the variable block in other words when the current block hits the bottom edge of the game area the game thread creates and spawns a new tetris block and the reference to the new tetras block object is stored in the variable block while the reference to the previous tetras block object is lost and it never gets drawn again because the drawblock method is responsible for drawing the tetris block object pointed to by the variable block it always draws the most recent tetris block object ignoring the previous ones so what we need to do is we need to have a program draw not only the currently falling block but all the blocks the thing is when a block stops falling it effectively stops being a block i mean if a block stops falling we can no longer move it and it can be partially removed from the game area when they make up a complete line in other words we do not need to treat the blocks that have stopped falling as tetris block objects which makes the task of drawing them somewhat easier let me show you what i mean we can imagine that the game area consists of background and foreground parts in the foreground we can have a falling block while in the background we can have the blocks that have stopped falling and we can treat all the blocks that have stopped falling as a single object just a pile of colored squares now the question is how do we represent that background what data type do we use actually we can use a similar data type that we use to represent blocks an array of arrays but not int arrays because we'll have tetris blocks of various colors we can't really use ones and zeros to represent the presence or absence of the color what we will use is an array of arrays of the type color in the game area class let's declare a member variable for the array and instantiate the array to default values inside the constructor now what would be the length of the array the background array must have the same dimensions as the game area grid so the background array should be instantiated with the following length if you're not sure what just happened on the screen i recommend that you pause the video and try to make sense of the background array now in the game area class we have the draw block method that is effectively responsible for drawing the foreground which means that we also need a method responsible for drawing the background let's add a new private void method and name it draw background just like the drawblock method the drawbackground method needs a graphics object to be able to draw and before we forget let's call the drawbackground method inside the paint component method like this so the draw background method will be responsible for drawing the contents of the background array in the game area and the code for that is very similar to the code of the draw block method now color is a reference data type which means the default value for color is null we can interpret null as no color so we only need to draw if the color is not null these four lines of code responsible for drawing a single square of the grid is largely copy pasted and what do we do with the code that gets copy pasted right we make it more annoying by moving that code into a separate method let's add a new private void method named drawgrid square and leave the parameters list blank for now let's now move these four lines of code to the draw grid square method to see that it needs a g a color and non-green x and y so these are what the method should get past as parameters let's now call the method inside the draw background method and inside the draw block method to see if this works let's scroll up to the constructor and set the zero zero element of the background array to say green now if we run our game the grid square in the top left corner will be green and it is green before we move on let's delete this line of code we don't need it anymore what we need to do now is we need to have the block that can no longer fall down become a part of the background to make sure it won't disappear from the screen for this let's add a new method to the game area class the method should be private and void let's name it move block to background long but more or less self-explanatory what this method will do is it will check every element of the current block's shape array and if the element is equal to 1 which means that it's a colored cell the method will set the corresponding element of the background array to the color of the block before we move on please look at the code we just added and make sure you understand how it works you might need to refer to the previous videos if it's hard for you to follow please do so if you need now where do we call this method at what point during the game do we move the block to the background right when we can no longer move it downwards so we can call moonblock to background method inside the move block down method right before it will return false and if we run our game now we will see that the block doesn't disappear after it stops falling and our program gets more and more tetris-like so step 3 complete alright let's summarize what we did in this video because the drawblock method only draws the currently falling block all previous blocks do not get drawn in the game area to fix this we added an array of color arrays to the game area class and the job of the array is to represent the background of the game area the background array is then drawn in the game area by the draw background method just like all dogs go to heaven all blocks eventually go to the background array alright so this was a relatively short video but before you move on to the next one please make sure you understand how the code that we added this time works and this is it for this video in the next one we'll make our game respond to keyboard key press for example we'll have it move the following block left and right when we press the left or right arrow key on the keyboard see you then bye you
Info
Channel: MrM Programming
Views: 1,341
Rating: undefined out of 5
Keywords: java, java tutorial, programming with java, how to code with java, object-oriented programming, object oriented programming java, oop, gui program, gui, netbeans gui, tetris, tetris java, tetris java swing, method overriding java, 2d arrays java, java tetris tutorial, oop with java, writing classes, writing methods, netbeans tetris, reference type variables, code refactoring
Id: 00uDZsAmr8o
Channel Id: undefined
Length: 12min 9sec (729 seconds)
Published: Fri Jan 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.