Pygame:如何根据输入顺时针/逆时针旋转一个矩形

4

到目前为止,我已经创建了一个矩形,它跟随用户的鼠标移动,并通过按下 a 键逆时针旋转,按下 d 键顺时针旋转。

目前该矩形跟随鼠标移动,但一旦用户开始旋转矩形,矩形就会变得非常卡顿和畸形。我需要帮助保持矩形不变和恒定的FPS。谢谢你的帮助!

以下是代码:

import pygame as py  

# define constants  
WIDTH = 500  
HEIGHT = 500  
FPS = 200

# define colors  
BLACK = (0 , 0 , 0)  
GREEN = (0 , 255 , 0)

# initialize pygame and create screen  
py.init()  
screen = py.display.set_mode((WIDTH , HEIGHT))  
# for setting FPS  
clock = py.time.Clock()  

rot = 0  
rot_speed = 2  

# define a surface (RECTANGLE)  
image_orig = py.Surface((1 , 100))  
# for making transparent background while rotating an image  
image_orig.set_colorkey(BLACK)  
# fill the rectangle / surface with green color  
image_orig.fill(GREEN)  
# creating a copy of orignal image for smooth rotation  
image = image_orig.copy()  
image.set_colorkey(BLACK)  
# define rect for placing the rectangle at the desired position  
rect = image.get_rect()
x, y = py.mouse.get_pos()
rect.center = (x, y)  
# keep rotating the rectangle until running is set to False
running = True  
while running:  

    x, y = py.mouse.get_pos()
    # set FPS  
    clock.tick(FPS)  
    # clear the screen every time before drawing new objects  
    screen.fill(BLACK)  
    # check for the exit  
    for event in py.event.get():  
        if event.type == py.QUIT:  
            running = False
    # making a copy of the old center of the rectangle  
    old_center =(x, y)
    # defining angle of the rotation  
    rot = (rot + rot_speed) % 360  
    # rotating the orignal image
    keys = py.key.get_pressed()
    rot_speed = .2 
    image_orig = py.transform.rotate(image_orig , 0)  
    rect = image_orig.get_rect()  
        # set the rotated rectangle to the old center  
    rect.center = (x, y)  
        # drawing the rotated rectangle to the screen  
    screen.blit(image_orig , rect)  
        # flipping the display after drawing everything  
    py.display.flip()
    if(keys[py.K_a]):
        rot_speed = .2 
        image_orig = py.transform.rotate(image_orig , rot)  
        rect = image_orig.get_rect()  
        # set the rotated rectangle to the old center  
        rect.center = (x, y)  
        # drawing the rotated rectangle to the screen  
        screen.blit(image_orig , rect)  
        # flipping the display after drawing everything  
        py.display.flip()
    if(keys[py.K_d]):
        rot_speed = -.2 
        image_orig = py.transform.rotate(image_orig , rot)  
        rect = image_orig.get_rect()  
        # set the rotated rectangle to the old center  
        rect.center = (x, y)  
        # drawing the rotated rectangle to the screen  
        screen.blit(image_orig , rect)  
        # flipping the display after drawing everything  
        py.display.flip()
    rect.center = (x, y)

py.quit()  
1个回答

5

主要问题在于您不断旋转和操作原始图像,这会导致图像变形。请参见如何使用Pygame围绕其中心旋转图像?

计算图像的新角度:

rot = (rot + rot_speed) % 360  

创建一个绕其中心旋转的新图像:

image = py.transform.rotate(image_orig, rot)  
rect = image.get_rect(center = (x, y)) 

并将旋转后的图像 blit

screen.blit(image, rect)  

当按下 AD 键时,新的方向分别设置为 rot_speed=.2rot_speed=-.2

完整的示例代码:

import pygame as py  

# define constants  
WIDTH = 500  
HEIGHT = 500  
FPS = 200

# define colors  
BLACK = (0 , 0 , 0)  
GREEN = (0 , 255 , 0)

# initialize pygame and create screen  
py.init()  
screen = py.display.set_mode((WIDTH , HEIGHT))  
# for setting FPS  
clock = py.time.Clock()  

rot = 0  
rot_speed = .2  

# define a surface (RECTANGLE)  
image_orig = py.Surface((1 , 100))  
# for making transparent background while rotating an image  
image_orig.set_colorkey(BLACK)  
# fill the rectangle / surface with green color  
image_orig.fill(GREEN)  
# creating a copy of orignal image for smooth rotation  
image = image_orig.copy()  
image.set_colorkey(BLACK)  
# define rect for placing the rectangle at the desired position  
rect = image.get_rect()
x, y = py.mouse.get_pos()
rect.center = (x, y)  
# keep rotating the rectangle until running is set to False

running = True  
while running:  

    x, y = py.mouse.get_pos()
    # set FPS  
    clock.tick(FPS)  
    # clear the screen every time before drawing new objects  
    screen.fill(BLACK)  
    # check for the exit  
    for event in py.event.get():  
        if event.type == py.QUIT:  
            running = False

    # rotating the orignal image
    keys = py.key.get_pressed()
    if keys[py.K_a]:
        rot_speed = .2 
    if keys[py.K_d]:
        rot_speed = -.2 

    # defining angle of the rotation  
    rot = (rot + rot_speed) % 360  
    # rotating the orignal image
    image = py.transform.rotate(image_orig, rot)  
    rect = image.get_rect(center = (x, y))  
    # drawing the rotated rectangle to the screen  
    screen.blit(image, rect)  
    # flipping the display after drawing everything  
    py.display.flip()

py.quit() 

1
看起来不错,谢谢你的帮助! - aTALLindian
抱歉,新用户 :) - aTALLindian
@aTALLindian 谢谢。不用谢。 - Rabbid76

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接