How to Program a Game! (in Python)

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 มิ.ย. 2024
  • 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.2K

  • @KeithGalli
    @KeithGalli  3 ปีที่แล้ว +34

    Hey everyone! If you are looking to build out this game further, check out this video: th-cam.com/video/731LoaZCUjo/w-d-xo.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 ปีที่แล้ว +19

    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...

  • @KeithGalli
    @KeithGalli  6 ปีที่แล้ว +234

    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 ปีที่แล้ว +12

      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?😅

  • @mapplemoore197
    @mapplemoore197 3 ปีที่แล้ว +7

    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 ปีที่แล้ว +9

    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 ปีที่แล้ว +91

    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 ปีที่แล้ว +9

      Great stuff! Happy to hear this :)

    • @ardaalpergoz1823
      @ardaalpergoz1823 3 ปีที่แล้ว +2

      I am thinking the same

    • @veloukaliappan2295
      @veloukaliappan2295 3 ปีที่แล้ว +1

      @@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

  • @matthewtam1145
    @matthewtam1145 3 ปีที่แล้ว +7

    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

  • @bde7864
    @bde7864 5 ปีที่แล้ว +13

    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 ปีที่แล้ว +247

    this guy's camera is actually amazing

    • @asishraz6173
      @asishraz6173 4 ปีที่แล้ว +5

      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 ปีที่แล้ว +2

      @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?

  • @youssefadel5491
    @youssefadel5491 4 ปีที่แล้ว +204

    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 ปีที่แล้ว +8

      same, when did you start coding?

    • @youssefadel5491
      @youssefadel5491 4 ปีที่แล้ว +11

      @@anantkhanna4954 last year, at 11 years old

    • @anantkhanna4954
      @anantkhanna4954 4 ปีที่แล้ว +7

      @@youssefadel5491 nice

    • @extremetitan2847
      @extremetitan2847 4 ปีที่แล้ว +7

      @@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 ปีที่แล้ว +4

      Am 11 and i want to start coding and programming games

  • @Herculuke
    @Herculuke 8 หลายเดือนก่อน

    One of the best tutorials out there, keep it up!

  • @6977202865
    @6977202865 4 ปีที่แล้ว +2

    You teached me the most importand thing in coding!!!! USE THE DOCIMENTATION!!!!!! thank you soo much!

  • @colbablast6579
    @colbablast6579 4 ปีที่แล้ว +6

    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 ปีที่แล้ว +1

      *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

  • @rakeshkumarkuwar6053
    @rakeshkumarkuwar6053 4 ปีที่แล้ว +5

    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 ปีที่แล้ว +5

      Aww that's awesome to hear :).

  • @garryregan7216
    @garryregan7216 5 ปีที่แล้ว +1

    Managed to make it work! Enjoyed the video, thanks Keith

  • @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. 👍

  • @bricezakra
    @bricezakra 5 ปีที่แล้ว +6

    Just got done programming this game!You are amazing man! 👍🏿 🙂

  • @asishraz6173
    @asishraz6173 4 ปีที่แล้ว +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 ปีที่แล้ว +1

      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.

  • @veisystems267
    @veisystems267 4 ปีที่แล้ว +1

    Loved it... really quick and practical intro to pygame. Much thanks!

  • @adamansbah7017
    @adamansbah7017 3 ปีที่แล้ว

    with your help, I am able to build my first ever game in python. kudos to you.

  • @PeterKLee
    @PeterKLee 4 ปีที่แล้ว +4

    thank you for making amazing python tutorial, Keith! It is greatly helpful & easy to understand!

  • @javier2384
    @javier2384 5 ปีที่แล้ว +6

    Done with the course! Nice, clear, and comprehensive guide and clear voice! Big thumbs up! :D

    • @KeithGalli
      @KeithGalli  5 ปีที่แล้ว +1

      Glad you enjoyed!!

  • @gamerinyourtown5592
    @gamerinyourtown5592 2 ปีที่แล้ว

    this is one of the best tutorials. Thank you for making this. I really appreciate it.

  • @kirteekumari3459
    @kirteekumari3459 4 ปีที่แล้ว +1

    @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.

  • @felipeuchoa9384
    @felipeuchoa9384 5 ปีที่แล้ว +6

    Really good job, I couldn't just get out without leaving a like and subscribing.

  • @ptortello
    @ptortello 5 ปีที่แล้ว +3

    @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 ปีที่แล้ว +1

      Glad this video could help, even if it was just for a couple hours :). Keep your head up, you got this!

  • @sinaabbasi7670
    @sinaabbasi7670 ปีที่แล้ว

    you are very helpful Keith, thank you man, respect from Italy.

  • @safimichael7856
    @safimichael7856 3 ปีที่แล้ว

    I love this tutorial, good job mate.

  • @felixwang0702100117
    @felixwang0702100117 5 ปีที่แล้ว +8

    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

  • @f.j.bradman1761
    @f.j.bradman1761 5 ปีที่แล้ว +6

    thanks for your lecture ,learn a lot from it

  • @plazzmator
    @plazzmator 4 ปีที่แล้ว +3

    Thanks for great tutorial!
    Here is my own collision conditional: if obj_y - (enemy_y + 50)

  • @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.

  • @abhishekroy5119
    @abhishekroy5119 5 ปีที่แล้ว +4

    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! :) :)

  • @matheworson9669
    @matheworson9669 5 ปีที่แล้ว +3

    bro you got this, your good at teaching

  • @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.

  • @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.

  • @danielnilsson1592
    @danielnilsson1592 4 ปีที่แล้ว +3

    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 ปีที่แล้ว +3

      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.

  • @antek6445
    @antek6445 4 ปีที่แล้ว +7

    I just love how your camera is in higher framrate than your screen recording

  • @paulal1899
    @paulal1899 4 ปีที่แล้ว

    Thanks man this was super useful!! Keep up the good work

  • @RedTheHated
    @RedTheHated 4 ปีที่แล้ว

    Thanks, man. I'm new to python and learned quite a bit from this video.

  • @Lyttii_
    @Lyttii_ 4 ปีที่แล้ว +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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      @@Lyttii_ thz bro🔥🔥

  • @theginja581
    @theginja581 5 ปีที่แล้ว +4

    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 ปีที่แล้ว +2

      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!

  • @TheSuccessfulLeader
    @TheSuccessfulLeader 4 ปีที่แล้ว

    Thanks, Keith: I am learning and getting better and better...!

  • @vishnuaravind6093
    @vishnuaravind6093 3 ปีที่แล้ว

    your explanation is really nice keep up the awesome work

  • @itssammy777
    @itssammy777 5 ปีที่แล้ว +11

    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.

  • @jky9955
    @jky9955 5 ปีที่แล้ว +13

    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 ปีที่แล้ว +8

      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 ปีที่แล้ว +5

      @@KeithGalli Thank you so much! I have successfully got the code to work.

    • @KeithGalli
      @KeithGalli  5 ปีที่แล้ว +6

      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 ปีที่แล้ว +1

      @@beamjim1728 Do you still need help? Just seeing this now

  • @jeremyadamson9618
    @jeremyadamson9618 4 ปีที่แล้ว

    Thanks again Keith! Super helpful stuff.

  • @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.

  • @flyboyjb2209
    @flyboyjb2209 5 ปีที่แล้ว +4

    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

  • @chaudharycodes
    @chaudharycodes 4 ปีที่แล้ว +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 ปีที่แล้ว +2

      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 ปีที่แล้ว +2

      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

  • @JayGuts
    @JayGuts 3 ปีที่แล้ว +2

    I really appreciate your transparency. Specifically, I think its great that you include look ups in your videos.

  • @soybeanz8582
    @soybeanz8582 4 ปีที่แล้ว

    oh almost keith congrats on almost hitting 100k subs!

  • @soybeanz8582
    @soybeanz8582 4 ปีที่แล้ว +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 ปีที่แล้ว +1

      Hahaha thank you! :). One day I hope to bring more of that character into this channel!

  • @hamad87ad
    @hamad87ad 5 ปีที่แล้ว +4

    Please make a program that solves a Sudoku puzzle using python .

    • @AlanSanu
      @AlanSanu 3 ปีที่แล้ว

      bruh lol culd help

  • @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!)

  • @user-tm8ff8xf4w
    @user-tm8ff8xf4w 4 ปีที่แล้ว

    thank you very much for this such a good run through, i learnt a lot !!

  • @larryxie4759
    @larryxie4759 5 ปีที่แล้ว +8

    Sometimes when I run the code and the player gets close to the enemy I lose.

    • @nielsbakker7455
      @nielsbakker7455 4 ปีที่แล้ว +1

      you should try and remove the break command. that worked for me

    • @wuhu0
      @wuhu0 4 ปีที่แล้ว +1

      the hitbox is messed up

  • @MaenMarashdeh
    @MaenMarashdeh 4 ปีที่แล้ว +3

    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

  • @dexterellis7973
    @dexterellis7973 5 ปีที่แล้ว

    Thank you so much! I've learned a lot from this video.

  • @alimamulma3sum14
    @alimamulma3sum14 5 ปีที่แล้ว +2

    The ease and the simplicity is another level in the video bro
    Thank you for this from the bottom of my heart

  • @JoloJustus
    @JoloJustus 5 ปีที่แล้ว +8

    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 ปีที่แล้ว +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.

  • @RaghavSuresh
    @RaghavSuresh 3 ปีที่แล้ว +3

    1:51 how did he go that blank screen. please explain

    • @thelavagod
      @thelavagod 3 ปีที่แล้ว

      Search cmd or Command Prompt

  • @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

  • @lamikar
    @lamikar 5 ปีที่แล้ว

    Thanks Keith, gonna go home and try this out. subscribed :)

  • @davislietuvietis3343
    @davislietuvietis3343 4 ปีที่แล้ว +35

    24:18 did i just see him typing with his hands up or something is wrong with my eyes???

    • @KeithGalli
      @KeithGalli  4 ปีที่แล้ว +33

      Confirmed. I am a wizard 😎.

    • @alekaditez3830
      @alekaditez3830 4 ปีที่แล้ว +4

      @@KeithGalli I believe your clips are off line, my good sir. If you want i can edit your videos.

    • @shawnjames3242
      @shawnjames3242 4 ปีที่แล้ว +1

      @@alekaditez3830 😂 nice try

    • @ruthobembe5566
      @ruthobembe5566 4 ปีที่แล้ว

      Same here

    • @luyandantshudu9839
      @luyandantshudu9839 4 ปีที่แล้ว

      me too

  • @detroitdrycleaning3658
    @detroitdrycleaning3658 4 ปีที่แล้ว +4

    if pip isnt recognized, and you know you've downloaded pip, try "py -m pip install python"

    • @DoggerPerson
      @DoggerPerson 4 ปีที่แล้ว +1

      try doing -m pip install --upgrade pip ,it'll install the current update

    • @jameeboo
      @jameeboo 4 ปีที่แล้ว

      @@DoggerPerson it says win error

  • @justshan2413
    @justshan2413 4 ปีที่แล้ว +1

    I'm in grade 9 and you helped me with this tutorial.
    Thanks a lot

  • @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 :)

  • @slamalracing5191
    @slamalracing5191 5 ปีที่แล้ว +3

    14:43

  • @ilan_kayesar
    @ilan_kayesar 5 ปีที่แล้ว +5

    oops, it seems like 11 people missed the like button!

  • @IHATEBADMUSIC1000
    @IHATEBADMUSIC1000 5 ปีที่แล้ว

    thanks for the great tutorial keith

  • @apk2626
    @apk2626 4 ปีที่แล้ว

    Thanks for the tutorial, I enjoyed it.
    you have a new sub.

  • @rambhardwaj5555
    @rambhardwaj5555 5 ปีที่แล้ว +3

    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?

  • @jacobrichard9569
    @jacobrichard9569 5 ปีที่แล้ว +6

    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!

  • @cceabe4640
    @cceabe4640 3 ปีที่แล้ว

    well explained and useful, i will check that website. thanks for your help with python!

  • @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 ปีที่แล้ว +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 😑

  • @vladleustean1638
    @vladleustean1638 4 ปีที่แล้ว +1

    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"

  • @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

  • @JayMaverick
    @JayMaverick 4 ปีที่แล้ว +2

    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!

  • @kngklulzyz4797
    @kngklulzyz4797 4 ปีที่แล้ว +2

    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

  • @davidkayode2737
    @davidkayode2737 4 ปีที่แล้ว

    Thanks a lot , Your videos are really helpful.

  • @jeffkamau3432
    @jeffkamau3432 5 ปีที่แล้ว +1

    Had a small time understanding but i guess i need more practice but either way you are a great teacher Keith

  • @ghaziou86
    @ghaziou86 4 ปีที่แล้ว

    Good job Keith, conditions for detect_collision function could be just simpler as below
    if E_y> HEIGHT-Player_size:
    pass
    elif E_y > P_y-Enemy_size and abs(E_x-P_x)

  • @jackbryan5851
    @jackbryan5851 5 ปีที่แล้ว +2

    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).

  • @victorpetursanchez-brunete2764
    @victorpetursanchez-brunete2764 2 ปีที่แล้ว +1

    Great video.
    Quick question, what shortcut do you use to comment multiple lines?

  • @cradoo7862
    @cradoo7862 4 ปีที่แล้ว +1

    I love this tutorial cause I want to learn to how to how to make a game and this is just cool

  • @jenniferdelossantoslopez7746
    @jenniferdelossantoslopez7746 6 ปีที่แล้ว +1

    This helped me a lot, Thanks!!

    • @KeithGalli
      @KeithGalli  6 ปีที่แล้ว

      Glad to hear it! :)

  • @sarahgonzalez162
    @sarahgonzalez162 5 ปีที่แล้ว +1

    To not go out of bounds:
    if event.key==pygame.K_LEFT and x> 0:
    x -=player_size
    elif event.key==pygame.K_RIGHT and x

  • @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

  • @moresteroidsbiggerdeltoids674
    @moresteroidsbiggerdeltoids674 2 ปีที่แล้ว

    Keith we need some more game tutorials

  • @krypton7412
    @krypton7412 4 ปีที่แล้ว

    Thanks for the tutorial!

  • @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!

  • @rofiihamdi65
    @rofiihamdi65 6 ปีที่แล้ว +2

    Thanks man for sharing, i learned a lot from your videos. Keep it up!

  • @perinoveriza1658
    @perinoveriza1658 5 ปีที่แล้ว

    i like your style teaching...❤️👍👍👍

  • @mayaken_88
    @mayaken_88 5 ปีที่แล้ว

    Thanks for the tutorial.

  • @TomGrabowskiYouTube
    @TomGrabowskiYouTube 5 ปีที่แล้ว

    I'm using Visual Studio and my drop enemies function doesn't stop after the if statement. The function includes everything below it.

  • @The07sid
    @The07sid 3 ปีที่แล้ว

    Thankyou, I was able to create this Game

  • @tjhuntable
    @tjhuntable 4 ปีที่แล้ว

    Thanks Keith, learning Python and following your tutorials to help me gauge my progress. Still wouldn't be able to build from scratch.
    For those interested, I added code to make the PLAYER stop at the edges of the screen.
    In the GAME LOOP. EVENT TYPE, ADD the code after AND.....
    if event.key == pygame.K_LEFT and x > (WIDTH-WIDTH): #MAKES block stop at x = 0.
    x -= player_size[0] #moves block by player size, which is 50, put in 5 to see how slow.
    pass
    elif event.key == pygame.K_RIGHT and x < (WIDTH-player_size[0]):
    # makes block stop at x 800 - 50
    x += player_size[0]
    pass
    PS. my variable for player_size[50, 50]. Thats why I call the index player_size[0]. whereas Keith only used player_size = 50.
    please let me know if there is a more polished way to achieve this. Im still such a NOOB.

  • @kandyhowatt3651
    @kandyhowatt3651 3 ปีที่แล้ว

    Your ARe AMAZING EVERY PART OF YOU THANK YOU SOOOO MUCH MY VERY FIRST GAME :)))))))))))))))