How to Program a Game! (in Python)

  Рет қаралды 492,804

Keith Galli

Keith Galli

Күн бұрын

Follow me / keithgalli for more tech content!
In this video we walk through building a simple game using the python programming language. This is a great exercise to apply your python skills in a fun way. We use the pygame library in this video. A video outline will be posted in the comment section. If you want to review any of the topics covered in this video, there are links below to all of my python tutorial videos.
If you are curious about implementing additional features to this game, leave a comment down below!
Make sure to SUBSCRIBE if you enjoyed this video, it encourages me to keep making them.
Have fun with your games!!! :)
Source code!
github.com/KeithGalli/Basic-P...
Check out the follow-up video where we refactor the code and add additional features!
• Professional Code Refa...
---------------------------------------------
PYTHON TUTORIAL SERIES:
Setup & Installation: • Why Should you Learn P...
1. Math & Variables: • Math & Variables in Py...
2. Conditional Statements (if, elif, else): • Conditional Statements...
3. Functions: • Functions in Python - ...
4. Lists & Tuples: • Lists & Tuples in Pyth...
5. For Loops & While Loops: • For Loops & While Loop...
---------------------------------------------
Follow me on social media!
Instagram | / keithgalli
Twitter | / keithgalli
---------------------------------------------
Video outline!
0:00 - Introduction
1:10 - Pygame Installation
2:57 - Creating a blank screen
4:40 - Making the game loop (+handling quit event)
7:46 - Drawing a rectangle
13:57 - Moving the rectangle with keypresses
20:10 - Blocks dropping from the sky
27:59 - Setting the frames per second rate (FPS)/Controlling speed of falling blocks
31:14 - Detecting Collisions
41:13 - Multiple enemies falling from sky (lists)
54:10 - Adding additional features (score, increasing difficulty, background/player colors)
-------------------------
Follow me on social media!
Instagram | / keithgalli
Twitter | / keithgalli
-------------------------
If you are curious to learn how I make my tutorials, check out this video: • How to Make a High Qua...
*I use affiliate links on the products that I recommend. I may earn a purchase commission or a referral bonus from the usage of these links.

Пікірлер: 1 200
@KeithGalli
@KeithGalli 3 жыл бұрын
Hey everyone! If you are looking to build out this game further, check out this video: kzfaq.info/get/bejne/bZlhf9KSv6m5mqM.html In it we refactor our code to use classes, fix the glitch where you can move off the screen, and add images to the enemy blocks falling down :).
@cristianawp7608
@cristianawp7608 3 жыл бұрын
i want to make an multiplayer game for me and my friend to play cards but idk how to make it multiplayer, can u make an video about that? sorry if i want too much
@ogonek
@ogonek 3 жыл бұрын
In year 1991 I was 10 years old and I programmed my first shooter game with spaceships and crafts using "Basic" programming language on 8-bit computer including some puzzle games, also hacked few other games. Unfortunately my parents were keep saying that the computers will bring me nothing in my life and they will never support me. That was the reality in a soviet republic. Our childhood was ruined by fake preceptions of what we should become in life and what they were telling us at school. At the end of the days that turned me into rebellion, punk, electronic musician and I missed how this technology was developed it time, but I was really into the games and computer programming. Growed up with illigally imported Atari ( that game was forbidden in our country) present from my auntie since I was at the kindergarten. I'm sure that there is kids at same situation still somewhere...
@mapplemoore197
@mapplemoore197 3 жыл бұрын
by far the most helpful tutorial on python I've found. I was having trouble finding this 'real world' application. Watching the code written outside does worlds better then memorizing mysterious pieces of information. This makes so much more sense!
@bensname7378
@bensname7378 5 жыл бұрын
You are an extremely natural teacher. I appreciate how you consider the mistakes someone coding this for the first time would come across and "fall into" those errors as a chance for us viewers to correct our own understanding as we watch. Thank you for the video!
@ginocarlo7724
@ginocarlo7724 5 жыл бұрын
Man you're awesome! I did everything! It took me about 6 hours to finish and learn, and I liked every second of it
@KeithGalli
@KeithGalli 5 жыл бұрын
Great stuff! Happy to hear this :)
@ardaalpergoz1823
@ardaalpergoz1823 3 жыл бұрын
I am thinking the same
@veloukaliappan2295
@veloukaliappan2295 3 жыл бұрын
@@KeithGalli hey really man this is a amazing fuck:)
@veloukaliappan2295
@veloukaliappan2295 3 жыл бұрын
*not fuck luck*
@wallacewalter577
@wallacewalter577 3 жыл бұрын
@Velou Kaliappan You know very well you did it on purpose, don’t act like it was a mistake lol
@bde7864
@bde7864 5 жыл бұрын
keep up the good work bro! i learned a lot,subscribed with notifications turned on! cant miss any of your content tbh! THANK YOU
@extremetitan2847
@extremetitan2847 4 жыл бұрын
this guy's camera is actually amazing
@asishraz6173
@asishraz6173 4 жыл бұрын
Hye everyone, I have written a full article about the above game. I tried to make it simple and understandable. I really want your opinions, about how it is. It will be really helpful for me, in order to write more such articles. Feel free to share your views. medium.com/@asishraz/basic-2d-game-in-python-38574c339c50
@asishraz6173
@asishraz6173 4 жыл бұрын
@Hinx Hell - that's what brother. I made it in a text form, for my better understanding. And I wanted to know, whether it was justified or not
@InfamousKurutta
@InfamousKurutta 4 жыл бұрын
@Hinx Arcade Kind of like a cheat sheet
@ayushkumarsharma8674
@ayushkumarsharma8674 3 жыл бұрын
Yea you are right
@echomatmat9341
@echomatmat9341 3 жыл бұрын
He has good quality isn't he?
@headlessaphid2902
@headlessaphid2902 3 жыл бұрын
Thanks man. You really helped me creating my first python game. Your tutorials are very helpful and easy to follow along with. I’ve hit like and subscribe for your amazing tutorial. Keep up the good work. 👍
@garryregan7216
@garryregan7216 5 жыл бұрын
Managed to make it work! Enjoyed the video, thanks Keith
@colbablast6579
@colbablast6579 4 жыл бұрын
This was a great tutorial! I even was able to add some stuff like smooth movement where you just hold down the key!
@glitchneon8419
@glitchneon8419 4 жыл бұрын
What was the code?
@colbablast6579
@colbablast6579 4 жыл бұрын
*Outside of main loop* key_left = False key_right = False *Inside of the "for event in pygame.event.get():" loop* if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: key_left = True elif event.key == pygame.K_RIGHT: key_right = True elif event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: key_left = False elif event.key == pygame.K_RIGHT: key_right = False *Inside of the main loop directly after the previous loop* if key_left: player_pos[0] -= player_speed elif key_right: player_pos[0] += player_speed (player_speed is just how many pixels my player moved)
@raffaelg757
@raffaelg757 4 жыл бұрын
Or just replace that with for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_pos[0]-=PLAYER_SPEED if keys[pygame.K_RIGHT]: player_pos[0]+=PLAYER_SPEED
@raffaelg757
@raffaelg757 4 жыл бұрын
PLAYER_SPEED being the number of pixel at which the player moves every tick. I added a menu where the player can buy some speed with coins they gather
@youssefadel5491
@youssefadel5491 4 жыл бұрын
I am only 12 years old and your tutorial helped me so much to learn python! Thank you so much for the tutorial
@anantkhanna4954
@anantkhanna4954 4 жыл бұрын
same, when did you start coding?
@youssefadel5491
@youssefadel5491 4 жыл бұрын
@@anantkhanna4954 last year, at 11 years old
@anantkhanna4954
@anantkhanna4954 4 жыл бұрын
@@youssefadel5491 nice
@extremetitan2847
@extremetitan2847 4 жыл бұрын
@@youssefadel5491 Damn I've been trying to code for a while but lost interest, I've only properly started coding this year lol
@froggy_3353
@froggy_3353 4 жыл бұрын
Am 11 and i want to start coding and programming games
@6977202865
@6977202865 4 жыл бұрын
You teached me the most importand thing in coding!!!! USE THE DOCIMENTATION!!!!!! thank you soo much!
@Herculuke
@Herculuke 8 ай бұрын
One of the best tutorials out there, keep it up!
@bricezakra
@bricezakra 5 жыл бұрын
Just got done programming this game!You are amazing man! 👍🏿 🙂
@javier2384
@javier2384 5 жыл бұрын
Done with the course! Nice, clear, and comprehensive guide and clear voice! Big thumbs up! :D
@KeithGalli
@KeithGalli 5 жыл бұрын
Glad you enjoyed!!
@veisystems267
@veisystems267 4 жыл бұрын
Loved it... really quick and practical intro to pygame. Much thanks!
@kirteekumari3459
@kirteekumari3459 4 жыл бұрын
@Keith Galli, you did a very good job!!! this was my first python game and it was very explanatory. I look forward in adding few more features to it as sound and background image. Thank you so much for this, hatts off to yor patience. It was fun learning with you and you expressions really were same as us!! I was very happy since i created that rectangle lol.
@realnice3672
@realnice3672 4 жыл бұрын
same lol
@PeterKLee
@PeterKLee 4 жыл бұрын
thank you for making amazing python tutorial, Keith! It is greatly helpful & easy to understand!
@felipeuchoa9384
@felipeuchoa9384 5 жыл бұрын
Really good job, I couldn't just get out without leaving a like and subscribing.
@paulal1899
@paulal1899 4 жыл бұрын
Thanks man this was super useful!! Keep up the good work
@gamerinyourtown5592
@gamerinyourtown5592 2 жыл бұрын
this is one of the best tutorials. Thank you for making this. I really appreciate it.
@rakeshkumarkuwar6053
@rakeshkumarkuwar6053 4 жыл бұрын
Thanks for a such a great tutorial. I show this game to my mom and she was delighted while playing. That was the best part of it.
@KeithGalli
@KeithGalli 4 жыл бұрын
Aww that's awesome to hear :).
@matthewtam1145
@matthewtam1145 3 жыл бұрын
To make the enemies move smoother, you could: ... screen.fill(BG_COLOR) if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT: enemy_pos[1] += 1 ... clock.tick(100*SPEED) This would make enemies fall down smoother, improving gameplay. :)
@reqssss
@reqssss 11 ай бұрын
where do i put this
@RedTheHated
@RedTheHated 4 жыл бұрын
Thanks, man. I'm new to python and learned quite a bit from this video.
@adamansbah7017
@adamansbah7017 3 жыл бұрын
with your help, I am able to build my first ever game in python. kudos to you.
@asishraz6173
@asishraz6173 4 жыл бұрын
WOW man, really loved the way you took us to the whole tutorial. amazing speech delivery and I understood each and everything which you stated. Whenever you added a new piece of code, I used to delete the whole file and write from my own understanding and as a result, it makes it easy for me to understand the whole code. Thanks a lot, man. Really a very very helpful video for anyone. Please upload more videos regarding python and gaming. Please... Thumbs up from my side. Salute to you.
@KeithGalli
@KeithGalli 4 жыл бұрын
Aww thanks so much! Going to work on releasing another game tutorial in the next couple months. I plan on having it focus on how to utilize Object Oriented Programming (classes) in game development
@asishraz6173
@asishraz6173 4 жыл бұрын
@@KeithGalli - ohh that will be more awesome. Just wanted to ask one thing, after we finish these type of videos, what should we do, in terms of improving ourselves? I hope, you understood my concern. I really want to learn python like hell. That's why I wanted to know
@DraG0Ne
@DraG0Ne 4 жыл бұрын
This man is an absolute legend instead of skiming through the code he spends 2 minutes of his time on each line to explain what it does. I love python.
@asishraz6173
@asishraz6173 4 жыл бұрын
@@KeithGalli - medium.com/@asishraz/basic-2d-game-in-python-38574c339c50 I have written a full article on your above video, DO let me know, how is it? It will be very helpful for me to write more articles related to gaming and programming.
@felixwang0702100117
@felixwang0702100117 5 жыл бұрын
Thanks a lot Keith, you inspired me a lot from this video
@asishraz6173
@asishraz6173 4 жыл бұрын
Hye everyone, I have written a full article about the above game. I tried to make it simple and understandable. I really want your opinions, about how it is. It will be really helpful for me, in order to write more such articles. Feel free to share your views. medium.com/@asishraz/basic-2d-game-in-python-38574c339c50
@user-tm8ff8xf4w
@user-tm8ff8xf4w 4 жыл бұрын
thank you very much for this such a good run through, i learnt a lot !!
@soybeanz8582
@soybeanz8582 4 жыл бұрын
oh almost keith congrats on almost hitting 100k subs!
@ptortello
@ptortello 5 жыл бұрын
@Keith Galli thanks a lot for this video. I was in a bad day yesterday and when I started watching this and coding this, man.. I just forgot all my problems for a couple hours thanks to you.
@KeithGalli
@KeithGalli 5 жыл бұрын
Glad this video could help, even if it was just for a couple hours :). Keep your head up, you got this!
@f.j.bradman1761
@f.j.bradman1761 5 жыл бұрын
thanks for your lecture ,learn a lot from it
@TheSuccessfulLeader
@TheSuccessfulLeader 4 жыл бұрын
Thanks, Keith: I am learning and getting better and better...!
@lamikar
@lamikar 5 жыл бұрын
Thanks Keith, gonna go home and try this out. subscribed :)
@abhishekroy5119
@abhishekroy5119 5 жыл бұрын
Loved the video.... super useful for me as a beginner to pygame. i tried other videos which were difficult to understand but you did make it seem really easy. Thanks a ton for this and a huge thumbs up from me 👍🏼
@KeithGalli
@KeithGalli 5 жыл бұрын
Glad you enjoyed! :) :)
@antek6445
@antek6445 4 жыл бұрын
I just love how your camera is in higher framrate than your screen recording
@sinaabbasi7670
@sinaabbasi7670 Жыл бұрын
you are very helpful Keith, thank you man, respect from Italy.
@alimamulma3sum14
@alimamulma3sum14 5 жыл бұрын
The ease and the simplicity is another level in the video bro Thank you for this from the bottom of my heart
@matheworson9669
@matheworson9669 5 жыл бұрын
bro you got this, your good at teaching
@theginja581
@theginja581 5 жыл бұрын
Hey, Keith, I was wondering if you could change the color of the player rectangle along the progress at the game, like when you have 10 score its change the color of the player to green. I hope you could help me. btw very good video I learned a lot from it.
@KeithGalli
@KeithGalli 5 жыл бұрын
Glad you enjoyed video! :) So you can definitely do that. You should define a variable called playercolor that is initialized in the same way the other colors are. Instead of drawing the player as RED, you substitute in the playercolor we defined. Now as the score increases in the game you can add conditional statements to change the value of playercolor. So for example if you defined playercolor = (255,0,0) initially, you could add an if statement like if score == 10: playercolor = (0,255,0). The printing of the player will use whatever playercolor was most recently set to. Hope this helps!
@theginja581
@theginja581 5 жыл бұрын
@@KeithGalli Thanks for the help, I glad you answered me!
@goodlookinouthomie1757
@goodlookinouthomie1757 2 ай бұрын
Thank you for this Keith, it's a really interesting and fun script to follow along with; your style is methodical and approachable. In truth this lesson a little ahead of my skill level so, despite getting it all running great and generally understanding the functions, I think I need to step back and do some of the more basic stuff. After watching your more recent videos, I hope life is treating you kindly.
@jeremyadamson9618
@jeremyadamson9618 4 жыл бұрын
Thanks again Keith! Super helpful stuff.
@danielnilsson1592
@danielnilsson1592 4 жыл бұрын
Great tutorial once again Keith! I got an idea of adding a "shoot" function, so the player can fire and eliminate enemy blocks to get points instead of dodging them. Is that a doable project for a beginner? Or do I need deeper skills than your tutorial series here to pull that off?
@KeithGalli
@KeithGalli 4 жыл бұрын
You definitely can do it! It's just a matter of translating it into programming logic. Basically you can think of bullets very similar to the falling blocks (but in reverse) . You can leverage the collision function to see when bullets collide with specific blocks and then pop that block off of your enemy_list. It might not be easy to do at first, but challenging yourself is the best way to learn! :)
@realnice3672
@realnice3672 4 жыл бұрын
that was a good idead to be honest.
@Lyttii_
@Lyttii_ 4 жыл бұрын
I designed the background color so that it changes color slowly in the range of the whole color wheel :> Had some difficulties and after everything was solved i decided to change the color changing speed from 9 to 2 but realized it wouldn't work since I had some numbers there that needed to be % 9 = 0 so i used the random.random() function to make it so that the color changing function goes through only 50% of the time. that way i didn't need to touch the code in order to change it's speed. Big brain.
@jaydenaldum7419
@jaydenaldum7419 4 жыл бұрын
Can I please get the code for that
@Lyttii_
@Lyttii_ 4 жыл бұрын
@@jaydenaldum7419 yeah it's probably not that good since I had been coding for only a month at that time but it worked I guess... looking back I think these were the parts that I did: 1. in the beginning of the code where the constants were: RED = (255, 0, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) BACKGROUND_COLOR = (0, 0, 0) R = [255, 154] G = [154, 255] B = [154, 255] 2. under the "while not game over" part: R, G, B = background_color(R, G, B) rgb = (R[0], G[0], B[0]) screen.fill(rgb) 3. and the function was like this: def background_color(R, G, B): if random.random() < 0.5: if B[0] < 253 and R[0] != 154 and G[0] != 255: B[0] += 3 elif B[0] > 156 and R[0] == 154 and G[0] == 255: B[0] -= 3 elif R[0] > 156 and G[0] != 255 and B[0] != 156: R[0] -= 3 elif R[0] < 253 and G[0] == 255 and B[0] == 156: R[0] += 3 elif G[0] < 253 and R[0] != 253 and B[0] != 156: G[0] += 3 elif G[0] > 156 and R[0] == 253 and B[0] == 156: G[0] -= 3 elif R[0] == 156 and G[0] == 253 and B[0] == 253: R = [154, 255] G = [255, 154] B = [255, 154] else: R = [255, 154] G = [154, 255] B = [154, 255] return R, G, B (if you look at how the values of R, G and B change in the color wheel from the site, you can get an idea of what the code means. I think you can control the changing speed from "if random.random() < 0.5:" by changing the 0.5 to 0.1 you can make it 5 times slower I think.)
@jaydenaldum7419
@jaydenaldum7419 4 жыл бұрын
@@Lyttii_ thz bro🔥🔥
@dexterellis7973
@dexterellis7973 5 жыл бұрын
Thank you so much! I've learned a lot from this video.
@patrickmbarker
@patrickmbarker 5 жыл бұрын
Keith, you are a good teacher. I would love to see some intermediate Python tutorials from you. Thanks!
@KeithGalli
@KeithGalli 5 жыл бұрын
Will start posting videos on more intermediate concepts as soon as I finish my Master's degree in May! I appreciate the support.
@itssammy777
@itssammy777 5 жыл бұрын
Is there an easy way to implement some simples images instead of those rectangles? without having to change the entire code? Nice work men, it helped me a lot
@ethanreed2672
@ethanreed2672 4 жыл бұрын
Read it up on the pygame documentation... I'm sure there's a way.
@soybeanz8582
@soybeanz8582 4 жыл бұрын
keith i saw your old channel and i saw tons of videos you were pretty funny and i also saw you skateboarding when you were younge i didnt know how talented and funny you are keith
@KeithGalli
@KeithGalli 4 жыл бұрын
Hahaha thank you! :). One day I hope to bring more of that character into this channel!
@plazzmator
@plazzmator 4 жыл бұрын
Thanks for great tutorial! Here is my own collision conditional: if obj_y - (enemy_y + 50)
@vishnuaravind6093
@vishnuaravind6093 3 жыл бұрын
your explanation is really nice keep up the awesome work
@hamad87ad
@hamad87ad 5 жыл бұрын
Please make a program that solves a Sudoku puzzle using python .
@AlanSanu
@AlanSanu 3 жыл бұрын
bruh lol culd help
@flyboyjb2209
@flyboyjb2209 5 жыл бұрын
hi great video, i subbed how can you turn the blocks into different characters.
@thatsright2352
@thatsright2352 4 жыл бұрын
I would like to know about it too :o
@Sohzy
@Sohzy 4 жыл бұрын
look into sprites for pygame i bet
@ifennaobiezu8578
@ifennaobiezu8578 4 жыл бұрын
This is really cool. I learnt a lot and my game is working pretty well. Thanks Keith
@KeithGalli
@KeithGalli 4 жыл бұрын
That's awesome!! You're very welcome Ifenna :)
@justshan2413
@justshan2413 4 жыл бұрын
I'm in grade 9 and you helped me with this tutorial. Thanks a lot
@jky9955
@jky9955 5 жыл бұрын
Hello Keith, how would you edit this code so that the player continuously moves in the left or right direction while the left/right arrow key is pressed down?
@KeithGalli
@KeithGalli 5 жыл бұрын
So in addition to the KEYDOWN event, there is also a KEYUP event. Basically you'll want to add some logic that says move left/right after you press keydown. That movement will continue until that same key is lifted up, triggering the KEYUP event. On the keyup event you'll want to stop the player motion. Does this help?
@jky9955
@jky9955 5 жыл бұрын
@@KeithGalli Thank you so much! I have successfully got the code to work.
@KeithGalli
@KeithGalli 5 жыл бұрын
That's awesome! Glad I could help :)
@lisasamuelsson2911
@lisasamuelsson2911 5 жыл бұрын
I've tried this as well, but I can't get it to work... Any tips?
@KeithGalli
@KeithGalli 5 жыл бұрын
@@beamjim1728 Do you still need help? Just seeing this now
@chaudharycodes
@chaudharycodes 4 жыл бұрын
Quick question: how do I set/ generate walls? The enemies are generated on the screen but the block is not limited to the screen, sooo I can hide in one side(out of the screen) as the apocylapse happens ;)
@KeithGalli
@KeithGalli 4 жыл бұрын
Lol yeah you shouldn't be able to hide from the apocalypse! The easiest solution is to add an if statement to the logic that moves the player x position. The key press should only move the block left if the position is greater than zero and it should only move the block right if the position is less than the (screen width - player_size)
@compro1191
@compro1191 4 жыл бұрын
if player_pos[0] = Width - player_size[0] : player_pos[0] = Width - player_size[0] elif player_pos[1] = Height: player_pos[1] = Height - player_size[1] ..... try this , though cant figer out why u can escape from the right corners
@PepperBopps
@PepperBopps 4 жыл бұрын
if event.key == pygame.K_LEFT: if x > 0: x -= player_size elif event.key == pygame.K_RIGHT: if x < 750: x += player_size
@safimichael7856
@safimichael7856 3 жыл бұрын
I love this tutorial, good job mate.
@xanderthedragon2826
@xanderthedragon2826 3 жыл бұрын
Im watching this during a middle school game jam basically and I'm new to coding so this is helpful its better than stealing code which I was doing.
@KeithGalli
@KeithGalli 6 жыл бұрын
Video outline! 1:10 - Pygame Installation 2:57 - Creating a blank screen 4:40 - Making the game loop (+handling quit event) 7:46 - Drawing a rectangle 13:57 - Moving the rectangle with keypresses 20:10 - Blocks dropping from the sky 27:59 - Setting the frames per second rate (FPS)/Controlling speed of falling blocks 31:14 - Detecting Collisions 41:13 - Multiple enemies falling from sky (lists) 54:10 - Adding additional features (score, increasing difficulty, background/player colors) Also, remember to subscribe if you enjoyed the video! :)
@anuragsingh-kv6de
@anuragsingh-kv6de 5 жыл бұрын
import sys import random import pygame pygame.init() width = 800 height: int = 600 RED = (255, 0, 0) BLUE = (0, 0, 255) player_size = 50 player_pos = [width / 2, height - 2 * player_size] enemy_size = 50 enemy_pos = [random.randint(0, width - enemy_size), 0] clock = pygame.time.Clock() SPEED = 10 enemy_list = [enemy_pos] BACKGROUND_COLOR = (11, 238, 207) Score=0 myFont = pygame.font.SysFont("momospace",35) screen = pygame.display.set_mode((width, height)) game_over = False def set_level(Score,SPEED): if Score= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y < (e_y + enemy_size)): return True return False while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: x = player_pos[0] y = player_pos[1] if event.key == pygame.K_LEFT: x -= player_size elif event.key == pygame.K_RIGHT: x += player_size player_pos = [x, y] screen.fill(BACKGROUND_COLOR) drop_enemies(enemy_list) Score= update_enemy_position(enemy_list,Score) SPEED=set_level(Score,SPEED) text = "Score" + str(Score) label = myFont.render(text,1,RED) screen.blit(label,(width-200,height-40)) if collision_check(enemy_list,player_pos): game_over = True exit() draw_enemies(enemy_list) pygame.draw.rect(screen, RED, (player_pos[0], player_pos[1], player_size, player_size)) clock.tick(30) pygame.display.update()
@konsta2451
@konsta2451 5 жыл бұрын
why i cant draw a rect
@yoimtv1945
@yoimtv1945 5 жыл бұрын
hey! thanks for this amazing video. but in my program enemy player fluctuat
@violetkimani3118
@violetkimani3118 5 жыл бұрын
HI Keith! Great video! mine is all done but I realize that the enemy only comes down once, however the score keeps tallying. I can't seem to pinpoint the issue where do I check? Thanks
@drmorcant
@drmorcant 5 жыл бұрын
@@anuragsingh-kv6de can u send me that please?😅
@JoloJustus
@JoloJustus 5 жыл бұрын
Thanks for the tutorial, it was really helpful for me to learn python! But I've still got one question: Why do you define "SPEED" as a constant? I thought constants are values that won't get changed throughout the program, but SPEED is changing.. please help! :D
@KeithGalli
@KeithGalli 5 жыл бұрын
So I think when I originally defined SPEED I didn't have plans for it to change, and then when I added set level, I didn't think too much about how I defined it. So you are correct, SPEED is not a constant and should not have been declared with all caps. Great question.
@jackbryan5851
@jackbryan5851 5 жыл бұрын
Cool video with a lot of good content. Would have been nice to set bounds on the player movement (I just added a few and statements to the KEYDOWN event conditionals) so they can't move outside the screen as well as a game over window (Still working on this so it displays High Scores).
@apk2626
@apk2626 4 жыл бұрын
Thanks for the tutorial, I enjoyed it. you have a new sub.
@larryxie4759
@larryxie4759 5 жыл бұрын
Sometimes when I run the code and the player gets close to the enemy I lose.
@nielsbakker7455
@nielsbakker7455 4 жыл бұрын
you should try and remove the break command. that worked for me
@wuhu0
@wuhu0 4 жыл бұрын
the hitbox is messed up
@davislietuvietis3343
@davislietuvietis3343 4 жыл бұрын
24:18 did i just see him typing with his hands up or something is wrong with my eyes???
@KeithGalli
@KeithGalli 4 жыл бұрын
Confirmed. I am a wizard 😎.
@alekaditez3830
@alekaditez3830 4 жыл бұрын
@@KeithGalli I believe your clips are off line, my good sir. If you want i can edit your videos.
@shawnjames3242
@shawnjames3242 4 жыл бұрын
@@alekaditez3830 😂 nice try
@ruthobembe5566
@ruthobembe5566 4 жыл бұрын
Same here
@luyandantshudu9839
@luyandantshudu9839 4 жыл бұрын
me too
@JayMaverick
@JayMaverick 4 жыл бұрын
Dude, just this tutorial helped me make the connection between the theoretical garbage in my Python book and the actual practical application I want to make. Thanks!
@FunnyFunnyMoneyHoney
@FunnyFunnyMoneyHoney 10 ай бұрын
Thanks. I always learn something new watching your videos. My problem is I want to go from beginner to advanced immediately. I've decided to watch all of your videos even if I covered the same material on a different channel.
@MaenMarashdeh
@MaenMarashdeh 4 жыл бұрын
Take it from me I made a mistake during first 20 minutes and I had to go back step by step to fix the code. Robots truly take everything littlerly
@RaghavSuresh
@RaghavSuresh 3 жыл бұрын
1:51 how did he go that blank screen. please explain
@thelavagod
@thelavagod 3 жыл бұрын
Search cmd or Command Prompt
@JayGuts
@JayGuts 3 жыл бұрын
I really appreciate your transparency. Specifically, I think its great that you include look ups in your videos.
@shahzadsadruddin3916
@shahzadsadruddin3916 4 жыл бұрын
Great Video .Kieth you have good skills at teaching programming language. I hope you will continue to deilver your ideas in programming language like python
@detroitdrycleaning3658
@detroitdrycleaning3658 4 жыл бұрын
if pip isnt recognized, and you know you've downloaded pip, try "py -m pip install python"
@DoggerPerson
@DoggerPerson 4 жыл бұрын
try doing -m pip install --upgrade pip ,it'll install the current update
@jameeboo
@jameeboo 4 жыл бұрын
@@DoggerPerson it says win error
@safir2241
@safir2241 4 жыл бұрын
dude, you are the bomb diggity. i gave up after about 45 minutes but learned a whole bunch about python and pygame. I really am not in the mood to spend 3 hours going over an error because I lost sleep, so now i'll just move on, but thanks. (Now i know how to do collision!)
@tomsupis8724
@tomsupis8724 4 жыл бұрын
Great video, thanks a lot. What was a surprise for me, was that when we made an enemy_list and called the intake? enemy_pos, I didn't know that in for-loops we have to use that. In the beginning, I just used 'for enemy in enemy_list ' and then spent some time wondering why it's not working. I compared our codes, and it took me a while, but I figured it out. :)) Its a great tutorial and learned a lot, Thanks!
@sonutyagi2822
@sonutyagi2822 3 жыл бұрын
I am also 13 years old and you helped me. But when I run this, it's showing an error
@speedygod4446
@speedygod4446 3 жыл бұрын
Then find out what line and correct it 😑
@ilan_kayesar
@ilan_kayesar 5 жыл бұрын
oops, it seems like 11 people missed the like button!
@magictricks2385
@magictricks2385 5 жыл бұрын
18 now
@ilan_kayesar
@ilan_kayesar 5 жыл бұрын
Yeah i think they should get a new mous
@arandomgamerdude5801
@arandomgamerdude5801 5 жыл бұрын
Or it didn't work for them
@eichlergm2341
@eichlergm2341 5 жыл бұрын
now 25
@bde7864
@bde7864 5 жыл бұрын
27
@vladleustean1638
@vladleustean1638 4 жыл бұрын
Hello, I found this guide fun to explore. Thanks for giving us such content. P.S. at the collision function, it would be much easier to just use "if abs (p_x - e_x) < player_size and abs (p_y - e_y) < player_size"
@rofiihamdi65
@rofiihamdi65 5 жыл бұрын
Thanks man for sharing, i learned a lot from your videos. Keep it up!
@slamalracing5191
@slamalracing5191 5 жыл бұрын
14:43
@rambhardwaj5555
@rambhardwaj5555 5 жыл бұрын
hello !!! please read more ... I have copied all process but I don't know that how to move that rectangle ... I have used all the keys but the rectangle didn't move at all!!!
@tractor2899
@tractor2899 5 жыл бұрын
Exactly! I can't figure out how to do this either......
@KeithGalli
@KeithGalli 5 жыл бұрын
Make sure you include the line that resets the player position after you update x (player_pos = [x, y])
@adityarajasekar5138
@adityarajasekar5138 5 жыл бұрын
Why do you need to ask him to read more, when you can actually directly type it above?
@jeffkamau3432
@jeffkamau3432 5 жыл бұрын
Had a small time understanding but i guess i need more practice but either way you are a great teacher Keith
@IHATEBADMUSIC1000
@IHATEBADMUSIC1000 5 жыл бұрын
thanks for the great tutorial keith
@jacobrichard9569
@jacobrichard9569 5 жыл бұрын
import pygame import random import sys #initialize game have to do every time pygame.init() myFont = pygame.font.SysFont("arial", 35) WIDTH = 800 HEIGHT = 600 RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0,0, 255) WHITE = (255, 255, 255) BACKGROUNDCOLOR = (0, 0, 0) #player size and position player_size = 50 player_pos = [WIDTH/2, HEIGHT-2*player_size] #enemy size and position enemy_size = 50 enemy_pos = [random.randint(0, WIDTH-enemy_size), 0] enemy_list = [enemy_pos] #bullet bullet_size = 10 bullet_pos = [player_pos[0]+20, player_pos[1]+30] #speed of enemy (computer does it) speed = 10 #set the screen height screen = pygame.display.set_mode((WIDTH, HEIGHT)) score = 0 game_over = False #function that says how long things take clock = pygame.time.Clock() def set_level(score, speed): if score < 6: speed = 4 elif score e_y and p_y < (e_y + player_size)): return True return False def detect_collision(enemy_pos, player_pos): p_x = player_pos[0] p_y = player_pos[1] e_x = enemy_pos[0] e_y = enemy_pos[1] if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x + player_size)): if (e_y >= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y -1: bullet_pos[1] -= speed * 3 if collision_check(enemy_list, player_pos): game_over = True break elif collision_check_bullet(enemy_list, bullet_pos) or bullet_pos[1] < 0: bullet_pos = [player_pos[0] + 20, player_pos[1] + 30] drop_enemies(enemy_list) score = update_enemy_pos(enemy_list, score) speed = set_level(score, speed) text = "Score: " + str(score) label = myFont.render(text, 1, WHITE) screen.blit(label, (WIDTH-200, HEIGHT-40)) draw_enemies(enemy_list) pygame.draw.rect(screen, RED, (bullet_pos[0], bullet_pos[1], bullet_size, bullet_size)) pygame.draw.rect(screen, RED, (player_pos[0], player_pos[1], player_size, player_size)) clock.tick(30) pygame.display.update()
@jacobrichard9569
@jacobrichard9569 5 жыл бұрын
there hahaha. thanks!
@jacobrichard9569
@jacobrichard9569 5 жыл бұрын
Ismail Amiri my bad i was asking his advice as to how tp fix it
@jacobrichard9569
@jacobrichard9569 5 жыл бұрын
I think back is ctrl z or something
@KeithGalli
@KeithGalli 5 жыл бұрын
Sorry folks for the delay! Do you guys both still want help?
@KeithGalli
@KeithGalli 5 жыл бұрын
@Ismail Amiri I replied to the comment you directly posted. Post your code and the error you are getting as a response there!
@cradoo7862
@cradoo7862 4 жыл бұрын
I love this tutorial cause I want to learn to how to how to make a game and this is just cool
@davidkayode2737
@davidkayode2737 4 жыл бұрын
Thanks a lot , Your videos are really helpful.
@cceabe4640
@cceabe4640 3 жыл бұрын
well explained and useful, i will check that website. thanks for your help with python!
@kngklulzyz4797
@kngklulzyz4797 4 жыл бұрын
OMG Thank you i am 11 years old and wait to make coding for a living and be an IT this has helped me so much and now my friends are playing it and staring it around
@sultanarazia7082
@sultanarazia7082 3 жыл бұрын
Me too! i just do programming for fun. so excited to show off to my friends!
@KenzieAleron
@KenzieAleron 3 жыл бұрын
@@sultanarazia7082 same
@Aurumk1
@Aurumk1 5 жыл бұрын
Ugh thank god for you. Thank you for teaching me this easy system so I can make some cool games.
@XxxNikGaxxX
@XxxNikGaxxX 4 жыл бұрын
"easy" well good luck 2 u
@bobbysingh5666
@bobbysingh5666 4 жыл бұрын
THANKS SO MUCH! Ii am so happ that you gave me the pygame.org website that make understand more what is happening!
@jenniferdelossantoslopez7746
@jenniferdelossantoslopez7746 6 жыл бұрын
This helped me a lot, Thanks!!
@KeithGalli
@KeithGalli 6 жыл бұрын
Glad to hear it! :)
@victorpetursanchez-brunete2764
@victorpetursanchez-brunete2764 2 жыл бұрын
Great video. Quick question, what shortcut do you use to comment multiple lines?
@yashpatel2631
@yashpatel2631 2 жыл бұрын
I am 10 years old and your tutorial helped me a lot, other tutorials just tell you how to download it and then they just do this simple command: print(helloworld), and then end the video
@waelmhimdi7479
@waelmhimdi7479 5 жыл бұрын
thank you for the video it helped me recreate the game i also added a background and background music and coded the game oop ( orientated object) i faced a lot of bugs but i got a good smooth game in the end
@KeithGalli
@KeithGalli 5 жыл бұрын
That's awesome! Love that you coded it object oriented.
@serendipitous263
@serendipitous263 3 жыл бұрын
That was cool!! Your code worked great. By the way, can you add the boundaries to the screen so that the player doesn't go away while i keep pressing either arrow keys?
4 жыл бұрын
KEITH you deserve NOBEL PRIZE OF MAKING PYTHON FRIENDLY TO SENIORS like me. Last sunday I spent four hours learning about lists, watching at your video and practising and making new solutions of your explanations. I am really grateful, pal, best wishes from Guatemala, I will try to record something and send you the link of video. I am old programming stuff, and sought after Python but never was so interested, because some of the folks are kind of .... less didactic.
@KeithGalli
@KeithGalli 4 жыл бұрын
Thank you so much for your kind words! Very happy to hear that the videos have been helpful :)
4 жыл бұрын
I have, at last, a good way to organize my learning curve at Python, I have also started with the turtle, and I expect to dive deep into your lessons, in the next near months, or years, who knows. For the time being, I will share my advance with local students, who do not speak English (sigh). Dad payed me an English course when I was 15, my daughter is becoming an English teacher at the university, so English is the key to handle this kind of learning. Your voice is clear and understandable, thanks again. I will have a nice lunch on your honor, lol.
@lioneltran1521
@lioneltran1521 4 жыл бұрын
Thank you that's a nice video and teach me quite a lot! :D
@KeithGalli
@KeithGalli 4 жыл бұрын
You're very welcome!!
@krypton7412
@krypton7412 4 жыл бұрын
Thanks for the tutorial!
@moresteroidsbiggerdeltoids674
@moresteroidsbiggerdeltoids674 2 жыл бұрын
Keith we need some more game tutorials
@MustafaYasarr
@MustafaYasarr 5 жыл бұрын
Incredibly informative video thank you very much for your efforts!
@jcgamer1650
@jcgamer1650 4 жыл бұрын
Your tutorial is the best my idle was being dumb as hell and deleting some of my code
@mayaken_88
@mayaken_88 5 жыл бұрын
Thanks for the tutorial.
@joegliba3461
@joegliba3461 4 жыл бұрын
made the game!!! thank you so much
2 YEARS of PYTHON Game Development in 5 Minutes!
4:54
Coding With Russ
Рет қаралды 845 М.
PINK STEERING STEERING CAR
00:31
Levsob
Рет қаралды 23 МЛН
Василиса наняла личного массажиста 😂 #shorts
00:22
Денис Кукояка
Рет қаралды 8 МЛН
Универ. 10 лет спустя - ВСЕ СЕРИИ ПОДРЯД
9:04:59
Комедии 2023
Рет қаралды 2,5 МЛН
Luck Decides My Future Again 🍀🍀🍀 #katebrush #shorts
00:19
Kate Brush
Рет қаралды 5 МЛН
Python Generators Explained
28:37
Tech With Tim
Рет қаралды 145 М.
How to Program a GUI Application (with Python Tkinter)!
1:05:14
Keith Galli
Рет қаралды 943 М.
👩‍💻 Python for Beginners Tutorial
1:03:21
Kevin Stratvert
Рет қаралды 3 МЛН
I Made a Neural Network with just Redstone!
17:23
mattbatwings
Рет қаралды 633 М.
How I Learned to Hold My Breath for 4 Minutes
14:42
Mike Boyd
Рет қаралды 9 МЛН
Get Started in Pygame in 10 minutes!
10:19
Coding With Russ
Рет қаралды 270 М.
Bad Game Design - (Some) NES Games
16:52
Snoman Gaming
Рет қаралды 957 М.
How do computers read code?
12:01
Frame of Essence
Рет қаралды 3 МЛН
PINK STEERING STEERING CAR
00:31
Levsob
Рет қаралды 23 МЛН