Game Development in Python 3 With PyGame - 8 - Score, challenge, conclusion

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

Develops a game using PyGame with Python 3 and adds a scoring function

Full Transcript

hello and welcome to the eth pame tutorial video with Python 3 where we left off uh we generate our blocks we uh crash if we run into the blocks or the side of the screen and now what we want to do is we want to have some sort of scoring system and then we can take this scoring system not only to track our progress but also measure our progress uh in the game and then increase difficulty based on the progress so uh with that let's go ahead and get started so the first thing we're going to want to do is um we want to have some sort of way of doing a a scoring system okay so probably the best thing to do is like you just put the score in like the top left or something like that or the top right so what we're going to go ahead and do is we're going to go we're going to put it in the top left for now and uh what we're going to need first of all is some sort of function that's going to do that for us so I'm going to call mine Define things unor dodged and this function is going to have one parameter and that's going to be count so like how many things have we dodged and what we're going to do is basically every time a screen is res or a block is reset uh we're going to say well we dodged it so we'll add one to the count but as far as displaying um this count uh we just need to say font and Define font and we're going to use py game. font. CIS font um so that's just this default system font basically none and then we'll do 25 and then we're going to say text equals font. render and we're going to render to screen do oops dodged uh and then colon and then we'll add a space here plus string of count so the string version of you know the the count whatever it is and then we'll use uh anti- and then we're going to use the color black uh for things dodged so that's going to put it on our screen now we actually have to um calculate that so before our Loop starts uh we're going to need to we start at zero so we'll say we'll just say dodged equals zero so we have a starting number here and then every time we reset so we asked this question like if thing um is greater than the display height so we're asking there if um if that's happened then we then we run this right the next question or the next the thing we'll do here is we'll say dot yeah dodged plus equals 1 and then um that's all we really need to do there and then also before or I guess I guess right after we draw a car since this is where we're kind of drawing everything thing is like right around here um we'll do that as well so we'll say um what was it things uncore dodged and then the count was just this dodged variable things underscore dodged I believe is what we used yeah okay so that should display to our screen um our score hopefully and it did not close this close this p things dodged dodged [Music] um oh what we what we forgot to do is we didn't blit it to the screen so every time um that we want to we can make something and then we kind of put it in this in the background and then we'll display it so we have to blit so to do that we're going to do game uh display and then we run blit what do we want to blit well we want to blit text and then where do we want to blit it and that's going to be uh 0 0 so save let's run that again there we go so now we have dodged up here and watch out okay so now it's going up every time we actually Dodge one of these things so cool now we'll close out of this okay and so now we've got the score that goes up every time that we Dodge something but the game is still pretty boring yeah we're tracking progress but we're not actually doing anything with the progress so one of the things that we can do is we can just make this progressively more and more difficult so we can come over uh here and every time we do dodged plus equals one we also know that well we've dodged one so we don't have to actually like reference that variable at all we just know that like it's been reset so the other thing that we can do is we could say uh thing we could well first of we can do a couple of things one we could speed up the things so our things our things things I have a speed of seven right now let's make it three to start um or four uh thing speed what we can do is we can say thing undor speed plus equals 1 so every time we have one to our score we're also going to do plus one to the speed of the objects so we'll save and run that so it starts off relatively slow kind of starts speeding up getting faster even faster and just each time we Dodge one they get a little quicker on us pretty soon this is going to be impossible to dodge if you get one at anyway so you can do that and pretty much make your game nearly impossible um the other thing that we can do as well is thing speed plus equals one um for now uh I'm just going to comment this out um we can we can increase that in a variety of ways the other thing we can do is we can change width and height of the things that we're trying to avoid so for example we could do thing the hardest one would be width but if you change the height it just means that you have to wait that much longer to get somewhere generally the safest place to be would be like right in the middle as we get things bigger but you'll see what I mean in a second so we can say thing width thing underscore width uh plus equals and then we'll grow it compared to our score or our dodged amount so we can say plus equals dodged time like 1.2 or something like that just to slowly make it a little bigger a little bigger a little bigger so we'll saveone in that and so you can see the first one obviously is 100 um 100 pixels that one got a little bit wider or at least it should have I can't tell visually if they're getting wider or not we did do plus off the screen man I want to see one on the screen yeah it looks like they're definitely getting slightly wider that was no longer a box this would go a lot faster if we uh um sped up the pace as well but yeah you can see they're getting definitely wider and wider and wider still easy to avoid for the most part and that's why I kind of like to pick up the pace a little bit um just to add slightly uh more challenge to it so for example um we'll just exit out okay and we'll also do things speed plus equals one and get started here so nice slow pace to begin and then things start going getting out of hand here pretty soon probably not too bad at start huh it's pretty easy no problemo oh my goodness little harder a little harder still maintaining my middle position oh oh my God they're moving too fast oh goodness challenge anybody to beat my score oh man that was impossible So eventually the game gets pretty much impossible but just want to give you guys a couple ideas about how you can kind of increase the difficulty you don't have to increase it even this fast you could you can change thing speed doesn't have to go up by one either you could go slower than that um so the next thing I want to do is just cover a couple like for example as you saw if the blocks get big enough or you crash into the side our text isn't really the best color for text or maybe it's our blocks that aren't the best color for our blocks so now I just want to cover really quickly um we can change a few things first of all you always want to make sure um like for example your score is drawn last that way nothing overlaps the score so you always want to make the score drawn last because when someone crashes or something if something's overlapping their score they're going to be relatively angry because they can't see their score so uh you want to do that that and uh luckily we're already doing that but then you could change the color of the score too so our score is black so something that's black covers the score we're not going to we're not going to see it anyways like they're going to mesh so we want to change the color now at least for me you can go on to like various uh websites and you can find like you can look up RGB color code so you can get like a color wheel and select a specific color um for now I'm just going to uh give you guys some but yeah you can look them up or you can even go to like paint and usually you can like uh pick a color and paint like this and then you can just take the red green and blue values of of that as well so you can do that um so I'm going to say uh block underscore color equals and the color I was going to use was 53 115 255 and then block color we'll copy that and then we're going to come down to where we draw our things um and color will just be this right here so we'll say block color or actually well we're passing through color so actually we need to do that when we call things here so we call it black we'll say BL color so we can save and run that and now we have like this blue okay and now you crashed it could overlap the blue wouldn't be a big deal our score is drawn last so we'll always see score um and all that so you can obviously change the color and uh do that kind of stuff so that's really the basics that I wanted to cover with P game obviously there's a whole lot of stuff that you can do with pame you can make um all kinds of games you can check out py game.org and just look at all of the games people make hopefully now that you have a lot of the basics of pame down you could look at various games and kind of see how they do it um someone did ask me to give a slight challenge at the end of some of my uh series here and so I think the challenge I'm going to uh give you guys is adding multiple blocks without hardcoding it so what I mean by that is um you have maybe a counter here that says uh thing count and you obviously start at one but one of the other ways that we could increase difficulty is by increasing the number of blocks not increasing their size necessarily but just adding more to to have to dodge and so and so you could do that instead of and increase size and speed or do some sort of combination of those things so my challenge would be adding more uh more things uh to a avoid without hard coding them in as in not doing something like things and then making a function for like uh like this like things two and things three like not building a function for that but just adding them in like a for Loop or something like for um for thing in range of thing count and then creating them that way so good luck anyway um I might add on to this probably not uh I just wanted to get one out there for uh Python 3 so anyway if you guys have any questions or comments or have ideas on how to improve uh this game pretty sure we could also improve this function here I believe um this statement here there is probably I think there's a flaw in this statement as well I'll let someone it's just redundancy really in this statement that we don't really need to ask but uh anyway um I'll leave that to homework as well if you guys want but anyways uh if you guys have any questions or comments uh or improvements feel free to leave them below as always thanks for watching thanks for all the support and subscriptions and until next time

Original Description

Here, we wrap up our Python 3 with PyGame game development tutorial. We add a scoring function, as well as increasing the game's difficulty using a few different options to make the game harder. I hope you all enjoy, and I encourage you to keep building on top of it! See http://pythonprogramming.net for sample code PyGame with Python 3 Playlist: http://www.youtube.com/playlist?list=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO http://seaofbtc.com http://sentdex.com http://hkinsley.com https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related Reads

Up next
Wix AI Tools Explained: Complete 2026 Guide
TheFigCo
Watch →