import pygame
import random
import requests
import base64
from io import BytesIO

# Define the screen dimensions
SCREEN_WIDTH = 1900
SCREEN_HEIGHT = 1000

# Define the image dimensions
IMAGE_WIDTH = 800
IMAGE_HEIGHT = 800

# Define the colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# Define the artists and their monthly Spotify listeners
artists = {
“Young Thug”: {“listeners”: 28887553},
“Yungeen Ace”: {“listeners”: 1294188}
}

# Initialize the game
pygame.init()

# Create the game window
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption(“Guess the Artist”)

# Define the fonts
font = pygame.font.Font(None, 36)
listeners_font = pygame.font.Font(None, 24)
question_font = pygame.font.Font(None, 40)

# Load the images
artist_images = {}

# Function to fetch the artist image using the Spotify API and resize it
def fetch_artist_image(artist, client_id, client_secret):
# Check if the image is already fetched
if artist in artist_images:
return artist_images[artist]

# Base64 encode the client ID and secret
client_credentials = base64.b64encode(f"{client_id}:{client_secret}“.encode()).decode()

# Request an access token
token_url = “https://accounts.spotify.com/api/token”
headers = {
“Authorization”: f"Basic {client_credentials}”
}
data = {
“grant_type”: “client_credentials”
}
response = requests.post(token_url, headers=headers, data=data)
access_token = response.json()[“access_token”]

# Search for the artist on Spotify
search_url = f"https://api.spotify.com/v1/search?q={artist}&type=artist"
headers = {
“Authorization”: f"Bearer {access_token}“
}
response = requests.get(search_url, headers=headers)
data = response.json()

# Get the first artist result
artist_info = data[“artists”][“items”][0]

# Get the image URL of the artist
image_url = artist_info[“images”][0][“url”]

# Download the image and resize it
image_data = requests.get(image_url).content
image = pygame.image.load(BytesIO(image_data)).convert()
resized_image = pygame.transform.scale(image, (IMAGE_WIDTH, IMAGE_HEIGHT))

# Store the resized image
artist_images[artist] = resized_image

return resized_image

# Function to check the user’s guess
def check_guess(artist1, artist2, guess):
if guess == “1” and artists[artist1][“listeners”] > artists[artist2][“listeners”]:
return True
elif guess == “2” and artists[artist2][“listeners”] > artists[artist1][“listeners”]:
return True
else:
return False

# Pause duration in milliseconds
pause_duration = 2000

# Game loop
running = True
score = 0
display_listeners = False # Flag to indicate if the listeners’ count should be displayed
show_listeners = False # Flag to indicate if the listeners’ count should be shown after making a guess

while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Clear the screen
screen.fill(BLACK)

# Select two random artists
first_artist, second_artist = random.sample(list(artists.keys()), 2)

# Fetch the artist images
client_id = ‘048322b8ea61411a826de68d0cb1c6d2’
client_secret = ‘29f94d572c6240cf99fc3f13ce050503’
first_artist_image = fetch_artist_image(first_artist, client_id, client_secret)
second_artist_image = fetch_artist_image(second_artist, client_id, client_secret)

# Render the text surfaces for the artist names
first_artist_text = font.render(f"1 - {first_artist.title()}”, True, WHITE)
second_artist_text = font.render(f"2 - {second_artist.title()}“, True, WHITE)

# Render the question text
question_text = question_font.render(“Which artist has more monthly listeners?”, True, WHITE)

# Determine the positions of the text surfaces
first_artist_pos = (400, 50) # x,y
second_artist_pos = (SCREEN_WIDTH // 2 + 400, 50)
question_pos = ((SCREEN_WIDTH - question_text.get_width()) // 2, 10) # y

# Display the artist images
screen.blit(first_artist_image, (SCREEN_WIDTH // 4 - IMAGE_WIDTH // 2, 150))
screen.blit(second_artist_image, (3 * SCREEN_WIDTH // 4 - IMAGE_WIDTH // 2, 150))

# Display the text surfaces
screen.blit(first_artist_text, first_artist_pos)
screen.blit(second_artist_text, second_artist_pos)
screen.blit(question_text, question_pos)

# Render the score text surface
score_text = font.render(f"Score: {score}”, True, WHITE)
score_pos = (20, 20) # Position of the score text
screen.blit(score_text, score_pos)

if display_listeners and show_listeners:

# Render the text surfaces for the listeners’ count
first_listeners_text = listeners_font.render(f"{artists[first_artist][‘listeners’]:,} listeners", True, WHITE)
second_listeners_text = listeners_font.render(f"{artists[second_artist][‘listeners’]:,} listeners", True, WHITE)

# Determine the positions of the listeners’ count
first_listeners_pos = (400, 100)
second_listeners_pos = (SCREEN_WIDTH // 2 + 400, 100)

# Display the listeners’ count
screen.blit(first_listeners_text, first_listeners_pos)
screen.blit(second_listeners_text, second_listeners_pos)

pygame.display.flip()

# Prompt the player for their guess
guess = None
while guess is None:
for event in pygame.event.get():
if event.type == pygame.KEYUP:
if event.key == pygame.K_KP1:
guess = “1”
show_listeners = True
elif event.key == pygame.K_KP2:
guess = “2”
show_listeners = True
elif event.key == pygame.K_q:
running = False
guess = “quit”

if guess == “quit”:
running = False
break

# Update the flag to display listeners after making a guess
display_listeners = True
pygame.display.flip()

if check_guess(first_artist, second_artist, guess):
score += 1
pygame.time.wait(pause_duration) # Pause execution for 2 seconds
else:
# Display the incorrect guess message
play_again_text = font.render(“You guessed incorrectly, do you want to play again? (y/n)”, True, WHITE)
play_again_pos = (
SCREEN_WIDTH // 2 - play_again_text.get_width() // 2, SCREEN_HEIGHT // 10) # x links, x rechts, y
screen.blit(play_again_text, play_again_pos)
pygame.display.flip()

waiting_for_input = True
while waiting_for_input:
for event in pygame.event.get():
if event.type == pygame.KEYUP:
if event.key == pygame.K_y:
waiting_for_input = False
score = 0 # Reset the score
display_listeners = False # Hide the listeners’ count
show_listeners = False
first_artist, second_artist = random.sample(list(artists.keys()), 2) # Select new artists
first_artist_image = fetch_artist_image(first_artist, client_id, client_secret) # Fetch new images
second_artist_image = fetch_artist_image(second_artist, client_id, client_secret)
break # Exit the inner loop and generate new artists
elif event.key == pygame.K_n:
waiting_for_input = False
running = False
break # Exit the inner loop

# Reset the flags
display_listeners = False
show_listeners = False

# Quit the game
pygame.quit() how do i change it so the listener amount is shown after geussing both correct and wrong but dissapears in the next round