Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

import pygame import random Define the screen dimensions SCREENWIDTH = 1500 SCREENHEIGHT…

  1. User

    import pygame import random

    Define the screen dimensions

    SCREEN_WIDTH = 1500 SCREEN_HEIGHT = 800

    Define the colors

    BLACK = (0, 0, 0)

    Define the artists and their monthly Spotify listeners

    artists = { "NOT": {"Image":"./fotos/NOT/$NOT_1.jpeg", "listeners": 7781046},

    "21 Savage": {"Image":"./fotos/21_Savage/21 Savage_1.jpeg", "listeners": 60358167},
    
    "9lokknine": {"Image":"./fotos/9lokknine/9lokknine_1.jpeg", "listeners": 1680245},
    
    "A Boogie Wit Da Hoodie": {"Image":"./fotos/A_Boogie_Wit_Da_Hoodie/A Boogie Wit Da Hoodie_1.jpeg", "listeners": 18379137},
    
    "Ayo & Teo": {"Image":"./fotos/Ayo_&_Teo/Ayo & Teo_1.jpeg", "listeners": 1818645},
    
    "Bhad Bhabie": {"Image":"./fotos/Bhad_Bhabie/Bhad Bhabie_1.jpeg", "listeners": 1915352},
    
    "Blueface": {"Image":"./fotos/Blueface/Blueface_1.jpeg", "listeners": 4890312},
    
    "Bobby Shmurda": {"Image":"./fotos/Bobby_Shmurda/Bobby Shmurda_1_.jpeg", "listeners": 2523069},
    
    "Cardi B": {"Image":"./fotos/Cardi_B/Cardi B_1.jpeg", "listeners": 30319082},
    
    "Central Cee": {"Image":"./fotos/Central_Cee/Central Cee_1.jpeg", "listeners": 22520846},
    

    }

    Initialize the game

    pygame.init()

    Define the font and font size for the artist names

    font = pygame.font.Font(None, 36)

    Initialize the font module

    pygame.font.init()

    Initialize the game window and caption

    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Guess the Artist")

    Create a clock object to control the frame rate

    clock = pygame.time.Clock()

    score = 0

    while True:

    # Select two random artists
    first_artist, second_artist = random.sample(list(artists.keys()), 2)
    
    # Define the desired width and height for the images
    image_width = 800
    image_height = 600
    
    # Load the images for the artists
    first_artist_image = pygame.image.load(artists[first_artist]["Image"]).convert()
    second_artist_image = pygame.image.load(artists[second_artist]["Image"]).convert()
    
    # Resize the first artist's image
    first_artist_image = pygame.transform.scale(first_artist_image, (image_width, image_height))
    
    # Resize the second artist's image
    second_artist_image = pygame.transform.scale(second_artist_image, (image_width, image_height))
    
    # Display the images
    screen.blit(first_artist_image, (0, 0))
    screen.blit(second_artist_image, (SCREEN_WIDTH // 2, 0))
    
    # Render the text surfaces for the artist names
    first_artist_text = font.render(f"1 - {first_artist.title()}", True, (255, 255, 255))
    second_artist_text = font.render(f"2 - {second_artist.title()}", True, (255, 255, 255))
    
    # Blit the artist names
    screen.blit(first_artist_text, (0, image_height))
    screen.blit(second_artist_text, (SCREEN_WIDTH // 2, image_height))
    
    # Update the display
    pygame.display.flip()
    
    # Clear the screen
    screen.fill(BLACK)
    
    # Prompt the player for their guess
    guess = input(f"\nWhich artist has more monthly Spotify listeners - 1. {first_artist.title()} or 2. {second_artist.title()}? ")
    guess_lower = guess.strip().lower()
    
    if guess_lower == 'quit':
        print("Thanks for playing!")
        break
    
    elif guess_lower not in ['1', '2', first_artist.lower(), second_artist.lower()]:
        print("Invalid input. Please enter the name of one of the two artists or 'quit' to end the game.")
    
    elif guess_lower == first_artist.lower() or guess_lower == '1' and artists[first_artist]["listeners"] > artists[second_artist]["listeners"]:
        print(f"You guessed correctly! {first_artist.title()} had {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.")
        score += 1
    
    elif guess_lower == second_artist.lower() or guess_lower == '2' and artists[second_artist]["listeners"] > artists[first_artist]["listeners"]:
        print(f"You guessed correctly! {second_artist.title()} had {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {first_artist.title()} has {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.")
        score += 1
    
    else:
        print(f"\nSorry, you guessed incorrectly. {first_artist.title()} had {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.\n")
        print(f"Your current score is {score}.")
    
        # AskApologies for the incomplete response. Here's the continuation of the code:
    
        # Ask the player if they want to play again
        play_again = input("Would you like to play again? (y/n) ")
        if play_again.lower() == 'n':
            print(f"Your final score is {score}. Thanks for playing!")
            break
        else:
            score = 0 correct the code so the names of the artists is centered unter the images
    
  2. Assistant

    To center the artist names under the images, you can update the blit positions of the text surfaces. Instead of using (0, image_height) and (SCREEN_WIDTH // 2, image_height), you can calculate the x-coordinate based on the width of the images and the width of the text surfaces.

    Here's the updated code:

    # Calculate the x-coordinate for centering the text surfaces
    first_artist_x = (image_width - first_artist_text.get_width()) // 2
    second_artist_x = (image_width - second_artist_text.get_width()) // 2 + SCREEN_WIDTH // 2
    
    # Blit the artist names
    screen.blit(first_artist_text, (first_artist_x, image_height))
    screen.blit(second_artist_text, (second_artist_x, image_height))
    

    By calculating the x-coordinate using (image_width - text_surface.get_width()) // 2, the text will be centered under the images.

  3. User

    where should this be located

  4. Assistant

    Apologies for the confusion. The updated code should be placed immediately after rendering the text surfaces.

    Here's the updated code placement:

    # Render the text surfaces for the artist names
    first_artist_text = font.render(f"1 - {first_artist.title()}", True, (255, 255, 255))
    second_artist_text = font.render(f"2 - {second_artist.title()}", True, (255, 255, 255))
    
    # Calculate the x-coordinate for centering the text surfaces
    first_artist_x = (image_width - first_artist_text.get_width()) // 2
    second_artist_x = (image_width - second_artist_text.get_width()) // 2 + SCREEN_WIDTH // 2
    
    # Blit the artist names
    screen.blit(first_artist_text, (first_artist_x, image_height))
    screen.blit(second_artist_text, (second_artist_x, image_height))
    

    By placing this code after rendering the text surfaces, the x-coordinate for blitting the text surfaces will be properly calculated and centered under the images.

  5. User

    it works but the pygame doesnt update the images

  6. Assistant

    Apologies for the oversight. To update the displayed images in Pygame, you can add a delay or loop within the while loop. This allows the program to show the images and wait for the player's input before proceeding to the next iteration.

    Here's an example of how to add a delay:

    # Display the images
    screen.blit(first_artist_image, (0, 0))
    screen.blit(second_artist_image, (SCREEN_WIDTH // 2, 0))
    
    # Update the display
    pygame.display.flip()
    
    # Delay for 2 seconds
    pygame.time.wait(2000)
    

    This will display the images for 2 seconds before proceeding to the next iteration of the loop.

    You can adjust the delay duration as needed to give the player enough time to view the images.

  7. User

    imean the images dont update with the new artists from the new round

  8. Assistant

    I apologize for the confusion. In order to update the images with new artists for each round, you need to load the images and render them within each iteration of the while loop.

    Here's the corrected code placement:

    while True:
        # Select two random artists
        first_artist, second_artist = random.sample(list(artists.keys()), 2)
    
        # Load the images for the artists
        first_artist_image = pygame.image.load(artists[first_artist]["Image"]).convert()
        second_artist_image = pygame.image.load(artists[second_artist]["Image"]).convert()
    
        # Resize the images
        first_artist_image = pygame.transform.scale(first_artist_image, (image_width, image_height))
        second_artist_image = pygame.transform.scale(second_artist_image, (image_width, image_height))
    
        # Display the images
        screen.blit(first_artist_image, (0, 0))
        screen.blit(second_artist_image, (SCREEN_WIDTH // 2, 0))
    
        # Render the text surfaces for the artist names
        first_artist_text = font.render(f"1 - {first_artist.title()}", True, (255, 255, 255))
        second_artist_text = font.render(f"2 - {second_artist.title()}", True, (255, 255, 255))
    
        # Calculate the x-coordinate for centering the text surfaces
        first_artist_x = (image_width - first_artist_text.get_width()) // 2
        second_artist_x = (image_width - second_artist_text.get_width()) // 2 + SCREEN_WIDTH // 2
    
        # Blit the artist names
        screen.blit(first_artist_text, (first_artist_x, image_height))
        screen.blit(second_artist_text, (second_artist_x, image_height))
    
        # Update the display
        pygame.display.flip()
    
        # Clear the screen
        screen.fill(BLACK)
    
        # Rest of the code...
    

    By moving the image loading and rendering code into the while loop, the images will be updated with new artists for each round.

  9. User

    it works for one round but then the window ' is not responding'

  10. Assistant

    I apologize for the inconvenience. It seems that the while loop is running continuously without giving control to the event handling system of Pygame. This can cause the window to become unresponsive.

    To fix this issue, you can add an event handling section inside the while loop to handle events such as quitting the game or closing the window. This will allow the game window to remain responsive.

    Here's an example of how to add the event handling section:

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
    
        # Rest of the code...
    

    By checking for the pygame.QUIT event, you can gracefully quit the game when the user closes the window.

    Make sure to place this event handling section inside the while loop but before the section that updates the display and clears the screen.

    Additionally, it's a good practice to include a delay within the while loop using clock.tick() to control the frame rate of the game.

    Here's an example of how to use the clock tick:

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
    
        # Rest of the code...
    
        # Add a delay to control the frame rate
        clock.tick(60)
    

    By calling clock.tick(60) at the end of the while loop, you can set the frame rate to 60 frames per second.

    I hope this resolves the issue and makes the game window responsive.

  11. Assistant

    Apologies for the incomplete response. Here's the updated code with the event handling section and the clock tick:

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
    
        # Select two random artists
        first_artist, second_artist = random.sample(list(artists.keys()), 2)
    
        # Load the images for the artists
        first_artist_image = pygame.image.load(artists[first_artist]["Image"]).convert()
        second_artist_image = pygame.image.load(artists[second_artist]["Image"]).convert()
    
        # Resize the images
        first_artist_image = pygame.transform.scale(first_artist_image, (image_width, image_height))
        second_artist_image = pygame.transform.scale(second_artist_image, (image_width, image_height))
    
        # Display the images
        screen.blit(first_artist_image, (0, 0))
        screen.blit(second_artist_image, (SCREEN_WIDTH // 2, 0))
    
        # Render the text surfaces for the artist names
        first_artist_text = font.render(f"1 - {first_artist.title()}", True, (255, 255, 255))
        second_artist_text = font.render(f"2 - {second_artist.title()}", True, (255, 255, 255))
    
        # Calculate the x-coordinate for centering the text surfaces
        first_artist_x = (image_width - first_artist_text.get_width()) // 2
        second_artist_x = (image_width - second_artist_text.get_width()) // 2 + SCREEN_WIDTH // 2
    
        # Blit the artist names
        screen.blit(first_artist_text, (first_artist_x, image_height))
        screen.blit(second_artist_text, (second_artist_x, image_height))
    
        # Update the display
        pygame.display.flip()
    
        # Clear the screen
        screen.fill(BLACK)
    
        # Add a delay to control the frame rate
        clock.tick(60)
    
    # Rest of the code...
    

    Please ensure you have the complete code, including the event handling section, to prevent the window from becoming unresponsive and to control the frame rate.

  12. User

    now the game asks if the player wants to play again after correctly guessing, this shouldt be the case # Initialize the game pygame.init() # Define the font and font size for the artist names font = pygame.font.Font(None, 36) # Initialize the font module pygame.font.init() # Initialize the game window and caption screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Guess the Artist") # Create a clock object to control the frame rate clock = pygame.time.Clock() score = 0 # Game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Select two random artists first_artist, second_artist = random.sample(list(artists.keys()), 2) # Load the images for the artists first_artist_image = pygame.image.load(artists[first_artist]["Image"]).convert() second_artist_image = pygame.image.load(artists[second_artist]["Image"]).convert() # Define the desired width and height for the images image_width = 800 image_height = 600 # Resize the first artist's image first_artist_image = pygame.transform.scale(first_artist_image, (image_width, image_height)) # Resize the second artist's image second_artist_image = pygame.transform.scale(second_artist_image, (image_width, image_height)) # Display the images screen.blit(first_artist_image, (0, 0)) screen.blit(second_artist_image, (SCREEN_WIDTH // 2, 0)) # Render the text surfaces for the artist names first_artist_text = font.render(f"1 - {first_artist.title()}", True, (255, 255, 255)) second_artist_text = font.render(f"2 - {second_artist.title()}", True, (255, 255, 255)) # Calculate the x-coordinate for centering the text surfaces first_artist_x = (image_width - first_artist_text.get_width()) // 2 second_artist_x = (image_width - second_artist_text.get_width()) // 2 + SCREEN_WIDTH // 2 # Blit the artist names screen.blit(first_artist_text, (first_artist_x, image_height)) screen.blit(second_artist_text, (second_artist_x, image_height)) # Update the display pygame.display.flip() # Clear the screen screen.fill(BLACK) # Prompt the player for their guess guess = input(f"\nWhich artist has more monthly Spotify listeners - 1. {first_artist.title()} or 2. {second_artist.title()}? ") guess_lower = guess.strip().lower() if guess_lower == 'quit': print("Thanks for playing!") running = False elif guess_lower not in ['1', '2', first_artist.lower(), second_artist.lower()]: print("Invalid input. Please enter the name of one of the two artists or 'quit' to end the game.") elif guess_lower == first_artist.lower() or guess_lower == '1' and artists[first_artist]["listeners"] > artists[second_artist]["listeners"]: print(f"You guessed correctly! {first_artist.title()} had {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.") score += 1 elif guess_lower == second_artist.lower() or guess_lower == '2' and artists[second_artist]["listeners"] > artists[first_artist]["listeners"]: print(f"You guessed correctly! {second_artist.title()} had {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {first_artist.title()} has {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.") score += 1 else: print(f"\nSorry, you guessed incorrectly. {first_artist.title()} had {artists[first_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners and {second_artist.title()} has {artists[second_artist]['listeners'] / 1e6:.1f}M monthly Spotify listeners.\n") print(f"Your current score is {score}.") # Ask the player if they want to play again play_again = input("Would you like to play again? (y/n) ") if play_again.lower() == 'n': print(f"Your final score is {score}. Thanks for playing!") running = False else: score = 0 pygame.quit()

  13. Assistant

    Apologies for the confusion. To prevent the game from asking if the player wants to play again after correctly guessing, you can modify the code to remove the code block related to playing again.

    Here's the modified code:

    # Initialize the game
    pygame.init()
    
    # Define the font and