vendredi 8 juillet 2016

Removing a list item until the list is empty

I currently have a grid displaying on my screen and one block being filled. I would like every block to be filled with a random color. So I would like to remove the coords_list(random_coord) from the list when its called so my pygame.draw.rect doesn't try to print a new color over top of an existing one.

An error occurs when I put the block of code starting from from itertools import product to pygame.draw.rect.............:

coord_location = coords_list[random_coord] index is out of range


My code:

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




Aucun commentaire:

Enregistrer un commentaire