python 在pygame中旋转矩形

pdkcd3nj  于 2022-10-30  发布在  Python
关注(0)|答案(1)|浏览(183)

我尝试使用以下代码在pygame中使一个白色矩形像时钟的指针一样旋转,

import random, pygame, math, sys
from pygame.locals import *

Blue = (0,0,255)
Black = (0, 0, 0) 
Green = (0,255,0)
White = (255,255,255)

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Sailing!')

FPS = 30
fpsClock = pygame.time.Clock()

Sail = pygame.Surface([100,10])
Sail.set_colorkey (Black)
Sail.fill(White)

degrees = 0
hyp = 100
x = 200
y = 150

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    adj = 10 * math.cos(degrees)
    opp = 10 * math.sin(degrees)
    dx = adj + 200
    dy = opp + 150

    rotatedSail = pygame.transform.rotate(Sail, degrees)
    Sail_rect = Sail.get_rect(topleft = (dx, dy))
    DISPLAYSURF.fill(Blue)
    DISPLAYSURF.blit(rotatedSail, Sail_rect)
    pygame.display.flip()

    fpsClock.tick(FPS)

    degrees += 1

但是矩形的旋转方式很奇怪。如果你能让这个建议尽可能简单,尽可能接近我的代码,我会很感激,因为我才刚刚开始学习。另外,我知道使用矩形的图像更容易,但是我正在尝试使用表面。有人能帮忙吗?

bttbmeg0

bttbmeg01#

你需要得到旋转后的矩形的边界矩形,并通过(xy)设置矩形的中心(参见如何使用PyGame围绕图像中心旋转图像?):

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    rotatedSail = pygame.transform.rotate(Sail, degrees)
    rotatedSail_rect = rotatedSail.get_rect(center = (x, y))
    DISPLAYSURF.fill(Blue)
    DISPLAYSURF.blit(rotatedSail, rotatedSail_rect)
    pygame.display.flip()

    fpsClock.tick(FPS)

    degrees += 1

围绕另一个点而不是中心点旋转对象要复杂得多。在PyGame中如何围绕偏心轴旋转图像的答案中描述了一个通用的解决方案。
完整示例:

import pygame, math, sys
from pygame.locals import *

Blue = (0,0,255)
Black = (0, 0, 0) 
Green = (0,255,0)
White = (255,255,255)

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Sailing!')

FPS = 30
fpsClock = pygame.time.Clock()

Sail = pygame.Surface([100,10])
Sail.set_colorkey (Black)
Sail.fill(White)

degrees = 0
hyp = 100
x = 200
y = 150

def blitRotate(surf, image, pos, originPos, angle):

    # calcaulate the axis aligned bounding box of the rotated image
    w, h         = image.get_size()
    sin_a, cos_a = math.sin(math.radians(angle)), math.cos(math.radians(angle)) 
    min_x, min_y = min([0, sin_a*h, cos_a*w, sin_a*h + cos_a*w]), max([0, sin_a*w, -cos_a*h, sin_a*w - cos_a*h])

    # calculate the translation of the pivot 
    pivot        = pygame.math.Vector2(originPos[0], -originPos[1])
    pivot_rotate = pivot.rotate(angle)
    pivot_move   = pivot_rotate - pivot

    # calculate the upper left origin of the rotated image
    origin = (pos[0] - originPos[0] + min_x - pivot_move[0], pos[1] - originPos[1] - min_y + pivot_move[1])

    # get a rotated image
    rotated_image = pygame.transform.rotate(image, angle)

    # rotate and blit the image
    surf.blit(rotated_image, origin)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    DISPLAYSURF.fill(Blue)
    blitRotate(DISPLAYSURF, Sail, (x, y), (0, 5), degrees)   
    pygame.display.flip()
    fpsClock.tick(FPS)
    degrees += 1

相关问题