Learning Pygame by making Pong Part 2: Adding the score and a countdown timer

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ธ.ค. 2024

ความคิดเห็น • 131

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

    I'm so glad you actually explain the mechanics of the things we are useing instead of just expecting us to follow blindly and be smart enough to just automatically understand it all via intuition. This is REAL teaching right here. Thank you so much and you have an amazing talent for teaching that many people don't have.

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

      Thank you :)

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

      I totally agree. People who go "Write this, this and this. Oh and don't forget that!" don't really teach anything and just puts me one step closer to copying the code on the repo they provide.

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

      In short, thank you for you amazing tutorials dude. Much apprechiated!

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

      You are totally true

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

      yes absolutely true

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

    dude this vid is amzing pls dont stop

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

    thank you so much I am about to add it to my code now. Keep making your videos becuase they are so good

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

    men, I watched both videos and I love it, I could understand everythig thx by create thsi tutorials :D

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

    Had to go through this one a couple of times, but worth it. Thanks. Good learning.

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

    Relearning python and this has helped me a lot i like the way you keep it simple and explain..

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

      Hey, glad it was useful. I actually made a larger tutorial series on Udemy on game development that is free for the next 3 days. Check it out if you want to see more from me: rb.gy/w4zxqt

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

    Bro, your all videos helped me so so so much to learn. love from INDIA, and a request - keep making pygame tutorials..... Waiting for your next video

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

    I love how you say hello in each video!!

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

    Dude..
    I am very thankful that I got your channel😊😊❣️

  • @karlolackovic4267
    @karlolackovic4267 2 ปีที่แล้ว +1

    03:03 Adding a score counter
    10:57 Creating a timer in Pygame

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

    clear code, I will be so happy if you make another totoriol like this one. :3

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

    Thanks for the tutorials, bro.

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

    Amazing teaching. Thank you

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

    Love these videos. New SUB right here!

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

    Thanks for the video. I enjoyed following along to this and the first part of the tutorial.

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

    I followed this awesome tutorial, grat job

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

    Nice tutorial but a correction needed at 23:08. Here any value evaluated to True can't be used. True works since it's converted to integer 1 implicitly during execution of "current_time - score_time". If you had used the string as you said, you'd get "unsupported operand type(s) for -: 'int' and 'str'" error.

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

    i am lucky that i got this channel
    thank to god

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

    Very neat and clear tutorial. Thanks

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

    Thank you it was very helpful for me to improve my programming skills. 🗿❤️

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

    Thank you very much, it’s very clear. 👌Great job 👏

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

    Since the score is not a rectangle object can i somehow also acces it's "middle" and "top" and all of that?

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

    22:36 Guys, type the screen height divided by 2

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

    I'd love to see more about timers in pygame. It's pretty complex, maybe we could have it in other tutorial as a refreshment?

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

      you mean, like this: th-cam.com/video/YOCt8nsQqEo/w-d-xo.html

  • @nirnrootnoises
    @nirnrootnoises 10 หลายเดือนก่อน +1

    Hey ClearCode, awesome tutorial and easy follow. Aprecciated all of the steps!
    Maybe an stupid question but... The oponnent doesn’t reach the bottom of the screen. It stops approximately 10 to 20 pixels before reaching the bottom and then starts moving upwards again. I’m not sure why this is happening 😅

  • @mayhemx0119
    @mayhemx0119 2 ปีที่แล้ว +1

    OK so iv got a problem all works untill the count down timer
    My code is cut and paste of yours but the value never returns like it should and instead returns the same value as the current time no clue why
    I setup print along the path I see it taking the data the reset to none everything works except it pulls the exact same data timestamp as the current time does
    Edit : I fixed it I made an error I ball start forgot to add if and separate ball speedx,ball speedy = 0,0
    So I understand what the the statment does it sets the ball speed to 0 but why did this break it ? I'll keep thinking why this happened

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

    hello, i love this tutorial, simple to follow, well done. would you mind adding a toggle option to choose if it's a single player game, with CPU.. or a 2 player game.. and use W / S for the player to the left of the screen and UP / DOWN for the player on the right of the screen
    again, thanks a lot for your videos. allows me to practice and revise some syntax and logic in python. +1 sub from me. much love and happy holidays

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

    Hey, is there any problem in using the pygame.time.delay() method, the method showed in this video is not working for me for some weird reason(problem a mistake somewhere). But i was able to get the same result with the delay function.

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

      I'm about to do the same thing as the value returned for both sets is the same along the path
      No clue why this work here but mine while printed along the way is either the same as current time or none or 0 depending on how it set

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

    Thx for your videos, I need more!!!! :)

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

    At 10:37 I can’t run the code because it says that “ball_start” is not defined

    • @nirnrootnoises
      @nirnrootnoises 10 หลายเดือนก่อน

      I had the same problem. I think he changed the name of the function from the part 1 video. It was "ball_restart()" so i changed it to "ball_star()" i'm late for sure but i hope you solved the problem. 🙂

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

    Great Tutorial!
    Is it possible to use the time library and time.sleep function to put a delay of 2 secs after the ball is placed at the center whenever someone gets a point?

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

      Hey, thank you. And I made a dedicated tutorial on timers: th-cam.com/video/YOCt8nsQqEo/w-d-xo.html

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

    So basically these surfaces are like layers in photoshop and we need to put the layer we need to display above them, righttt???

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

    Great tutorial. Thank you!

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

    Hey Clear Code,
    I got this error while i was testing the game.
    DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
    screen.blit(number_three, (screen_width/2 - 10, screen_height/2 + 20))
    Even after this error, my game still runs. It does not exit automatically.
    Please help me...

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

      I even used int() function... Still does not work.

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

      Hey, this line shouldn't close the game, so am not sure what you mean. But if you int() around both of the coordinates the error should disappear

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

      Thanks a lot! That worked!
      The actual problem was i was using int() to all the values.
      Love the videos. I have a competition of pygame in my country(I am in the 8th standard). So, i was preparing for that,
      and stumbled upon the vids. Thanks!

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

      Same ...

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

      How to solve

  • @Henry-es1cp
    @Henry-es1cp 3 ปีที่แล้ว

    Thanks a lot man! Keep up your work please.... =)

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

    Thanks ❤❤

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

    amazing video!!!!!!! you got a new sub!!!

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

    you are the best.

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

    you might not see this but if you do how can I fix this error in my terminal it says "unsupported operand type(s) for -: 'int' and 'Nonetype'". I really hope that you get to respond to this.
    edit: I found it out, I just needed to change the "None" to "int" because I just tried to do what the IDE wanted me to do.

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

    Just a reminder of where I left off: 16:25 (You’re welcome, future me)

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

      What I do is right click on the video and select "Copy video URL at current time" and then paste that into a comment in my code. Then when I come back to my code I can just copy and paste the link into a browser.

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

    In my instance, whenever the ball launches to the top left corner, the opponent is unable to move his paddle to hit the ball in time. I tried to fix this by increasing the opponent_speed variable, but was unsuccessfully. As I made opponent_speed arbitrarily high, the opponent's paddle movements became choppier, but were still unable to reach the ball when launched to the top left. Any tips on how to make an ai that can plays close to perfect?
    Interestingly, this problem occurs in my instance by not yours found on GitHub because I used custom dimensions (height = 830, width = 1280) for the screen.

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

      Hey, Yes, I noticed. I will make a few more tutorials on it for better ai and some other things. Afraid it is a bit too long too explain in a comment.

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

      @@ClearCode No worries, I got some help on it from my Python users group. Besides the two statements that stop the paddle from moving off screen, you can control the opponent paddle movement perfectly with:
      opponent.centery += min(ball.centery - opponent.centery, opponent_speed)

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

      That is indeed a workable solution to make the opponent play perfect, but it also makes the game a bit difficult I'd say. It is probably best to use timers and only let the opponent adjust the direction like every 500 milliseconds. Actual complex ai is a tad complex though.

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

      @@jimmy_py6861 Here is a good resource for ai: www.gameaipro.com/

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

      I showed my father and he did this alot. He said its an old trick or something

  • @user-ng4bc3cv6g
    @user-ng4bc3cv6g 4 ปีที่แล้ว +1

    Tutorial looks cool.
    What is your syntax font please?

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

      I am using sublime and never changed the font, so I think it is a font called 'Consolas'

    • @user-ng4bc3cv6g
      @user-ng4bc3cv6g 4 ปีที่แล้ว +1

      @@ClearCode Thank you. I'm always messed up with the font. But you gave me proper stable solution.

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

      @@user-ng4bc3cv6g Could you explain what your problem with the font is? I am not sure if I understand

    • @user-ng4bc3cv6g
      @user-ng4bc3cv6g 4 ปีที่แล้ว +2

      @@ClearCode It is because of the font size when you have a 'Consolas' font size between 14 - 18 it is pixelated and not smooth. I just made it up to 20. And also I' am using a font changer package that has a lot of monospaced fonts.
      I just thought that the font that are you using is one of them. But you told it's consolas. Thanks.
      Sublime was very useful when you have the mini left screen of your code.

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

    please help i am really stuck, my ball does not restart after adding the score, i checked my functions as well but somehow it does not works any more :'(

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

      Hey, I'd need more detail to help. But a really common error is to forget an indentation somewhere. Do you get an error message?

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

      I was too stuck at this place but after seeing carefully I saw that carefully It
      seems to me like i forgot to put parenthesis () after the ball_restart function
      hope it helps

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

    i dont know why i cant get the timer to appear as an error is popping up saying referenced before assignment although it is an error that i can get rid of by getting rid of the if statements under def ball_start but then the game wont have timer lol

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

    I know it's probably fixed in future tutorials but I did this instead of re writing "screen.blit(number, (WINDOW_SIZE[0]/2 - 8 , WINDOW_SIZE[1]/2 - 11))"
    if current_time - score_time < 700:
    number = game_font.render('3', False, light_grey)
    elif current_time - score_time < 1400:
    number = game_font.render('2', False, light_grey)
    elif current_time - score_time < 2100:
    number = game_font.render('1', False, light_grey)
    else:
    number = False
    if number:
    screen.blit(number, (WINDOW_SIZE[0]/2 - 8 , WINDOW_SIZE[1]/2 - 11))

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

    What programs do you use to write and run the code?

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

      @@cadds8055 Thanks!

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

      @@markus808 I'm actually using sublime :P

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

      @@ClearCode I heard that Pycharm is really good. Which one would you recommend for a beginner though?

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

      @@markus808 I wouldn't overthink it if you are just starting out. Basically all code editors are fine these days. If you are happy with pycharm go with pycharm.
      2 things you might want to look for though: If you learn python in a class or course, try to use the editor the course uses, can make things a little easier. Second, especially if you start, having a code editor that autocompletes methods and functions is quite useful, so you see all the stuff you can do. Pycharm has that one, so you should be good. (this is what I am talking about: www.jetbrains.com/help/pycharm/auto-completing-code.html)
      But generally, don't overthink it. The important thing for you is to write lots of code, how you do that isn't all that important :)

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

      @@ClearCode Thanks, I used your tutorial to make a pong game with pygame. The Pycharm autocorrect thing made it easier to fix the bugs. (spelling errors)

  • @dungngoquang8354
    @dungngoquang8354 10 หลายเดือนก่อน

    I did it, thank you

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

    Congratulations

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

    I have a problem when I put in the arguments for player ball and opponent it says that I have a deprecation error and that the rect numbers are a float not an integer
    For example
    Ball = (screeen_height/2 - 15,screen_length/2 - 15, 30, 30) if you could help with this that'd be great

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

      hey, that is easy to fix, just put int() around each value. The correct line would be: Ball = (int(screeen_height/2 - 15),int(screen_length/2 - 15,) 30, 30)
      Python started to be a bit annoying about converting floats to integers from version 3.8 but since I used 3.7 it did not come up.

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

    Whenever I try to implement the score_time I get this error: Expected type 'int", got 'None' instead. Any idea how to fix this?

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

    In some moments when the ball hits the left and right sides, it is hooked. Can you help me fix it?

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

      I made some follow up videos to fix that!

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

    Hello, is there any way that every time the ball collides with the player and opponent, the ball's speed is increased?

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

      Yes, whenever the ball hits the paddle instead of multiplying by -1 multiply by lets say -1.5, that will increase the speed of the ball. Just play around with the numbers

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

    Why u dont use classes? Class for player, class for Ball, class for scene?

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

    Hey, can someone tell me what theme is he using? Thanks!

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

    please help bro. I am facing problem in pycharm when I run the code it doesn't shows the score which is 0 but it shows (player_score) and (opponent_score)

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

      oh sorry that was curly braces problem solved. EPIC VIDEO

  • @Ugty-c3i
    @Ugty-c3i 4 ปีที่แล้ว

    Hey, I have a Problem. I got this error: Cannot reference 'choise' in 'random.pyi'. I don't know what to do...

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

      It would be 'choice', not 'choise'

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

    Hello Clever Code you are awesome programmer, i watch your video, i wanna know which ide you use, pls reply, i subscribe and liked your video

  • @Pablo-gw5lu
    @Pablo-gw5lu 3 ปีที่แล้ว

    Whats his color theme?

  • @junegaupp-wagener4278
    @junegaupp-wagener4278 3 ปีที่แล้ว +1

    you explain how to mak a timer i import time

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

    for the timer , what about using delay ? btw, great tutorial thank you so much for this

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

      thank you :) And you could use delay, but it pauses the entire code whereas the intention here is to just pause the ball. Admittedly though, in this case the difference would only be that you can't move the paddle when the ball is restarting.

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

      @@ClearCode oh yeah true i didn't think of it thoroughly i guess, anyways amazing contents and best of luck in the futur :)

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

      Couldn't you just thread that last so the ball mechanics run outside the main loop
      I ask cuz I don't know new to all this

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

    I'm on an old version and couldn't use f string so I used str(player_score)

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

    Can't you just put the text on the main surface instead of onto another one?

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

    Vielen dank!!!!!!

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

    wha do you use instead of f"

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

      I am not sure what you mean, is it f-strings?

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

      @@ClearCode yes , for me it says that python 3.5 doesn't support f strings

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

      @@alexanderkovalenko7304 oh yes, f-strings have only been introduced with python 3.6. You are aware that you are using a python version that is half a decade old, right?
      Updating your version of python would be the easiest solution and I am afraid I don't know from the top of my head what tool were available in that python version. Maybe check out the method .format, I think that would work.

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

      For reference, the latest version of Python is 3.83

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

      Clear Code I have 3.8 downloaded but oy harm is being weird

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

    can I get the code anywhere?

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

      Yes, of course: github.com/clear-code-projects/Pong_in_Pygame

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

      @@ClearCode thx

  • @Henderson123
    @Henderson123 10 หลายเดือนก่อน

    7:29

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

    huge

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

    Ugochuselvezasub!

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

    Your instructions at 5:15 do not work, i do not believe freesansbold is in every computer like you stated, instead i used this line....game_font = pygame.font.SysFont(None, 48) and it works now.
    also you refactored a bunch of stuff from the last video and renamed some of the functions such as ball_restart() to ball_start(), maybe don't change things drastically from the last video with out going over it first.

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

      Yeah, sorry about that. It was quite a while since the first and second tutorial and I didn't realise that I made changes to the code in the meantime.

  • @1Peu2Lov
    @1Peu2Lov ปีที่แล้ว

    The socre_time doesn't work for me... Like I got the error ( "Expected type 'int', got 'None' instead" ). the score_time = pygame.time.get_ticks() seems to not work.

    • @1Peu2Lov
      @1Peu2Lov ปีที่แล้ว

      probleme fix lol , sorry