删除列表项直到列表为空

时间:2016-07-09 00:02:26

标签: python loops random rect

我目前在屏幕上显示一个网格,一个区块正在填充。我希望每个块都填充一个随机颜色。因此,我希望在调用时从列表中删除coords_list(random_coord),以便我的pygame.draw.rect不会尝试在现有颜色的顶部打印新颜色。

当我将代码块从from itertools import product开始到pygame.draw.rect.............时发生错误:

  

coord_location = coords_list [random_coord] index超出范围


我的代码:

from itertools import product
coords_list = list(product(range(30,1170,30), range(30,870, 30)))

#random map co-ordinate
random_coord = random.randint(0,1065)
coord_location = coords_list[random_coord]
coord_loc_x = coord_location[0]
coord_loc_y = coord_location[1]
#Random color
random_choice = random.randint(0,2)
if random_choice == 0:
    terrain = blue
if random_choice == 1:
    terrain = green
if random_choice == 2:
    terrain = tan
##print(coord_location)
##random_terrain_type = terrain_types(random_choice)
pygame.draw.rect(screen, terrain, (coord_loc_x,coord_loc_y,30,30), 0)

line_start = 30
line_end = 30
top_line_start = 30
bottom_line_end = 30
for i in range(29):
    pygame.draw.line(screen, black, (30, line_start), (1170, line_end),1)
    line_start += 30
    line_end += 30
for i in range(39):
    pygame.draw.line(screen, black, (top_line_start, 30),     (bottom_line_end, 870), 1)
    top_line_start += 30
    bottom_line_end += 30

2 个答案:

答案 0 :(得分:1)

问题是1065超出范围索引

>>> coords_list = list(product(range(30,1170,30), range(30,870, 30)))
>>> len(coords_list)
1064

这意味着最大索引是1063(python列表索引从0开始)

你的随机索引函数应该是

random.randint(0,1063)

或者,如果您不想对数字进行硬编码,random库也有

random.choice(coords_list)

无论长度如何,都会从列表中选择一个随机项

答案 1 :(得分:0)

我看到两种解决方法。主要问题是这段代码:

random_coord = random.randint(0,1065)
coord_location = coords_list[random_coord]
coord_loc_x = coord_location[0]
coord_loc_y = coord_location[1]

方式一不依赖于以随机顺序填充的正方形。我们假设每个方块都需要填充,所以我们只需按随机颜色填充它们:

for coords in coords_list:
  coord_location = coords
  coord_loc_x = coord_location[0]
  coord_loc_y = coord_location[1]
  #Random color
  random_choice = random.randint(0,2)
  if random_choice == 0:
    terrain = blue
  if random_choice == 1:
    terrain = green
  if random_choice == 2:
    terrain = tan
  ##print(coord_location)
  ##random_terrain_type = terrain_types(random_choice)
  pygame.draw.rect(screen, terrain, (coord_loc_x,coord_loc_y,30,30), 0)

  line_start = 30
  line_end = 30
  top_line_start = 30
  bottom_line_end = 30
  for i in range(29):
    pygame.draw.line(screen, black, (30, line_start), (1170, line_end),1)
    line_start += 30
    line_end += 30
  for i in range(39):
    pygame.draw.line(screen, black, (top_line_start, 30),     (bottom_line_end, 870), 1)
    top_line_start += 30
    bottom_line_end += 30

方式二将以随机颜色随机填充正方形:

import random
while len(coords_list) > 0:
  coord_location = random.choice(coords_list)
  coords_list.remove(coord_location)
  coord_loc_x = coord_location[0]
  coord_loc_y = coord_location[1]
  #Random color
  random_choice = random.randint(0,2)
  if random_choice == 0:
    terrain = blue
  if random_choice == 1:
    terrain = green
  if random_choice == 2:
    terrain = tan
##print(coord_location)
##random_terrain_type = terrain_types(random_choice)
  pygame.draw.rect(screen, terrain, (coord_loc_x,coord_loc_y,30,30), 0)

  line_start = 30
  line_end = 30
  top_line_start = 30
  bottom_line_end = 30
  for i in range(29):
    pygame.draw.line(screen, black, (30, line_start), (1170, line_end),1)
    line_start += 30
    line_end += 30
  for i in range(39):
    pygame.draw.line(screen, black, (top_line_start, 30),     (bottom_line_end, 870), 1)
    top_line_start += 30
    bottom_line_end += 30