Game Development in Python 3 With PyGame - 7- Crashing
Key Takeaways
The video covers game development in Python 3 using PyGame, focusing on handling events like crashing into objects by checking for collision between objects and determining crossover between car and object.
Full Transcript
hello and welcome to the seventh python game development with uh py game and specifically Python 3 uh tutorial video in the last video uh we made these objects appear they come at us they start at random places and you know they move and whatnot and in this video what we want to do is we want to have something actually happen when we crash into these little objects cuz our objective here is to avoid the objects um so we want to have some sort of handling for if we happen to run into them so close out of this and we'll get started so in our script um again just like we have you know these this group of logic here uh we're going to keep building on this group of logic so uh we'll come down uh to this point and we'll create some logic now this one's a little more challenging than did the left side of the car across or did the right side of the car Cross or did the top of the object um cross over the bottom this one a few things have to be the case um and I think the best way to illustrate this is by like drawing it in paint or something so here's paint and um we'll just say you know this is our car and uh we'll make our car green on the inside just to uh differentiate between the box and then we have a box and the Box let's do this I get it to you know you've got a box and as the Box comes down in order we have to ask a few questions so if the box is here um this is like this layer here actually we can use a line you know there's cross over here but there's not cross over here so it's so the Box didn't hit so um the Y is lower than the Y and it's crossing over the Y of our object whatever it is so our main character which is a race car it's crossing over but the X's aren't crossed over so we don't have a collision here um conversely um we could have this right and uh we are actually within both bounds like both walls so we're within here but we're not within the YX so we're within the X's but we're not within the Y at the same time um we can't ask if we're in both X's but we have to ask if we're in one of the X's so for example um let's see here there we go anyway this would be a collision right um and not because you know we're not in the way of um you know this one obviously they're not lined up but this one is the Collision right so we have to check whether it's hitting this one or it's hitting this one and it's also got to be hitting the Y so one of the sides and the Y has to be hit so that's the kind of logic that we have to build so I'm not going to save that um so now we come over here and so now we're going to start asking some questions for our logic so the first thing we want to ask is if if the if the tip of the car is higher um which means less than really in in the with uh y right so we're asking if y if the x is or if the car's y position is higher or has a less lesser y value than the Y of the Box bottom of the Box um we might have a collision there so we'll say if Y is less than thing uncore start y now I know we called this variable thing start y but we only did that just at the very beginning but now things start Y is like the position it starts from before it gets modified basically but anyway things start y plus thing underscore height so thing start y shoot we closed our um or paint let me open up paint again but uh thing start y if we had a box right so here's our box thing start Y is up here it's asking if this is lower than um our race car which is here right but we actually want to know if this is crossing over the race car so if any point of this is less than the race car that's when we really want to know if there's some crossover going on so in fact U I'm going to keep this open for now um so that's what we're asking right now is if that if the bottom um line and really uh what we're asking is not uh if we just if we didn't do the image height it would be like this Mark here um but actually we're asking if this whole thing right because it's just the Y right so if that is crossing um our car's top um if that's the case then we might have crossover but not necessarily right so this we would just say uh we'll just say print step one and what this is just going to do is it's going to Output to our console that you know we we're finding we're saying there's some cross over here so for example we can run this and here is our game and as it was going of course you couldn't see it on mine but hopefully on yours you could see um a as the Box started to come below um here we go uh at this point right as it's crossing over it starts spamming out step one step one step right so we've got y crossover so we could actually change this to Y uh crossover now um the next thing we need to know is if the X's are um crossing over so the question is here uh is if x is greater than thing uncore start X and so if the x is greater than um this x so and then keep in mind X is the location of our car so let's say this is our car we're asking if the top left of our car here is greater than uh here basically so if if x is uh greater than that which basically means if if as long as it's not like past the car pretty much so if x is greater than things start X and um well this one's going to be kind of a long long so we have to ask multiple questions but stick with me so if that's the case um so basically if if the object is possibly Crossing if that then if that's the case and X is less than thing undor start X Plus thing oops [Music] thing think start X Plus thing underscore width so so again the X's are are the horizontal movement basically so if this if x is less than um the start X which is here plus the width so that's here okay so we have to be less than okay and then the question is um if x is less than things start X plus uh thing width or oops not capital or not capital or or X+ carore width um is greater than so X Plus car width so we're asking now X Plus car width is this point right here if X Plus car width is greater uh greater than thing underscore start X so uh let me make this a little um let me make the text smaller we're still going to go off the screen but um it'll be a little easier to see so let's move this and move this down okay so and so we've got if this and this or this um or X Plus car width is great than uh thing uncore starx and X Plus car score width is less than so this is asking if that was greater than stardex but then we also need to know if X Plus car width is less than thing uncore start X plus the thing width so basically we're asking if we're within the boundaries of this and this because we have to be within somewhere within those boundaries um in order for this to actually occur but as I was saying before uh that's a collision fully being within the boundaries is a collision and then being over here it would still be a collision so you have to kind of keep this in mind anyway if all of that is the case um then we'll say print um X crossover and then at this point we Institute a crash okay so let's save that and let me just fix my window and now let's run that and see how we do so we have a little car first we want to make sure we can avoid and then cross over like that um and it looks like we can and now let's go ahead and have a crash okay so we crashed there easy enough uh let's check the other side of a crash caught it nice so we crashed there as expected um and I'm trying to think of any other the other thing you just want to check like if you were to like drive over it right away that's kind of helpful or if you're under it but then you get out from it that would be another instance now the crash I just had the problem with that even though it doesn't look like we crashed it's because our our image is 73 pixels wide because we had to make a box around our car um and that's just yeah there it is again and it really would have hit the rear tire I suppose you could make the argument um but because we're calling our image really a square um that's why we're actually still having a collision here anyway um so the next thing let's see I guess we could we could cut it off at this point so so now we have some boxes to avoid um and you know that's okay you know we can keep playing that but this this game is relatively boring um it's easy to skip the boxes generally the way a game is made is the first few levels or the first like you know few seconds at least um are going to be easy it gets the player kind of acclimated to how the game works but then challenge is escalated and that's what we want to do we want to kind of escalate the challenge also there's really no no tracking here like how good are we doing like anything like that so we want to have some sort of scoring system as well so we want scoring system and then also um increasing of some sort of difficulty making this more and more challenging so that's what we're going to be doing uh in the next video so if you guys have any questions or comments on this one uh feel free to leave them below otherwise as always thanks for watching thanks for all the support subscriptions and until next time um
Original Description
In this PyGame tutorial, we cover how to handle events like crashing into objects.
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
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
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: AI Systems Design
View skill →
🎓
Tutor Explanation
DeepCamp AI