Python Platformer Tutorial | Animations, Sprites, Collisions, Jumping, Gravity...

Sdílet
Vložit
  • čas přidán 8. 07. 2024
  • Today, I'll be sharing how to build a platformer game in Python. This game will have pixel-perfect collision, animated characters, and much much more!
    Thanks to Dice for sponsoring today’s video! When it comes to finding a job in tech, make things easier on yourself, and #LevelUpWithDice to help you find the right match! Check out the links below to start your journey with Dice.com.
    Dice, where tech connects: bit.ly/3V6tR9Y
    Career Events: bit.ly/3EiALlL
    Ultimate Guide to a Successful Career in Tech: bit.ly/3tE4osV
    Find Jobs: bit.ly/3V5qEHK
    Assets and Completed Code: github.com/techwithtim/Python...
    Pygame in 90 Minutes: • Pygame in 90 Minutes -...
    Space Invaders in Pygame: • Pygame Tutorial - Crea...
    How To Install Pygame (Mac/Linux): • How to Install Pygame ...
    How To Install Pygame (Windows): • How to Install Pygame ...
    💻 ProgrammingExpert is the best platform to learn how to code and become a software engineer as fast as possible! Check it out here: programmingexpert.io/tim and use code "tim" for a discount!
    ⭐️ Timestamps ⭐️
    00:00:00 | Project Demo
    00:02:41 | Project Brief/Getting Started
    00:04:16 | Project Setup
    00:05:27 | Asset Walkthrough
    00:06:47 | Game Setup
    00:12:06 | Generating The Background
    00:19:24 | Creating The Player
    00:28:48 | Moving The Player
    00:32:29 | Adding Gravity
    00:36:28 | Loading/Splitting Sprite Sheets
    00:49:59 | Animating The Player
    00:57:23 | Adding Terrain/Blocks
    01:10:00 | Pixel Perfect Collision (Vertical)
    01:15:57 | Jumping & Double Jumping
    01:23:37 | Scrolling Background
    01:29:02 | Pixel Perfect Collision (Horizontal)
    01:37:25 | Adding Traps (Fire)
    01:46:18 | Player Collision With Traps
    01:52:00 | Conclusion
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    👕 Merchandise: teespring.com/stores/tech-wit...
    📸 Instagram: / tech_with_tim
    📱 Twitter: / techwithtimm
    ⭐ Discord: / discord
    📝 LinkedIn: / tim-ruscica-82631b179
    🌎 Website: techwithtim.net
    📂 GitHub: github.com/techwithtim
    🔊 Podcast: anchor.fm/tech-with-tim
    🎬 My CZcams Gear: www.techwithtim.net/gear/
    💵 One-Time Donations: www.paypal.com/donate?hosted_...
    💰 Patreon: / techwithtim
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    ⭐️ Tags ⭐️
    - Tech With Tim
    - How To Make a Pygame
    - Pygame Programming
    ⭐️ Hashtags ⭐️
    #techwithtim #developer #python #pygame

Komentáře • 179

  • @TechWithTim
    @TechWithTim  Před rokem +42

    What are your favourite types of games to make in Python??

    • @specialx9856
      @specialx9856 Před rokem +11

      TopDown RPG

    • @tried5736
      @tried5736 Před rokem

      Thanks I really wanted to make a platformer game

    • @timon1816
      @timon1816 Před rokem +3

      sandbox and simulation

    • @madhupappala2577
      @madhupappala2577 Před rokem +2

      Rpg top down

    • @muhammadmz8791
      @muhammadmz8791 Před rokem

      first thank you a lot because of your tutorials then personally I came toward pygame after python tutorials, to learn how to program and more about python, then I will thank you a lot if you use all advance python topics in your pygame tutorials like decorators or multi-threading and processing and ... 🙏

  • @thoserandomshredders
    @thoserandomshredders Před rokem +8

    As a beginner python coder: your tutorials help me so much. Keep making them!

  • @ArunManivannan
    @ArunManivannan Před rokem +1

    Wow ! I managed to code along until the end. Fantastic content compressed into less than 2 hours ! Have taken away some points for further research. As always, thanks a lot for your time, Tim !

  • @rachinter
    @rachinter Před rokem +44

    This guy is pouring so much time into his videos to make it the best. Its absurd that these videos are not getting enough views. We should appreciate people like this. ❤️💯👍

  • @chesslife5640
    @chesslife5640 Před rokem +6

    Thank you for the tutorial Tim ! Its incredible how you manage to simplify problems so it is easy to follow and understand

  • @JeraWolfe
    @JeraWolfe Před rokem

    27 Minutes in, you just explained how you need to do all this stuff upfront, before you can explain it, but the brilliant part of that thoughtful consideration of yours is that, I'm completely tracking, following along, and I have a pretty good idea of why you're doing it this way. When I tried an earlier simpler tutorial, I used 'gravity' and 'shift' and an update loop, but didn't yet figure out I needed to determine direction, and played around with animation states equivalent to your animation frames.
    Figuring it out on my own for the first time.
    So validating to see I was on the right path, and you just cleared all the confusing noise and brush from that path and now I'm speeding along.
    Thank you, Tim.
    Amazing, simply Amazing, tutorial.

  • @MrTactics26
    @MrTactics26 Před rokem +6

    I went back to school to learn Python. Loving it so far! Thanks for the hard work. Subbed.

  • @cjn9ne
    @cjn9ne Před rokem +3

    This simply deserves way more views

  • @Funky_Mode
    @Funky_Mode Před rokem +1

    I will work really hard to expand on your amazing game using the assets you provided!

  • @shivenlak
    @shivenlak Před rokem

    These videos are so underrated they help me so much and are so useful, please keep making them and never stop.

  •  Před rokem +4

    Incredible tutorial! Thanks.

  • @emmang2010
    @emmang2010 Před rokem +4

    Amazing video as always!

  • @samanthaecotothermia7896

    Thanks so much for making these tutorials! I’m definitely one of those “monkey see monkey do” learners and these are incredibly helpful for me

  • @Ismail_Dagli
    @Ismail_Dagli Před rokem +41

    Comment for the algorithm!

  • @delltoid5980
    @delltoid5980 Před rokem

    I’m really excited to try this!!!

  • @CarloObrad
    @CarloObrad Před rokem

    another amazing tutorial keep it up Tim!!!

  • @Dice
    @Dice Před rokem +1

    Great video, Tim!

  • @Dave-DIY
    @Dave-DIY Před rokem +1

    For the algorithm and for my favorite coder on yt! Danke.

  • @Gay338
    @Gay338 Před rokem

    Dude, you're insane. Thank you

  • @graysenbowman9376
    @graysenbowman9376 Před 6 měsíci

    I appreciate your videos Tim! Thanks!

  • @zGuii99z
    @zGuii99z Před rokem

    You're awesome, thanks for your hard work!

  • @MDonteMoore
    @MDonteMoore Před rokem +5

    Thank you for another masterpiece.

  • @Vanukass
    @Vanukass Před rokem +2

    Great tutorial!
    As a sort of beginner, this was still easy enough to follow. Though some things are still a bit lost on me, like how they actually work. Was thinking of going through most of the stuff in the code and trying to add comments describing what is going on to get a bettter grasp.
    Edit. my engrish sucks more than I thought

  • @TheTeleportingProgrammer

    You are my inspo!

  • @bloxanim8444
    @bloxanim8444 Před rokem +1

    I love this man I have watched all his python videos and his just amazing

  • @seanyB0123
    @seanyB0123 Před 5 měsíci

    I'm 01:40 into this video, and I am freaking excited about this!!!! I have a really cool idea for a platformer and I'm hoping this will help me realise it :]

  • @Confuciuss
    @Confuciuss Před rokem

    will follow this tomorrow

  • @mathew1855
    @mathew1855 Před rokem +1

    Love the video man. I had so many questions I would looking for the answer to online and you helped me figure it all out. Great work, you're helping so many people.

    • @uno3688
      @uno3688 Před rokem +1

      help why does mine get error on the draw(window, background, bg_image) part??

    • @danyalahmed3995
      @danyalahmed3995 Před rokem

      @@uno3688 I also get an error when I try to add player, it says AttribueError:'Player' object has no Attribute 'draw' and I followed the video 100% up to that point

    • @uno3688
      @uno3688 Před rokem

      @@danyalahmed3995 ty, another problem is that I don't use folders like his bcs I just downloaded one bg

    • @ankithg3101
      @ankithg3101 Před rokem

      @@uno3688 you have to add ,player (after bg_image)

  • @daveruble5150
    @daveruble5150 Před rokem

    I did it!
    typed the whole thing.
    Pretty neat.

  • @smanzoli
    @smanzoli Před rokem

    Thanks a lot!
    To continue, the best way to create (design) the levels for this exact game would be a csv file with the map, where each character translates to a sprite/tile... Because we need an easy way to place the blocks.
    Let's say a level has 200 x 10 blocks... so we need a csv file with 100 columns and 10 rows... import it and relate it to the tiles.

  • @cotekokotrees1527
    @cotekokotrees1527 Před 11 měsíci

    Thanks I have learned a lot of pygame watching and copying your video now I can create my own game

  • @linsieharris7041
    @linsieharris7041 Před 10 měsíci

    Glad I found this.

  • @aunrizvi8063
    @aunrizvi8063 Před rokem

    You are my favorite coding youtuber

  • @paulthomas1052
    @paulthomas1052 Před rokem

    Great intro and tutorial. Thanks so much :)

  • @TheBl4ckPe4rl
    @TheBl4ckPe4rl Před měsícem

    Thank you friend

  • @Ashir1
    @Ashir1 Před rokem +1

    Hi tim i am from Pakistan and i follow you since last 1 year and your tutorial is good and i learn from many thing in python

  • @anuperera174
    @anuperera174 Před 3 měsíci +1

    Thank u sm!

  • @Ivan-hj8li
    @Ivan-hj8li Před rokem

    Insane brother

  • @tcgvsocg1458
    @tcgvsocg1458 Před rokem

    wow huge work

  • @saadedi75
    @saadedi75 Před rokem

    thanks for all video

  • @boolean0_046
    @boolean0_046 Před rokem

    GREAT JOB!!!!!!!!!!!!!!!!!

  • @3lite6am3r
    @3lite6am3r Před rokem

    My man, you are a real one that saved my mind from disintegrating ❤

  • @samplaying4keeps
    @samplaying4keeps Před rokem +5

    Hi Tim! Thanks so much for this I'm excited to get started on it. I was trying to download all the assets from the Github first and for the enemies there's a PNG that says "link on the project page" but I can't see to find that.

  • @punuravathsara
    @punuravathsara Před rokem +1

    Very Nice Video 👍👍

  • @stefano_er
    @stefano_er Před rokem

    thanks!

  • @eradboi
    @eradboi Před rokem

    Good job

  • @kenjik8736
    @kenjik8736 Před rokem +1

    Hi I have a question, while moving the character and it staying in idle it bounces up and down. Is there a way to fix it?

  • @nikratlehead
    @nikratlehead Před rokem

    Top ! Thanks

  • @myJapanTV013
    @myJapanTV013 Před rokem

    Thanks a big fan!
    ***Humble request, please put tutorial for pytest as well 🙏

  • @pietraderdetective8953
    @pietraderdetective8953 Před rokem +6

    Great work Tim!
    Any plan on doing some game project using Godot?
    Its syntax is really similar to Python..but the game engine is much more powerful than Pygame.

  • @dmkanz
    @dmkanz Před rokem +1

    Great Video - quick question - where do you go to get the pngs for all the assests or how can i create my own?

  • @Gilmore_Coding
    @Gilmore_Coding Před 10 měsíci

    @TechWith Tim First off your amazing and have helped me learn a lot with python , but quick question in your def handle_vertical_collision you make a list with collided_objects but you never go back and reference the list would you be able to explain why, and what is the purpose off appending to the list ?

  • @katiedevinney4872
    @katiedevinney4872 Před rokem

    Thanks for the video! I've gotten through everything except for adding the fire. When I run the code for the Fire class, it says there is a key error for 'off' based from the line __init__ self.image = self.fire["off"][0]. Any ideas how to fix it?

  • @user-jx1ke4ek1g
    @user-jx1ke4ek1g Před 2 měsíci

    Excellent tutorial, could you add enemies in a next video?

  • @SkyFly19853
    @SkyFly19853 Před rokem

    Real Nice...

  • @musicfan4952
    @musicfan4952 Před rokem +3

    Okay so i did everything but for each block i make i have to copy and paste the line of code he showed so i was wondering if there's any less of a tedious way to make blocks.

  • @nikolaysavinov1891
    @nikolaysavinov1891 Před rokem +2

    There is a very tricky bug in your code - running animation consists of jump phase and landing phase, and in jumping phase sprite collision detects that he is not touching block and start falling - increase Y position. Then in landing phase you get a collision just by change to next sprite, and this treated by X movement as a collision, and code doesn't change X position. This is easy to see, if you add delay when get X collision, and run left and right.

    • @blackpanda3405
      @blackpanda3405 Před 7 měsíci +1

      yea if you spam jump button you will stay in air and even go up if you are fast and how do you fix it ?

    • @nikolaysavinov1891
      @nikolaysavinov1891 Před 7 měsíci

      @@blackpanda3405 I've uploaded my version of this platformer on GH, user Nick507

  • @tomcotom8
    @tomcotom8 Před rokem +1

    When I make my own sprite PNGs with Photoshop I keep getting error 'pygame.error: Unsupported image format' It works just with yours. How did you make them? What's the catch? Thanks

  • @henryplayz485
    @henryplayz485 Před rokem

    Can you make a video on how to do more levels and how to add stuff in to the platformer

  • @arnoldnyasetia4798
    @arnoldnyasetia4798 Před rokem

    Big fan

  • @ondrasuchanek6467
    @ondrasuchanek6467 Před rokem

    you have constant size of the window, but what if I need to set it differently for each level of the game? since "window" is parameter of the main function, I don't see any way how to change it inside main game loop :(

  • @eurosat7
    @eurosat7 Před rokem

    This is amazing for learning. Thank you for your time! :)
    I have a question: Is this the way code and classes are organized in python in general?
    I have 40k+ hours experience in php and am a technical lead. And for my taste this could use some cleaning.

  • @sofixfan_limlight
    @sofixfan_limlight Před rokem

    i love python!!!!!!!

  • @-Barny
    @-Barny Před 6 měsíci

    Thank you very much for that video. Can I use that code and assets in my commercial project? Thank you

  • @Fahim_Uchiha416
    @Fahim_Uchiha416 Před rokem +1

    Im currently getting a problem of type error module object is not callable. When i was creating the player i did everything same as 28:32

  • @lolducks
    @lolducks Před rokem

    Hello, i was wondering is there a way to change how the player character looks?

  • @sofixfan_limlight
    @sofixfan_limlight Před rokem

    your channel is pretty!

  • @georgeenticknap4216
    @georgeenticknap4216 Před rokem

    great video and easy to follow!! I can not seem to find the link for all 20 enemies that are mentioned on an image in the asset folder?
    do you have the link for this?

  • @atarimae7718
    @atarimae7718 Před rokem

    how can i make the background move with parallax effect? im new to programming and dont know much, Thankk you!

  • @chikwendumiracle2737
    @chikwendumiracle2737 Před rokem

    Nice

  • @SkyFly19853
    @SkyFly19853 Před rokem

    Could you make a tutorial on how to use Tilemap Editor with Python?

  • @fanboieditz
    @fanboieditz Před 11 měsíci

    How do you create multiple flames in your platformer, if I try to make a list just like blocks it gives me an error

  • @nancytran7336
    @nancytran7336 Před rokem

    Hi! i need help. In the chapter loading & splitting sprite sheets i got the same Error: "self.sprite = self.SPRITES['idle_' + self.direction][0]
    KeyError: 'idle_left'". My def of moving in both direction are ok. have anyone an idea? i am a newbie and google don't help :D

  • @inko567
    @inko567 Před rokem

    I'm happy

  • @God_Bananna
    @God_Bananna Před rokem

    How exactly do u change the sprites to our liking?

  • @atracom71slavyansk4
    @atracom71slavyansk4 Před rokem +1

    Hello. I like your video. I think there is a bug in this program. When player is hitted doesn't work horizontal collide.

  • @shenzmao
    @shenzmao Před rokem

    Cool

  • @trivu5022
    @trivu5022 Před rokem +1

    i have a problem in line player=Player(100,100,50,50).
    it says that "pygame.sprite.Sprite.add() argument after * must be an iterable, not int" how to fix it

    • @mostlyLK
      @mostlyLK Před rokem

      same! did u figure it out?

  • @daveruble5150
    @daveruble5150 Před rokem +1

    I'm stumped. Tim's backgrounds are working perfectly and my background remains black. when I click the ❌ to quit, my chosen background is revealed briefly as the window collapses. Its loading the correct image but isn't drawing until after I quit.
    EDIT! As I was writing out the 'problem' I got an inkling of the solution: I had the indentation fubar'd. tabbed the draw call right once and voila!
    Now I can continue!
    Tim moves around so well in the editor that it's critical for me to pay close attention as the indention isn't 2nd nature to me, yet.

  • @avgrobloxanimator
    @avgrobloxanimator Před rokem

    I am already getting errors at 31:57 which is stating the error:
    self.rect.x += dx
    TypeError: unsupported operand type(s) for +=: 'getset_descriptor' and 'int'

  • @Clendy321
    @Clendy321 Před rokem

    hello, I fixed the error at the end of loading sprite sheets but still it couldnt load i tried saving but it still didnt work

  • @Boogie-wi7hd
    @Boogie-wi7hd Před 5 měsíci +1

    Great vid bro! But I couldn't understand how the sprite creating works. I would love if anyone explained it to me in the comments section

  • @bini420
    @bini420 Před rokem

    cool

  • @ethanlee4976
    @ethanlee4976 Před 11 měsíci

    I love your videos. Can you make a pokemon game pls

  • @MateuszBanach-eb3zl
    @MateuszBanach-eb3zl Před rokem

    I'm just making my first pygame project and was wondering - can I create a separate class for a main loop? I saw someone's code written that way but then someone else advised me that it's not recommended.

  • @johnpayne9265
    @johnpayne9265 Před 7 dny

    I am getting unsupported image format error (using the provided png files) while creating the background (time 13:03) function (def get_background).

  • @MeLoveCH3DDER
    @MeLoveCH3DDER Před 9 měsíci

    I can't load background image because it said FileNotFoundError: No file 'assets\Background\Gray.png' found in working directory Pls help me on that. Thank you so much for your help

  • @jtiguy9375
    @jtiguy9375 Před rokem +1

    1:43:34 my program still tells me that the list index is out of range but my code is identical, anyone else have this problem?

    • @jtiguy9375
      @jtiguy9375 Před rokem

      Now i can not collide with the top of the fire after drawing it now i have solved the issue.

  • @arkie87
    @arkie87 Před 9 měsíci

    35:11 FYI that is not how gravity/acceleration works. You are over complicating it. Gravity does not increase the velocity by more the longer it is falling. The acceleration is constant, meaning the velocity increases by a constant value each time step.

  • @ndjndxs7285
    @ndjndxs7285 Před rokem

    Your Dice link in the description via bitly isn't working

  • @jotaframo
    @jotaframo Před rokem

    When scrolling the background my blocks move but the collisions don't detect it, any ideas?

  • @aliyousif8765
    @aliyousif8765 Před rokem

    thank you for the tutorials that you do. your A* star algorithm helped me a lot
    but one question, why you use pygame instead of raylib it has a python binding and as far as i heard its better

  • @tomlee6534
    @tomlee6534 Před rokem

    my pyame is showing import pygame could not be resolved?

  • @shauryananda9905
    @shauryananda9905 Před rokem

    [WinError 3] The system cannot find the path specified, this error is coming pls tell what to do

  • @Oybekjon_Nuriddinov
    @Oybekjon_Nuriddinov Před 9 měsíci

    Thank you for your interesting video. It is amazing. Can I translate it into my Uzbek language. It would be for those who don't know English. If you permit I will write who is the author of the video and from which channel it is taken. Can I do it?

  • @boxhead-zk7sn
    @boxhead-zk7sn Před 3 měsíci

    Pls if possible can you make tutorial or hint on how to make a level editor for this game🙏🙏🙏🙏🙏🙏🙏

  • @yasinkilicaslan1991
    @yasinkilicaslan1991 Před rokem

    I have python 3.11 installed and to pip pygame i need 3.10, is anything will happen if i download and use both versions?

  • @thebigc1462
    @thebigc1462 Před rokem

    1:09:30

  • @WhatsNextFam
    @WhatsNextFam Před 10 měsíci

    It says player_vel is not defined im getting a name error someone please help

  • @noxiusphant9360
    @noxiusphant9360 Před rokem

    Why doesn't my player flip but I don't get any errors in the code? tell me what's wrong please