Question: I need to make pygame display a differnt value for each coin in my code. code provided below. import pygame import random pygame.init ( )

I need to make pygame display a differnt value for each coin in my code. code provided below. import pygame
import random
pygame.init()
# Create a display surface
WINDOW_WIDTH =600
WINDOW_HEIGHT =300
display_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# Set FPS and clock
FPS =10
clock = pygame.time.Clock()
# Set the game values
VELOCITY =5
# Load images
dragon_image = pygame.image.load("dragon_right.png")
dragon_rect = dragon_image.get_rect(topleft=(25,25))
coin_images =[
pygame.image.load("coin.png"),
pygame.image.load("coin_blue.png"),
pygame.image.load("coin_green.png")
]
coin_values ={
"coin.png": 10,
"coin_blue.png": 20,
"coin_green.png": 30
}
coin_image = random.choice(coin_images)
coin_rect = coin_image.get_rect(center=(WINDOW_WIDTH //2, WINDOW_HEIGHT //2))
background_image = pygame.image.load("star_background.png")
def draw_rotating_ellipse(surface, color, rect, angle):
ellipse_rect = pygame.Rect(0,0, rect.width, rect.height)
ellipse_rect.center = rect.center
rotated_ellipse = pygame.Surface(rect.size, pygame.SRCALPHA)
pygame.draw.ellipse(rotated_ellipse, color, rotated_ellipse.get_rect())
rotated_ellipse = pygame.transform.rotate(rotated_ellipse, angle)
surface.blit(rotated_ellipse, ellipse_rect)
# The main game loop
running = True
angle =0
while running:
# Process events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Get keys
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and dragon_rect.left >0:
dragon_rect.x -= VELOCITY
if keys[pygame.K_RIGHT] and dragon_rect.right < WINDOW_WIDTH:
dragon_rect.x += VELOCITY
if keys[pygame.K_UP] and dragon_rect.top >0:
dragon_rect.y -= VELOCITY
if keys[pygame.K_DOWN] and dragon_rect.bottom < WINDOW_HEIGHT:
dragon_rect.y += VELOCITY
# Check for collision
if dragon_rect.colliderect(coin_rect):
coin_rect.x = random.randint(0, WINDOW_WIDTH - coin_image.get_width())
coin_rect.y = random.randint(0, WINDOW_HEIGHT - coin_image.get_height())
coin_image = random.choice(coin_images)
# Fill display surface with star background
display_surface.blit(background_image, (0,0))
# Draw boundaries
pygame.draw.rect(display_surface, (0,255,0), dragon_rect, 1)
pygame.draw.rect(display_surface, (255,255,0), coin_rect, 1)
# Blit assets
display_surface.blit(dragon_image, dragon_rect)
draw_rotating_ellipse(display_surface, (255,0,0), coin_rect, angle)
display_surface.blit(coin_image, coin_rect)
# Update display
pygame.display.update()
# Tick the clock
clock.tick(FPS)
# Update rotation angle
angle +=1
pygame.quit()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!