如何在python3(Pygame)中连续旋转图像?

ffx8fchx  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(103)

我试着做一个类似饼干点击器的游戏,我试着让中心饼干旋转,但什么也没有发生:(.之前它只是旋转离开屏幕,但后来我删除了代码,因为我放弃了,但现在我做了其他的事情,我有点需要旋转现在。(我刚开始像两周前,所以不要判断)以下是我的代码:

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)

字符串

gcuhipw9

gcuhipw91#

pygame.transform.rotate返回一个新的旋转的pygame.Surface对象。您必须blit新曲面:

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)

    # [...]

字符串
请参阅如何使用Pygame围绕图像中心旋转图像?

相关问题