Creating an animated button in Pygame

  Рет қаралды 34,461

Clear Code

Clear Code

Күн бұрын

A simple elevated button made in pygame.
Timestamps:
00:00 - Intro
00:21 - Basic logic
01:58 - Creating the basic button
11:40 - Click logic
19:03 - adding styling / elevation
Code: github.com/clear-code-project...

Пікірлер: 60
@mixlaproduction
@mixlaproduction 3 жыл бұрын
Awesome tutorial 💯 Now I just need a game for it
@amburger1984
@amburger1984 2 жыл бұрын
make a click me game where you click the button and you get 1 point
@carloscardoso2806
@carloscardoso2806 Жыл бұрын
You are awesome, I don't even speak English fluently but I've already learned so much from your videos. I wish you every sucess and happiness. Thank you!
@stoufa
@stoufa 5 ай бұрын
Great tutorial! I learned PyGame from your videos, so, thanks a lot of the effort! 😊👏 I would add a "self.pressed = False" in the else block of the "self.top_rect.collidepoint(mouse_pos)" check, just to invalidate the click if the user clicks and release the button outside the button bounding box, we fixed this aesthetically but the click registers once we go back to the button (once we hover back on the button even without clicking!).
@genericgamedev1304
@genericgamedev1304 2 жыл бұрын
I combined this with the JSON tutorial to create the ability to clear scores and delete the text file. Thanks for the tutorials.
@shuaibbenhamouda548
@shuaibbenhamouda548 3 жыл бұрын
i literily love you your so underated it makes me sad how underated you are
@Klannahar
@Klannahar 2 жыл бұрын
I did my button class on different way (more complex), but this tutorial is explain it well enough to understand for beginers as well. ;-) I did not know u can set up border radius. :D I made mine with 2 cirlce and 1 rect what looks cool. :)
@Klannahar
@Klannahar 2 жыл бұрын
Rounded edge come to pygame with 2.0, good to know.
@razedphoenix8226
@razedphoenix8226 Жыл бұрын
Thanks for this awesome tutorial :)
@rubentorres7429
@rubentorres7429 3 жыл бұрын
We want more videos! Saludos desde argentina hermano
@codingperks
@codingperks 2 жыл бұрын
Awesome! But you should create an inherited class for the elevated button to make it cleaner
@angadsingh6478
@angadsingh6478 3 жыл бұрын
finally i can make a start menu for my game
@jewie3171
@jewie3171 2 жыл бұрын
Great Tutorial
@mathnovice8886
@mathnovice8886 Жыл бұрын
there's a bug in this code, u didnt set self.pressed to False when the rect is not colliding with the mouse_pos. So, let's say, if I move my cursor to the rect and hold left click, self.pressed will become True, now if i move my cursor away from the rect and release the left click, self.pressed is still True. This means that the next time the rect collide with my mouse_pos, even if i m not left clicking, it will still execute the code inside because the self.pressed is not set to False. I think you need another self.pressed = False at the else part (if self.top_rect.collidepoint(mouse_pos) is False)
@pcb5135
@pcb5135 Жыл бұрын
Nice catch
@carlfranz6805
@carlfranz6805 2 жыл бұрын
Nicely done. Very clear as usual. I very much prefer your tutorial style to most of the others. 👍 Just one minor question... what do you return from the class to notify the rest of the program that a click has happened and not get a bunch of clicks? Returning self.pressed from the draw method isn't it. But I'll admit I'm just a little drunk so might be a little thick.
@generalsteam1120
@generalsteam1120 2 ай бұрын
This is my problem too! My guess is to create a method to check if the button is clicked and add another variable to the button class that tracks if that button was clicked that cycle for the new method to return.
@Klannahar
@Klannahar 2 жыл бұрын
@Clear Code : Idk if u reading comments or not, but i would like to ask u, about on click events. I made like 2-3 different way to work, but idk what would be the best solution for it. I do have a base button class, and some other custome button class what inherit from base button and then i just instantiate the custome buttons as many times i want. The button settings such as pos, width, height, color etc are stored in a configuration (ini) file.
@viiia11ctsebastian57
@viiia11ctsebastian57 3 жыл бұрын
Nice
@linuxeducation4058
@linuxeducation4058 Жыл бұрын
This button style was added in Duolingo ;)
@neutrinosssss
@neutrinosssss 3 жыл бұрын
Hi clear code. Can you make a tutorial about making mobile apps using KiVy?
@pranavnyavanandi9710
@pranavnyavanandi9710 2 жыл бұрын
Yes, I am looking into app making as well. I would like details about aspects like networking, db connectivity, etc. The process of how exactly an app is made. You have any tips? Thanks.
@samedifference5504
@samedifference5504 2 жыл бұрын
how can you use the button with local variables, (I am currently using tons of global variables inside button logic)
@Hoco30
@Hoco30 8 ай бұрын
You can use ic in debugging. Wich pritns better then python's print thing and saves your time. And thank you for this tutorial! Iwas searching for how to make button in pygmae and I didn't understand anything but you have expalained very well hope you write a book about pygame.
@yashmityadav47
@yashmityadav47 10 ай бұрын
If i have to add different functions on different buttons rather than "click" so how I can do this
@woutrottiers910
@woutrottiers910 3 жыл бұрын
can you make an tutorial on classes?
@Klannahar
@Klannahar 2 жыл бұрын
Check Corey Schafer's python OOP tutorial videos. He have good one what i can recomed.
@NolanMurphyWhitehead
@NolanMurphyWhitehead Жыл бұрын
I'm having an off problem with this. I created a button and it works fine, but when I try to create more than one button, only the first one works. Any tips for this?
@haridasmahato4312
@haridasmahato4312 3 жыл бұрын
Can you make a video on how to make a pop up window using pygame only there is no video on KZbin
@Joana-dv9mm
@Joana-dv9mm 2 жыл бұрын
hi!! can i make it a clickable link? I was searching for that but i'm not really sure if I can
@ClearCode
@ClearCode 2 жыл бұрын
this should help: stackoverflow.com/questions/68761290/how-to-insert-url-link-inside-a-pygame
@Joana-dv9mm
@Joana-dv9mm 2 жыл бұрын
@@ClearCode but when i put it opens a black screen how can i put it inside the platform i have? edit: i got this one but the link is not moving acording to the world.. its static in the coordinates i defined. How can i put them moving?
@krentor1
@krentor1 Жыл бұрын
how did you learn this dude this is so hard
@rittwikde5145
@rittwikde5145 3 жыл бұрын
@Clear Code how to make a classic bounce game.. it's pretty tough sir cannot understand the logic please make a tutorial on it
@rittwikde5145
@rittwikde5145 3 жыл бұрын
I tried it.. I don't get how to get the obstacles in a random position
@4B3K_
@4B3K_ 2 жыл бұрын
@@rittwikde5145 you could use random.randint() to get random numbers
@span4ev
@span4ev 2 жыл бұрын
I looked for 10 minutes and I can't figure out why screen and gui_font are not passed to the Button class instance? Yes, it works, but why does it even work? When creating an instance of a class, these arguments are not passed to it. Shouldn't it be like this? : classButton: def __init__(self, screen, pos, width, height, gui_font, text):
@parupururin
@parupururin Жыл бұрын
Because the way it is implemented, screen and gui_font are both global variables. So they could be acceded for any code scope.
@SKSam-bq6zt
@SKSam-bq6zt 3 жыл бұрын
YEYYYY he's back
@gokulprasanth3942
@gokulprasanth3942 3 жыл бұрын
How to get source code from python apps? Please make as video
@Klannahar
@Klannahar 2 жыл бұрын
What do u mean? If u have the ".py" file then u have the source code. It is not exe what u can not read directly as a text file.
@abrarmasumabir3809
@abrarmasumabir3809 3 жыл бұрын
please make a video on how to make a pygame for android using pygame subset for android....plz❤❤❤❤❤
@Klannahar
@Klannahar 2 жыл бұрын
For android u should use kivy instead of pygame since pygame not supporting multi touch, while kivy does and pygame is not realy for android.
@abrarmasumabir3809
@abrarmasumabir3809 2 жыл бұрын
@@Klannahar kivy sucks...it's just a beginner library for newbies....
@Klannahar
@Klannahar 2 жыл бұрын
@@abrarmasumabir3809 May i ask you why do you think that?
@abrarmasumabir3809
@abrarmasumabir3809 2 жыл бұрын
@@Klannahar You will find professional game dev people who use pygame but no one takes kivy thar seriously cause it's just a nooby python library.
@Klannahar
@Klannahar 2 жыл бұрын
@@abrarmasumabir3809 Well pygame and kivy made for 2 different resion. U can make game with both and android / ios app with i beleave both, but kivy for sure. For android app i still say kivy is better, but for games pygame is better (not on phone).
@chalkypj
@chalkypj 2 жыл бұрын
Fantastic tutorial helped me a lot. However I am relatively new to python and pygame and have integrated this into a simple program I'm writing and it works well apart from one little problem. When I click the button I created, the code I wrote for the click runs too quick and I lose the animation of the button. Apart from not seeing the animation, everything works fine. I'm probably being dense and just don't fully understand the code.
@ClearCode
@ClearCode 2 жыл бұрын
hey, not sure I understand the problem. How does the rest of the code affect the animation of the button? And I now the feeling of code not working, happens to me all the time as well :)
@chalkypj
@chalkypj 2 жыл бұрын
@@ClearCode What i have is a Game class that holds the button class. I have a quit button that I simply want to use to quit the game and I use an if statement to check if it has been pressed 'if self.quitBtn.pressed:' I quit from inside the statement and it works fine apart from I don't get to see the button animation.
@magictech19
@magictech19 Жыл бұрын
I think u need to set a max framerate so that it runs a little slower
@magictech19
@magictech19 Жыл бұрын
not sure though
@aditgaur1585
@aditgaur1585 3 жыл бұрын
3rd
@VexYTGX
@VexYTGX 3 жыл бұрын
Second:(
@harshdhami3065
@harshdhami3065 3 жыл бұрын
First
@quannguyenhoang1218
@quannguyenhoang1218 3 жыл бұрын
4th :((
@murilo0825
@murilo0825 Жыл бұрын
so you tellin me it takes a whole class just to make a button in pygame? ☠
@bobojenkins5805
@bobojenkins5805 4 ай бұрын
so if you have 3 buttons youre looking at 160 lines of code. Nice.
@toshiroutte
@toshiroutte 4 ай бұрын
No You just add 2 more button objects and draw them in the loop, it's just 4 extra lines of code.
@anakakaenekulo2975
@anakakaenekulo2975 3 ай бұрын
R u restarted?
Debugging in Pygame
12:28
Clear Code
Рет қаралды 13 М.
Get Started in Pygame in 10 minutes!
10:19
Coding With Russ
Рет қаралды 270 М.
I CAN’T BELIEVE I LOST 😱
00:46
Topper Guild
Рет қаралды 27 МЛН
Children deceived dad #comedy
00:19
yuzvikii_family
Рет қаралды 3,9 МЛН
Cameras in Pygame
1:13:21
Clear Code
Рет қаралды 114 М.
Create Buttons in PyGame! (Python Button Class Tutorial)
22:33
LeMaster Tech
Рет қаралды 10 М.
Pygame СОЗДАНИЕ КНОПКИ С АНИМАЦИЕЙ
24:26
Захаров Андрей
Рет қаралды 7 М.
I Made a Neural Network with just Redstone!
17:23
mattbatwings
Рет қаралды 625 М.
Multiple Scenes in Pygame Tutorial (Gamestates)
15:46
Coding with Sphere
Рет қаралды 10 М.
Scrolling Backgrounds in Pygame
7:53
Paget Teaches...
Рет қаралды 83 М.
HOW TO MAKE A MENU SCREEN IN PYGAME!
5:09
baraltech
Рет қаралды 123 М.
PyGame Beginner Tutorial in Python - Adding Buttons
18:55
Coding With Russ
Рет қаралды 161 М.
How To Use Joysticks & Controllers in PyGame
14:52
NeuralNine
Рет қаралды 18 М.
Menus - Pygame Tutorial
7:37
DaFluffyPotato
Рет қаралды 169 М.