파이게임에서 이미지 회전

Maxim Maeder 2023년6월21일
  1. 파이게임에서 이미지 회전
  2. 파이 게임에서 중심 회전 이미지
파이게임에서 이미지 회전

이 튜토리얼은 파이게임에서 이미지를 회전하는 방법을 알려줍니다.

파이게임에서 이미지 회전

transform.rotate 메서드를 사용하면 파이게임에서 이미지와 표면을 쉽게 회전할 수 있습니다. scale 메서드와 마찬가지로 새 표면을 반환합니다.

먼저 회전할 표면을 제공한 다음 회전 각도를 제공해야 합니다.

# Before the main loop
image = pygame.image.load("image.png")
image = pygame.transform.rotate(image, 45)

# in the main loop
screen.blit(image, (30, 30))

메인 루프에서 표면을 회전하는 경우 아래 코드와 같이 기존 표면을 덮어쓰지 않고 대신 새 표면을 만드는 것이 가장 좋습니다.

rotation += 1
shown_image = pygame.transform.rotate(image, rotation)

screen.blit(shown_image, (30, 30))

파이 게임에서 중심 회전 이미지

회전이 왼쪽 위 모서리를 중심으로 이루어짐을 알 수 있지만 대부분 중심을 중심으로 회전하기를 원할 것입니다. 아래 코드는 바로 그 역할을 합니다.

rotated_image = pygame.transform.rotate(image, 45)
new_rect = rotated_image.get_rect(center=image.get_rect(center=(300, 300)).center)

screen.blit(rotated_image, new_rect)

완전한 코드:

# Imports
import sys
import pygame

# Configuration
pygame.init()
fps = 60
fpsClock = pygame.time.Clock()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))

image = pygame.image.load("image.png")
rotation = 0

# Game loop.
while True:
    screen.fill((20, 20, 20))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    rotated_image = pygame.transform.rotate(image, 90)

    new_rect = rotated_image.get_rect(center=image.get_rect(center=(300, 300)).center)

    screen.blit(rotated_image, new_rect)
    pygame.display.flip()
    fpsClock.tick(fps)

출력:

pygame에서 이미지 회전

작가: Maxim Maeder
Maxim Maeder avatar Maxim Maeder avatar

Hi, my name is Maxim Maeder, I am a young programming enthusiast looking to have fun coding and teaching you some things about programming.

GitHub

관련 문장 - Pygame Image