How to make an image in pygame stay still when rotated?(如何让PYGAME中的图像在旋转时保持静止?)
本文介绍了如何让PYGAME中的图像在旋转时保持静止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我一直在尝试让图像指向鼠标,这在某种程度上是有效的。图像指向鼠标,但它在四处移动。我不知道这是不是形象上的问题,但如果有任何帮助,我们将不胜感激。如果您不知道,方向是通过获取鼠标位置和图像位置之间的差值,通过atan函数运行它,除以6.28,然后乘以360来计算的。这将导致鼠标偏离图像的程度。代码是这样的。另外,我应该归功于图像的开发者,所以在这里。由mynamepong从www.flaticon.com
制作的图标import pygame
import math
pygame.init()
win_height=800
win_width=800
win=pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("Rotation Test")
black=(0,0,0)
carx=200
cary=200
clock=pygame.time.Clock()
car=pygame.image.load("inkscape images for games/car.png")
car=pygame.transform.scale(car,(100,100))
while True:
mouse=pygame.mouse.get_pos()
clock.tick(60)
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
angle=math.atan2(mouse[0]-carx,mouse[1]-cary)/6.28*360
win.fill(black)
car_rotated=pygame.transform.rotate(car,angle)
win.blit(car_rotated,(carx,cary))
pygame.display.update()
推荐答案
您需要设置汽车中心,然后围绕该点旋转汽车。
尝试此代码:
import pygame
import math
pygame.init()
win_height=800
win_width=800
win=pygame.display.set_mode((win_width,win_height))
pygame.display.set_caption("Rotation Test")
black=(0,0,0)
# center of car rect
carx=400
cary=400
clock=pygame.time.Clock()
car=pygame.image.load("inkscape images for games/car.png")
car=pygame.transform.scale(car,(100,100))
while True:
mouse=pygame.mouse.get_pos()
clock.tick(60)
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
angle=math.atan2(mouse[0]-carx,mouse[1]-cary)/6.28*360-90
win.fill(black)
car_rotated=pygame.transform.rotate(car,angle)
new_rect = car_rotated.get_rect(center = (carx, cary))
win.blit(car_rotated,(new_rect.x,new_rect.y))
pygame.display.update()
输出
这篇关于如何让PYGAME中的图像在旋转时保持静止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:如何让PYGAME中的图像在旋转时保持静止?
基础教程推荐
猜你喜欢
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
