All gameplay footage shown in the background is from my own games. They're all open source (at least the ones that are released projects): dafluffypotato.itch.io/ For those of you who are saying I’m wrong about SDL. Read the docs for the update from 1 to 2. It *says* it added hardware acceleration. SDL2 is very different from the SDL Pygame originally used (and I mentioned that in the video). SDL2 adds hardware acceleration and Pygame 2 will give access to that functionality. Pygame was created in 2000 and the development stalled for a long period of time, so it was never updated to use SDL2. Pygame 2 is a huge improvement over 1.9.x.
Small tip for pygame performance is to prerender images, if you´re drawing shapes and lines, draw it on a surface on load and then paint that surface on screen every frame, it will be a lot faster then drawing lines every frame... this is only for the case the lines aren´t moving relative to one another, because if not, it won´t make much sense to do that
A lot of programmers don't know about this, which is ironic since even the old days of game programming this technique is heavily used for optimisation since Assembly alone did not help.
@@nathanlamaire Yeah, I thought "baking" was a pretty well known process, but it´s not common to see people using it more often. It´s amazing how much you can precalculate using only textures in a shader
@@oscartriano7262 Could you explain the concept briefly? Let's assume I have an enemy that shoots stuff at the player; how would I not create a new bullet object every time he shoots and destroy that object when it hits something?
@@Milkymalk Use flags, like "used" so if a bullet collides with an enemy or something set its flag to "false". Here you dont destroy or create an object, just reuse them.
Cython is also perfect to optimize your games, :) and it's wonderfully easy. Cython translates your Python code to C. For a beginner it is as easy as just compile a pure python file with Cython. This alone can increase your execution speed by 2 - 5 times! You can then start to slowly add C-Types and functions. Cython in fact is so good that it beat most code written directly in C, at least if you are just an average programmer.
Please explain? How can python code (but translated to C) beat C itself? Shouldn't the language itself always run faster than when it is being translated to from another language, or at least run the same because they are translating to the same thing?
@@ralfwright C itself dose run faster. Please read the sentence as is: Cython in fact is so good that it beat most code written directly in C, *at least if you are just an average programmer* C just don't magically runs fast if you write a mess of a code and writing good C code is very hard. Most average programmers might see that their Cython--Code runs faster than their C code. However a skilled C programmer will write faster code than Cython.
@@ralfwright not an expert on this, but i think i know what's up. compilers are pretty smart these days and here we are talking about average programmers. an average programmer might not be able to write a highly efficient c code, but he can write a pretty good python code. the compiler on the other hand is designed to translate that python code to a c code in the most efficient way, so at the end the translated code might be better than a c code written by an average programmer.
You may not have heard of it, but there was a game engine developed using Python called Zero Engine, which was a proprietary engine used and created by DigiPen Institute of Technology whom I worked for and went to tech classes at. It was pretty alright. I don't remember too much, and I believe it was either discontinued or is simply no longer in use as many of the classes teach Unity instead.
Thank you, this argument has been a pain since the beginning, even tho, for the moment, I’ve never had performance issue with pygame (always converted image and avoided to write useless code)
@@Beeftitan I meant the sprites. I never made big projects with thousands of rects, so I never had the necessity to look for other optimization methods :)
since i started working on shader work, i moved to godot, but i'll always have a special place with learning pygame as it taught me all the basics of programming
Honestly. PyGame for 2D games is perfectly fine in terms of speed although I would say that a language that desperately needs more libraries or docs for existing libraries is the nim programming language. It manages to get that beginner friendly rapid code development down, while still being a fairly high performance statically typed language and yes I am a little biased, cause it is similar to Pascal mixed with Python. Bite me for likeing Pascal I guess XD. SDL2 does already have a wrapper in nim for those who are wondering.
A nice and informative video. Just to add some more, there are a lot of ways to increase speed of python code, like using numpy for heavy array and matrix calculations, writing your own C extensions, using another python implementation like PyPy or mixing your code with language extensions like Cython.
with my optimized tile system when i stress test, i can load around 1.3k individual tiles with around 70 fps i guess i have to check out your video on the particle system
im stupid, i can use the same for particles because smaller areas are way faster to load i have no trouble rendering 10k of them if they are small enough
what optimizations? I made no optimization to my game, didn't even implement a chunk system, and my friend was getting 240fps with vsync on and 400 with vsync off, while playing a level with thousands of objects. Were those 1.3K tiles in the same area, on the screen? Or was it like a normal level with around 1.3k tiles?
That's not true at all lol, aside from the obvious fact that tons of large corporations actively fund and contribute to the JS ecosystem, it has enjoyed being the only language for frontend for quite some time. Also, most people using JS are benefitting from JIT compilation but most people using python are not. All these things add up and this is just sad because Python is clearly the more elegant language.
@@raianmr2843 Hmm. I thought pygame would be faster even though python doesn't have JIT because pygame's backbone SDL is written in C. To be honest, I don't know much about python nor SDL though. Faster WebGL libraries and frameworks like Three.js and PIXI.js are always the talk of the town in the Javascript world, but maybe Canvas2D is faster than most people will give it credit for. It's performance has actually seemed to improve over the years, I can tell you that much.
@@l5248 Oh yah canvas 2d is fast, I mean essentially javascript is calling out to chrome methods written in c++ and displayed using the gpu, so rendering would be at least equal to sdl and given javascript is better optimized at runtime, the whole system should be faster on the whole compared to pygame
Amazing video, as always! At 8:35, UPBGE (basically blender game engine) is a 3D game engine that uses python. I'm also writting my own free engine (Cave Engine) in C++, but scriptable in python.
I was wondering how have you done it that, the borders of images don't turn semi-transparent while rotating on such low resolutions. Could you explain it?
Hi. Im working on a pixel art pygame game. I usually load images, scale them up, store them in variables that i assign to other variables every frame, and then blit them all onto the screen and update it every frame, and i dont convert them because that makes them opaque. Now i wonder if i should maybe store them downscaled and draw them onto a surface and then scale that surface up and blit it onto the screen instead? (also makes it pixel perfect pog) I love this channel btw
Thanks. I'm currently working on a pygame game. As a beginner I prioritize my learning experience over an efficient game. I haven't had any performance issues so far even though I didn't put the .convert() behind the load image. I changed that.
@@thinkingspace3438 By now I do have performance problems. At least when I call my perlin noise generators for the clouds too often, aka when the wind is too fast and the clouds fly by. Is there a more efficient way than using the noise.pnoise2?
@@thinkingspace3438 I have this as a casual project and I like to do things in low effort. I'm aware that pygame is not the product to do high performance games and if I don't find a solution I will just do workarounds or less resource hungry game features. With that in mind how do you do multiprocessing in python? Also do you mean writing a few lines in c-code which get imported as some form of library into my pygame game?
@@benrex777productions9 the multiprocessing lib allows you to make processes (kinda like threads but they are really executed at the same time), and the ctypes lib allows for using functions written in C
2:49 yes, it does, so you shouldn't say "SDL Makes Pygame Slow" when in reality pygame just couldn't figure out how to implement hardware acceleration fully after 8 years of SDL2 existing
Pay attention to the very next sentence. It’s being implemented right now. Pygame was made in 2000 long before SDL2 was released and the development nearly stalled for an entire decade before getting picked back up in ~2018 or so.
Hi, i've been trying out pygame for a while now, but i find raw coding everything really tedious, is there a wrapper or a makeover over pygame that makes it moreeasier to code in
It really just depends on how you think about things imo. I find Pygame fits my thought process well and adding features is very low effort. Arcade may be worth a shot if Pygame feels tedious.
Worrying about whether your tools are going to eventually give you performance problems and researching potential performance issues is a satisfying alternative to doing the much, much harder work of designing and building a fun game. If you have a demo of a fun platformer but some SDL FFI issue is affecting framerates, it would be more straightforward to port that to C++ and eliminate the issue than to have a super efficient game engine but no game and have to design a fun, original platformer that feels really good to play.
Well, there is one game genre that I'm not convinced you can do efficiently in any language other than the most speedy ones (C/C++, Rust, Nim, etc), and that is *falling sand games* (stuff like Powder Game or Noita). I would like to be proven wrong though. I don't like static typing...
iirc the main use case he once talked about was to avoid autocomplete and autodebug sometimes because they are tools that, although they can make you faster when you know what you're doing, they aren't as helpful when you're trying to learn from your mistakes and expand your knowledge on something like, for example, a Python library you've never used. And I suspect it helps that you already get the IDLE once you've just setup Python. However, I think he mostly uses VS Code as of right now.
Curious as to why you are using pygame, and what appears to be pygame1 without using any Opengl rendering. I hear pyglet has faster framerate without really changing anything. Also pygame can render opengl which allows you to offload atleast updating the screen which id assume would be a performance gain. You have pygame create a headless surface essentially and then have opengl render. The issue I am having is framerate drops pretty quickly when the camera needs to move, or when the scenes get large. I know you implemented a chunk system, but im just curious as to why you are sticking with pygame 1 and without pyopengl
i've dabbled in raylib/sfml/sdl using c++ and have to say that for me, pygame is the perfect place to start if only because setup is just so darned simple; a beginner can get any number of quick easy wins under their belt (which is a great motivator to keep going) compared to the torture chamber that is the tooling and build system of c++ sure, later on, if you want a bazillion aliens and particle systems etc, needing to squeeze out every drop of performance then, maybe - maybe - go c++ but get the basics down first with pygame/python would be my advice
Please do more videos about this, because my project is stupid ambitious and I don't want to learn Lua. Also--I'm not quite sure how to say this, but--you don't _sound_ like a fluffy potato, somehow. Have you considered changing your name to "DaPotatoStraw"?
If i write .convert() behind the image file like in 5:30 in this video, it doesn´t work in my game. Is this because I use python 3 or is there a problem with this specific file? Somewhere I read that there is a problem with 32 Bit Images. The Datatype is PNG.
I'm curious about your decision to use Pygame over an engine for you commercial titles. What are the advantages when compared with an engine like Godot, which uses a similar language for scripting? I'm planning to make a game with Zig and OpenGL, to get a solid understanding of low level systems, but I would work with a game engine if I were going to sell something. I'm not trying to give advice, because I have a 2.2/34 experience level (don't ask). I'm just looking to learn more :)
It depends on what you're doing. Engines are slightly less flexible, but frameworks/libraries require you to write boilerplate and some systems. I use it because I have my own framework on top of Pygame that's more efficient to develop with than an engine (for the types of games I make).
I dont think sdl is the issue why pygame is slow. I mean, löve (and alot of steam games) also uses sdl but it's not slow. Perhaps it's python + the way it was written using sdl
Hey, I have a question you might know the answer to. I'm wondering if there is a python library that has an API very similar to pygame.Surface but instead of drawing in 2d mode will draw in OpenGL mode instead. I am thinking of writing such a library if one does not yet exist. Making a 2d game using 3d mode had many advantages over 2d. I haven't been able to find such a library in my research but I thought I'd ask to see if maybe with your wider experience you knew of any I've missed. Thanks in advance!
@@ekrem_dincel Not really, no. Pyglet seems to be an alternative to pygame. What I'm thinking is more an extension of pygame. Pygame (and the SDL it is built on) really is great, with 3d really being the only weak part of it. Going full OpenGL though, you lose the simplicity that pygame has with doing things. A big part of what makes pygame successful is how easy it is to use.
@@ekrem_dincel There is nothing wrong with pyglet. It just isn't what I have in mind here. It takes a different approach to doing things than using pygame does, and I'm looking to keep the pygame way of doing things. For myself personally, I prefer having control over the main loop instead of using a callback system.
@@hossumquat you can write your own game loop in pyglet. Pyglet supports that and even have a doc page on that. But writing your own loop in pyglet is mostly pointless because not only that it allows you to query any user input or schedule any function, the default game loop of pyglet is heavily optimized for multiple OSes specifically. As a person that used both pygame and pyglet I encourage you to try using pyglet, you seem like you don't really know it and it is not hard to use even though the default way of handling things is not very similar to pygame.
To compare Python to C in terms of yelling at your subordinates C/C++ will get straight to the point about why you are getting yelled at Python will include a lot of unnecessary details and mistakes you did some other day that you forgot you did. Hence the performance issue.
how do you recommend I start learning Python? I know the basics of python and made a register/login system with no encryption but I don't know how I should go further from here. I don't have a clear goal in mind other than I want to be able to make software and games with python.
If you wanna start learning Pygame, just straight up learn how to mess with libraries and start reading Pygame's docs. You'll probably want to watch some video tutorials, at least for knowing how to create a simple blank window and how to display some graphics. But once you get a general idea of how it works, the Pygame docs can serve you as a great handbook for helping you with the library's functions. I'd recommend to start by thinking of a not that difficult game to make and just find out how you'll do it. For example, I'm planning on making a Pong clone.
Please reply if you see this, would you recommend using VERY low resolution images, scaled up instead of regular low res images (does this make a big difference to performance?)
SDL is cool for its simplicity but it's strange to see this library in use for something other than just create context for the OpenGL, the Vulcan, etc. It is more designed to process input and work with audio. P.S. I meant that pygame is great but I think pure SDL was not the best idea to make such game libraries.
Nowadays, SDL2 is used for windowing and input for games (if you're willing to write the renderer in OpenGL or Vulkan), but I've seen people use SDL2 functions (which are usually just OpenGL calls) for things like game jams.
Interview questions will get you a job. Making games is a better problem solving experience imo. You will very rarely use those concepts from interview questions in normal jobs.
So my main problem is dealing with classes while learning sfml, it never gets to my head when i see tutorials using them I was able to make sense of what to do with classes in python while learning pygame somewhat but not in c++. But at a point my potato pc wasn't able to handle my "copied" mario game from yt tutorials so i thought it might be good to switch to c++ and now apparently i need visual studio in order to set things up easily, especially classes and that visual studio is too much hard on the memory, that i started getting memory issues while running programs. Should i switch back to pygame since I'm accustomed to python alot more Or do i go for language like Lua and pair it with Löve
For 3d rendering, as long as it is not high definition, why not let numpy handle the 3d object image color information and flatten it before displaying it on the screen? Would that performance still be slow?
Yes, modern games do conversions from 3D objects to 2D raster in GPU. Numpy will be faster than straight up python, but it still runs on CPU in most cases (see CuPy, basically identical to numpy and runs on GPU)
@@agcummings11 yes I get that the performance would be better by using GPU. But my point was: as long as it is pixel art like his games, would numpy with CPU still deliver to display "3D" images with acceptable FPS?
Hi I am pretty newbie at python, i use 3d maya for 20 years i am newbie there too.. You say 3d + shaders not used very well in python.. OK i belive that. Pls explain to me that why all of the 3d programs post production softwares (long list belive me i wont start), integrated python at the first place and made it into a common universal language?
From my own experience, love2d is a lot faster, and it just feels more modern imo. You can also write shader code and pretty easily pack the game into an exe (it uses a system where it basically packages your love2d project together with love2d)
let's say you have a list of objects, and you want to check if they collide. wouldn't you just loop over every single object? i guess if you have to a build a graph of some sort, you'll want to use dictionaries to keep track of interactions?
I'm just using pygame as a graphics library to make an app right now rather than to make a game so performance isn't really a major issue for me thankfully
Thanks for this video. I was watting for it. Last week I implemented image particles (particles generated with image as the particle) and i can't get 60 fps. Sure that i have bad code on it, and needs to improve. If someone have any kind of hint int this aspect, I'm accepting. Thanks
I successfully used `numba.cuda` to implement shaders and 3d rendering without using another language. You can use just use (mostly pythonic) code and run at a few hundred FPS with a full screen shader when your GPU is good enough. (More if SDL and pygame would do a better job at exposing the GPU side buffers).
I said that it shouldn’t be done with Pygame, not that it shouldn’t be done with Python. I mentioned multiple ways you can work on 3D games with shaders (such as Ursina) in the video.
@@DaFluffyPotato Oh, I don't disagree. I am saying that you can use pygame as the core and easily do the performance critical parts (like shaders or 3d rendering) also in 'Python' while still using pygame.
@@ABrainrotAwayFromHeaven that depends of the the complexity of the game, on my own i try to not update everything all the time and i use multiprocessing for the different things, the only performance issues i have come from rendering big surfaces
I just wrote my first game in pygame and the only problem I have is that the keyboard scanning for player input seems extremely slow. it is like having 'sticky' keys.
1- be sure to collect the inputs correctly, there's a key pressed event and a key unleashed event 2- don't tick the line that gets the inputs, it's a newb mistake
I always heard people say that python is slow, so I wanted to test that. Got to a conclusion that its almost 170x slower than c++, with using only the barebones of the language
C++ and C are compiled languages, but python is interpreted, surely this affects its speed? I'm no expert, I don't really understand why once source code has been written in a Python interpreter, where it is easy to test, it can't be put through a 'Python Compiler' to create a high speed executable file.
@@IPlayWithFire135 Hi, that's what I'm trying to find out, because in the past (I'm old) interpreted languages were translated into machine code at run time. (I've been out of computing for a while, I'm trying to do a bit again) There is also an issue of portability I am trying to find out about. C executable files were portable, because they were in machine readable instructions so the computer did not need to 'know' the language. Thanks.
I’m a bit confused about this, but I’m using .convert_alpha instead of .convert, is there a difference, and if so, which is better and why? I tried looking it up for a while and it seems like maybe they do the same thing? Then I got annoyed and just went back to designing character sprites
@@DaFluffyPotato okay, so I have a TON of sprite animations that all have different geometries even though they are the same character model… I am using transparencies and I would like for them to have damage collision with their pixels instead of with a rectangle, I think I should probably be using convert_alpha and I’m looking into the collision thing