1

So I'm currently making a clicker game, and I already have some pretty good looking stuff. But since I want to polish the game, I would like to add a spinning coin animation to the coin that's in the center of the screen.

I have a coin.py file and this is how it looks like:

import pygame

class Coin():
    def __init__(self, screen):
        self.screen = screen

        self.image = pygame.image.load('pyfiles/images/click_button.png')
        self.image_x = 230
        self.image_y = 130

        self.scale_change_x = 10
        self.scale_change_y = 10

    def blitme(self):
        self.screen.blit(self.image, (self.image_x, self.image_y))

And the current gameplay looks like:

Game Demo

As you can see, when my cursor goes on the coin image, it turns yellow. But now, I want it to not only turn yellow but to spin like this image ( that I found on google ):

Spinning coin image found on google

What code should I add to my coin.py file to make it do this when my cursor goes on ( collides with ) the coin?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
GGberry
  • 929
  • 5
  • 21
  • Your coin is just a 2D image. Do you have am animated coin? (gif or sprite sheet) – Rabbid76 Dec 06 '20 at 21:35
  • I tried using `.gif` extension files as an image, but pygame apparently can't open those. When I try, I get this error: `pygame.error: Couldn't open [image_name.png]` – GGberry Dec 06 '20 at 21:47

1 Answers1

1

If you have an animated GIF, see Animated sprite from few images.


If you don't have an animated GIF or sprite sheet of a coin, you can achieve a similar effect by squeezing and flipping the coin along the y-axis.
Use pygame.transform.scale() to scale an image and pygame.transform.flip() to flip it. The following code snippet creates a spin effect of a coin Surface that depends on a specific angel:

new_width = round(math.sin(math.radians(angle)) * coin_rect.width) 
rot_coin = coin if new_width >= 0 else pygame.transform.flip(coin, True, False) 
rot_coin = pygame.transform.scale(rot_coin, (abs(new_width), coin_rect.height))
window.blit(rot_coin, rot_coin.get_rect(center = coin_rect.center))

Minimal example:

import pygame
import math

pygame.init()
window = pygame.display.set_mode((400, 400))
font = pygame.font.SysFont(None, 40)
clock = pygame.time.Clock()

coin = pygame.Surface((160, 160), pygame.SRCALPHA)
pygame.draw.circle(coin, (255, 255, 0), (80, 80), 80, 10)
pygame.draw.circle(coin, (128, 128, 0), (80, 80), 75)
cointext = pygame.font.SysFont(None, 80).render("10", True, (255, 255, 0))
coin.blit(cointext, cointext.get_rect(center = coin.get_rect().center))
coin_rect = coin.get_rect(center = window.get_rect().center)
angle = 0

run = True
while run:
    clock.tick(60)
    current_time = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill(0)    
    new_width = round(math.sin(math.radians(angle)) * coin_rect.width) 
    angle += 2
    rot_coin = coin if new_width >= 0 else pygame.transform.flip(coin, True, False) 
    rot_coin = pygame.transform.scale(rot_coin, (abs(new_width), coin_rect.height))
    window.blit(rot_coin, rot_coin.get_rect(center = coin_rect.center))
    pygame.display.flip()

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174