Python 如何在pygame中围绕轴心点旋转元素?
我试图在pygame中围绕一个轴心点旋转一个元素(在我的例子中是字体),我的代码如下:Python 如何在pygame中围绕轴心点旋转元素?,python,python-3.x,pygame,pygame-surface,pygame-tick,Python,Python 3.x,Pygame,Pygame Surface,Pygame Tick,我试图在pygame中围绕一个轴心点旋转一个元素(在我的例子中是字体),我的代码如下: import pygame, time pygame.init() display_width = 800 display_height = 800 window = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption("Dials")
import pygame, time
pygame.init()
display_width = 800
display_height = 800
window = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Dials")
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
clock = pygame.time.Clock()
font = pygame.font.SysFont('Arial', 50, True)
#All Images of Dials
tutorial = [pygame.image.load('Dial_Images/tutorial_base.png')]
tutorialInputs = [[1,4,3,4], [1,4,3,2], [1,4,3,2], [1,4,3,4]]
class Dial:
def __init__(self, x, y, radius, inputs):
self.x = x
self.y = y
self.radius = radius
self.inputs = inputs
self.columns = len(self.inputs)
self.rows = len(self.inputs[0])
def drawDial(self):
for i in range(0, self.columns):
pygame.draw.circle(window, red, (self.x, self.y), self.radius)
pygame.draw.circle(window, black, (self.x, self.y), self.radius, 1)
if self.rows == 4:
input_1 = font.render(str(self.inputs[0][0]), 1, black)
input_2 = font.render(str(self.inputs[0][1]), 1, black)
input_3 = font.render(str(self.inputs[0][2]), 1, black)
input_4 = font.render(str(self.inputs[0][3]), 1, black)
window.blit(input_1, (self.x - 4, self.y - self.radius + 10))
window.blit(input_2, (self.x + self.radius - 35, self.y - 15))
window.blit(input_3, (self.x - 4, self.y + self.radius - 40))
window.blit(input_4, (self.x - self.radius + 20, self.y - 15))
def level_1():
window.fill(white)
dial1.drawDial()
#all Dials Instantiated
dial1 = Dial(int(display_width/2), int(display_height/2), int((display_width/2) - 100), tutorialInputs)
score = 0
run = True
level1 = True
while run:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
clock.tick(100)
if level1:
level_1()
pygame.quit()
下面将创建一个窗口和一个带有黑色边框的圆圈,以及12点、3点、6点和9点的4个数字
我想围绕圆心旋转输入。任何允许我旋转输入的pygame函数
_围绕圆心旋转90度?我在pygame上看到了一些函数,比如pygame.math.vector和其他一些.rotate函数,但我想要最好的方法
此外,如果有一种方法可以清理我对输入位置进行编码的方式,以便它们在12点、3点、6点和9点对齐,这将很有帮助。仅在图像/曲面中保留文本,以便您可以使用pygame的功能将图像旋转到位(围绕图像中心) 然后从圆心开始,使用
半径
和角度
将其移动到圆的边界
angle = 0 # 90, 180, 270
x = dial_center_x
y = dial_center_y
x += radius * sin(radians(angle))
y += radius * cos(radians(angle))
使用pygame.Rect()
来保持位置和大小是很好的-它有.x
,.y
等属性,但也有.center
,.centerx
,,.centery
等属性,所以很容易在拨号圆上居中
image_rect = image.get_rect()
image_rect.center = dial_rect.center
然后您可以使用半径
和角度
将其移动到12点钟、3点钟、6点钟和9点钟的位置,甚至1点钟、2点钟等
angle = 0 # 90, 180, 270
image_rect.centerx += radius * sin(radians(angle))
image_rect.centery += radius * cos(radians(angle))
仅在图像/曲面中保留文本,以便您可以使用pygame的功能将图像旋转到位(围绕图像中心)
然后从圆心开始,使用半径
和角度
将其移动到圆的边界
angle = 0 # 90, 180, 270
x = dial_center_x
y = dial_center_y
x += radius * sin(radians(angle))
y += radius * cos(radians(angle))
使用pygame.Rect()
来保持位置和大小是很好的-它有.x
,.y
等属性,但也有.center
,.centerx
,,.centery
等属性,所以很容易在拨号圆上居中
image_rect = image.get_rect()
image_rect.center = dial_rect.center
然后您可以使用半径
和角度
将其移动到12点钟、3点钟、6点钟和9点钟的位置,甚至1点钟、2点钟等
angle = 0 # 90, 180, 270
image_rect.centerx += radius * sin(radians(angle))
image_rect.centery += radius * cos(radians(angle))
Pygame绘图不是由“元素”完成的,而是由形状完成的。像多边形和线这样的形状可以通过变换传递给它们的点来“旋转”,pygame中的旋转字体已经在这里讨论过了pygame绘图不是由“元素”完成的,而是由形状完成的。像多边形
和直线
这样的形状可以通过变换传递给它们的点来“旋转”,pygame中的旋转字体在这里已经讨论过了。我改变了一些东西,但它起了作用。我很难理解为什么使用text\u rect.centerx+=self.radius2*math.sin(angle\u rad)
和text\u rect.centerx+=self.radius2*math.sin(angle\u rad)
sin不应该代表y值,cos应该代表x值。我理解为什么你用角度_rad=180度来解释沿y轴负向移动向上的原因,但我试着用我的头脑来理解这个逻辑。。。无论如何,谢谢!我从不记得sin()
是否应该用于x
或y
——我将它用于x
,它起作用了,所以我没有更改它。顺便说一句:pygame使用与数学不同的坐标系-(0,0)
位于左上角nay
下面(0,0)的值为正值,而不是负值。在时钟中,12点钟有角度0
,但在数学中它可能有角度-90
或270
,因为3点钟会有角度0
我改变了一些东西,但它起作用了。我很难理解为什么使用text\u rect.centerx+=self.radius2*math.sin(angle\u rad)
和text\u rect.centerx+=self.radius2*math.sin(angle\u rad)
sin不应该代表y值,cos应该代表x值。我理解为什么你用角度_rad=180度来解释沿y轴负向移动向上的原因,但我试着用我的头脑来理解这个逻辑。。。无论如何,谢谢!我从不记得sin()
是否应该用于x
或y
——我将它用于x
,它起作用了,所以我没有更改它。顺便说一句:pygame使用与数学不同的坐标系-(0,0)
位于左上角nay
下面(0,0)的值为正值,而不是负值。在时钟中,12点钟具有角度0
,但在数学中,它可能具有角度-90
或270
,因为3点钟具有角度0