微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

皮游戏以度为单位获取车轮角度

如何解决皮游戏以度为单位获取车轮角度

我正在使用 pygame 从我的赛车方向盘获取输入数据。有一个名为 get_axis方法,它返回一个角度作为从 -1 到 1 的浮点数(其中 -1 对应于车轮向左旋转的最大角度,而 1 对应于右侧(不是弧度))。我想将其作为旋转角度(以度为单位)。

import pygame

WIDTH = 360
HEIGHT = 480
FPS = 50
BLACK = (0,0)

pygame.init()
pygame.joystick.init()
joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]

j = joysticks[0]    
j.init()

pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

running = True
wheelRange = ... # need to get range racing wheel

while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    i = 0
    print("value: " + str(j.get_axis(i) * wheelRange) + " deg") #print current wheel position

    screen.fill(BLACK)
    pygame.display.flip()

pygame.quit()

解决方法

import pygame
from sys import exit as _exit

#handles events 
def handleEvents(events):
    exitGame = False
    for event in events:
        if event.type == pygame.QUIT:
            pg_window.close()


def write(display:pygame.display,text:str,pos:tuple,size:int,color:tuple):
    font = pygame.font.Font(pygame.font.get_default_font(),size)
    surf = font.render(f"{text}",True,(color[0],color[1],color[2]))
    display.blit(surf,(pos[0],pos[1]))


class PG_Widnow_UI:
    def __init__(self,width,height):
        pygame.init()
        self.widht = width
        self.height = height
        self.window = pygame.display.set_mode((width,height))

    def update(self):
        pygame.display.flip()

    def clear(self,r,g,b):
        self.window.fill((r,b))

    def close(self):
        pygame.quit()
        _exit()


windowWidth = 1000
windowHeight = 50
pg_window = PG_Widnow_UI(windowWidth,windowHeight)

while True:
    pg_window.clear(255,255,255)

    events = pygame.event.get()
    handleEvents(events)

    mousePos = pygame.mouse.get_pos()

    one_to_minus_one = (mousePos[0] / (windowWidth/2))-1
    in_degrees = ((one_to_minus_one + 1) * 360)/2

    write(pg_window.window,f"steering wheel: {one_to_minus_one}",(0,0),15,(255,0))
    write(pg_window.window,f"in degrees: {in_degrees}",20),0))

    pg_window.update()

所以总结一下,你可以做如下转换

in_degrees = ((one_to_minus_one + 1) * 360)/2

如果方向盘总共转动 900 度,那么您可以乘以 900。

in_degrees = ((one_to_minus_one + 1) * 900)/2

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。