Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need each coin to display a different value when hit by the dragon and I need to add a rotating elipse to this pygame.

I need each coin to display a different value when hit by the dragon and I need to add a rotating elipse to this pygame. The code is provided below. import pygame, 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 games values
VELOCITY =5
# load images
dragon_image = pygame.image.load("dragon_right.png")
dragon_rect = dragon_image.get_rect()
dragon_rect.topleft =(25,25)
# load multiple coin images
coin_images =[pygame.image.load("coin1.png"), pygame.image.load("coin2.png"), pygame.image.load("coin3.png")]
coin_image = random.choice(coin_images)
coin_rect = coin_image.get_rect()
coin_rect.center =(WINDOW_WIDTH//2,WINDOW_HEIGHT//2)
# load star background
background_image = pygame.image.load("star_background.png")
# the main game loop
running = True
while running:
# process each update
# listen for quit request
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# get keys to see who is pressed down
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 collison of the two objects
if dragon_rect.colliderect(coin_rect):
print('HIT')
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) # select a new random coin image
# 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)
display_surface.blit(coin_image, coin_rect)
#update display
pygame.display.update()
# tick the clock
clock.tick(FPS)
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

Students also viewed these Databases questions

Question

What is a money center bank and a regional bank?

Answered: 1 week ago