Как в библиотеке Pygame в Python плавно повернуть изображение?
Я хочу чтобы мое изображение при удерживании определенной клавиши поворачивалось.
def __init__(self, x, y, w=100, h=100):
super(Car, self).__init__()
self.image = pygame.transform.rotate(car_image, 180)
self.rect = pygame.Rect(x, y, w, h)
self.speed = speed
self.reversing = False
def update(self):
#if 0 <= self.rect.y <= heidth and 0 <= self.rect.x <= whidth:
if forward:
self.rect.y += 10
if back:
self.rect.y -= 10
if left:
Прошу строго не судить. Это мой первый вопрос)
Ответы (1 шт):
Автор решения: mrBars1k
→ Ссылка
Пример реализации поворота изображения при зажатой кнопки (пробел):
import pygame
import sys
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
image = pygame.image.load('IMAGE.png')
image_rect = image.get_rect(center=(width // 2, height // 2))
rotation_angle = 0
rotation_speed = 2
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
rotation_angle += rotation_speed
rotated_image = pygame.transform.rotate(image, rotation_angle)
rotated_rect = rotated_image.get_rect(center=image_rect.center)
screen.fill((0, 0, 0))
screen.blit(rotated_image, rotated_rect)
pygame.display.flip()
clock.tick(60)
Устанавливаем изначальный угол поворота на 0.
Проверяем циклом зажата ли клавиша K_SPACE.
Если клавиша зажата, то угол поворота меняется на значение, указанное в rotation_speed.