Pygame Tutorial for Beginners - Python Game Development Course
HTML-код
- Опубликовано: 31 июл 2024
- Learn how to use Pygame to code games with Python. In this full tutorial course, you will learn Pygame by building a space invaders game. The course will help you understand the main game development concepts like moving characters, shooting bullets, and more.
💻 Code: github.com/attreyabhatt/Space...
🎥 Course created by buildwithpython. Check out his RUclips channel: / @buildwithpython
⌨️ (0:00:00) Installing Python and Pycharm
⌨️ (0:04:12) Creating Our First Game Window
⌨️ (0:12:39) Changing the Title, Logo and Background Color
⌨️ (0:20:33) Adding Images into Our Space Invader Game
⌨️ (0:29:57) Movement Mechanics in Game Development
⌨️ (0:38:13) Keyboard Input Controls & Key Pressed Event
⌨️ (0:49:53) Adding Boundaries to Our Game
⌨️ (0:53:28) Creating the Enemy
⌨️ (0:59:14) Movement Mechanics of the Enemy Space Invader
⌨️ (1:06:51) Adding a Background Image
⌨️ (1:12:08) Creating Bullets for Shooting
⌨️ (1:26:21) Shooting Multiple Bullets at Space Invaders
⌨️ (1:34:24) Collision Detection
⌨️ (1:48:05) Creating Multiple Enemies
⌨️ (1:55:08) Adding Text and Displaying Score
⌨️ (2:03:17) Adding Sounds and Background Music
⌨️ (2:09:05) Game Over
--
Learn to code for free and get a developer job: www.freecodecamp.org
Read hundreds of articles on programming: www.freecodecamp.org/news
This has to be the most useful learn-by-doing Pygame tutorial I've seen. You start with the basics, explain what you want to do and how Pygame allows you to do it, and work it in with concepts a learning programmer should be aware of, without jumping right into the more complex aspects.
First guy who has made a proper tutorial glad to see that people like them exist in this world
Indians rock bro. Wait r u indian????
You are so much true
@@37.vigneshgopal3 l am Indian. I am proud
@@object5369 yeaaaa
Yea man
1:35:00
For collision detection pygame actually has a method called 'colliderect' which detects collision between rectangles. So, if you want to check if the enemy rectangle has collided with the bullet rectangle, instead of checking the distance between the two for every frame, you just write:
if enemy_rectangle.colliderect(bullet_rectangle)
[whatever you want to do when the enemy collides with a bullet, or vice versa]
Hope this helps
thanks for the tip
Is it simple rectangle overlap? Or does it then further check for pixel overlap (if there is transparency in either image)?
@@hossumquat im trying to figure out the same, gotten any results? Also how do you call the enemy rectangle if the enemy is an image?
helped soo much thanks
why are you even here your an expert
I imagine you might not be monitoring the comments anymore by now, but I'm finding this really helpful! I have to try to help some students pick up some programming skills, and this tutorial is great! The detail that you go into about why things have to be the way they are is very helpful. Thanks!
Great tutorial bruh, really easy to follow along to without speed up or slow down on video play speed. Didn't slow talk the basics like it's somehow news that a String can be a variable value or something. Just gets right into a python library and explains how to build a game.
bruh listening to indian dudes on youtube is wayyyyy better than school
Lol I was just thinking the same
no it is not
.
@@its_layth2530 you might be enjoying something else. Why tell everybody??
Right?! I was assigned to make a game using pygame but my teachers such a horrible teacher that no one understood anything 😃-
@saketh p
#ThioJoke
Awesome Tutorial! Thank you for the spread of your knowledge! Super grateful for people like you who take the time to explain how and why it works and why it does not! That is definitely good teaching practices! I wish my CS teachers were as good as you when it comes to explaining the internals of code! Much appreciated! Take care!
I watched the whole thing. THANK YOU SO MUCH.
I learned so much.
Legend.
Very beginner friendly, you have taught in a step by step manner that allows us students to try them out , which is very helpful and needed. Thank you .
Really cool coding exercise covering the basics. Great fun while following throw it and playing with the functions here and there. Keep up the good work and kindly share more courses like this one. Also looking forward to a object-oriented approach for games involving lots of sprites. Thanks again.
I love this guy! He explains all the function of python so well, it really makes me want to learn more. Keep it up!
Thank you! I know less than jon snow about programming, but I followed your every step and I made my own game with my own cool graphics and story. And I think I understood most of it, even the collision part. Great teacher.
I still can't figure out why this channel is so amazing with its video contents and on top of that ABSOLUTELY FREE.
ikr
This channel is pure HEAVEN!
Don't give them any ideas
@@@samuelamare3080 Dont worry this will always be free they have a paid version which is like an other site named CodeAcademy. However i like how the quality of the content is so high but it still is free, its like standfords free machine learning course videos
they still get ad money bruh
well this is one of the best tutorials that I've seen.
It's so easy to understand and well-explained also those recaps after each lesson are so awesome
Thank you so much 🙏🙏
This was a really good tutorial. He explained everything in detail. Highly recommend.
How do you add an image/icon to Pycharm? I had a lot of trouble trying to import a logo for the icon
1:06:00 these 2 conditions can be simplified as
if enemyX = 736:
enemyDx = -enemyDx
enemyY += enemyDy
this way the enemy speed value can be modified in the global only instead of changing in 3 different places
Yeah.
Working with variables is much more comfortable than putting numbers.
Ok but how do you make them return to 0 or 736 depending on if they end up less than 0 or more than 736
@@adhirajsingh1806You don't have to. He already changed the movement direction. For example, enemy approach a border and turn back instantly and go to opposite direction with the same speed.
This is one of the best tutorials i have ever seen so far. You explain every line of code without leaving room for confusion. Thanks you for everything!
see u around.... officer
You are so right
Many RUclipsrs and teachers skip over the function of each line of code or just spend so long on theory that even interested students don’t want to learn anymore... You are the complete opposite, you explain each line of code, what it actually does, how it runs and go at a nice and steady pace so that everyone can understand! Hats off to you sir!
yup
NICEEE!!
U are correct
Completely true. That is exactly why I stopped my previous pygame course and moved to this one
some how after adding sound effect the game feels soooo cool... well by the way must complete the full tutorial. its gr8.
I have searched a lot of videos for pygame but none was as good as this
Thank you
Its good that you are repeating many things again and again, its actually really helpful for us, beginners as we tend to miss out on details sometimes.
This tutorial was amazing, I've learned so many concepts in python game development.
Python wasn't really designed for it.. it is really important, though, to apply Pygame knowledge to future computer vision projects (if you choose to do so). CV2 is super valuable in combination with pygame and blitting.
Brilliant Man, brilliant video. I like that this man is teaching python as well as telling us all the resources for the needy things. This is just awesome. Thank you for this tutorial of space invaders.
Fantastic tutorial! Thank you for taking the time to do this. Much appreciated.
It's funny how he explains that adding negative numbers is subtracting but also assumes we know the distance between two points algebraic equation
If you've installed python (by yourself)you definitely know the distance between two points formulas in all it's forms. It's math. If you think this is hard, Wait until you need to apply trig functions.
@@troyna77 I'm afraid this is actually objectively false. The principals of Algebra-However basic in this example- have absolutely nothing to do with the ability to use a computer to connect to the internet and then click the download button on the Python public website. Furthermore, on your-poorly hidden- implication that you can't be a real programmer without the backhand knowledge of both Algebra and-at least fundamental- Trigonometry, there are plenty of programmers who have created respectable and renowned programs without the use of any Algebraic Formulas for calculating distance or any Trigonometry. And though I understand it's probably a bit hard for you to understand, these people are just as much of a programmer as you and me. This comment isn't a good-willed attempt to clarify the mindset of the instructor, it's a short-sighted jab at someone who just wanted to make a joke. And quite frankly, the fact that there are people like this outwardly representing this art in this way appalls me.
@@TheRealLyonic wowza
@@TheRealLyonic well said ... trigonometry also isn't objectively "difficult" to use in programming, you're just calling a function and letting it do all the calculations for you. The only important thing is to know why and when you need to call a particular function and what part of the result matters to you. Someone who needs to know can learn it by reading some simple rules about trigonometry and what the terminology means. But in no way is trigonometry connected to how good of a programmer someone is, unless they work in a very specific field where it's critically important. I'm DirectX/C++ programming and Unity/C# development (which is considerably more complex) I only used trigonometry in a few isolated cases where I found some novel use for the properties of triangles, like determining how high off a ground an object will be if it's rotated about an axis X-distance away from by so many radians. That guy's comment reads like someone with no dev experience trying to sound like an elitist expert by tossing around some things that sound intimidating and pretending to understand them lol.
@@GameDevNerd I agree completely! And appreciate the well-thought out reply. Also, I respect the DirectX/C++ programming stuff, I focus more on Java and the Web Development tools (HTML, CSS, and Javascript), and always found that stuff to be a bit complicated for my understanding. Though, I will say, I actually worked in Unity with C# for a while too a good bit ago, which was relatively easy to migrate into language wise, since Java and C# are almost the exact same language in terms of Syntax. Though it did take a while for me to learn the ins and outs of Unity as a game engine, working with Rigidbodies and all the other various elements in a game. All that being said, love the input, and thanks for the reply ❤️
Wow! You have answered all my questions! It's just like two weeks ago I said I wanted to make a game app and I had no idea at all where to start. So I began watching videos on RUclips and I found your video. I am so greatful for your video bro. I certainly subscribed to your channel and I look forward to more of your educational videos. I know you said it's all basic stuff but bro, for me, oh men believe me, it's so professional that I would call it advanced. But you made it so logical and fun that I understood everything. Muchas gracias...Merci beaucoup.
Really enjoying how he explains everything well. Bit tedious for when you know basics, but it is a really good course, def recommend
Thanks for posting. You approach works much better than several other resources that I've tried.
the video: bullet_state = "Fire"
boolean: "Am I a joke to you?"
ikr
Some things in this just don't make sense. Like the spaceship seems to be shooting from it's left and right cannons, but really you shoot a huge bullet from the nose of your ship. But hey, that's not the point, right? You should be focused on the education!
Truuueee haha😂
Boolean is fired
Sorry
@@4glassmilk635 😂😂😂
Appreciated how digestible you made everything. Tough material to do that with 👍👍
BARELY
Thank you so much! I have been wanting to learn how to make a game with python for ages!
Thank you soo much! It's soo good that you comment every thing you writhe using the #, it's VERY VERY helpful for beginners! :)
Only this man can make a spaceship question their existence
Haha
Haha
Haha
Haha
Haha
You are really a good teacher . I don't know why but i guess Indian college would be disappear in the upcoming years if legends like you keep teaching.
Keep going it love you 3000
The things we learnt in 2 hours in this video our college's will take more than 1 month to cover all of this method .....
You made it vey nicely and easy. Please keep making tutorials like this so we can learn more. Thank you for investing your time for us. Lots of love for you.
Thankyou again.
WOW! This tutorial is incredible! Thank you very much!
In my few months of learning coding
this video has brought a lot of positive vibe to me as
i finally got the feeling of creating something solid real
thank you
Great tutorial! After watching I made it so the enemies could fire bullets to make it a bit more interesting
Just to Clarify would you take the spaceships shooting speed and then put it on an enemy sorry for asking a stupid question I am New to Python
how did u set at what place the bullets will spawn?
@@hello-wx9wh the enemy variables
Great tutorial! I am new to coding and have loved this tutorial! I have learned so much about pygame!!! Thanks
Thank you for this. I missed coding with python and i don't know where to start again; this is a great start for me.
49:30 lol, at least you said something to laugh during the tension! 😂😂
⌨️ (0:00:00) Installing Python and Pycharm
⌨️ (0:04:12) Creating Our First Game Window
⌨️ (0:12:39) Changing the Title, Logo and Background Color
⌨️ (0:20:33) Adding Images into Our Space Invader Game
⌨️ (0:29:57) Movement Mechanics in Game Development
⌨️ (0:38:13) Keyboard Input Controls & Key Pressed Event
⌨️ (0:49:53) Adding Boundaries to Our Game
⌨️ (0:53:28) Creating the Enemy
⌨️ (0:59:14) Movement Mechanics of the Enemy Space Invader
⌨️ (1:06:51) Adding a Background Image
⌨️ (1:12:08) Creating Bullets for Shooting
⌨️ (1:26:21) Shooting Multiple Bullets at Space Invaders
⌨️ (1:34:24) Collision Detection
⌨️ (1:48:05) Creating Multiple Enemies
⌨️ (1:55:08) Adding Text and Displaying Score
⌨️ (2:03:17) Adding Sounds and Background Music
⌨️ (2:09:05) Game Over
Enjoy!
Thank you
you've did an awesome job
Did you just copy paste from desciption
@sajid chowdhury really? exact same titles seconds and emojis
sajid chowdhury yeah but your tried to pass it off as ur own
just finished this tutorial - it's great. Many thanks for making this and making it easy to follow.
This is how teaching should be!!!🙏🙏🙏
Huge respect, from a student to you, sir...
Getting your Oscar ready.
It was like a damn good crash course.
Best way to learn when watching these videos for me at least was to watch each section, example: displaying the window, adding icon. Then go and try it yourself without re watching that section. If you run into a problem remembering what he typed just go look it up and search around instead of just re-watching what he typed out. Thank you for these videos!
Thank you a lot , this video is brilliant created by you hoping soon to give more tutorials on this channel!!
What a Amazing Tutorial , this one is great one, I have ever seen and it was good explanation too.
59:00
(just letting myself know where i left off)
EDIT 2: Finished :)
I can't express how happy I am for this video
Thank you! IT REALLY HELPED OUT! I hope all of us get better in the future!:D
You just earned yourself a like !
Thank you so much sir
It will help for my school' project🙏
completed the tutorial. Yayyy! thanks for the amazing tutorial.
Lots of fun to follow along with , im new to coding and these codecamp tutorials are my best friend atm LOL
Ok! This is just mind blowing i was searching for pygame and here notification comes.... :)
Very very very much thank you for providing all useful/ informative stuff for free.
Well glad I could help. And freecodecamp comes to the rescue!
@@buildwithpython why not create separate classes for different entities?
@@Importunity I could have but didn't want to make it too complicated for beginners. They are here to learn to learn Pygame. People who already know about classes can add it easily on their own.
Is it just me, or did this dude just clear up most of the magic tricks I have seen in my life.
Ikr destoying the whole magic industry
@@jorgeskts ah yes, the magic industry
tru tho
I want to press the like button 100000 times. This is the best video I have ever seen on this topic. Great Sir..keep it up.
ur explaination is at another level and u made me understand the actual coding thx a lot
Cool tutorial. [1:39:20] you can use math.hypot instead of pow and sqrt.
distance = math.hypot(enemyX - bulletX, enemyY - bulletY)
Since Python 3.7 or 3.8 math.hypot can take more than two elements, so you can use it also to calculate distances in 3 or more dimensions.
Thank you!this helped me a lot :)
+1 for this, thanks mate
when i tried this, I got the error the TypeError: 'int' object is not subscriptable
when using sqrt and pow, it worked fine
Does it calculate distance without doing anything else?
@@leob8967 yea it basically means hypotenuse, which is the distance in this case
I have an exam tomorrow and i watch this video till the end.
*Yep thats worth it*
i have two weeks to make a game and I have to learn this, plus I have school for one more week.
*ill be fine right?
I watched it on exam day. Love freecode camp
this is the best tutorial, no one teached properly like him for pygame.
This is really awesome video. I'm very new with Python and it's hard to understand the process even the functions,... It's really great that you explained every single details of what you are doing. Please make more videos like this. I'm so happy to find this video
waaao! who says nothing is perfect!! thanks alot, it really helped me alot!
GAME OVER its a good way to end this course
You are amazing MATE!!! Thanks
only around 20minutes in, but already the best programming tutorial for this that i have ever seen
Really enjoyed this tutorial - excellent work !!
You know that a tutorials going to be good when u have an indian talking :)
Ya absolutely Indian English is easy to understand for both indians as well as others
@TerrorWest lol ur being self racist. I think it's good
@TerrorWest yeah, I agree with you it's really annoying special when his accent has so much effect on the English characters so, I really have so much respect for Indians who can overcome his accent those are only Indians who can really impress me
@@m-gendy1512 wow ur as bad as that white police
@@Avighna Actually like it is annoying because for coding we Indians have only watched tutorials by foreigners and we get addicted to that sound. Plus Indian accent if heard on devices is very annoying to be it anyone. NotRude
UR THEEE BEST, im only 12 and was eager to learn python all of the other tutorials weren't specifying png file size so everything was going hay wire, you explain everything properly 10/10 would recommend!!
can you send the codes please :(
holy moly you are young learning coding! glad to see the younger demographic enjoys coding as much as i am!
@@tuis6276 i changed from pycharm to sublime text and all the files are lost, im sorry
@@ConnerCobe well they don't say coding is for all ages for nothing
I'm 14 :'(
that was an awesome tutorial i was following along a different oen and this one is so much better good job.
Thanks man , only video i needed for a long time
1:03:00
it can be done in vs code, using alt+click trick.
this has to be the best beginner game dev course ever.
concise, simple and easy to understand.
would recommend it to others 10/10
yeah these are the same guys who actually helped me get started with coding
Thank you so much for such clear explanation ... You guys are blessing !
he ffucking explained the pygame library so easily that i cant define, i was stuck on understanding how to use them, these 2 hours are the most productive of my life.......more power to u bro, please give us an option to applaud your work through money...
Cool intro to python game development 💪
Thanks :)
Great tutorial!
PyGame is awesome. I'm just beginning to learn it.
By the way, I LOL'd at 1:23:27.
How is that funny?
Fantastic tutorial! Very nicely done!!
Perfect tutorial to learn how games are developed... This helped me a lot.
How many guys are noticed this amazing guys voice was changed in the class of adding audio to game
i needed 3 days to finish, but now i am way smarter and understood it
Excellent tutorial, best game tutorial I've seen. Thank you.
This was amazing! I feel like I learned a lot - THANKS
2:00:25
- "And when we shoot someone"
*Misses drasticaly
😂
i need help there as well
Thank you so much Sir..... After 5 days i have created this game with the help of your video.....
Which pubg
👈@@planet.ai1 nahi bro bro mera channel dekho
Man, this helps me a lot. I srsly struck gold with yt recommendation huh.
Lots kudos!
Thank you for these amazing tutorials! You're awesome!
thank you for your perfect work, thanks to you I am learning a lot from a french friend
I'm a 12 year old and it's really very easy learning Pygame with you . Keep it up!!👍👍
Same!
@@kumarpulivarthi8780 I know right
Yes it proves you are a *12years old* as only a *12year old* will tell his age in a public forum
@@shankhanilsaha8858 does it matter?
@@bogdanasaurus no just felt like saying
it's very clear dude! Thanks so much!
Salute , you sir 🙏
you made a two hour video and subtitles too
It surely took you so much time
could somebody please tell me how did he get to use that image in the video I cannot figure it out when I download the image I try to put it in my code but it gives a error can you help me?
To center player along x-axis can also be auto-calculated instead of hardcoding playerX = 370
playerX = screen.get_width() / 2
technically this would place the left edge of playerImg at center. to align the center of playerImg at the center of the screen you'd need:
playerX = screen.get_width() / 2 - playerImg.get_width() / 2
Thanks so much! Really clear and easy to understand.
i created my first ever game thank you so much bro
the tutorial is not even bored .well understanding .no error
Finally, someone has made a tutorial about Pygame!
I hope they also make a video about Python GL and 3d games in Python.
Even how to make a doom 1 like in Python!
If you want to make 3d games I recommended panda3d
@@oltb3415 yes, I know about Panda3d, but I also like to know more about Open GL and Python scripting in Blender 3d.
I hope they make more tutorials about it.
Thanks! I haven't yet. But I do love some Pygame!
@@buildwithpython You are welcome! I understand. Me too!
@A Google User But if you use Cython and other combinations, you can make an excellent game.
There is even a 3d RPG game made by Python.
You can watch it on RUclips.
Started and completed the whole course in 4 hrs no skips and not discontinuty... Just because of the way this guy explains Stuff..
umm the course is 2 hrs lmao
Great tutorial for beginners! Thank you
Although I struggle for 3 days, but nice for beginners like me. Thank you so much! Hope u have the best when prodiving very detailed and free-acess education for people!