Game Development in Python 3 With PyGame - 17 - Pause function

preview_player
Показать описание


In this PyGame with Python 3 programming tutorial, we cover how to add a pause function to our game. With this, we can allow the user to pause and find themselves on a nice pause GUI, where they can continue to play, or quit.

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Рекомендации по теме
Комментарии
Автор

Many people seem to have an issue with the Continue button. Even I was facing the same issue but it was because I had not made changes to the buttons by adding game_loop and quitgame in the lines of code to play and quit. I went back to the 15th Tutorial on this topic and followed whatever he did and voila! Everything is working now.

tinkoo
Автор

After spending the better part of two hours finding installers and cursing the day I ever picked up something more sophisticated than an abacus, it gave me a certain sense of schadenfreude to watch you squirm.

But I did get it to run. So thanks!

EvanSiegel
Автор

@sentdex...I know this series of videos are years old, but i just discovered them recently...in my attempt to get more comfortable with python programming in a fun way. For what it is worth...I just wanted to send you a huge THANK YOU for making these videos. 17 sessions in, I am bummed that the end is near. I like the short bite-sized sessions. I like your casual (and entertaining) teaching style. I like that you show us your coding mistakes and debugging (super valuable learning opportunities). While I am still far from competent as a Python programmer...I have understood your lessons well enough to modify your code and make some additional functionalities in my version of the racing game. Solidifying Python understanding by making fun little games has to be the best way to begin the programming journey. Once again...thank you.

sennabullet
Автор

Nice video as always. One simple thoughts. To avoid using global variables (because sometimes it can be troublesome), one solution is check button_press and return that state, i.e.


def button(msg, x, y, w, h, color_inact, color_act, action=None):
...
# button action
if x + w > mouse_pos[0] > x and y + h > mouse_pos[1] > y:
pygame.draw.rect(gameDisplay, color_act, (x, y, w, h))
if mouse_click[0] == 1:
button_press = True
if action is not None:
action()
else:
pygame.draw.rect(gameDisplay, color_inact, (x, y, w, h))
...
return button_press


then in game_pause(), one can check button_press and jump out of the while loop, i.e.


def game_pause():

pause = True
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_quit()
...
continue_press = button(msg='continue!', x=150, y=450, w=100, h=50, color_inact=green, color_act=green_bright)
if continue_press:
pause = False
...

kefeimo
Автор

To fix continue button not working:
set global variable pause to pause = True
and make sure you've deleted pause = True under your def paused() statement

ManintheMakingPodcast
Автор

If your quit button is not working, you probably kept the strings in episode 15. Go back to that episode and watch closer to the end when he changes the "play" and "quit" strings to game_loop and quitgame. I had trouble with that too.

louie
Автор

it did not work when i pressed continue but when i deleted pause = True in the def paused() function it worked

Gandalfbaws
Автор

Hi, I'm from Brazil, maybe my English will not be so good.
But anyway, I have watched all the videos of this playlist, these videos have helped me a lot. So I always click on youtube ads to give you some feedback!

Now, about the video, I could not find where I went wrong so I'm commending here, the pause function works, but only once, if I try to use more than once during the game does not work, can you help me?

amandamata
Автор

This is one of the hardest things I had to deal with, Pause Functions!!! so many problems that I can't solve.

Linkz
Автор

why not to make a button for pause with | | two vertical bar symbol which does pause function upon clicking

priyanshusharma
Автор

My buttons work perfectly when drawn (thank you for the tutorials) however, I cannot seem to make a button that has an image as the display. Initially, I was hoping that I could make the button transparent and after some searching around I discovered that pygame does not support transparent shapes. Do you by chance have any recommendations on how to use an image as a button, I have thought about hiding the nevermind, I just came up with a solution while typing this out XD.


Thank you again for the tutorials!!!!

SnowyGlory
Автор

Hi, I have followed along your videoes and managed to do a pause function which have 5 buttons; continue, restart, controls, rules and quit. if you click on "Rules" or "Controls", a function to blit a image of the rules/controls will happen. I've managed to go from main menu -> controls/rules -> main menu. If i start the game, pause and click on the "controls" or "rules" it will blit the rules/controls image, but i cant manage to return to the game/pause menu. Is there a good/easy way to do this? :)

Chrix
Автор

When I pause the quit button works but not the continue button

TheWiklands
Автор

First I just want to thank you for the very helpful videos!
My question is, is it possible to play a video or a short clip if a certain command happens?
That'd be very helpful! thank you

crimsondragon
Автор

Hey. There is a small question. Can I use html5 gui in python? I'll try to explain, for example - I have an interface made in html5 (animation and svg files). And I want to use this interface in the game))))

ion
Автор

I don't understand why when on we go to the paused screen by pressing 'p', the main game gets paused. There is no code saying the blocks to stay where they are, the score to stay the same, and etc. Can someone explain... thanks!

eugene
Автор

Is this wrong use of code? :
# in pause / intro, instead of button(... I did declare continue / start button like this:
pause = button("Continue", 150, 450, 100, 50, green, bright_green, "pause")

# and then in button function just :

if action == game_loop or action == "pause":
return False
# and at the end of button()
return True

... is that bad use of code, not something I should do?
because this is way easier to understand to me and I guess you can change the last argument to something like "toggle" to make it smoother

thinboxdictator
Автор

My pause menu does not work. Ihave checked it in debug mode and I saw that it went into the while pause() loop, but it did not go further down. I tried to put a print("I'm in the while loop"), but it did not print anything. I'm using Python 3.6. Somebody please help me.

jdsflk
Автор

Can you make, like, various modules, for example, buttons, colors, obstacles, etc, and then import them to the main module? 

GusTheWolfgang
Автор

or instead of all the extra paused() function just run the pause() function through game_loop and have pause return nothing when you hit continue

josephmojo
visit shbcf.ru