python-3.x 3个不同的值如何得到1%?

7uhlpewt  于 2023-02-17  发布在  Python
关注(0)|答案(1)|浏览(228)

我有这个代码,它应该是一个颜色选择游戏,你可以编辑不同的rgb值与键:“asd”和“jkl”。我希望它给予点的基础上,时间和准确性和东西,但我也希望一个米告诉你你有多接近给定的颜色。所以,如果颜色是50,50,50,你应该能够看到一个百分比值,你有多接近。所以,如果颜色是51,51,51,它会像98%,但如果颜色是255,255,255,它像10%。这甚至可能吗?我目前的设置是percent = (r/50)+(g/50)+(b/50)(假设颜色是50,50,50),但它根本不工作。

import pygame,sys,time,random
pygame.init()

playerx = 0
playery = 0
sizex=500
sizey=200

r = 255
g = 0
b = 0
color = (r,g,b)
speed = 1
sleep=0.01
col1=random.randint(0,255)
col2=random.randint(0,255)
col3=random.randint(0,255)
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Hello World")
pygame.draw.rect(win, (50,50,50), pygame.Rect(0, 300, sizex, sizey))#do the culoro with col1 and stuff

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                
                percent = (r/50)+(g/50)+(b/50)
                print(percent)
        #if event.type == pygame.KEYDOWN:


    #win.fill((255,255,255))
    
    pygame.draw.rect(win, color, pygame.Rect(playerx, playery, sizex, sizey))

    #pygame.draw.rect(win, (0,255,255), pygame.Rect(playerx, playery, sizex/5, sizey/5))
    keys = pygame.key.get_pressed()
   

    



    
        
    if  r  +speed <=255 and keys[pygame.K_a]:
        #print("this should be working")
        r+=speed
        color=(r,g,b)
        time.sleep(sleep)
    if keys[pygame.K_s] and g + speed <=255:
        g+=speed
        color=(r,g,b)
        time.sleep(sleep)
    if keys[pygame.K_d] and b +speed<=255:
        b+=speed
        color=(r,g,b)
        time.sleep(sleep)
    if keys[pygame.K_j] and r  - speed >=0:
        r-=speed
        color=(r,g,b)
        time.sleep(sleep)
    if keys[pygame.K_k]and g -speed >=0:
        g-=speed
        color=(r,g,b)
        time.sleep(sleep)
    if keys[pygame.K_l] and b -speed>=0:
        b-=speed
        color=(r,g,b)
        time.sleep(sleep)
    

    

    #time.sleep(0.2)
    pygame.display.update()
jtw3ybtb

jtw3ybtb1#

类似这样的东西会给予你一个度量,你可以通过它来衡量一个猜测在所有三个颜色通道上的准确性。

guess = [50, 36, 120]
answer = [83, 21, 187]

accuracy = 0
for channel in range(3):
   accuracy += abs(answer[channel]-guess[channel])
accuracy /= 3*2.55

print(f"Accuracy: {100-accuracy}%")

注意100-,它将不准确性度量转化为准确性度量。

相关问题