Pygame Tutorial - Tetris (Part 4)
Key Takeaways
Creates a Tetris game using Pygame
Full Transcript
hey guys and welcome to the fourth video in my PI game Tetris series in this video I'm gonna be showing you guys a few different things so in the last video I mentioned I was gonna be doing more cosmetic stuff so that is true in this video I'm going to be showing you how to do the main menu screen how to draw text in the middle the screen how to make things look a little bit better and I'm also gonna be adding a little bit of functionality to the game in terms of making the piece fall faster as it moves or as time moves forward so obviously we want the game to get more difficult as time goes on so what we're gonna do to do that is increase the speed at which the piece falls down the screen it's pretty easy I'll show you how to do that I'm going to be adding score and we're gonna be displaying that score and that's probably about it for this game so the first thing I want to show you is how to add the piece falling faster so to do this we need to create a variable in our main here and I'm just gonna make this equal to what are we going to say fall all right let's say level underscore time okay and this is gonna just represent how much time has passed and it's gonna be similar to full time and level is just because as we increase speed like the level is going up kind of whatever okay so say level time plus equals clock dot get underscore wrong time and we're gonna do exactly what we did with making the piece move down the screen except we're just gonna change the speed at which it moves down the screen so what I'm gonna do here is when I make another if statement I'm just simply going to say if level underscore time divided by a thousand is greater than and then you can put in whatever time you want here now I'm gonna do five seconds so every five seconds we increase the speed okay and then we can decide what we want to increase the speed by so I'm gonna say a level underscore time we need to first reset this back to zero so that we continue to do this and then what I'm gonna actually do here is gonna say if level underscore time is greater than and we want to put like a terminal velocity here so at what speed do we stop increasing the time because we obviously we can't get to a negative false speed otherwise you're just gonna instantly fall and we don't want it to get too fast that the game's unplayable so we need to pick a speed now a good speed that I kind of found was 0.12 because anything faster than that is like pretty fast but I mean you guys can play with that and change that around it's really easy number just to fix right and then if we're greater than that we're just simply gonna subtract a value from level underscore time now this value the magnitude to the size of this value that you're subtracting is gonna determine like how quickly you increase the speed as well as obviously at the time so I put originally zero point zero one and I mean depending on how long it takes you to play a Tetris game this is pretty fast because that means after 50 seconds you're already gonna be down to zero point one seven which is very quick and almost at terminal velocity so I recommend you do 0.005 that means it'll take about a minute and 40 seconds before you're gonna be close to reaching this terminal velocity which might be good if you're playing Tetris for a decent amount of time okay so you guys can play with those numbers but that is going to move it down faster I would show you but it's gonna take like a decent amount of time for to continue moving faster to just play with that okay just trust me all right the next thing we want to do is add score so we need to first create a score variable and I'm gonna say score it's equal to zero like that and incrementing score is pretty simple I'm gonna say and you guys can make this whatever you want whenever we clear a row I'm gonna add ten to our score because you had ten pieces or ten cubes or whatever that you cleared okay so actually the way that we need to do this is we're going to return a value from clear rows which is going to be equal to score now I'm gonna go to clear rows and in here I'm simply just gonna return i NC okay and I NC is the increment so how many rows that we actually ended up clearing so if we clear more than one row we're gonna get a value of two we can multiply that by whatever our score increments gonna be and then yeah we can just add that to the score so this is pretty straightforward if we go to clear rows we're gonna say score is equal to clear rows multiplied by ten all right or actually not equal to we're gonna have to say plus equal to clear rows plus ten now what we need to do is we need to display this score on the screen somewhere the user can see then we're actually going to save this score as well when we lose the game okay so the way that we can display this is we're gonna have to do it in draw window but we're gonna have to pass this score to draw window so after our grid object we're gonna pass score like that and we're gonna go to draw window and we're simply going to add another parameter here score like that now I'm just going to set it equal to zero as a default parameter so in case we call it somewhere else and we forget to add that score that's fine we don't need that so what I'm gonna do now is I'm actually gonna go to draw next shape and at draw next shape you can see if I run the program quickly here it shows us a little text object okay now I want to get this similar text object so I'm just gonna copy this fall into the label SX sy and I'm just gonna bring it down here into draw window now it doesn't really matter where you draw it I'm just gonna plop it in the middle here and obviously I'm going to change this from next shape to be score now we need to obviously display what the actual score is so we can just say plus str score like that and that should be displaying for us okay so now we have SX and sy which is giving us the play area I believe or no this is actually the position in which we wanted to draw it okay so what I'm gonna do is I'm gonna say win dot blitz or is it surface stir this dog blitz our text object which is the label and then where do we want to draw it to and draw SX sy now I'm just gonna have a look up here to see where I did it here so SX plus 10 sy minus 30 so what I might want to do is we can add plus time to this so it's aligned but then we're out gonna subtract we're actually add to the Y so it's lower so I'm gonna add let's say like 80 like that so let's test this out and see if it's working score so it's not low enough so we'll add this I do 120 and see what how where we're getting enough scores again although enough to go 160 and again you guys can play with these our score equals zero perfect and then maybe I'll just add 10 more pixels here so it's more in the center and you know whatever that's good enough for me okay good so that is working well so we've got the score and we were moving down faster now what else do we need to do what we need to do now is work in main and you and to figure out what happens when we actually lose the game so we have this check lost and all this simply does is just quit the game for us when that happens but we want to do something else when we lose and we want to display something to the screen so actually before I go and main menu I'm gonna go up here to a function that I told you guys I wanted to hear that that I was going to finish which is draw a text in middle now this is gonna take a service text and what else do we want say size and color okay so surface text size color this way you can use this in another function or oh sorry and another program and you can use them multiple times with give every text size and color you want so we're gonna set up a font train say font equals PI game dark font dot sys fonts by now you probably know this I'm just gonna pick Comic Sans we're gonna make it equal to size and bold is gonna be equal to true now you can make this false if you want or just omit it completely and then it won't make it bold pretty straightforward make a label label is equal to again font render and then we're gonna say text one for anti-aliasing and color like this okay now we'll just simply draw this in the middle of the screen so to do this will say surface double its label comma and now we're gonna do a little bit of math here so talk underscore left under short X plus play underscore width divided by 2 not three - and we're gonna get the label width and the way we do this I don't believe I've actually done this yet is get underscore width I think I'm just gonna check yeah get width and then the reason I have this in brackets because I'm gonna divide this by two as well and this will essentially give us the middle position where we should be drawing in terms of x-coordinate okay so now I'm gonna give top underscore left y plus play underscore height divided by two okay and now same thing - label dot get underscore height in brackets and / - I don't know why can't tab over here okay / - and that should hopefully be working for us okay so we can test that I'm just a second so we've got this it's bleeding now whatever text we want to the screen so this makes it really easy for us to draw anything to the middle of the screen so the first thing I want to draw to the middle of screen is like a you lost message so when it says you've lost what I want to do is draw obviously you lost and tell them that so let's say draw text middle and then what do we want to draw well let's simply draw what surface first actually that we need so we'll say win and then we're going to pass it text and in this case we're gonna say you lost exclamation point sighs let's go for E and color let's go wait and you obviously you guys can play around with that now after I draw that and I display the screen or update this screen by game display update just cuz we haven't called the window yet draw window when we do this then what we're gonna do is we're just gonna delay so I say pygame dot time delay and I'm just gonna delay one and a half seconds just so once this comes up the user has a second to look at it because then we're gonna move back into our main menu screen which you'll see okay so I'm just gonna fast forward this until I lose and then you guys can see that it's working and there we go so we got you lost and then it's late and then just quit because I have this PI game dot display dot quit here okay so now we're gonna move to is main menu everything else is working I told you guys it's not like super difficult a lot of this stuff and it main menu what we're gonna do now is we're just gonna make it pop up whenever you start the game and whenever you lose and just gonna say hit any key to like move forward to continue okay so I'm just gonna set up another game loop in here so we're gonna say run it was true well run and this is gonna be really similar to the loop we've done above it just cept just a lot shorter we're and we're gonna just draw in here without using a different function just because we're not drawing very much so it's not important that we wouldn't call it like update the screen with a defined function so I'm just gonna fill with black just so we kind of are getting rid of everything we're gonna draw text middle and what I'm gonna do here I'm gonna say press any key to play okay and then we'll give this a size of 60 and a color of white once again okay so that's being drawn in the middle we'll update the display so PI and up display don't update like that and then we're gonna say for event and PI game dot event tech gets a similar cool you've already typed up top we're gonna say if event type equals equals PI game got quit we always want one of these not quit Q then we are simply going to say run oops run is equal to false and then we can tab out here and we can just make sure that we are actually quitting the display so don't quit and we'll get rid of this PI game dot display dot quit and then we're gonna add one more event and this event is gonna be a key down so whenever they press any key on the keyboard then that is going to move this forward and we're simply gonna call this main function where we'll play and I'll explain how this kind of flows in just a second because it's really nice okay so if event dot type equals equals PI game dot all capitals key down so if they hit any key then what we're gonna do is we're gonna say main like that okay and I believe that is about it okay yeah so let's see here my game don't display that quit looks correct okay so the way this is gonna work pretty much is when we run the game we start by running main menu so we run main menu and then we go into this main loop where all its gonna do is just say press any key to play and that's gonna be drawn in the middle of the screen once that happens we're gonna check if they're hitting any key if they hit any key we're gonna call mate and in which case sir let me close this so it's easier to see if we hit main then we go into the main game loop and we go and we do this loop until eventually we lose once we lose it displays you lost on the screen makes this loop equal to false which means it brings us back into this main menu loop and we can play again by hitting any key pretty straightforward okay so let's test this out to make sure this is working draw text missing one potential argument color ah because I didn't give a surface my bad so let's just do win and there we go okay so press any key to play press space missing one protect me okay sorry guys and this is why we test our program okay win and now let's try there we go and now we're playing it shows the score and you guys can mess around it I haven't actually tested the score so I'll just do this really quickly and see if I can clear row without having to fast forward which we may be able to do not using that one okay let's see there you go and score is equal to ten so the score system is working as well and that's relief because I hadn't even tested that before I put this in the video okay perfect oh when you press the X brings you to press any key to play you press it again you can do that so if we wanted to change it so when you hit the X key in the main thing and you want to close the game then in the main loop here sorry what you're gonna do is sort of saying run equals false just do PI game let's display dot quit so you can do both you and say run equals false and then you can quit the display as well okay so what's next so I said we were going to save the score and potentially display that so if we want to save the score we're gonna have to do something when we lose so we're gonna have to update like at the current score and we're gonna use a text file to do this so that you can play multiple times so what I'm gonna do now say new file just create a new text file and he's gonna call this scores dot txt okay and here I'm just gonna put a zero as a default value it's really important you put a default value to start otherwise you might run into some issues so just create a new text file make sure it's in your directory with the main with the script okay now what I'm gonna do is I'm gonna create another function it doesn't really matter where it goes so I'm gonna say define update underscore score and we're gonna give it to score okay now in here what I'm gonna do is I'm gonna say with open and this is how we're gonna open our text file and it's like update and display with open squares text in R so in read mode and this action needs to be in quotation marks as well we are going to say F yeah I have to do this as F sorry I say F dot read lines like this okay and that'll be equal to lines is equal to F dot read lines so that means our score is going to be well the first line so score is equal to lines zero and we're gonna add this dot strip so that we remove any like backslash ends because if you don't know in a text file when you hit the next line the way that it actually tracks that you have another line is it puts a character that looks like this on the line it's invisible to you in the text file when you're viewing it but when you read it in with code you actually read in this character so we just want to make sure we strip that off in case it is there I'm gonna say score equals this and then we'll just say okay so we've done that and since we're topping out now at all it's gonna automatically close our file what we're gonna do now is we are going to open the file again except in this time we're gonna open it right mode which means we're gonna overwrite whatever is in the file and we're simply gonna say if score is I guess I'm gonna change this to end score standing for new score if score is greater than n score then we'll say F dot and we'll say write lines actually right I think and we'll simply put in STR of score which is this updated score otherwise so if that's not true we just have to write back the other score because we're at clearing it when we open this with W mode okay so rinse it F dot right and then or sorry this is gonna be my do and and score yeah sorry so I kind of messed up in this way it should be the other way around but whatever we're just checking if the original score is greater than the new score if it is we'll write the original score otherwise we'll write the new score so this works still it just not the way that I was thinking about it when I was kind of talking about that okay so update score should work and then you know you can actually return let's just return actually a naught we don't need to return anything so I haven't pre written this yet so I'm kind of still just going through how I want it to work okay what we'll do here after we set run equal to false we'll say update score and we'll give it whatever the current score is and then this should be working in the scores dot txt file so let's run this I'll fast forward through and make sure things working [Music] okay so we got an error not support it between instance of string and int okay so if score greater the end stress which mean we have to convert the student int so if we go back up to score and where did I make this okay score is going to int score like that okay and that's hopefully work because that score comes in as an int and we'll run it one more time one more fast forward sorry without guys [Music] okay and another issue lines zero Linda list index out of range okay I'm gonna have a look at this and come back okay so this code is actually working just fine the issue was that since the last time when I crashed we didn't end up writing anything into the file so we had a blank file and then we were going lines to zero where is it here and obviously there was no line 0 so that crashed but if you just add any numbers like my add 0 back in here two squares dot txt and save it then this works fine okay so that's it for saving scores now we need to display those saved scores so I'm kind of gonna leave this up to you guys where you want to display it I don't really know where I want to put my saved score like high score maybe on the left side of the screen it doesn't really matter so if I want to draw it on the left side of the screen then I can do something like we're gonna have to first get this max score right so I'm gonna have to make a new function called define max score or last score or whatever you want to call it and if you call max score what we're gonna do is we're just gonna do the exact same thing we did here so we can say it with open score set text score zero slide your strip accepting this time we're gonna return score like that okay now we actually do here to is we could just simply replace this whole line with max score and just say score equals max score be the efficient way to do it and I mean feel free to do that if you want orally but the other way okay so now we have something that's getting us max score so how are we gonna use this well when we call main I guess when we call me or we can call main menu does it like you guys if you want to draw it in the main menu can do that otherwise draw it in the main to draw it in the main all you're gonna do is gonna say like last score is equal to u max score which is going to be whatever is stored in that text file and then to draw this we're gonna have to pass this again into when we read wrong'd window so in draw window I'm just gonna say last score we can go up to draw window again say last underscore score equal to zero and you're pretty much it gonna copy exactly what we have here with this font and the split so let me just comment this and say this is current score just so I could remember you know how to comment say lost score high score whatever you want to say here and then we can just say score and we actually get rid of this font because we already defined it will say high score equals and in this case last underscore score okay draw that to the screen this is probably not where you want to draw it you probably wanna draw it top left X minus like some value like minus 30 top left Y maybe plus I don't know like 300 or something so now we can see where this is actually gonna draw if we check this out so let's run this press any key so it's not obviously showing up where we want to so we have to subtract more from this so let's subtract like I don't know 100 and then this was plus too much let's see plus 200 and high score so again like you got to move it over more so we'll move over to go under this time and hopefully this works okay so high score zero scores here and that's obviously because at the current moment we have no our score is just zero in that text file okay so I think that is pretty much it for this video if you guys have any questions any errors anything that I did in this video that you weren't sure about leave a comment down below we'll be sure to get back to you also quick side note here I do have a discord server I forgot to talk about it since I released like a channel update thing but if you guys want to join that I help out a lot of people you can chat with me it's a cool kind of community there's a link in the description completely free and also if you guys enjoyed the video and you want to support the channel and more than just liking and subscribing I do have a patreon account and I would really appreciate any donations that come from that anyway so that's been it for this video if you guys enjoyed please make sure you leave a like and subscribe and I will see you again in another tutorial [Music]
Original Description
In this series I will be coding tetris in python with pygame. I will be walking you through a full tutorial on creating tetris with pygame. This pygame turorial is meant for intermediate programmers with some experience, but everyone is welcome to follow along.
In this specific video I will be walking you through some more cosmetic aspects of the game, scoring, increasing difficulty and saving high scores.
Starter File: https://techwithtim.net/tutorials/game-development-with-python/tetris-pygame/tutorial-1/
Full Code: https://techwithtim.net/tutorials/game-development-with-python/tetris-pygame/tutorial-4/
**************************************************************
Support the Channel: https://www.patreon.com/techwithtim
Join my discord server: https://discord.gg/pr2k55t
Twitter: https://twitter.com/TechWithTimm
Need Help With Python? https://www.fiverr.com/techwithtim
**************************************************************
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Pygame
- Python Tutorials
- Pygame Tutorial
- Pygame tetris turorial
- Tetris tutorial python
- Pygame tetris
- Tetris Pygame
- Tetris in Python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 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
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
Related Reads
📰
📰
📰
📰
Cultivate Curiosity in Tech
Dev.to · Fabio Sarmento
From Apple Health Data to Clinical Storytelling: Building an AI-Powered Report with Python and Gemini
Dev.to · Romina Elena Mendez Escobar
Gemini Notebook: Vet Articles Before You Save
Medium · AI
Claude HUD: Adding a Terminal Heads-Up Display to Claude Code
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI