Creating Tetris in Python with pygame - Beginner Tutorial (OOP)

Поделиться
HTML-код
  • Опубликовано: 17 ноя 2024

Комментарии • 121

  • @programmingwithnick
    @programmingwithnick  4 месяца назад

    I hope you took away lots from this video. Let me know below!
    PS: I created a course named "Object Oriented Programming Made Easy"! Sign up at bit.ly/3NaMfg4. I think you will enjoy it!

    • @okoruwaimmaculate4650
      @okoruwaimmaculate4650 3 месяца назад

      What do you do when you have all the functions and the blocks are not responding to it? It is not rotating, moving left or right. In fact not responding to keyboard strokes in general?

    • @okoruwaimmaculate4650
      @okoruwaimmaculate4650 3 месяца назад

      I used print statements and noticed that the keystrokes were working but not changing the movement of the blocks.

    • @flyboyvii
      @flyboyvii Месяц назад

      There is still have a chance that the newly spawned block overlap others just before game over. I have modify this line on game.py-> draws function
      if self.block_fits() == True:
      self.current_block.draw(screen, 11, 11)
      to prevent it drawing on the screen.
      Anyway, this is still the best Python Tetris Tutorial I 've ever seen, Great job!

  • @DandaMan2022
    @DandaMan2022 5 месяцев назад +6

    This was really easy to follow and fun to program! One thing I did after is adding a storage function like in modern Tetris games. You have a window on the right for which block you are storing (you can store e.g by pressing shift) and can replace your current block with that block. You can't replace a current block that you have stored though.

  • @Charlotte-b9w4y
    @Charlotte-b9w4y Месяц назад

    I love this tutorial sm. It's not one of those python tutorials where the guy just writes code and doesn't explain it, it's great that u taught us how the code and logic works 😁

  • @Wayko6217
    @Wayko6217 10 месяцев назад +3

    Added a function to update the speed after reaching 2000 points and also level. Still have to work on the logic behind it because if the value modules is not equal 0 it doesn't update but if I search other it increases automatically. I moved the GAME_UPDATE to the game.py and added the code below to the update_score section
    if self.score % 2000 == 0:
    self.newSpeed = Game.update_speed(self.newSpeed)
    self.level += 1
    if self.newSpeed > 50:
    pygame.time.set_timer(self.GAME_UPDATE, self.newSpeed)
    else:
    self.newSpeed = 50

    def update_speed(current_speed):
    current_speed -= 10
    return current_speed

  • @helloworld9531
    @helloworld9531 Месяц назад

    One of the best tutorials I have ever seen. A simple, to-the-point explanation ...................
    Thank you for such a wonderful course
    🙌🙌🙌

  • @MinhNhateptrai
    @MinhNhateptrai Месяц назад

    By far the best tutorial on OOP in Python, Pygame. Well done Nick, and thank you very much for all your work!

  • @ericleboullenger9348
    @ericleboullenger9348 11 месяцев назад +4

    By far the best tutorial on OOP in Python, Pygame. Well done Nick, and thank you very much for all your work!

  • @ThatGuyAndres13
    @ThatGuyAndres13 Год назад +10

    I love how you go into detail with this video about the code, I just started python yesterday and I'm doing well because of you! Thank you!

  • @shravanpanchal8920
    @shravanpanchal8920 Год назад +7

    Nick, your tutorials are quite unique and quite detailed, I love your videos man, I think it is much better than other similar projects on youtube, and I have a request : could you please make more of such python game or other python projects, it would be very fun to learn from you.
    💯

  • @paulotrano103
    @paulotrano103 3 месяца назад +1

    This was honestly really fun to code, I'm currently trying to get in more practice for coding in Python and this was a nice side project to work on, hopefully you make more tutorials like this.

  • @commandprompt7171
    @commandprompt7171 Год назад +2

    thank you so much for explaining the game logic with readable code, I was trying to code tetris in both C++ and python by myself, and I was struggling at the move rows down logic and clearing the rows logic, I felt like I was going insane, and most of the solutions on google searches are mostly magic values and unreadable code, witch it does not help at all, but your solution by far is the cleanest tetris code I've seen, it might take more code lines, but I do appreciate to be able to read all those class methods.

  • @Flanx-ul5hj
    @Flanx-ul5hj 6 месяцев назад

    Finally understood how object oriented programming works, thanks a lot!!

  • @neomatrix-1
    @neomatrix-1 5 месяцев назад

    this was insanely helpful and helped me grasp a lot of concepts in python. Along with that, it also taught me how to approach different problems and how to implement them inside of python. Thank you so much!

  • @antoinesadarnac5652
    @antoinesadarnac5652 Год назад +2

    Thank you, I learnt a lot and succeeded in making snake by myself after this video without any help

  • @thegrayfox666
    @thegrayfox666 10 месяцев назад +1

    I really enjoyed working on this project alonside. Thank you! :D

  • @programmingwithnick
    @programmingwithnick  Год назад +27

    Hello guys! I hope you enjoyed the video! There is an error in the code as it appears in the video. In the block.py file the undo_rotation method should be like this:
    def undo_rotation(self):
    self.rotation_state -= 1
    if self.rotation_state == -1:
    self.rotation_state = len(self.cells) - 1
    The error is fixed in the github repository. Sorry for this.

    • @jeffersonfuckner3617
      @jeffersonfuckner3617 Год назад +1

      I was stuck in the error but, then I decided to check on comments and here is the answer. kkk Tks

    • @jeffersonfuckner3617
      @jeffersonfuckner3617 Год назад +1

      I'm now at the automatic droping part.

    • @armandoherrera1717
      @armandoherrera1717 Год назад

      I was stuck in this error lol. I thought it had something to do with me writing it in JavaScript lol. I was panicking. Then I asked chat GPT and it solved it for me, but here was the answer all the time 🤣

    • @mrgary4u819
      @mrgary4u819 6 месяцев назад

      Thanks for the fix! I suggest you pin this comment so it's easier to find. Loved the explanation and the attention to every detail.

    • @engineeringwithandy6434
      @engineeringwithandy6434 25 дней назад

      I was just thinking this wasn't quite right and read down the comments :)

  • @frankspaceyhelder
    @frankspaceyhelder Месяц назад

    Great tutorial. Was able to do it and really happy with how it came out! Thanks

  • @claireoneill3955
    @claireoneill3955 10 месяцев назад

    This tutorial was a really good starting point for my Tetris project. Thanks so much for the help!

  • @robin3883
    @robin3883 10 месяцев назад +2

    i have found a new solution for undo method. for example, when rotating at the edge, it should rotate and move the block inside the screen.
    my code is:
    def rotate_block(self):
    self.current_block.rotate_block()
    while self.block_in_screen() == False:
    self.current_block.move(0,1)
    seems unnecessary but hope it helps

  • @gianmarcoferraro2544
    @gianmarcoferraro2544 10 месяцев назад

    A very clear and complete tutorial. Thank you

  • @daisyb1469
    @daisyb1469 11 месяцев назад

    In chapter "Rotate the blocks" , method rotate(self), second line - I think it should be: if self.rotation_state == len(self.cells) - 1.

  • @quinnfly1708
    @quinnfly1708 6 месяцев назад

    you are the reason i passed my OPP assessment in com sci

  • @manuelgarciagarcia2501
    @manuelgarciagarcia2501 Год назад

    Excellent tutorial, these videos will serve as a bible for me to consult how to make many types of games. Thank you very much for sharing all this material.

  • @freesoftwares6042
    @freesoftwares6042 7 месяцев назад +1

    okay , i subbed and liked , i like this side of youtube

  • @ItsMeKippun
    @ItsMeKippun 11 месяцев назад

    nice work dude!, now i can finish my college final tasks :)))

  • @paulthomas1052
    @paulthomas1052 Год назад

    Great demo and fantastic code for this Tetris game. Thanks so much !

  • @manoloexplaintome6348
    @manoloexplaintome6348 6 месяцев назад

    Thank you por making this amazing video. I've enjoyed it a lot.

  • @nicholasflint9480
    @nicholasflint9480 2 месяца назад

    Nice tutorial! Is there a way to make it so you can hold the down button and have the block move down continuously?

  • @dragonstar78372
    @dragonstar78372 2 месяца назад +2

    Cool tutorial but i can’t seem to make a python file. Its just not an option for some reason😢

  • @서릐-l1n
    @서릐-l1n 24 дня назад

    Where using Visual Studio, in main.py game_grid = Grid()
    I wrote game_grid.print_grid(), and 'print_grid' appeared in the 'Grid' object. What should I do?

  • @ks111541
    @ks111541 Год назад +4

    Hi, I found an error. If a block is in it's last rotation state and you try to rotate it when it's not possible then first the rotate function will set the rotation_state to 0 and next the undo_rotation function will decrease the rotation_state by 1, so the index of the dictionary will be -1 which will crash the game. The line in undo_rotation "self.rotation_state -= 1" should be after the if statement in else statement , not before. Besides that, the video is great! Much appreciated!

    • @programmingwithnick
      @programmingwithnick  Год назад +1

      Good catch! You are absolutely right! I totaly missed it. I played the game for hours and never crashed. I looked over this piece of code of hours and never occured to me that the value can become negative. Thanks for sharing!

    • @programmingwithnick
      @programmingwithnick  Год назад +2

      Now, that I recall me, programming this piece of code, I wanted the method to look like this:
      def undo_rotation(self):
      self.rotation_state -= 1
      if self.rotation_state == -1:
      self.rotation_state = len(self.cells) - 1
      which works. I messed up the if statement.

    • @olssonolov
      @olssonolov Год назад +4

      To avoid tis you could also use the mod4-methode:
      def rotate(self):
      self.rotation_state = (self.rotation_state+1)%4
      def undo_rotate(self):
      self.rotation_state = (self.rotation_state-1)%4

    • @hypebeaver4150
      @hypebeaver4150 11 месяцев назад

      nice@@olssonolov

    • @David-fq5un
      @David-fq5un 8 месяцев назад

      Much better, go from 3 lines to ONLY 1 line@@olssonolov

  • @Tims_Projects
    @Tims_Projects Год назад

    Great work Nick, it's quite detailed, I will have to watch it in small intervals to take it all in.👍😁👍

    • @programmingwithnick
      @programmingwithnick  Год назад

      Thank you Tim. Yes, it is a quite long video but it will definately help you understand the game mechanics of this ingenius game and a few more advanced programming practices.

  • @anke4347
    @anke4347 Год назад

    This is a fantastic tutorial. Thank you!

  • @jimmyting1103
    @jimmyting1103 6 месяцев назад +1

    Hello Nick big fan i am having some trouble with 29:33 with line 22 givingme an error which says -> AttributeError: 'Grid' object has no attribute 'draw' ive looked over atleast 100 times and all of my code is exact as it is on screen at this current time in the video i even completely rewrote all of main.py and grid.py any reason why this is?
    i am using sublime as well

  • @Blazaram
    @Blazaram 7 дней назад

    Thank you for the video sir :)

  • @RAKSHITHNPOOJARY
    @RAKSHITHNPOOJARY Год назад +1

    Hi Great video, But i facing an error When move the blocks to left the boundary check is done correctly but when try to move it to the right it goes out of bound .Any reson why it might have occured

  • @ravilapieri
    @ravilapieri 6 месяцев назад +1

    Amazing job!! Thank you very much. Could I use this code with my students, with the credit to your work and channel?

    • @programmingwithnick
      @programmingwithnick  6 месяцев назад

      Yes, of course. This is why I make these videos. To help as many people as possible to learn programming.

    • @ravilapieri
      @ravilapieri 6 месяцев назад

      ​@@programmingwithnick Thank you!!! Your teaching is fantastic and the code is very organized..
      👏👏

  • @AshishKumar-of6xe
    @AshishKumar-of6xe Месяц назад

    Thank you Nick. It is very helpful. I have a request to you, I do not want to start the game directly. First it gives the option to "Play Now" and when we enter , the game starts and at the end of the end , it should show a messege "Play Again". How do i do it? Please help in this Nick.

  • @netaduvinsky2833
    @netaduvinsky2833 6 месяцев назад

    Hi nick I have a problem whit the creating blocks
    Can you help me?

  • @SpirosLymperopoulos
    @SpirosLymperopoulos Год назад

    Great video,we learn so much from this channel!

  • @S-qz4pk
    @S-qz4pk 11 месяцев назад

    23:01 the color of the back ground which is dark blue doesn’t appear
    and also the list of lists of zeros
    i wrot the same code but nothing appears

  • @nathanapiag511
    @nathanapiag511 Год назад

    Hello Nick! When I tried running the code by the end of step 5 (moving the blocks) if press any of the 3 keys (move_left, move_right, move down) the program ends with an error saying that the " object 'Game' does not have an attribute "move_left" for example if i press the left key. I have already defined it in the game class same as your code. I don't know what to do I can't progress to step 6 as it cannot properly run and I cannot move the blocks. Thank you

  • @EmirKayaloglu
    @EmirKayaloglu Год назад +1

    FileNotFoundError: No file 'Sounds/rotate.ogg' found in working directory 'C:\Users\Packard bell\Desktop\Python Örneklerim'.
    Unfortunately, I received another error and my game is not working.

    • @GrooveProducciones
      @GrooveProducciones 7 месяцев назад

      estas en el directorio incorrecto, ubicate en la carpeta en donde se encuentre el archio correspondiente

  • @trevorgardner9466
    @trevorgardner9466 Год назад

    Great video, I really learned a lot! But I was wondering how I would implement a block holding feature?

  • @daisyb1469
    @daisyb1469 11 месяцев назад +1

    I don't know why my blocks keep going (partially) out of the grid whenever I move them left or right.

    • @nicholasflint9480
      @nicholasflint9480 2 месяца назад +1

      I had the same issue. In game .py in function block_inside, unindent 'return True' once so it lines up with the for loop instead of the if statement

    • @theyellowjacket
      @theyellowjacket Месяц назад

      @@nicholasflint9480 thanks man, I was having the same problem. Thanks for the solution

  • @oum_iyad
    @oum_iyad 9 месяцев назад

    Nice, love Tetris

  • @HangThuy-mw2zh
    @HangThuy-mw2zh 5 месяцев назад

    WOWWY BOWWY WOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOW.
    Your tutorial so perfecto.

  • @wasgehtdaab
    @wasgehtdaab 9 месяцев назад

    😎Can you use images instead of colors? I partially managed it myself, but it doesn't work with current_block and next_block.

  • @zeellynwokenta
    @zeellynwokenta 6 месяцев назад

    Please could someone show me how to do the Grid file import, I use pycharm, so the ctrl n and ctrl s thing didn't work. Thanks

  • @Gamnastyle92
    @Gamnastyle92 11 месяцев назад

    Somehow there is an error for the sound code although I created a file called Sounds with the music you implemented in.

  • @fvelez78
    @fvelez78 Год назад

    Hi, great tutorial... I´m doing this... however I´ve a problem... when I move the block to right, this one is left out by a rectangle. I print positions into the for of the get_cell_positions method and for LBlock in the initil position after moving evrything to the right the position are: (0,16)(1,16)(1,17)(1,18)...plopppp... Why the column are greater than 10?

  • @itsmanyashukla2546
    @itsmanyashukla2546 Год назад

    continuosly showing this error to me ->
    in grid.py file under the def draw method
    pygame.draw.rect(screen , self.colors[cell_value] , cell_value)
    TypeError: rect argument is invalid
    also game screen is disappearing immediately but before making grid.py it was staying on the window unless i click the red cross...
    can you help me out real quick please!!!!

  • @IronDragon2940
    @IronDragon2940 Год назад +1

    I keep running into this error
    This is for the grid.py
    return [dark_grey, green, red, orange, yellow, purple, cyan, blue]
    ^
    IndentationError: unindent does not match any outer indentation level
    [Finished in 106ms]

  • @entity_dragons2013
    @entity_dragons2013 14 дней назад

    cannot import name Grid form grid help please

  • @geencaiilemalaguena2474
    @geencaiilemalaguena2474 Год назад

    heeeeeeeelp someone help me writing the codes in vs code and in line 13 it says "sys" not defined Pylance, is it normal?what should i do?

  • @rayhanfathurrizki4154
    @rayhanfathurrizki4154 Год назад +1

    sir, i have an error, when i tried to run, this appeared
    File "c:\Users\asus\Desktop\mencoba game2\main.py", line 29, in
    game.draw(screen)
    File "c:\Users\asus\Desktop\mencoba game2\game.py", line 43, in draw
    self.current_block.draw(screen)
    File "c:\Users\asus\Desktop\mencoba game2\block.py", line 29, in draw
    for tile in tiles:
    TypeError: 'method' object is not iterable
    do u have any suggestion?

    • @programmingwithnick
      @programmingwithnick  Год назад +1

      IN the block.py file you have an error. Check if you have place some () where it not needed. Check your code again very carefully.

  • @S-qz4pk
    @S-qz4pk 10 месяцев назад

    Nick I wrote the same codes you wrote but nothing appears on the screen if the game ,even the screan cames quickly and dissappears ,what should i do

    • @WormPancake
      @WormPancake 9 месяцев назад

      Make sure you have your lines of code lined up

  • @isaacleydon7301
    @isaacleydon7301 Год назад

    Mine has a problem where after 7 blocks have landed another block doesn't spawn. Any idea what might be wrong?

    • @programmingwithnick
      @programmingwithnick  Год назад +1

      Maybe you have forgotten to recreate the blocks list. Give it a check.

  • @alejo_skrt772
    @alejo_skrt772 6 месяцев назад

    Thank you!!!!

  • @EmirKayaloglu
    @EmirKayaloglu Год назад +1

    if self.grid[row][column] == 0:
    ~~~~~~~~~^^^^^
    IndexError: list index out of range
    First of all, it's a great example, I'm almost at the end of the game, but I can't get rid of this mistake, please help

  • @nymaztek0315
    @nymaztek0315 10 месяцев назад

    I am a newb to coding. How do I add a hard drop if the space bar is hit in this code? If anyone is able to help I would appreciate it

  • @Abubakr2007
    @Abubakr2007 Год назад +1

    What software did you use to code?

  • @kookie.dough09
    @kookie.dough09 7 месяцев назад

    Hello, thank you for this detailed tutorial for beginners. I just want to point out one thing, I have an error in 56:45:
    AttributeError: 'Tblock' object has no attribute 'column_offset'
    Can anyone please provide me with a solution because I have been stuck on this error for the past 15 minutes.
    Thank you dearly.

    • @kookie.dough09
      @kookie.dough09 7 месяцев назад

      (btw I am using VScode which may differ, thank you.)

  • @practicalbong1497
    @practicalbong1497 Год назад

    absolutely amazing

  • @gauravdhamsekar3802
    @gauravdhamsekar3802 Год назад

    Array are used?

  • @shubham7854
    @shubham7854 Месяц назад

    thanksyou

  • @edm8535
    @edm8535 Год назад

    Hi. Just finnished the "Checking for Collisons" part of your video. Everything went great until I realized that if the user moves a piece into another piece from either left or right, it intertwines and locks in place. Do you have a fix for this? I'll continue on in the video in hope that maybe you also get this error. If not, hopefully you can get back to me. Thanks in advance!

    • @programmingwithnick
      @programmingwithnick  Год назад +2

      I fix this problem in the video in the Checking for Collisons chapter. Are you sure you finished watching it?

    • @edm8535
      @edm8535 Год назад +1

      @@programmingwithnick Hi, you are correct. Seems I ran into the problem and stopped paying attention to you explaining the fix. Thanks for the help.

  • @RanaaMohsen
    @RanaaMohsen 11 месяцев назад

    Traceback (most recent call last):
    File "/Users/mac/PycharmProjects/gamee/r2.py", line 19, in
    blockk = LBlock()
    ^^^^^^^^
    File "/Users/mac/PycharmProjects/gamee/blocks.py", line 7, in __init__
    super().__init__(1)
    TypeError: Block.__init__() takes 1 positional argument but 2 were given
    I keep getting this error and i dont know how to fix it even though i wrote every single line as the video

  • @Nothern-p2e
    @Nothern-p2e 3 месяца назад

    It says sys is not defined

  • @GamingGOATS-ly4kf
    @GamingGOATS-ly4kf Год назад

    when I try to import grid from Grid it gives me import error

  • @DaCom3AK
    @DaCom3AK 11 месяцев назад +21

    watch with 1.25x speed, thank me later

  • @wasgehtdaab
    @wasgehtdaab 9 месяцев назад

    😎

  • @Rahasmayaikahaniyan
    @Rahasmayaikahaniyan Год назад

    Please share the Source code of this game

  • @amrnagy248
    @amrnagy248 11 месяцев назад

    he tell me the attribute print_grid isnt exist

  • @kapykta842
    @kapykta842 8 месяцев назад

    very hard tutorial

  • @iamgoodatme
    @iamgoodatme 3 месяца назад

    Thank you @DaCom3AK

  • @andrewanimatesstuff
    @andrewanimatesstuff 10 месяцев назад

    by the time i got to 49:37 i got an error at blocks.py
    Block.__init__() got an unexpected keyword argument 'id'
    heres the code:
    from block import Block
    from position import Position
    class LBlock(Block):
    def __init__(self):
    super().__init__(id = 1)
    self.cells = {
    0: [Position(0, 2), Position(1, 0), Position(1, 1), Position(1, 2)],
    1: [Position(0, 1), Position(1, 1), Position(2, 1), Position(2, 2)],
    2: [Position(1, 0), Position(1, 1), Position(1, 2), Position(2, 0)],
    3: [Position(0, 0), Position(0, 1), Position(1, 1), Position(2, 1)]
    }
    can anyone help?

    • @gasoline27
      @gasoline27 7 месяцев назад

      Have you solved the problem yet?

  • @Rafael-oq9vu
    @Rafael-oq9vu Год назад +1

    here i am learning python after making a tetris game in c

    • @b.c.34
      @b.c.34 Год назад

      Nice.....i would like to know how you handled the rotation of the shapes....i want to design same using C++....
      Thanks in advance