Python模拟练习TypeError:不可用类型:'list'

时间:2015-07-31 12:27:09

标签: python

我在互联网上找到了源代码,并且正在改变它的一部分作为练习和学习(我不是故意做任何Plaigirism或其他东西)。问题发生在方法_generate_empty_noise_grid(self, map_width, map_height)中。

我想在0范围内创建1y < 20 s的随机序列:但我的源代码不起作用。

import pygame
import random
import math
import time


class MapGrid():
    def __init__(self, map_width, map_height):

        # set map values
        self.map_width = map_width
        self.map_height = map_width

        # generate outside rooms
        self.outside_terrain_grid = self._generate_empty_noise_grid(self.map_width, self.map_height)

    def _generate_empty_noise_grid(self, map_width, map_height):

        new_map_grid = [] # create our new list
        for x in range(map_width):
            new_map_grid.append([]) # add our columns to the array
            for y in range(map_height):
                if y > 20: 
                    new_map_grid[x].append(random.choice([0,1])) # fill in our rows
                else: 
                    new_map_grid[x].append([0])


        return new_map_grid

    def _generate_outside_terrain(self, empty_outside_terrain_grid, number_of_generations):
        '''
        creates a bubble effect with cellular automaton
        '''
        grid = empty_outside_terrain_grid
        number_of_generations = number_of_generations

        for x in range(number_of_generations):
            next_grid = []
            for column_index, column in enumerate(grid):
                next_column = []
                next_grid.append(next_column)
                for tile_index, tile in enumerate(column):

                    top_left = grid[column_index - 1][tile_index - 1]
                    top_mid = grid[column_index][tile_index - 1]

                    if top_mid == 1: 
                        next_cell = 1 
                    elif top_mid == 0: 
                        next_cell = 0 

                    next_column.append(next_cell)

            grid = next_grid

        return next_grid




if __name__ == '__main__':
    # general map stats
    map_width = 140
    map_height = 30

    # start with one generation
    tile_size = 8

    map_grid = MapGrid(map_width, map_height)
    #print map_grid.outside_terrain_grid

    pygame.init()

    screen = pygame.display.set_mode((map_width * tile_size,map_height * tile_size))

    one_tile = pygame.Surface((1, 1))
    one_tile.fill((0,0,0))
    zero_tile = pygame.Surface((1, 1))
    zero_tile.fill((255,255,255))
    colors = {0: zero_tile, 1: one_tile}

    background = pygame.Surface((map_width * tile_size,map_height * tile_size))

    clock = pygame.time.Clock()

    first_gen = True
    timer = 12

    running = True
    while running == True:
        clock.tick(3)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        if first_gen:
            themap = map_grid.outside_terrain_grid
        else:
            themap = map_grid._generate_outside_terrain(themap, 1)

        for column_index, column in enumerate(themap):
            for tile_index, tile in enumerate(column):
                screen.blit(colors[tile], (tile_index * tile_size, column_index * tile_size))

        pygame.display.flip()

        if first_gen:
            timer -= 1
            if timer < 0:
                first_gen = False

    pygame.quit()

2 个答案:

答案 0 :(得分:1)

您的地图形式不正确。第一行的前几个单元格如下所示:

[[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]...

但它应该是这样的:

[0, 0, 0, 0, 0, 0, 0, 0, 0....

问题出在_generate_empty_noise_grid。您应该在添加整数时添加列表。

else: 
    new_map_grid[x].append([0])

这应该是

else: 
    new_map_grid[x].append(0)

答案 1 :(得分:-1)

不确定我是否理解你的问题,但你说你想要:&#34;我想要做的是创建0和1的随机序列,仅在y <1的范围内。 20&#34;

但是在我的代码中我找到了:

for y in range(map_height):
    if y > 20: 
        new_map_grid[x].append(random.choice([0,1])) # fill in our row
    else: 
        new_map_grid[x].append([0])

所以,如果我不误解一切,那不应该是y&lt; 20?

因此:

for y in range(map_height):
    if y < 20: 
        new_map_grid[x].append(random.choice([0,1])) # fill in our row
    else: 
        new_map_grid[x].append([0])

干杯