gpt4 book ai didi

python - Pygame - Python - 立方体上的 360 度角,光标/指针/鼠标方向

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:46 37 4
gpt4 key购买 nike

我正在尝试解决这个角度问题,我注意到坐标中有两个 0,0,也许这就是阻止立方体进行 360 度旋转的原因,请遵循视频和代码。

有人可以帮助我吗?

video here

import pygame
import sys
import os
import math

def main():
pygame.init()

clock = pygame.time.Clock()
screen = pygame.display.set_mode([1000, 500])
pygame.display.set_caption('Example 1')

player = pygame.image.load(os.path.join('img', 'rect.png')).convert_alpha() #path to cube ./img/rect.png

pygame.font.init()

font = pygame.font.get_default_font()
font_angle = pygame.font.SysFont(font, 44, True)

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


clock.tick(60)
screen.fill((255, 255, 255)) #screen background color

player_rect = player.get_rect() #player rect (for center position)

mouse_x, mouse_y = pygame.mouse.get_pos() #mouse position (x, y)

#to define angle - start
hypo = math.sqrt(math.pow(mouse_x - (player_rect[0] + player_rect.centerx), 2) +
math.pow(mouse_y - (player_rect[1] + player_rect.centery), 2))

cos = (mouse_x - (player_rect[0] + player_rect.centerx)) / hypo
sin = (mouse_y - (player_rect[1] + player_rect.centery)) / hypo

angle = (180 / math.pi) * - math.atan2(sin, cos)
#end

newplayer = pygame.transform.rotate(player, angle) #rotate cube


screen.blit(newplayer, [300, 100]) #show cube in screen

text = font_angle.render(str("%.2f" % angle), 1, (255, 0, 0)) #show angle in mouse position

screen.blit(text, ((mouse_x+20), mouse_y)) #show text

pygame.display.update() #update frames

main()

最佳答案

您只需从 mouse_x 位置中减去 player_rect.centerx 位置(与 y 相同)并将其传递给 math.atan2:

dist_x = mouse_x - player_rect.centerx
dist_y = mouse_y - player_rect.centery
angle = -math.degrees(math.atan2(dist_y, dist_x))

在 while 循环开始之前创建矩形并传递所需的中心坐标。图像旋转后,使用 get_rect 创建一个新的矩形,并传递前一个矩形的中心坐标以使其保持居中。并在 player_rect 处位 block 传输图像(这意味着在其 topleft 坐标处)。

import pygame
import sys
import os
import math


def main():
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode([1000, 500])

# player = pygame.image.load(os.path.join('img', 'rect.png')).convert_alpha() #path to cube ./img/rect.png
player = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(player, (100, 200, 255), [(0, 0), (50, 15), (0, 30)])
player_rect = player.get_rect(center=(500, 250)) # player rect (for center position)

font = pygame.font.get_default_font()
font_angle = pygame.font.SysFont(font, 44, True)

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

clock.tick(60)
screen.fill((255, 255, 255))
mouse_x, mouse_y = pygame.mouse.get_pos()
dist_x = mouse_x - player_rect.centerx
dist_y = mouse_y - player_rect.centery
angle = -math.degrees(math.atan2(dist_y, dist_x))

newplayer = pygame.transform.rotate(player, angle)
# Create a new rect and pass the center of the old rect.
player_rect = newplayer.get_rect(center=player_rect.center)

screen.blit(newplayer, player_rect) # Blit it at the player_rect.
text = font_angle.render(str("%.2f" % angle), 1, (255, 0, 0))
screen.blit(text, ((mouse_x+20), mouse_y))
pygame.display.update()

main()

关于python - Pygame - Python - 立方体上的 360 度角,光标/指针/鼠标方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53796277/

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