gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:11:05 25 4
gpt4 key购买 nike

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





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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com