[Python] My code is making the screen briefly go black for about (0.3 seconds) while I change...

Discussão em 'Python' iniciado por Stack, Maio 25, 2025.

  1. Stack

    Stack Membro Participativo

    My screen is briefly going black while I change from Scene1 to Scene1_1. I tried adding a 3 second delay to see if that would help- but it didn't. I'm not sure what I can change now to make it work?

    import pygame
    import sys
    import os

    from template import Template


    class Scene1:
    def __init__(self):
    pygame.init()

    self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
    self.width, self.height = self.screen.get_size()

    self.fps = 8
    self.num_frames = 58
    self.path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "day1/day1_anime/")

    self.frames = self.load_frames(self.path, self.num_frames)
    self.exit_button()
    self.run()

    def load_frames(self, path, num_frames):
    frames = []
    for i in range(num_frames):
    filename = os.path.join(path, f"frame{i}.png")
    image = pygame.image.load(filename).convert_alpha()
    image = pygame.transform.smoothscale(image, (self.width, self.height))
    frames.append(image)
    return frames

    def exit_button(self):
    self.exit_font = pygame.font.SysFont("Bradley Hand", 20)
    self.white = (255, 255, 255)
    self.button_color = (88, 117, 113)

    self.exit = self.exit_font.render('Quit', True, self.white)

    self.exit_rect = pygame.Rect(0, 0, 70, 50)
    self.exit_rect.center = (1400, 900)
    self.text_exit_rect = self.exit.get_rect(center=self.exit_rect.center)

    def run(self):
    frame_idx = 0
    clock = pygame.time.Clock()
    running = True

    while running:
    mouse = pygame.mouse.get_pos()

    # animation frames
    self.screen.blit(self.frames[frame_idx], (0, 0))

    # exit button
    pygame.draw.rect(self.screen, self.button_color, self.exit_rect)
    self.screen.blit(self.exit, self.text_exit_rect)

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    sys.exit()

    if event.type == pygame.MOUSEBUTTONDOWN:
    if self.exit_rect.collidepoint(mouse):
    pygame.quit()
    sys.exit()

    pygame.display.flip()
    frame_idx = (frame_idx + 1) % self.num_frames
    clock.tick(self.fps)

    if frame_idx == self.num_frames - 1:
    running = False

    self.screen.blit(self.frames[-1], (0, 0))
    pygame.display.flip()
    pygame.time.delay(3000)

    # Run Scene2 after Scene1 finishes
    next_scene = Scene1_1(self.screen)
    next_scene.run()


    class Scene1_1(Template):
    def __init__(self, screen):
    self.screen = screen

    super().__init__()

    self.scene2_bg = pygame.image.load('day1/day1_anime/frame57.png').convert_alpha()
    self.scene2_bg = pygame.transform.smoothscale(self.scene2_bg, (self.screen_width, self.screen_height))

    self.delay = 1500
    self.delay_transition = 4000

    self.start_time = pygame.time.get_ticks()
    self.popup_visible = False

    self.clock = pygame.time.Clock()

    def run(self):
    running = True

    while running:
    mouse = pygame.mouse.get_pos()

    self.screen.blit(self.scene2_bg, (0, 0))

    current_time = pygame.time.get_ticks()
    elapsed = current_time - self.start_time

    if elapsed > self.delay:
    self.available = True
    else:
    self.available = False
    #text bar

    if self.available:
    self.screen.blit(self.bg, self.text_bar_rect)

    #inventory button
    pygame.draw.rect(self.screen, self.button_color, self.inven_rect)
    self.screen.blit(self.inven_button, self.text_inven_rect)

    #save button
    pygame.draw.rect(self.screen, self.button_color, self.save_rect)
    self.screen.blit(self.save_button, self.text_save_rect)

    #options button
    pygame.draw.rect(self.screen, self.button_color, self.option_rect)
    self.screen.blit(self.options_button, self.options_button_rect)

    #popup
    if self.popup_visible:
    pygame.draw.rect(self.screen, self.button_color, self.popup)
    pygame.draw.rect(self.screen, self.button_color2, self.mini_x)
    self.screen.blit(self.x, self.x_text_rect)

    #exit button
    pygame.draw.rect(self.screen, self.button_color2, self.button_rect)
    self.screen.blit(self.quit_button, self.text_rect_button)

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    sys.exit()

    if event.type == pygame.MOUSEBUTTONDOWN:

    #quit button instructions
    if self.button_rect.collidepoint(mouse):
    pygame.quit()
    sys.exit()

    #save button instructions
    if self.save_rect.collidepoint(mouse):
    pygame.quit()
    sys.exit()
    #edit later

    #options instructions
    if self.option_rect.collidepoint(mouse) and not self.popup_visible:
    self.popup_visible = True

    if self.mini_x.collidepoint(mouse):
    self.popup_visible = False

    if self.inven_rect.collidepoint(mouse):
    self.popup_visible = False


    self.clock.tick(60)
    pygame.display.flip()


    import pygame
    import sys

    class Template:
    def __init__(self, screen = None):

    self.screen = screen
    self.screen_width, self.screen_height = self.screen.get_size()

    pygame.display.set_caption("Hi")

    self.button_font = pygame.font.SysFont("Bradley Hand", 20)

    self.white = (255, 255, 255)
    self.button_color = (88, 117, 113)
    self.button_color2 = (62, 95, 102)

    self.save()
    self.options()
    self.options_popup()
    self.exit_button()
    self.inven()
    self.text_bar()

    def save(self):
    self.save_button = self.button_font.render('Save', True, self.white)

    self.save_rect = pygame.Rect(0, 0, 70, 50)
    self.save_rect.center = (1200, 900)
    self.text_save_rect = self.save_button.get_rect(center = self.save_rect.center)

    def options(self):
    self.options_button = self.button_font.render('Options', True, self.white)

    self.option_rect = pygame.Rect(0, 0, 75, 50)
    self.option_rect.center = (1292, 900)
    self.options_button_rect = self.options_button.get_rect(center=self.option_rect.center)

    def options_popup(self):

    #popup rectangle
    self.popup = pygame.Rect(0, 0, 500, 450)
    self.popup.center = (self.screen_width // 2, self.screen_height // 2)

    #x button
    self.x = self.button_font.render('X', True, self.white)

    #square surrounding x button
    self.mini_x = pygame.Rect(self.popup.left + 10, self.popup.top + 10, 20, 20)
    self.x_text_rect = self.x.get_rect(center=self.mini_x.center)

    self.popup_visible = False

    def exit_button(self):
    self.quit_button = self.button_font.render('Quit', True, self.white)

    self.button_rect = pygame.Rect(0, 0, 70, 30)
    self.button_rect.centerx = self.popup.centerx
    self.button_rect.top = self.popup.top + 50
    self.text_rect_button = self.quit_button.get_rect(center = self.button_rect.center)

    def inven(self):
    self.inven_button = self.button_font.render('Inventory',True, self.white)

    self.inven_rect = pygame.Rect(0, 0, 100, 50)
    self.inven_rect.center = (1400, 900)
    self.text_inven_rect = self.inven_button.get_rect(center = self.inven_rect.center)

    def text_bar(self):
    self.bg = pygame.Surface((self.screen_width, 300), pygame.SRCALPHA)
    self.bg.fill((0, 0, 0, 150))

    self.text_bar_rect = self.bg.get_rect()
    self.text_bar_rect.midbottom = (self.screen_width // 2, self.screen_height)

    Continue reading...

Compartilhe esta Página