Game Development in Python 3 With PyGame - 18 - Game Over

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

Key Takeaways

PyGame with Python 3 is used to create a game over sequence, implementing a crash function with buttons for play again and quit, and discussing pause functionality.

Full Transcript

Hey guys, what is going on? Welcome to another Pi game tutorial video. In this video, what we're going to be doing is kind of repairing our crash sequence. So, we now know how to make these cool fancy buttons. So, now we want to use the cool fancy buttons. And we got a nice pause and we can continue and all that, but the crash is still ugly. It's got this like forced weight period and it's just nasty. So, we want to fix that next. So it just so happens we have a crash function. So what we can do is we can take the exact same functionality and idea of these paused sequence and we can transfer that to crashed. Before we get there though, I do just want to say that there are two methods for pausing things of course. One is the method we're using where it clears the screen and we don't see anything anymore. And then the other is like our crash sequence where it says you crashed but it does not wipe out uh the current score. So we hit P for pause but we see it cleared everything. We don't see the car anymore and we don't see the block. And that's okay in some respects. Like if you're playing Tetris it's kind of cheating to press pause and then think for a minute like okay what I need to do is put this object here and so on. This game isn't quite the same. I mean you could cheat a little bit like that. Um, but I will just show you guys obviously like with pause um to change the uh the way that works instead of uh filling the display with white all over again you could just choose to not fill white. I mean that's completely fine. Um, and what you could do is you'll notice that if we run this, um, and we hit go and we pause that real quickly, like it's not, it doesn't look horrible, but it's a little grainy. Um, so ideally, because it's just like pasting, pause, pause, pause, pause, pause right over itself over and over and over. Um, so obviously that might be like a little messy for for some folks, but um, so there's obviously a couple things you could do there. Um, first of all, you you know the buttons I you would kind of want to leave the buttons there because um, they are they're reactive buttons, but you could at least move all of this, right? You could just cut and then basically paste right up here and then shift this over with a control opening brackets. save and run. And now when you pause, it's not it's going to keep pasting these buttons for sure. Um, but that's not as big of a deal because well, how is the button actually drawn? The way the button is drawn is first we draw a rectangle, then we put the text on top. So in reality, these little surfaces of our buttons are constantly being uh overwritten. So that's why the text on our buttons doesn't do the same thing that our text here had done because it's actually is being cleared um just because of the button being re redrawn every time. So uh just keep that kind of stuff in mind. But anyway, so that's our pause function and now let's actually talk about uh what we we're going to talk about here and that is the crash function. So the crash function can be very much sorry I got such an itchy back. um basically this exact function. So once again we will just copy this function copy and we're going to go up to crash now and instead of message display you crashed and suck we'll just paste this instead of paused it's you crashed and uh we'll say crash equals true. Okay. While crash um Oh, you know what? Well, we have to um take crash and we need to put that to the top. So, where did we put pause? Here it is. So, paste crash equals. Actually, you know what? We don't need to do that. We don't need to do that. So let's get out of that real quick. Uh and the crashed function actually we can just do while true. Aha programming I win probably. Anyway you crashed while true we run these events. Simple enough. Now this will say play again. Again we might run over that button. So we might have to make the button bigger but that is okay. Play again. Quit. Instead of unpause, the play again event is actually uh game loop game underscore loop. So hopefully we can get away with this game underscore loop and true. I think we'll get away with that. That's not exactly the best coding in the world because you'll always be within this loop. But uh yeah, we could we could keep a global this I mean it won't I don't think should shouldn't result in a true memory leak uh with Python. Uh if this was a if Python was a more sophisticated language that might be a problem, but it's not. So we'll run this again and let's see if we can get away with this. So let's go ahead and crash into something. Okay, looks good so far. And we hit play again. And we get to play again. Awesome. So, easy enough. Uh, we never lose sight of the buttons because those are drawn last. And we get our score. We can still see that. So, that's pretty neat. Um, yeah. So, now we've got a proper looking game over functionality with quit buttons instead of having them press keys and forcing them to play again and all this nonsense. Um, so the game is is uh looking a little more little bit more respectable now. So now uh the next thing I want to do, and I'll just save it for the next video, is we want to um since this is our game, we don't really, you know, we we changed the title of the window, but this is a race game, not uh, you know, this is, I don't know, a really grainy looking snake. So the next thing I want to show uh is how we can change up that icon. And so that's what we'll do in the next video. So stay tuned for that. If you guys have any questions or comments on this video, please feel free to leave them below. As always, thanks for watching. Thanks for all the support and subscriptions.

Original Description

See http://pythonprogramming.net for sample code PyGame with Python 3 Playlist: http://www.youtube.com/playlist?list=PLQVvvaa0QuDdLkP8MrOXLe_rKuf6r80KO In this PyGame with Python 3 programming tutorial, we cover how to add a better "game over" sequence for our game. The previous game over screen was very poorly done. Since we know how to make a sort of screen informational / gui template now, we should upgrade our game over screen! 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

This video teaches how to create a game over sequence in PyGame with Python 3, including implementing a crash function and pause functionality. The instructor also discusses how to use buttons and the game loop.

Key Takeaways
  1. Create a crash function
  2. Implement pause functionality
  3. Use buttons for play again and quit
  4. Modify the game loop
💡 The crash function can be implemented using a while loop and buttons for play again and quit, and the game loop can be modified to accommodate this functionality.

Related Reads

Up next
How AI Detectors Actually Work #llms #ai #machinelearning #artificialintelligence #writing #chatgpt
Ascent
Watch →