+ 1
Does anyone know how to code a splash screen in python with pygame imported?
I am currently working on a project in python and I am struggling to find any code that will create a splash screen. I am a complete beginner in coding a game but I decided to challenge myself and create something complex. If you are able to help me, thank you in advance.
1 Respuesta
+ 3
I asked ChatGPT to generate a demo code for this task.  
import pygame
import sys
# Initialize Pygame
pygame.init()
# Splash screen settings
splash_width, splash_height = 600, 400
splash_screen = pygame.display.set_mode((splash_width, splash_height))
pygame.display.set_caption("Splash Screen")
# Main game settings
game_width, game_height = 800, 600
game_screen = None  # The main game screen will be initialized later
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Load splash screen image
splash_image = pygame.Surface((splash_width, splash_height))
splash_image.fill((0, 100, 200))  # Just a placeholder color
font = pygame.font.Font(None, 74)
text = font.render("Splash Screen", True, WHITE)
splash_image.blit(text, (150, 160))
# Display the splash screen
def show_splash_screen():
    clock = pygame.time.Clock()
    splash_duration = 3000  # milliseconds (3 seconds)
    start_time = pygame.time.get_ticks()
    while pygame.time.get_ticks() - start_time < splash_duration:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        splash_screen.fill(BLACK)
        splash_screen.blit(splash_image, (0, 0))
        pygame.display.update()
        clock.tick(60)
# Main game loop
def main_game():
    global game_screen
    game_screen = pygame.display.set_mode((game_width, game_height))
    pygame.display.set_caption("Main Game")
    clock = pygame.time.Clock()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        game_screen.fill((50, 50, 150))  # Placeholder for game screen
        font = pygame.font.Font(None, 50)
        text = font.render("Main Game Screen", True, WHITE)
        game_screen.blit(text, (250, 280))
        pygame.display.update()
        clock.tick(60)
    pygame.quit()
    sys.exit()
# Run the program
if __name__ == "__main__":
    show_splash_screen()
    main_game()



