Hi, so I was wondering how to do something in python. So I made a game using pygame. My "while" loop which is basically the main part of the program. I want to have the game pause basically and make a little menu appear with "resume", "options", "exit" etc. How would I pause this while loop without losing any of the data like scores and where all my cars are? Thanks. Ill be happy to answer any questions if I didnt explain it well! If you need to see any of my code just tell me! Thanks.
Pausing a While Loop. Pygame.
Page 1 of 19 Replies - 3573 Views - Last Post: 03 February 2011 - 06:37 PM
Replies To: Pausing a While Loop. Pygame.
#2
Re: Pausing a While Loop. Pygame.
Posted 02 February 2011 - 08:10 PM
Basically: create another (nested) loop.
In the code above, you will see that the inner while loop will "trap" all attention, pausing the game loop until the menu is exited.
Another option, of course, is to have this be an entirely separate function, which I would recommend!
...
while running:
ms = clock.tick(60)
# This is your main game loop.
# Somewhere in here you will set menu to True, perhaps on a keypress event?
...
while menu:
ms = clock.tick(60)
# Code to display options and get results here
...
if option == 1:
# Resume
menu = False
if option == 2:
# Options
...
In the code above, you will see that the inner while loop will "trap" all attention, pausing the game loop until the menu is exited.
Another option, of course, is to have this be an entirely separate function, which I would recommend!
#3
Re: Pausing a While Loop. Pygame.
Posted 03 February 2011 - 03:33 PM
Motoma, on 02 February 2011 - 08:10 PM, said:
Basically: create another (nested) loop.
In the code above, you will see that the inner while loop will "trap" all attention, pausing the game loop until the menu is exited.
Another option, of course, is to have this be an entirely separate function, which I would recommend!
...
while running:
ms = clock.tick(60)
# This is your main game loop.
# Somewhere in here you will set menu to True, perhaps on a keypress event?
...
while menu:
ms = clock.tick(60)
# Code to display options and get results here
...
if option == 1:
# Resume
menu = False
if option == 2:
# Options
...
In the code above, you will see that the inner while loop will "trap" all attention, pausing the game loop until the menu is exited.
Another option, of course, is to have this be an entirely separate function, which I would recommend!
Yes Thank you! It is working fine i made a small menu but the while loop that runs the main game* does not pause it keeps on going while my pause menu is popped up. When I press resume then it goes back to the game but the cars are have been moving while I was in the pause menu?Basically I press pause then when i press resume my game it shows the cars advance to how much you wait. Here is my code:
Pausemenu = False
Running = True
while Running:
if pygame.mixer.music.get_busy() == True:
pygame.mixer.music.set_volume(0.6)
sound.set_volume(0.5)
sound2.set_volume(0.2)
if pygame.mixer.music.get_busy() == False:
pygame.mixer.music.set_volume(0.0)
sound.set_volume(0.0)
sound2.set_volume(0.0)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
#-------------------------------------------------------#
#Test For Pause#
if event.type == MOUSEBUTTONDOWN:
Mpos = pygame.mouse.get_pos()
if Pause.collidepoint((Mpos)):
Pausemenu = True
while Pausemenu:
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
screen = pygame.display.set_mode((896, 836),0,32)
font = pygame.font.Font(None,30)
redcar = pygame.image.load(os.path.join('Images','Car.jpg')).convert()
bluecar = pygame.image.load(os.path.join('Images','Car2.jpg')).convert()
screen.blit(bluecar, (10,10))
screen.blit(bluecar,(250,230))
screen.blit(redcar,(250,10))
screen.blit(redcar,(10,230))
Resume = font.render('Resume', True, (0,0,255))
MainMenu = font.render('MainMenu', True, (0,255,0))
Exit = font.render('Exit', True, (0,255,0))
screen.blit(Resume, (110,100))
screen.blit(MainMenu, (100,150))
screen.blit(Exit, (130,200))
RR = pygame.Rect(110,100,100,30)
RMM = pygame.Rect(100,150,100,30)
RE = pygame.Rect(130,200,100,30)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
Pausemenu = False
if event.type == MOUSEBUTTONDOWN:
Mpos = pygame.mouse.get_pos()
if RR.collidepoint((Mpos)):
Pausemenu = False
if RMM.collidepoint((Mpos)):
MainMenu()
if RE.collidepoint((Mpos)):
pygame.quit()
sys.exit()
pygame.display.set_caption("Traffic Rush Pause")
pygame.display.update()
This post has been edited by Polofiesta: 03 February 2011 - 03:35 PM
#4
Re: Pausing a While Loop. Pygame.
Posted 03 February 2011 - 04:46 PM
ACTUALLY (!!!) you're problem is likely caused by your clock.tick() method, which is calculating how far the cars have moved. You are going to need to set the milliseconds passed to zero when you leave your game loop.
This post has been edited by Motoma: 03 February 2011 - 05:05 PM
#5
Re: Pausing a While Loop. Pygame.
Posted 03 February 2011 - 05:18 PM
Motoma, on 03 February 2011 - 04:46 PM, said:
ACTUALLY (!!!) you're problem is likely caused by your clock.tick() method, which is calculating how far the cars have moved. You are going to need to set the milliseconds passed to zero when you leave your game loop.
Thank You!
#6
Re: Pausing a While Loop. Pygame.
Posted 03 February 2011 - 05:59 PM
os.environ['SDL_VIDEO_CENTERED'] = '1'
screen1 = pygame.display.set_mode((300,300))
font = pygame.font.Font(None,30)
redcar = pygame.image.load(os.path.join('Images','Car.jpg')).convert()
bluecar = pygame.image.load(os.path.join('Images','Car2.jpg')).convert()
screen1.blit(bluecar, (10,10))
screen1.blit(bluecar,(250,230))
screen1.blit(redcar,(250,10))
screen1.blit(redcar,(10,230))
Resume = font.render('Resume', True, (0,255,0))
MainMenu = font.render('MainMenu', True, (0,255,0))
Exit = font.render('Exit', True, (0,255,0))
screen1.blit(Resume, (110,100))
screen1.blit(MainMenu, (100,150))
screen1.blit(Exit, (130,200))
RR = pygame.Rect(110,100,100,30)
RMM = pygame.Rect(100,150,100,30)
RE = pygame.Rect(130,200,100,30)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
Pausemenu = False
Running = True
screen = pygame.display.set_mode((896,836),0,32)
ms = clock.tick(60)
if event.type == MOUSEBUTTONDOWN:
Mpos = pygame.mouse.get_pos()
if RR.collidepoint((Mpos)):
Pausemenu = False
Running = True
ms = clock.tick(60)
screen = pygame.display.set_mode((896,836),0,32)
if RMM.collidepoint((Mpos)):
screen.fill((0,0,0))
pygame.display.set_mode((800, 600))
pygame.display.update()
MainMenu()
if RE.collidepoint((Mpos)):
pygame.quit()
sys.exit()
pygame.display.set_caption("Traffic Rush Pause")
pygame.display.update()
When I call to go to my function or call MainMenu() if the user clicks on main menu it gives me an error when I click it and here is what it says:
TypeError: 'pygame.Surface' object is not callable. And here is my MainMenu() function but I do not think anything is wrong with it as I use it many other times to call it and it works fine. And i used a module of pygames website for this menu.
def MainMenu():
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Traffic Rush Main Menu")
font = pygame.font.Font(None, 20)
red = (os.path.join("Images","Car.jpg"))
redcar = pygame.image.load(red).convert()
blue = (os.path.join("Images","Car2.jpg"))
bluecar = pygame.image.load(blue).convert()
screen.blit(bluecar,(10,10))
screen.blit(redcar,(740,10))
text = font.render("Programmed and designed by:", True, (0,255,0))
text2 = font.render("Name Here (C)",True, (0,255,0))
screen.blit(text,(560,530))
screen.blit(text2, (615,550))
AudioB = pygame.image.load(os.path.join("Images",'Audioblack.png')).convert_alpha()
AudioMuteB = pygame.image.load(os.path.join("Images",'Audio-mute-black.png')).convert_alpha()
screen.blit(AudioB, (10,550))
pygame.display.update()
menu = cMenu(50, 50, 20, 5, 'vertical', 100, screen,
[('Start Game', 1, None),
('Scores', 2, None),
('Help/Info', 3, None),
('Exit', 4, None),
('<--Play Music-->', 5, None),
('<--Mute-->' ,6, None)])
menu.set_center(True, True)
menu.set_alignment('center','center')
state = 0
prev_state = 1
rect_list = []
pygame.event.set_blocked(pygame.MOUSEMOTION)
while 1:
if prev_state != state:
pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
prev_state = state
e = pygame.event.wait()
if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
if state == 0:
rect_list, state = menu.update(e, state)
elif state == 1:
state = 0
screen.fill((0,0,0))
screen.blit(text,(560,530))
screen.blit(text2, (615,550))
screen.blit(bluecar,(10,10))
screen.blit(redcar,(740,10))
pygame.display.update()
DifficultyMenu()
elif state == 2:
state = 0
ScoreMenu()
elif state == 3:
state = 0
Help()
elif state == 5:
state = 0
pygame.mixer.music.play(1000)
screen.fill((0,0,0))
screen.blit(AudioB, (10,550))
screen.blit(text,(560,530))
screen.blit(text2, (615,550))
screen.blit(bluecar,(10,10))
screen.blit(redcar,(740,10))
pygame.display.update()
elif state == 6:
state = 0
pygame.mixer.music.stop()
screen.fill((0,0,0))
screen.blit(AudioMuteB, (10,550))
screen.blit(text,(560,530))
screen.blit(text2, (615,550))
screen.blit(bluecar,(10,10))
screen.blit(redcar,(740,10))
pygame.display.update()
elif state == 4:
pygame.quit()
sys.exit()
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update(rect_list)
if __name__ == "__main__":
MainMenu()
This post has been edited by Curtis Rutland: 20 November 2012 - 09:52 PM
Reason for edit:: removing personal info
#7
Re: Pausing a While Loop. Pygame.
Posted 03 February 2011 - 06:03 PM
Please post the code that is on the line the error message complains about, as well as the traceback in full.
#8
Re: Pausing a While Loop. Pygame.
Posted 03 February 2011 - 06:09 PM
This is what its giving me:
Traceback (most recent call last):
File "C:\Python27\TheNewBoston\Game\TrafficRushWithCollision.py", line 1819, in <module>
MainMenu()
File "C:\Python27\TheNewBoston\Game\TrafficRushWithCollision.py", line 1775, in MainMenu
DifficultyMenu()
File "C:\Python27\TheNewBoston\Game\TrafficRushWithCollision.py", line 1683, in DifficultyMenu
Easy()
File "C:\Python27\TheNewBoston\Game\TrafficRushWithCollision.py", line 491, in Easy
MainMenu()
TypeError: 'pygame.Surface' object is not callable
Do you need anything else? Thank you so much.
Traceback (most recent call last):
File "C:\Python27\TheNewBoston\Game\TrafficRushWithCollision.py", line 1819, in <module>
MainMenu()
File "C:\Python27\TheNewBoston\Game\TrafficRushWithCollision.py", line 1775, in MainMenu
DifficultyMenu()
File "C:\Python27\TheNewBoston\Game\TrafficRushWithCollision.py", line 1683, in DifficultyMenu
Easy()
File "C:\Python27\TheNewBoston\Game\TrafficRushWithCollision.py", line 491, in Easy
MainMenu()
TypeError: 'pygame.Surface' object is not callable
Do you need anything else? Thank you so much.
#9
Re: Pausing a While Loop. Pygame.
Posted 03 February 2011 - 06:14 PM
Right, now here's the problem:
You've defined MainMenu as two different entities, one as a function def MainMenu(): and one as a pygame.Surface font.render('MainMenu', True, (0,255,0)).
Change one, lose the error message.
You've defined MainMenu as two different entities, one as a function def MainMenu(): and one as a pygame.Surface font.render('MainMenu', True, (0,255,0)).
Change one, lose the error message.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|