随机数数据可视化逻辑问题

时间:2018-10-14 03:52:21

标签: python pygame logic

因此,我最近在观看了很酷的Numberphile视频后了解了数据可视化,所以我想用随机数制作自己的小数据可视化。我的想法是,我有一个随机整数列表,并根据该数字绘制一个不同的彩色圆圈。彩色键在下面的代码中说明。

但是,我的代码似乎有逻辑问题。该代码运行时没有错误,但是根据我之前的描述,我的程序无法正常运行。首先,我的列表中有10个整数,但是程序上只绘制了6个。另外,着色有点淡。谢谢您的帮助。

import pygame
pygame.init()

## COLOR KEY
 # 0 - Black
 # 1 - White
 # 2 - Red
 # 3 - Orange
 # 4 - Yellow
 # 5 - Green
 # 6 - Blue
 # 7 - Indigo
 # 8 - Violet
 # 9 - dark blue

display_width = 1000
display_height = 500
display = pygame.display.set_mode((display_width,display_height))

black = 0,0,0
white = 255,255,255
red = 255,0,0
orange = 255,165,0
yellow = 255,255,0
green = 0,255,0
blue = 0,0,255
indigo = 29,0,51
violet = 128,0,128
darkblue = 0,200,0

random_list = [9,8,5,9,4,7,5,1,9,0]
def programLoop():
    exitProgram = False
    while not exitProgram:

        for evt in pygame.event.get():
            if evt.type == pygame.QUIT:
                pygame.quit()
                quit()

        startN = 100

        display.fill(white)
        for n in random_list:
            color = black
            if random_list[n] == 0:
                color = black

            if random_list[n] == 1:
                color = white

            if random_list[n] == 2:
                color = red

            if random_list[n] == 3:
                color = orange

            if random_list[n] == 4:
                color = yellow

            if random_list[n] == 5:
                color = green

            if random_list[n] == 6:
                color = blue

            if random_list[n] == 7:
                color = indigo

            if random_list[n] == 8:
                color = violet

            if random_list[n] == 9:
                color = darkblue

            pygame.draw.circle(display,color,[n * 50 + startN,250],10)

        pygame.display.update()

programLoop()

1 个答案:

答案 0 :(得分:2)

调试程序的第一步是打印n, color, n*50 + startN。您会看到,您正在random_list中的索引上进行迭代,而不是超出您期望的范围。因此,第一个数字是9,您选中if random_list[9] == 0,即True,它设置了color = black并显示在550。

您可以遍历范围for n in range(len(random_list)):来解决此问题。


我建议将颜色放入列表或字典中,然后在enumerated random_list上进行迭代。然后,您将同时获得数字n和颜色索引,并且可以通过以下方式访问颜色:color = colors[color_index]

更新的代码:

darkblue = 0,0,200  # Your darkblue was green.
# Put the colors into a list.
colors = [black, white, red, orange, yellow, green, blue, indigo, violet, darkblue]
random_list = [9,8,5,9,4,7,5,1,9,0]

def programLoop():
    clock = pygame.time.Clock()  # A clock to limit the frame rate.
    exitProgram = False
    while not exitProgram:
        for evt in pygame.event.get():
            if evt.type == pygame.QUIT:
                exitProgram = True

        startN = 100

        display.fill((200, 200, 200))
        # Now enumerate the list, so that you'll get n and the color index
        # at the same time.
        for n, color_index in enumerate(random_list):
            color = colors[color_index]
            pygame.draw.circle(display, color, [n*50 + startN, 250], 10)

        pygame.display.update()
        clock.tick(60)  # Limit the frame rate to 60 FPS.

programLoop()
pygame.quit()