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

标签 python pygame

这个问题在这里已经有了答案:





How do I rotate an image around its center using Pygame?

(6 个回答)


去年关闭。




到目前为止,我已经制作了一个矩形,它跟随用户的鼠标并用 a 逆时针旋转。键和顺时针方向用 d key 。

目前矩形跟随鼠标,但一旦用户开始旋转矩形,矩形就会变得非常滞后和变形。我需要帮助保持矩形不变和恒定的 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()  

最佳答案

主要问题是您不断旋转和操纵原始图像。这会导致图像失真。见 How do I rotate an image around its center using 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)  

当按下 A 或 D 时,新方向由 rot_speed = .2 设置分别rot_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() 

关于python - Pygame:如何根据输入顺时针/逆时针旋转矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60416902/

相关文章:

python 属性错误 : module 'pygame' has no attribute 'display'

python - 需要帮助在 PyGame 中使用 for 循环加载图像

python - Pygame 贪吃蛇游戏

python - 用户需要移动鼠标来发射子弹

python - 球员运动

python - numpy 结构化数组中的子数组不连续

python - 将属性 getter 用作类的常量变量是个好主意吗

python - 根据梯度符号为数据分配颜色 (Python)

python - 任意维度的numpy数组切片

python - Python脚本的正确shebang