Reputation: 15
Okay, so basically I have this code for an adventure game, and when the first cutscene is over, I want the player of the game to press 'enter' and then the code would move to the next bit of the game. but the problem is that, when I try to blit the scene, the scene only shows up for a split second and then goes back to the original scene. I also tried to use a while loop, but that did absolutely nothing compared to when I didn't use it. can someone please explain to me what is going on? I cant figure it out myself, and I've searched for a solution for a while now until i decided to post the question here.
Also here's my code for the main loop of the game:
running = True
while running:
screen.fill((211, 211, 211))
# fills the surface with a nice grey color
alphaval -= 20
# makes the screen fade into the scene
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
# setting up the timer for the game
realtime = pygame.time.get_ticks()/1000
# animations start here
# cutscene 1
screen.blit(frame_0, (0, 0))
pygame.draw.rect(screen, (0, 0, 0), (0, 0, 320, 480), 15)
if realtime >= 1:
loading(337, 99)
fadein()
if realtime <= 9 and realtime >= 3:
channel1.play(pygame.mixer.Sound('missions\call.ogg'))
time.sleep(9)
elif realtime >= 9:
screen.blit(frameE_0, (320, 0))
pygame.draw.rect(screen, (0, 0, 0), (320, 0, 320, 480), 15)
if realtime >= 12.5:
screen.blit(dialE, (45, 300))
textbox("hello?", 65, 340)
if realtime >= 14:
textbox("who is this?", 125, 340)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and realtime >= 14:
if event.key == pygame.K_RETURN:
screen.fill((211, 211, 211))
screen.blit(frame_0, (0, 0))
pygame.draw.rect(screen, (0, 0, 0), (0, 0, 320, 480), 15)
screen.blit(frameE_0, (320, 0))
pygame.draw.rect(screen, (0, 0, 0), (320, 0, 320, 480), 15)
clock.tick(30)
pygame.display.update()
Upvotes: 1
Views: 428
Reputation: 210878
You must draw the scene in the application loop. The event loop is only executed once, when the event occurs. Add a state draw_scene = 0
. Change the state when the event occurs (draw_scene = 1
) and draw the scene depending on draw_scene
.
pygame.event.get()
get all the messages and remove them from the queue. See the documentation:
This will get all the messages and remove them from the queue. [...]
If pygame.event.get()
is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.
Get the events once per frame and use them in multiple loops. See Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?
draw_scene = 0
running = True
while running:
screen.fill((211, 211, 211))
alphaval -= 20
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
quit()
if draw_scene == 0:
screen.blit(frame_0, (0, 0))
pygame.draw.rect(screen, (0, 0, 0), (0, 0, 320, 480), 15)
if realtime >= 1:
loading(337, 99)
fadein()
if realtime <= 9 and realtime >= 3:
channel1.play(pygame.mixer.Sound('missions\call.ogg'))
time.sleep(9)
elif realtime >= 9:
screen.blit(frameE_0, (320, 0))
pygame.draw.rect(screen, (0, 0, 0), (320, 0, 320, 480), 15)
if realtime >= 12.5:
screen.blit(dialE, (45, 300))
textbox("hello?", 65, 340)
if realtime >= 14:
textbox("who is this?", 125, 340)
for event in event_list:
if event.type == pygame.KEYDOWN and realtime >= 14:
if event.key == pygame.K_RETURN:
draw_scene = 1
if draw_scene == 1:
screen.fill((211, 211, 211))
screen.blit(frame_0, (0, 0))
pygame.draw.rect(screen, (0, 0, 0), (0, 0, 320, 480), 15)
screen.blit(frameE_0, (320, 0))
pygame.draw.rect(screen, (0, 0, 0), (320, 0, 320, 480), 15)
pygame.display.update()
clock.tick(30)
Upvotes: 1