1

Im trying to make a cookie clicker - like game, and I'm trying to get the center cookie to rotate, but nothing happens :( . Before it just rotated off the screen, but then I deleted that code because I gave up, but now I've done other stuff and I kinda need the rotation now. (I just started like 2 weeks ago so don't judge) Here's my code:

import pygame, sys, time, random

pygame.init()

height = 650
width  = 800

cookies = 0
cps = 0

font = pygame.font.SysFont('Arial', 25)
clock = pygame.time.Clock()


cookie_surface = pygame.image.load('/Users/cameronbitter/Python/Game/cookie.png')
cookie_surface = pygame.transform.scale(cookie_surface, (275, 275))
cookie_rect = cookie_surface.get_rect(center = (width/2, height/2))

screen = pygame.display.set_mode((width, height))

auto_clicker = pygame.image.load('/Users/cameronbitter/Python/Game/mouse_cursor.png')
auto_clicker = pygame.transform.scale(auto_clicker, (480,360))
auto_clicker_rect = auto_clicker.get_rect(topleft = (450, -100))

rotation = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.QUIT
            sys.exit()
        if event.type == pygame.KEYDOWN:
            cookies += 1
            time.sleep(0)

    screen.fill((0,0,0))





    cookies_text_surface_p1 = font.render("Cookies :", True, (255,255,255))
    cookies_text_surface_p2 = font.render(f"                {cookies}", True, (255,255,255))

    pygame.transform.rotate(cookie_surface, (rotation))

    if rotation >= 360:
        rotation = 0

    rotation += 1
    print(rotation)

    screen.blit(cookies_text_surface_p1,(10, 10))
    screen.blit(cookies_text_surface_p2,(10, 11))
    screen.blit(cookie_surface, (cookie_rect))
    screen.blit(auto_clicker, (auto_clicker_rect))

    pygame.display.flip()
    clock.tick(60)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
FluffyFlounder
  • 140
  • 2
  • 8
  • 1
    Does this answer your question? [How do I rotate an image around its center using Pygame?](https://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame) –  Sep 06 '20 at 15:26

1 Answers1

0

pygame.transform.rotate returns a new and rotated pygame.Surface object. You have to blit the new surface:

while True:
    # [...]

    rotated_cookie_surface = pygame.transform.rotate(cookie_surface, rotation)
    rotated_cookie_rect = rotated_cookie_surface.get_rect(center = cookie_rect.center)

    # [...]

    screen.blit(rotated_cookie_surface, rotated_cookie_rect)

    # [...]

See How do I rotate an image around its center using Pygame?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks, it's now rotating but it rotates off the screen. I have already looked at the link you posted and it didn't help.... – FluffyFlounder Sep 06 '20 at 16:45
  • @FluffyFlounder So you did it wrong. Did you use the code from the answer? All you have to do is to copy this 3 lines of code. – Rabbid76 Sep 06 '20 at 16:46
  • I got it working! I had to update the rect_cookie to be the center or the rot_cookie_surface, not the center of the cookie_surface. Thanks for your help! – FluffyFlounder Sep 06 '20 at 16:55
  • @FluffyFlounder I recommend to create `rotated_cookie_rect` exactly as in my answer. – Rabbid76 Sep 06 '20 at 17:12