【问题标题】:How to rotate an image without losing pixel data? (Pygame)如何在不丢失像素数据的情况下旋转图像? (游戏)
【发布时间】:2016-10-27 17:27:31
【问题描述】:

这是我的代码(Python 3.5):

import sys
import pygame
pygame.init()

screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
running = True

class Actor:

    def __init__(self, x, y, w, h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.surface = pygame.image.load("GFX/player.bmp")

    def draw(self):
        screen.blit(self.surface, (self.x, self.y))

class Player(Actor):

    def __init__(self):
        Actor.__init__(self, 0, 0, 32, 32)
        self.directions = [False, False, False, False]
        self.speed = 0.1

    def update(self):
        if self.directions[0]:
            self.y -= self.speed
        if self.directions[1]:
            self.y += self.speed
        if self.directions[2]:
            self.x -= self.speed
        if self.directions[3]:
            self.x += self.speed

player = Player()

def rot_center(image, angle):
    orig_rect = image.get_rect()
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image

def redraw():
    screen.fill((75, 0, 0))
    player.draw()
    player.update()
    pygame.display.flip()

while (running):
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            sys.exit()
        elif e.type == pygame.KEYDOWN:
            if e.key == pygame.K_ESCAPE:
                sys.exit()
            if e.key == pygame.K_w:
                player.directions[0] = True
            if e.key == pygame.K_s:
                player.directions[1] = True
            if e.key == pygame.K_a:
                player.directions[2] = True
            if e.key == pygame.K_d:
                player.directions[3] = True
        elif e.type == pygame.KEYUP:
            if e.key == pygame.K_w:
                player.directions[0] = False
            if e.key == pygame.K_s:
                player.directions[1] = False
            if e.key == pygame.K_a:
                player.directions[2] = False
            if e.key == pygame.K_d:
                player.directions[3] = False
        elif e.type == pygame.MOUSEMOTION:
            player.surface = rot_center(player.surface, pygame.mouse.get_pos()[0] / 64)

    redraw()

非常简单的 pygame 代码。我有一个播放器,上面有我在 mspaint 上创建的简单图像,我使用 this function 旋转图像而不会导致内存不足问题。我正在用鼠标旋转图像(考虑一个“瞄准”某处的玩家)。这是原图:

这是稍微移动鼠标后极其丑陋的结果:

我知道使用 OpenGL(例如 Pyglet)我会获得更好的精度,但在这种情况下,来自 pygame 的旋转功能将完全没用。我错过了什么?我究竟做错了什么?

【问题讨论】:

  • 始终使用原始图像生成旋转版本。或者在图形编辑器中创建旋转图像并使用它们而不是pygame.transform.rotate

标签: python image pygame transform


【解决方案1】:

请记住,Python 中的表面只是像素网格,而不是数学上完美的矢量图形。旋转图像会稍微降低质量。正如您在图片中看到的那样,持续这样做最终会使图像变得无法识别。保持对原始图像的引用并且永远不要覆盖它。当您调用旋转时,请确保您以相对于原始图像的累积角度旋转原始图片,而不是先前的增量旋转版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2018-12-03
    • 1970-01-01
    • 2022-10-23
    相关资源
    最近更新 更多