- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 pygame 中使用 PS4 Controller ,我已经弄清楚如何捕获可以变化为 -1 或 1 的轴旋转,但我不知道如何将这些数字转换为色环 像比例一样将其转换为十六进制三元组数字。
模仿色环的值比任何东西都重要,因为我不希望操纵杆在不运动时捕获颜色。 Picture
(因为这有点令人困惑,本质上我希望能够移动操纵杆并根据其移动位置捕获准确的十六进制三元组数字)
这是我到目前为止的代码:
import pygame
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 62, 210, 255)
# This is a simple class that will help us print to the screen
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint:
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 25)
def print(self, screen, textString):
textBitmap = self.font.render(textString, True, BLACK)
screen.blit(textBitmap, [self.x, self.y])
self.y += self.line_height
def reset(self):
self.x = 25
self.y = 25
self.line_height = 30
def indent(self):
self.x += 10
def unindent(self):
self.x -= 10
pygame.init()
# Set the width and height of the screen [width,height]
size = [900, 1080]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("PS4Testing")
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Initialize the joysticks
pygame.joystick.init()
# Get ready to print
textPrint = TextPrint()
# -------- Main Program Loop -----------
while done==False:
# EVENT PROCESSING STEP
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
screen.fill(WHITE)
textPrint.reset()
# Get count of joysticks
joystick_count = pygame.joystick.get_count()
# For each joystick:
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
# Usually axis run in pairs, up/down for one, and left/right for
# the other.
axes = joystick.get_numaxes()
for i in range( axes ):
axis = joystick.get_axis( i )
textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
textPrint.unindent()
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 20 frames per second
clock.tick(20)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()
修改了 official pygame documentation 的代码
任何帮助将不胜感激
最佳答案
我的计划:
首先,我们需要找到操纵杆的角度。我们可以使用law of cosines来做到这一点和轴语句作为三角形边的长度(因为它们来自一个点/中心)。
在此 block 中存储轴语句:
for i in range( axes ):
axis = joystick.get_axis( i )
#Storing axis statement
if i == 0:
Xaxis = axis
elif i == 1:
Yaxis = axis
textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
我们存储它们是因为在 for 循环中我们一次只能获取一个语句。
然后定义一个函数来返回棒的角度。我们需要对 pi
和 acos
使用 math
模块。
def get_angle(Xaxis,Yaxis):
#To avoid ZeroDivisionError
#P.S. - you can improve it a bit.
if Xaxis == 0:
Xaxis = 0.001
if Yaxis == 0:
Yaxis = 0.001
#defining the third side of a triangle using the Pythagorean theorem
b = ((Xaxis)**2 + (Yaxis)**2)**0.5
c = Xaxis
a = Yaxis
#Using law of cosines we'll find angle using arccos of cos
#math.acos returns angles in radians, so we need to multiply it by 180/math.pi
angle = math.acos((b**2 + c**2 - a**2) / (2*b*c)) * 180/math.pi
#It'll fix angles to be in range of 0 to 360
if Yaxis > 0:
angle = 360 - angle
return angle
现在get_angle(Xaxis,Yaxis)
将返回摇杆的角度。东为 0°,北为 90°,西为 180°,南为 270°。
我们有棒的角度,我们可以用它来制作 HSV 颜色,然后将其转换为 RGB,因为色轮是基于颜色的色调。
色调与摇杆角度的范围相同,因此我们可以使用摇杆角度作为色调。
使用 Wikipedia 中的公式,定义一个将 HSV 转换为 RGB 的函数。
def hsv_to_rgb(H,S,V):
#0 <= H <= 360
#0 <= S <= 1
#0 <= V <= 1
C = V * S
h = H/60
X = C * (1 - abs(h % 2 -1))
#Yes, Python can compare like "0 <= 2 > 1"
if 0 <= h <= 1:
r = C; g = X; b = 0
elif 1 <= h <= 2:
r = X; g = C; b = 0
elif 2 <= h <= 3:
r = 0; g = C; b = X
elif 3 <= h <= 4:
r = 0; g = X; b = C
elif 4 <= h <= 5:
r = X; g = 0; b = C
elif 5 <= h < 6:
r = C; g = 0; b = X
m = V - C
#Final computing and converting from 0 - 1 to 0 - 255
R = int((r+m)*255)
G = int((g+m)*255)
B = int((b+m)*255)
return [R,G,B]
现在 hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
将返回 0 - 255 范围内的红色、绿色和蓝色列表(以便更轻松地进行十六进制转换)。在这种情况下,饱和度和值不是必需的。
最简单的部分。我们需要获取 RGB 列表并将值转换为十六进制。
colors = hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Converting to hex
lst = list(map(hex,colors))
#Cutting the "0x" part
for i in range(len(lst)):
lst[i] = lst[i][2:]
#If one of the colors has only one digit, extra 0 will be added for a better look
if len(lst[i]) == 1:
lst[i] = "0"+str(lst[i])
print(get_angle(Xaxis,Yaxis))
将打印诸如 #ff0000
、#00ff00
和 #0000ff
之类的内容。
您还可以使您的程序实时显示颜色变化,只需在此 block 中添加WHITE=colors
即可。 如果您患有光敏性癫痫,请勿使用它。
将第一个和第二个点的函数放在开头的某个位置。
添加从第一个点到 block 的存储
for i in range( axes ):
axis = joystick.get_axis( i )
textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
从 block 后的第三个点开始添加转换。我建议制作死亡区功能。
death_zone = 0.1
if abs(Xaxis) > death_zone or abs(Yaxis) > death_zone:
#If you prefer HSV color wheel, use hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Else if you prefer RGB color wheel, use hsv_to_rgb(360-get_angle(Xaxis,Yaxis),1,1)
colors = hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Converting to hex
lst = list(map(hex,colors))
#Cutting the "0x" part
for i in range(len(lst)):
lst[i] = lst[i][2:]
#If one of the colors has only one digit, extra 0 will be added for a better look
if len(lst[i]) == 1:
lst[i] = "0"+str(lst[i])
print("#"+"".join(lst))
这就是您的代码的样子:
附注- 您可能需要更改一些代码,因为我认为我的操纵杆轴没有正确捕获。
import pygame
import math
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 62, 210, 255)
def hsv_to_rgb(H,S,V):
#Accirding to https://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
#0 <= H <= 360
#0 <= S <= 1
#0 <= V <= 1
C = V * S
h = H/60
X = C * (1 - abs(h % 2 -1))
#Yes, Python can compare like "0 <= 2 > 1"
if 0 <= h <= 1:
r = C; g = X; b = 0
elif 1 <= h <= 2:
r = X; g = C; b = 0
elif 2 <= h <= 3:
r = 0; g = C; b = X
elif 3 <= h <= 4:
r = 0; g = X; b = C
elif 4 <= h <= 5:
r = X; g = 0; b = C
elif 5 <= h < 6:
r = C; g = 0; b = X
m = V - C
#Final computing and converting from 0 - 1 to 0 - 255
R = int((r+m)*255)
G = int((g+m)*255)
B = int((b+m)*255)
return [R,G,B]
def get_angle(Xaxis,Yaxis):
#To avoid ZeroDivisionError
#P.S. - you can improve it a bit.
if Xaxis == 0:
Xaxis = 0.001
if Yaxis == 0:
Yaxis = 0.001
#defining the third side of a triangle using the Pythagorean theorem
b = ((Xaxis)**2 + (Yaxis)**2)**0.5
c = Xaxis
a = Yaxis
#Using law of cosines we'll fing angle using arccos of cos
#math.acos returns angles in radians, so we need to multiply it by 180/math.pi
angle = math.acos((b**2 + c**2 - a**2) / (2*b*c)) * 180/math.pi
#It'll fix angles to be in range of 0 to 360
if Yaxis > 0:
angle = 360 - angle
return angle
# This is a simple class that will help us print to the screen
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint:
def __init__(self):
self.reset()
self.font = pygame.font.Font(None, 25)
def print(self, screen, textString):
textBitmap = self.font.render(textString, True, BLACK)
screen.blit(textBitmap, [self.x, self.y])
self.y += self.line_height
def reset(self):
self.x = 25
self.y = 25
self.line_height = 30
def indent(self):
self.x += 10
def unindent(self):
self.x -= 10
pygame.init()
# Set the width and height of the screen [width,height]
size = [900, 1080]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("PS4Testing")
#Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# Initialize the joysticks
pygame.joystick.init()
# Get ready to print
textPrint = TextPrint()
# -------- Main Program Loop -----------
while done==False:
# EVENT PROCESSING STEP
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
screen.fill(WHITE)
textPrint.reset()
# Get count of joysticks
joystick_count = pygame.joystick.get_count()
# For each joystick:
for i in range(joystick_count):
joystick = pygame.joystick.Joystick(i)
joystick.init()
# Usually axis run in pairs, up/down for one, and left/right for
# the other.
axes = joystick.get_numaxes()
for i in range( axes ):
axis = joystick.get_axis( i )
#Storing axis statement
if i == 0:
Xaxis = axis
elif i == 1:
Yaxis = axis
textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis) )
textPrint.unindent()
#If joystick is not in the center
#Death zone is used to not capture joystick if it's very close to the center
death_zone = 0.1
if abs(Xaxis) > death_zone or abs(Yaxis) > death_zone:
#If you prefer HSV color wheel, use hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Else if you prefer RGB color wheel, use hsv_to_rgb(360-get_angle(Xaxis,Yaxis),1,1)
colors = hsv_to_rgb(get_angle(Xaxis,Yaxis),1,1)
#Converting to hex
lst = list(map(hex,colors))
#Cutting the "0x" part
for i in range(len(lst)):
lst[i] = lst[i][2:]
#If one of the colors has only one digit, extra 0 will be added for a better look
if len(lst[i]) == 1:
lst[i] = "0"+str(lst[i])
print("#"+"".join(lst))
#You can use it to see color change in real time.
#But I don't recomend to use it if you have photosensitive epilepsy.
#WHITE = colors
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 20 frames per second
clock.tick(20)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit ()
关于python - 将操纵杆轴值转换为十六进制三元组代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49583332/
我有一个消息 static int[] message = { 0x01, 0x10, 0x00, 0x01, // port addres 01 - 08
如何将十进制转换为以下格式的十六进制(至少两位,零填充,不带 0x 前缀)? 输入:255 输出:ff 输入:2 输出:02 我尝试了 hex(int)[2:] 但它似乎显示了第一个示例而不是第二个示
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: Large numbers in Pascal (Delphi) 我正在尝试将 66 位值转换为十进制。 我注意到d
给定一个十进制数字列表,如何将每个数字转换为其等效的十六进制值,反之亦然? 例如: (convert2hex 255 64 64); ->(FF 40 40) (convert2dec FF 40 4
var color = Math.floor(Math.random() * 16777215).toString(16); var hex = Number.parseInt(col
我一直被教导 0-9 代表 0 到 9 的值,A、B、C、D、E、F 代表 10-15。 我看到这种格式 0x00000000,它不适合十六进制模式。有没有导游或导师可以解释一下? 我在谷歌上搜索了十
我目前正尝试像十六进制编辑器一样将文件读取为十六进制值。为了解释这个问题,让我们假设我有一个test.txt,里面有一个简单的“Hello world”。我正在尝试使用接近以下代码的程序以十六进制形式
我正在尝试获取元素背景颜色 $(document).ready(function(){ $.each('.log-widget',function(){ console.log($(t
0x40130020的十六进制值是 2.296883 的浮点值, 使用本网站 http://gregstoll.dyndns.org/~gregstoll/floattohex/ .这如何实现到 Lu
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,vis
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
谁能解释一下我们如何计算十六进制浮点常量的值。我在看书,发现0x0.3p10代表值192。 最佳答案 指数仍以十进制表示,但底数为二,尾数为十六进制。 所以 0.3P10 是 (3 × 16−1) ×
我正在尝试创建一个标签云,需要帮助来创建一个函数来计算应用于每个标签链接所需的颜色。 我有 3 个变量: 单个标签重要性(从 0.1 到 1) 最大(最重要)的标签颜色(十六进制代码,例如“fff00
大家好,我想发送尽可能短的字符串/值。如果我有以下内容 1)l23k43i221j44h55uui6n433bb4 2)124987359824369785493584379 3)kla^askdja
我知道你会写... background-color: #ff0000; ...如果你想要红色的东西。 你可以写... background-color: rgba(255, 0, 0, 0.5);
我有一些传递地理位置坐标的二进制数据流 - 纬度和经度。我需要找到它们编码的方法。 4adac812 = 74°26.2851' = 74.438085 2b6059f9 = 43°0.2763'
我想从 my_table 中选择 family,其中 family LIKE '%HEX(9D)' 家庭十六进制格式以 9D 十六进制结尾 我将excel文件转换为sqlite数据库但是 我的一些数据
我有一组二进制配置文件,每个文件有三个版本——每个文件的原始版本和两个不同修改的版本。我需要能够同时看到两个版本和原始版本之间的差异。 我需要的是一个二进制文件的三向差异工具。通过相当费力的谷歌搜索,
我正在尝试将(可变长度)十六进制字符串转换为带符号整数(我需要正值或负值)。 [Int16] [int 32]和[int64] 似乎可以在2,4+字节长的十六进制字符串上正常工作,但我在使用3个字节的
如何将十六进制的 unicode 写入 Facebook“您在想什么”框? 我尝试过写: \u00B9 "\u00B9" ¹ "¹" 到目前为止没有任何效果 (让我补充一下,我是在 M
我是一名优秀的程序员,十分优秀!