Creating an Environment for our Object - Intermediate Python Programming p.14
Skills:
LLM Foundations80%Prompt Craft70%Advanced Prompting60%Prompt Systems Engineering60%Agent Foundations50%
Key Takeaways
This video tutorial series covers intermediate Python programming, specifically creating a PyGame environment for an object, utilizing the Pygame library for game development and object-oriented programming principles.
Full Transcript
what's going on Welcome To Part 14 of our intermediate python series and part two of the objectoriented programming uh miniseries within that Series in this tutorial what we're going to be doing is just taking this you know class and actually inputting it into an environment basically so we're going to code the environment part here and uh and then we'll be able to get back into actual objectoriented programming so really this is even probably most mostly a game tutorial just utilizing this blob class anyway let's hop to it so we're going to import py game at this point you will need pame installed and we're going to import random because we are using random here and we need to make sure we're going to be able to do that now we're going to have a bunch of constants we're going to have width that's going to be 800 we're going to have height that's going to be uh 600 and then we're gonna have some colors these are just tupal of colors they are RGB and um I for they're additive I guess I'm trying to think of the proper word someone can comment below the proper word maybe additive Is It Anyway think of it as light so RGB we've got full red light full green light and full blue light that creates white light when you have all of the lights if you had no lights 000000 that would be black anyway we're going to give blue as you might guess blue would be zero red zero green but full blue and then we'll have one more for now and that's going to be red and red will be full red zero uh green zero blue cool so those are our colors now we're going to Define uh the game display and this is for pame and this will be py game. display. set mode and set mode takes a tupal of your width and your height py game. display. good now what we're going to say is py game. display. set caption not really necessary but we're going to call this blob world and then we're going to specify the clock so we can kind of handle uh frames per second as best as possible okay so we're done with kind of the initial setup now we're going to come below this class and I'm just going to make some space so I can scroll up generally pep8 suggests uh one line of white space between functions and two between classes so we'll Define draw environment here environment uh for now we'll just leave it empty and we're going to say game display. fill white so what's happening here is for every frame you have to redraw whatever you want to that frame and this is our way of clearing the frame so it'll have a white background and we'll draw on top of that white background so we're drawing or we're filling it with white and then we would in theory draw on top of it for now we're not going to do anything um and then we're going to run py game. display. update and that actually updates to your screen so when it comes to computer Graphics it's not necessarily the most processing intensive or the most kind of uh time intensive or whatever uh to to bring Graphics to screens but it is a it is a challenge process nonetheless and so generally what you'll do is you'll actually in the background like when we say game display. fill. white or game display Phil White um the user if they slowed the the game down enough right or watched it in slow-mo it's not like the background first is going to go all white and then little objects are going to be updated to the screen that's not how it's going to work what pame is going to do is truly in the back end it's going to okay it's going to say okay we're filling with white and then it says okay maybe we're going to draw an object which we will so maybe we've got a blob here it's going to draw a blob there and then when it's all said and done it's going to say okay this is the final um final result that we actually want to be the new frame and then it sends it to the screen that time for real and this is sending it to the screen this is in the background waiting for us to basically call this so py game. display. update cool and then we're also going to actually we'll leave that empty for now one space or one line between un functions we're now going to have our main function this is just going to contain the main part of our our program so first we're going to do uh we can Define well first we'll we'll Define The Blob here in a moment first let's just let's make the environment so while true we're going to say for event in py game. event. uh get this just grabs all the events from py Games events and we're going to say if event. type is equal to py game. quit so if it's a pame quit event um this is basically if they click the X in the top right corner or top left depending on what operating system I suppose uh anyway if that happens we're going to say py game. quit and then we're actually going to also quit stop the program otherwise uh still under the while true not so many over then what we're going to do is draw environment so basically if it's not a quit event this will draw environment and then we will clock. tick 60 and this is to hopefully if we can process quickly enough we will get 60 frames per second uh but we will not second but we will not uh will will not go over 60 frames per second this this is our way of capping the frames per second because our game the actual speed in which things happen in our game we're not working with a physics engine or anything like that so it's going to be entirely contingent on the frames per second themselves so um game designers are probably like the horror but anyways that's how we're going to do it so uh we're going to save that and uh now we're going to finally end with our uh if name equals don't forget your string there main what do we want to do we want to run main okay let's run it and see if that works I'm expecting some sort of typo something is going to be like Harrison you're dumb well I'm expecting something at least nothing happening I'm pressing run there we go oh okay great we actually got what we wanted surprising okay so as we can see it's just a white screen because that's all that's happening but our environment so far is acting the way we would hope that's good so uh we'll close that and now let's actually get the blob to interact in this environment so first what we can do is we need to create the object so we're going to say red blob equals it's going to be a a blob object and if you recall blob object does take one basically when you initialize this because the dunder init method has a an argument here for color when you actually create the object you must pass a pass a color but you could say like color equals red and actually have a default right just like default parameters and functions we're not going to but you could so at least not right now eventually we'll talk about why you might want to do that but for now we're not going to do that uh but blob and we're going to say that color will be red that's really all we need to do uh just just as a quick aside as as far as like Pepe and stuff is concerned luckily this line is short enough and it it like makes total sense red blob equals a blob when we see the words red it's relatively easy to to uh take from this that chances are we're creating a red blob you know that's pretty simple But as time goes on in in this in this uh this class gets much larger and maybe there's a lot more arguments eventually it gets to the point where the things that you're seeing it doesn't make sense it's not logical just reading this you don't you almost have to like go back to the the class and read it from it and uh so a lot of times it does make sense to say like color equals red um but in this case actually it would be like this uh but in this case it's not necessary but eventually we will hit a time when we've got a lot of parameters and we're exceeding 79 characters but I'm still going to suggest that you do this in times where it might be ambiguous or um not informative what that parameter or argument is anyway red blob we're passing uh oh we're not doing anything we've just created the object but now in draw environment let's go ahead and pass red blob to draw environment and then in the draw environment function let's say yeah let's take a blob so that's going to take the blob object and then after we've filled the game display is white we're going to come down here and we're going to say py game. draw. Circle where are we going to draw the circle we're going to draw to the game display uh what color is a circle well it's blob. color whatever that happens to be what is the location of the blob that's blob. x blob. y and then what is the size basically the radius of the circle blob. size so maybe it is pixels and it's radi Rus not diameter but radius and pixels uh I'm just referencing this 4 to8 I think that's what it is anyway make sure that this line occurs after the fill white if you have it before fill white it will literally it will draw the circle and then will paint white all over it and that would not be good so make sure that's coming second so now barring typos or logic errors we should get a red blob on the screen sure enough we do it's hard to see on this screen but I think you can see it it's right here awesome and you should be seeing that on your screen now how might we uh get this to be a little more animated well basically anywhere in the draw environment function we could just simply call lob. move so this is an important thing to take into consideration that when we pass this blob object or blob. move empty param or empty parenthesis let's run that really quick okay now your little Blob should be kind of wiggling around now one thing to note is that is modifying the actual blob itself red blob from blob. moove is being moved so if we were to come down here uh and print let's just print the X and Y so red uh blob. X and red blob. y so those will just print out to console you can see they're actually being moved the original object is being modified through this name so this name is is is still modifying that object and later on you'll see that you can pass even more many objects out of one time and they still do get um get modified so um I think that's a good spot for us to stop we still have a lot to cover obviously with object oriented programming and like I said this tutorial was more of getting the P game kind of environment set up um and kind of working with that object so anyways if you have any questions comments concerns uh up to this point if you're having any problems with py game or whatever feel free to leave them below otherwise I'll see you in the next tutorial
Original Description
Welcome to part 14 of the intermediate Python programming tutorial series. In this tutorial, we're going to be creating the PyGame environment for our object.
https://pythonprogramming.net
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
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: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI