Interested in what its like to be a computer science student? Check out my Podcast: anchor.fm/tech-with-tim Follow me on Twitter: twitter.com/TechWithTimm Join my Discord to Ask Questions and get help with pygame! discord.gg/pr2k55t SOURCE CODE: techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/pygame-animation/
Hey Tim, I really like your videos, and the podcasts as well. Please do more podcasts. It's been 4 months since your last one. Talk about more topics, and please give some tips on how us guys with knowledge of python, pygame, openCV, OOP, etc can make money online. And please do more podcasts and videos. Thanks, and keep up the great work !
EDIT: Answer was pygame.transform.scale() for those interested. Assuming adressed in later videos or can find out elsewhere but can sprites be made to scale with the window size or do you have to use other techniques for that?
@@kaldiaz9337 Did you put R1.png in the folder your python file is saved in? Because it has to be there. You can also put the full file path like "C:\Users\You\Pics\R1.png" [example] but you shouldn't do that on code you're giving to others as it wouldn't work for them unless they had the exact same folders.
I've been entertaining myself for the last 5 minutes over the fact that the player character has no jumping animation so he just rockets up into the atmosphere
I like how his voice is strong and that his mic isnt low because i find a lot of tutorials who have people who do that... But yeah this tutorial IS GOOOD
For people who have custom sprites I suggest this at the beginning of the file: from pygame import image as img Then instead of walk_right = [pygame.image.load("Custom sprite.png")] It's walk_right = [img.load("Custom sprite.png")]
Great Video Tim! For anyone who is lazy like me! Instead of loading the images the way Tim did it, here is an alternative walkRight = [pygame.image.load(f'R{n}.png' for n in range(1, 10)) and for left just change the variable name and the 'R' to a 'L'
This video series of tutorials is amazing, not to mention the free set of .png video game character graphics neatly loaded into one place with copy paste script to organize them all!! The troubles of trying to download free .png files off sketchy websites are no more! I am a first year CS major and learning python through your channel has made the process so much more fun. Seeing the animated character running instead of a basic moving shape was such a good feeling!
this is just fantastic, i was almost gonna give up python because i thought i can't even make simple animations on it but wow this can go far. amazing video
hey guys. if you are having trouble downloading the images from the link given in the description I suggest downloading the images from the inspector(right-click the download button and select inspect).
(11:43) just a little thing. I wanted it a bit smoother, so you guys in the comments can try it. Replace the clock.tick(27) with the double amount. --> clock.tick(54) and decrease the velocity, for example by 2. --> vel = 3.
These tutorials have been really good so far! The only problem I ran into was that I couldn’t load the images but it was just because the path to my python file was in my virtual environment folder for some reason
import os os.chdir("C:\\Users\\Toshiba\\Documents\\Programming\\Resources\\Game\\SPRITE1") Note: For those whose having trouble with loading images. Replace the (" "), with the location of your images. Make sure it has double slash.
if anyone is getting issue like "cant load the images" just do one thing --- close you notepad.make a new folder . paste your python program there and also paste the sprites/images directly there (not in a folder) and then run the program. thanks .....
Elegant solution for modifying walkCount: Instead of having an if statement to set walkCount = 0 you can do this in each if left: and if right: statements like: if left: win.blit(walkLeft[walkCount], (x, y)) walkCount = (walkCount + 1) % 9 elif right: win.blit(walkRight[walkCount], (x, y)) walkCount = (walkCount + 1) % 9 Because there are 9 elements in the array, using the modulo operator % (remainder of division) will make sure that walkCount always is in the range [0-8] which correctly accesses the sprites in the walking animation array. Preferably you'd define a small function to increase walkCount to avoid repeated code.
I had a problem for Deprecation Warnings. Right now I’m on Python 3.8.2 so that’s probably why I got these. If you are getting Deprecation warnings, use this code. import warnings warnings.filterwarnings(“ignore”, category=DeprecationWarning) Just in case anyone else gets the same problem (:
I want to add that starting in Python 3.4, we can use the Pathlib module to set up the filepaths we wish to use. I don't know if it's better or worse than os.path, but it's a possibility I wanted to share. So if your sprites are located, for example, in "C:\Users\User\Desktop\Game\Assets\Hero", and your script is in "C:\Users\User\Desktop\Game", all we need to do is: from Pathlib import Path #We define GAME_FOLDER so we can navigate through its files and folders whenever GAME_FOLDER = Path.cwd() #Now, when loading the sprites, instead of os.path.join(), simply navigate to the desired folder. #Because pygame.image.load() seems to accept strings and bytes, the filepath needs to be converted #to a string pygame.image.load(str(GAME_FOLDER/"assets"/"hero"/"R1.png"))
I had a problem getting the image files to load, but figured it out. I use the PyCharm interface to write the code, and had the shortcut file saved in the same location as the image. What I really needed to do was save the scratch file in that folder. After I moved the scratch file, and told PyCharm what to run it loaded fine.
If you can't open R1.png, take all the sprites and put it into another new folder. Then on your Python interpreter (for me it's Pycharm) type; Import os os.chdir(r' Copy and paste the link to the folder into here') So the paste would look like C:\etc\etc Then run it and hopefully it works. It did for me
Thanks! I've been trying to figure out what the problem was for about an hour or so and didn't even think to check the comments, then found this in under 30 seconds which solved the issue
I had the same issue. to resolve "Couldn't open R1.png" you have to enter the file location. This is from my mac walkRight = [pygame.image.load('/Users/BFaruch/Downloads/Game/R1.png')]
You have to put all the image files in the SAME file as your code file. You can't have the images in another folder inside of the folder that your code is in.
just for anyone having trouble with the images if you are storing the image in the same folder as the python program I created a variable for directory path using folder = os.path.dirname(__file__) and then pygame.image.load(os.path.join(folder,'bg.png')) also dont forget to import the os module I was having trouble with this as well so hope this helps
For all those with the error-Couldn't open R1.png, here's something to help you out First copy full path(i.e. URL of the image) Then..check the URL, generally it has single slashes(eg. pc\downloads) so..just double slash the complete URL (eg. pc\\downloads)....double slash COMPLETE URL(wherever slashes appear)
Thank you for a wonderful tutorial! I learned a lot by watching this tutorial series. I wondered if it would be OK if I used the sprites for my own project? If that is the case, who should I give the credit it to?
it's my second year of python and i'm trying to create a 2d top down racing game but i haven't been able to import the images of the cars for over 2 weeks now with no success. Thanks to your video my game now works perfectly. Thank you so much
when ever I try to move too far left or right it come up with Traceback (most recent call last): File "C:\Users\user\OneDrive\python\firstGame.py", line 85, in redrawGameWindow() File "C:\Users\user\OneDrive\python\firstGame.py", line 38, in redrawGameWindow win.blit (walkright[walkcount//3], (x,y)) IndexError: list index out of range
@@adriaan3428 Welp, all I had to do was replace 'walkCount += 1' by 'walkCount = (walkCount + 1) % 4' (because 4 sprites for each direction) and no more of this error.
You might want to create a character class where you store all the attributes and actions of the character. Then have the program just call methods of the character to cause it to move. The character "knows" how big it is, how to check the screen boundaries based on its size, how to jump etc.
That is because your game is not in the same file as your pictures if you add it there it will work. Or if you don't want you can add the place you put it before each file name for e.g. [pygame.image.load('/Users/Ranijith/Downloads/Game/R1.png')]
Traceback (most recent call last): File "C:\Users\user\Downloads\driwe.py", line 11, in car = pygame.image.load('Driwe/Driwe_car.png') pygame.error: Couldn't open Driwe/Driwe_car.png well how the hell could it open it?
@@marcusaurelius45 copy the image or music whatever you want to add to your project.then go to LOCAL DISC C >> USERS >> THEN SELECT THE ACC.YOU ARE USING i.e, YOUR COMPUTER NAME >> python projects >> select the name of your project and open that >> copy the file there which you want to add to your project... Hope you got the answer..
😩😩😩 please can you make me understand i am still waiting i dont understand i copied and pasted it on folder where my project is but not working 😩😩😩😩 please
char = pygame.image.load('standing.png') when I run the program it says: "invalid character in identifier" I copied it after yours and my script and images are in the same folder. Any ideas?
When copy/pasting over, it also copies in the newline character. Just delete a few off the end of the line until you remove the ) and retype it. Should fix the problem.
cant u just use a for loop and format strings to loop through 1 to 9 to load all the images? by appending them to a list? instead of hardcoding all of them? Like this: walkRight = [] walkLeft = [] for i in range(1, 10): walkRight.append(pygame.image.load(f'R{i}.png')) walkLeft.append(pygame.image.load(f'L{i}.png'))
Very useful tutorial and Thanks lot for explaining this is such simple and easy manner to understand. I can't thank you enough for this tutorial. I tried in another a set of images, and its works just fine.
If the images are on the same folder as the ".py" file it can be a copy-paste error, have to write it manualy (even if there is no difference between both)
Oh hey, I actually found out what was wrong in my line of code. So i had copied the lists from the top comment so that i didnt have to type it all and somehow the last ) in the background variable is wrong? so all i did was backspace and re-typed ) hope that helps you as well
Thanks for the Link your coding has helped me complete my summer goal on how to code on a program. Your tutorials are excellent, thank you so much. I can't wait to show my program and recommend your channel to my family.
I wasn't really interested in programming games, just looking for a way to become a better programmer, but low and behold - I've just become a games designer.This gives me some great ideas on developing my own games. Top lesson, thanx.
guys if your imgs are in another directory this is the code --> pygame.image.load(os.path.join("folder name (directory)","name of file.png" )) (need to import os module)
If you want import your character and dont want to type all time pygame.image.load, just set for example ld = pygame.image.load and do Walkright = [ld('R1.png')],its simpler.
Using pycharm I put the images in a folder called game and used for loops to load the images # Load player images player_walk_left = [] player_walk_right = [] # Left for img in range(1, 10): print(img) player_walk_left.append(pygame.image.load(os.path.join('Venv/Game', f'L{img}.png'))) # Right for img in range(1, 10): player_walk_right.append(pygame.image.load(os.path.join('Venv/Game', f'R{img}.png')))
Also you can use walkRight =[pygame.image.load('R'+str(i)+'.png') for i in range(1,10) ] walkLeft =[pygame.image.load('L'+str(i)+'.png') for i in range(1,10) ]
Please help I copied and pasted Jack Carter's link and it didnt work for some reason it says can't open R1 but then i opened R1 to see if it were corrupted and it wasn't
Instead of writing all of the sprite names out, you can simply do: from glob import glob walkLeft = sorted([pygame.image.load(file) for file in glob("L?.png")] ) walkRight = sorted([pygame.image.load(file) for file in glob("R?.png")]) in python 3 I guess
Hi, this really looks as a great tutorial. Could I just ask a few noob questions? 1. Why do you use 9 images for animations, instead of e.g. 4? Does it make a difference if I use 4 or 5 animations images (in other cases)? 2. Why do you divide the walk_count by 3, instead by some other number (e.g. 4)? Does it really need to be 27 // 3 or could it perhaps be 16 // 4 if there are 4 images in animation? 3. Why do you use numbers inside pygame.display.set_mode, instead of defined variables? For example: pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)? 4. For x and y character starting positions, why do you use fixed numbers, and not SCREEN_WIDTH - CHAR_WIDTH and SCREEN_HEIGHT - CHAR_HEIGHT respectively? 5. Within the jump_count, why do you use numbers (-10) instead of just a negative jump_count variable? Is it better to use the code for jumping as shown in the video or within your website tutorial? Thank you in advance.