PyGame Beginner Tutorial in Python - Loading Spritesheets

Sdílet
Vložit
  • čas přidán 6. 09. 2024
  • In this video I will explain how to extract individual images from a spritesheet in PyGame. I am going to explain how to load a spritesheet, create a function to extract an image from it and finally create a class that can be imported into project
    Code and assets on github:
    github.com/rus...
    Credits for assets used:
    arks.itch.io/d...
    Check out my other PyGame tutorials:
    Space Invaders coding tutorial: • Pygame Space Invaders ...
    Flappy Bird coding tutorial: • PyGame Flappy Bird Beg...

Komentáře • 121

  • @CanvasKreps-hp7uz
    @CanvasKreps-hp7uz Před rokem +41

    Mate ur pygame videos are absolute quality, u explain it so much clearer and better than my university. Big love

  • @sylvanfranklin6904
    @sylvanfranklin6904 Před 3 lety +77

    It saddens me that channels like this (that give actually good content and get straight to the point) don't get the attention they deserve. My advice is to make really appealing thumbnails

  • @wizardly
    @wizardly Před rokem +27

    Tip. Instead of using a defined color key, set the background to be transparent.
    image = pygame.Surface((width,height), pygame.SRCALPHA).convert_alpha()

    • @deanbond007
      @deanbond007 Před rokem +2

      Thank you this helps a lot

    • @SuperJSM
      @SuperJSM Před rokem +2

      Thanks for this! It's comments like these that help cover optimisation areas of coding that are sometimes forgotten by the tutorial creator. (No offence Russ, it was a great tutorial as it was!)

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

      Thanks brother ❤

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

      THANK YOU MEN!!!

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

      OMG THANK YOU. SPEND WHOLE DAY HOW TO REMOVE BLACK BORDER!!!

  • @timsim83
    @timsim83 Před rokem +5

    I used to sketch characters like Commander Keen onto a post-it note with a grid drawn on it and then redraw them, pixel by pixel, into the Urban Renewal Kit for Sim City when I was a kid. This stuff makes sense-THANK YOU!!!

  • @elianelebars5288
    @elianelebars5288 Před 2 lety +16

    Just the video I needed! Thanks a lot for your very clear and easy to follow explanations.

  • @jamesking4350
    @jamesking4350 Před rokem +4

    Used this to fix issues with images that had black or outlines.
    def get_image(sheet, frame, width, height, scale):
    image = pygame.Surface((width,height), pygame.SRCALPHA).convert_alpha()
    image.blit(sheet, (0,0), ((frame * width), 0, width, height))
    image = pygame.transform.scale(image, (width * scale, height * scale))

    return image

  • @user-ft3cv1ew4m
    @user-ft3cv1ew4m Před 5 měsíci +3

    wow so much easier than the other tutorials I've watched five stars ⭐⭐⭐⭐⭐
    you are so calm and just straight in your vids Thank you

  • @star_t1
    @star_t1 Před 3 lety +10

    As usual good video. And thanks for shooter and platformer tutorial. Now I'm making my own game thanks 😁

    • @CodingWithRuss
      @CodingWithRuss  Před 3 lety +1

      Thanks! Look forward to seeing your game :)

    • @star_t1
      @star_t1 Před 3 lety +2

      I'm commenting here again. While I was making game I got this error
      File: D:\(than it says file location). Line 108 in player_group.draw (screen)
      File: C:\ (it says location again) line 546, in draw surface.blits((spr.image, spr.rect) for spr in sprites)
      TypeError: Source objects must be a surface

    • @star_t1
      @star_t1 Před 3 lety +2

      I don't know what kind of error it is. I tried to fix it but than it says TypeError: 'Group' can't use blit (I didn't even used .blit function)

    • @CodingWithRuss
      @CodingWithRuss  Před 3 lety +2

      @@star_t1 Hard to say what the error is, but it works fine here so must be a typo. Check the code I've linked in the description and it should help find the error

  • @eugeneee4009
    @eugeneee4009 Před 2 lety +6

    Wow, I learnt a lot today. This channel is truly a gem!

  • @EliJohnson-f4v
    @EliJohnson-f4v Před 6 dny +1

    Thanks, Russ! Big help!

  • @tasinrahman2931
    @tasinrahman2931 Před rokem +2

    Love your videos. Especially the way you run through procedural code before getting into classes it really helped me understand the absolute basics

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

    Excellent tutorial. Well explained, easy to follow. I am off to the next one; animation

  • @soupnoodles
    @soupnoodles Před 3 lety +5

    Thank you so much, this makes my game so much faster

  • @HoRRoRlets
    @HoRRoRlets Před 3 lety +6

    Dude....
    This will be a great video. Went through this just a week ago on my own...Looking forward to working through it with you

  • @stephenkentperez7705
    @stephenkentperez7705 Před 4 měsíci

    sprite_sheet = spritesheet.SpriteSheet(sprite_sheet_image)
    making code readable is such a glorious endeavor.

  • @incogneeto982
    @incogneeto982 Před 3 lety +3

    Question because I am just learning. Is there a difference between making your while loop with
    Versus
    Is there a performance difference or is it just personal preference? Also, great videos. They are helping me a lot, thank you!

    • @CodingWithRuss
      @CodingWithRuss  Před 3 lety +1

      Thanks! It's just personal preference, it's how I saw it done when I was learning pygame so I stuck with it but either option would work.

    • @CubixCreations
      @CubixCreations Před 2 lety +4

      The first option, saving the run condition to a variable, is prob better, as it lets you to control when you want your game to stop during debugging etc.

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

    You could used get_pixel from the PIL module to sample the color of the top left most pixel. That way you don't need to tell it what the color to make transparent.
    from PIL import Image
    with Image.open('doux.png' **or just image location**) as image
    bad_color = image.get_pixel((0, 0))
    Yes the position needs to be in a tuple so you need the extra parentheses.
    Also Thank god for pygame.transform.scale_by
    just give it a surface and factor and away you go.

  • @_dotZero_
    @_dotZero_ Před 2 lety +3

    Great video! I just picked up pygame to use as part of a school project and am attempting to make a basic game. I'll definitely be passing this link along to others who are interested. I have one question though that's more of a side topic. I am using TIled to create my maps and export as an image and as a csv to track which sprites to draw. So far my tile_ids have line up nicely but I'm hoping someone here may know how to pull tile_ids from a tileset in TIled?

  • @navkd1854
    @navkd1854 Před rokem +2

    hi, I've pretty much done the code identical, however when I run it at the end it says on line 13 there is a type error: SpriteSheet.__init__() missing 2 required positional arguments: 'columns' and 'rows'.
    I can't identify the issue, please help

    • @CodingWithRuss
      @CodingWithRuss  Před rokem

      It could be that when you are creating the sprite sheet object from your main file, you aren't passing arguments into the brackets. So it is looking for those 2 arguments but it can't find them

    • @navkd1854
      @navkd1854 Před rokem

      @@CodingWithRuss i forgot to say thank you, i fixed that problem. is there anyway you could help me with my collision detection code? i have defined both the sprite_rect and the platform_rect and made the collision detection code but the sprite is not able to jump onto the platforms and goes straight through them. please can you help me as it is my NEA which is due in a couple of days? if i could email you my code or comment it, whatever is best.

  • @armanmaratuly1349
    @armanmaratuly1349 Před 4 měsíci

    Thank you! Very good, going to the next video to animate it

  • @taunhawk9888
    @taunhawk9888 Před rokem +1

    Great video! Consolidates what I learned from your Udemy class for building an RPG with pygame.

  • @art_made_here
    @art_made_here Před 4 měsíci +1

    Does anybody know why I get an error saying video system not in initialized

  • @vrajsavani
    @vrajsavani Před 2 lety

    Brilliant Tutorial. Onto The next part!

  • @smokespekter
    @smokespekter Před rokem +1

    this is so helpful, thank you so much!

  • @davivieira3619
    @davivieira3619 Před 3 lety +2

    Your video is awesome!

  • @RolandGustafsson
    @RolandGustafsson Před 2 lety

    you could include the width, height and background color key to the SpriteSheet properties, perhaps even scale

  • @evagok3698
    @evagok3698 Před rokem

    You have forgotten to say like the video and comment :)))
    You are best Jehuuehuue!!

  • @LawrenceAaronLuther
    @LawrenceAaronLuther Před 4 měsíci

    FYI he does explain this via OOP at 19:06

  • @oliverbosworth1045
    @oliverbosworth1045 Před 2 lety +1

    amazing tutorial!

  • @doggoluvr133
    @doggoluvr133 Před rokem

    followed this but because when you take the file in it makes everything black to transparent any black textures become see through, no explanation of how to change this or what really combines to cause this. and its very common to have black outlines which will be broken...

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

    How do I put files in the directory so I can code in the sprite in?

  • @Hunter-xz4qu
    @Hunter-xz4qu Před 8 měsíci

    My Sprites contain black colour and it just breaks the sprite. what am i supposed to do?

  • @guido1362
    @guido1362 Před rokem +1

    Great tutorial, but turning the black background transparent also turns all my art that uses the colour black transparent. Do you know how I can fix this?

    • @CodingWithRuss
      @CodingWithRuss  Před rokem

      If you have images with a transparent background, use .convert_alpha() at the end of the image.load() function. This will load the image in and maintain transparency

  • @dhpshow9277
    @dhpshow9277 Před 2 lety +1

    how do you do it if your sprite has black in it?

    • @LawrenceAaronLuther
      @LawrenceAaronLuther Před 4 měsíci

      just add image.fill((255, 0, 255) or whatever you prefer after "Image = pygame.Surface(). Then choose that same color for the image.set_colorkey()

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

    I want to use VS Code and this does not really help in how to get a sprite inside the pygame code.

  • @jamesw8699
    @jamesw8699 Před 2 lety

    Thank you very much. Im still having some issues with the sheet i downloaded, which is “indented” by a few picels. This got me somewhere though!

  • @DaSummit
    @DaSummit Před 2 lety +1

    Hello, Great tutorial. I am having one issue though, after making the spritesheet a standalone class I'm getting the error "SpriteSheet() takes no arguments" coming from the line that creates an object of the class 'sprite_sheet = spritesheet.SpriteSheet(sprite_sheet_image)'. Does anyone know why this might happen?

    • @toandiep47
      @toandiep47 Před 2 lety

      I had the same problem too. You want to go to spritesheet.py and on Line 4. def __init__(self. image):
      There's two underscores, I missed 1 on each side and that is making the SpriteSheet() takes no argument.

    • @smokespekter
      @smokespekter Před rokem

      i'm having the same problem lol

  • @9O94WIND
    @9O94WIND Před 2 lety +1

    thanks for the tutorial, now im thinking of making a game, but there is an error that keeps occuring in my code, it says that a module "pygame" does not exist what should i do :(

    • @CodingWithRuss
      @CodingWithRuss  Před 2 lety

      Did you figure it out already? You need to install pygame first, check out pygame.org

  • @h3l_l
    @h3l_l Před 17 dny +1

    i like how you say zero

    • @CodingWithRuss
      @CodingWithRuss  Před 17 dny

      I never noticed 😅

    • @h3l_l
      @h3l_l Před 17 dny

      @@CodingWithRuss I didn't mean to offend you I swear

  • @xxhoodbunnyxx
    @xxhoodbunnyxx Před 8 měsíci

    What can I use as an equivalent to paint on Macbook? Helpful video but I am a (new) Mac user.

  • @congsangnguyen3149
    @congsangnguyen3149 Před 3 lety +1

    thank you!

  • @ParleysWoodedJourney
    @ParleysWoodedJourney Před 2 lety

    Could you use a loop to load all the images from the sprite sheet instead of doing it one at a time? and load them into a list?

  • @vxxvp2042
    @vxxvp2042 Před 3 lety +1

    Great !!

  • @alperklc7782
    @alperklc7782 Před 3 měsíci

    thanks

  • @somebody31415
    @somebody31415 Před rokem

    Is it better to make a sprite sheet out of my animations rather than just writing an if loop and loading them into a list of lists? What does it accomplish, because it doesn’t sound any easier? Is it more efficient? Does it save memory?

    • @CodingWithRuss
      @CodingWithRuss  Před rokem

      If your images are already separated into individual frames then I would just stick with that and make a loop to load them into a list of lists. Sometimes when I download 3rd party assets though, they come as complete spritesheets so for those situations this method helps to automatically load the images in.

  • @andersbloch9847
    @andersbloch9847 Před rokem

    can you use an image which is transparrent beforehand so the sprites don't have the black background?

    • @CodingWithRuss
      @CodingWithRuss  Před rokem

      Yes but you have to add .convert_alpha() to the end of the "pygame.image.load" line so that the transparency is not lost

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

    What app you use for your sprite sheet?

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

      I think I used GIMP to arrange it, but I didn't draw the sprites themselves, they are 3rd party

  • @fastboialex9525
    @fastboialex9525 Před 2 lety

    Hey loved your vid and i wonder what software you used ;)

  • @Dachowda
    @Dachowda Před rokem

    What app are you using with the Dino images displayed?

    • @CodingWithRuss
      @CodingWithRuss  Před rokem

      I use sublime text for coding and I use GIMP for image editing

    • @Dachowda
      @Dachowda Před rokem

      @@CodingWithRuss Thank you! I drew my images using procreate, how would I make a spritesheet from them, would I have to import them to gimp or Microsoft paint?

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

    pls can some one help with this error
    dino_scrpit = image2.Image(dino)
    ^^^^^^^^^^^^^^^^^^
    TypeError: Image.__init__() takes 1 positional argument but 2 were given
    the image2 if the other python file with Image as it class

  • @krishdoesstuff8541
    @krishdoesstuff8541 Před 4 měsíci

    it keeps saying "no module named pygame" for me.

    • @CodingWithRuss
      @CodingWithRuss  Před 4 měsíci

      I have a short video to explain how to fix that error: czcams.com/video/0x_MEKr0OJQ/video.html

    • @krishdoesstuff8541
      @krishdoesstuff8541 Před 4 měsíci

      @@CodingWithRuss THANK YOU SO MUCH!!!

  • @user-le2lk1sg2v
    @user-le2lk1sg2v Před rokem

    total noob here, using the color key function is also removing the black outline/details on the asset i am using , can you suggest some fix. thanks in advance

    • @user-le2lk1sg2v
      @user-le2lk1sg2v Před rokem

      solved , thanks a lot , great video
      image = pygame.Surface((width,height), pygame.SRCALPHA).convert_alpha()

    • @CodingWithRuss
      @CodingWithRuss  Před rokem

      Welcome!

  • @liamcleetus6929
    @liamcleetus6929 Před 3 lety +1

    first, plz pin so i can show my brother

  • @quanvuinh20
    @quanvuinh20 Před 3 lety

    I made the video active image about spritesheet is not

  • @ToujoursIris
    @ToujoursIris Před 3 lety

    So nice! I hope you make a bomberman game in PyGame...I tried to do that but I could not load in sound...after some days, I realized that I wasn't using PyCharm, instead, I was using Anaconda...(Note: Anaconda Spyder doesn't support Mixer Module...)

    • @ToujoursIris
      @ToujoursIris Před 3 lety

      Still working on it tho...i just needed some tips and idea from u...i hope u reply

    • @ToujoursIris
      @ToujoursIris Před 3 lety

      Also bro, CONGRATS FOR UR 3000+ Subs!

    • @ToujoursIris
      @ToujoursIris Před 3 lety

      Im ur fan!

    • @CodingWithRuss
      @CodingWithRuss  Před 3 lety

      Thanks! I didn't know Spyder doesn't let you use mixer, good to know. Bomberman would be pretty cool!

    • @ToujoursIris
      @ToujoursIris Před 3 lety +1

      @@CodingWithRuss Sure, welcome...you always learn everything from everyone someday...

  • @legendrags
    @legendrags Před 2 lety

    how did u run the python file in sublime without a terminal!!

  • @small_weiner6486
    @small_weiner6486 Před 8 měsíci

    holy shit your monitor is huge

  • @MistereXMachina
    @MistereXMachina Před rokem

    What IDE are you using?

  • @basicallybrand
    @basicallybrand Před 2 lety

    what if you have a lot of rows?

    • @CodingWithRuss
      @CodingWithRuss  Před 2 lety +1

      It is a bit more tricky then but you could try adding a "for" loop to iterate over the rows and see how that works.

    • @_dotZero_
      @_dotZero_ Před 2 lety

      Try something like this... change the signature def get_sprite(self, row, col, width, height, scale, color): .... and then the blit would look like this: img.blit(self.sheet, (0, 0), ((col * width), (row * height), width, height))

    • @Anth.
      @Anth. Před rokem

      @@_dotZero_ I know this is really late but, I figured I would put this incase others read this. This does not seem to work,
      gives back Invalid Rect Style Argument. I tried multiple iterations of this. So, idk. If someone else knows how to get this to work, please share here. Thanks in advance if any does.

  • @xxxslayerkillerweedlitxxx3878

    More like GUS BUS

  • @earl.youtube
    @earl.youtube Před měsícem

    Windows 10 🥵🥵🥵

  • @ashwins4776
    @ashwins4776 Před 3 lety

    What editor you use for creating sprites..?