Home > Blockchain >  How to get one percent for 3 different values?
How to get one percent for 3 different values?

Time:09-09

I have this code and its supposed to be like a color game where you can edit different rgb values with asd and jkl. I want it to give points based on time and accuracy and stuff but I also want a meter to tell you how close you are to the given color. So if the color is 50,50,50, you should be able to see a percent value for how close you are. So if you are 51,51,51 it would be like 2% but if you are 255,255,255 its like 90%. Is this even possible? My current setup is percent = (r/50) (g/50) (b/50) ( assuming the color is 50,50,50) but it doesn't work at all. idk if what I'm asking is even feasible but it would be cool if someone could point me in the right direction! Thanks

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()

CodePudding user response:

Something like this will give you a metric by which you can measure the accuracy of a guess across all three colour channels.

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}%")

Note the 100- that turns the measure of inaccuracy into a measure of accuracy.

  • Related