我正在进行模拟,该模拟显示整个组如何在不同速度的物体移动时移动。下面是一个引用,我不知道这里有什么问题。
Traceback (most recent call last): File "C:\Users\JeeWoo Kim\Desktop\practice2.py", line 98, in <module>
map_grid = MapGrid(map_width, map_height)
File "C:\Users\JeeWoo Kim\Desktop\practice2.py", line 16, in __init__
self.outside_terrain_grid = self._generate_empty_noise_grid(self.map_width, self.map_height)
File "C:\Users\JeeWoo Kim\Desktop\practice2.py", line 49, in
_generate_empty_noise_grid
new_map_grid[x][y] += _individual_decision(self, sum_velocity, number_count)
NameError: name '_individual_decision' is not defined
代码 -
import pygame
import random
import math
import time
import numpy
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,2])) # fill in our rows
else:
new_map_grid[x].append(0)
number_count = 0
sum_velocity = 0
for y in range(map_height):
if new_map_grid[x][y] != 0:
number_count +=1
sum_velocity += new_map_grid[x][y]
new_map_grid[x][y] += _individual_decision(self, sum_velocity, number_count)
return new_map_grid
def _individual_decision(self, sum_velocity, number_count):
mu = 1.5 # average
sigma = 0.125 # standard deviation
average_velocity = sum_velocity / number_count
critical_number =np.random.normal(mu, sigma)
if average_velocity - critical_number < 0:
return 1
elif average_velocity - critical_number >= 0:
return 2
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_mid = grid[column_index][tile_index - 1]
mid = grid[column_index][tile_index]
d_top_mid = grid[column_index][tile_index + 1] % 2
d_mid = grid[column_index][tile_index] % 2
v_top_mid = math.floor((grid[column_index][tile_index+1] + 1) / 2)
v_mid = math.floor((grid[column_index][tile_index] + 1) / 2)
if d_mid <= v_top_mid:
grid[column_index][tile_index] == d_mid * 2 # vi = di
next_column+v_mid.append(grid[column_index][tile_index])
elif d_mid > v_top_mid:
if numpy.random.randint(0,2) == 1:
next_grid+1[next_column.append(grid[column_index][tile_index])]
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))
zero_tile = pygame.Surface((1, 1))
zero_tile.fill((0,0,0))
one_tile = pygame.Surface((1, 1))
one_tile.fill((255,255,255))
two_tile = pygame.Surface((1,1))
two_tile.fill((255,0,0))
three_tile = pygame.Surface((1,1))
three_tile.fill((0,0,204))
four_tile = pygame.Surface((1,1))
four_tile.fill((0,255,0))
colors = {0: zero_tile, 1: one_tile, 2:two_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()
答案 0 :(得分:0)
_individual_decision()
是一种实例方法,您应将其称为 - self._individual_decision()
。代码 -
new_map_grid[x][y] += self._individual_decision(sum_velocity, number_count)
此外,你不应该发送self
作为第一个论点,只是发送其他论点。