Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need my coins to all have different values and display them in the terminal please help. Pygame. Code provided below. import pygame import random

I need my coins to all have different values and display them in the terminal please help. Pygame. 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions