Выдает ошибку хотя все вроде написано правильно
это map
from utils import randbool
from utils import randcell
from utils import randcell2
from helicopter import Helicopter
CELL_TYPES = "??????"
#
class Map:
def generate_river(self, l):
rc = randcell(self.w, self.h)
rx, ry = rc[0], rc[1]
self.cells[rc[0]][rc[1]] = 2
while 1 > 0:
rc2 = randcell2 (rx, ry)
rx2, ry2 = rc2[0], rc2[1]
if (self.check_bounds(rx2, ry2)):
self.cells[rx2][ry2] = 2
rx, ry = rx2, ry2
l -= 1
def generate_forest(self, r, mxr):
for ri in range(self.h):
for ci in range(self.w):
if randbool(r, mxr):
self.cells[ri][ci] = 1
def generate_tree(self):
c = randcell(self.w, self.h)
cx, cy = c[0], c[1]
if(self.check_bounds(cx, cy) and self.cells[cx][cy == 0]):
self.cells[cx][cy] = 1
def print_map(self, helico): #Для чего функция?
print('⬜️' * (self.w + 2))
for ri in range(self.h):
print('⬜️', end="")
for ci in range(self.w):
cell = self.cells[ri][ci]
if(helico.x == ri and helico.y == ci):
print('8_2', end='')
if(cell >= 0 and cell < len(CELL_TYPES)):
print(CELL_TYPES[cell], end="")
print( '⬜️')
print('⬜️' * (self.w + 2))
def add_fire(self):
c = randcell(self.w, self.h)
cx, cy = c[0],c [1]
if self.cells[cx][cy] == 1:
self.cells[cx][cy] = 5
def update_fires(self):
for ri in range (self.h):
for ci in range(self.w):
cell = self.cells[ri][ci]
if cell == 5:
self.cells[ri][ci] = 0
for i in range(10):
self.add_fire
def check_bounds(self, x, y):
if(x < 0 or y < 0 or x >= self.h or y >= self.w):
return False
return True
def __init__(self, w, h):
self.w = w
self.h = h
self.cells = [[0 for i in range(w)] for j in range (h)]
tmp = Map(30, 10)
tmp.generate_forest(3, 10)
tmp.generate_river(11)
tmp.print_map()
это main
from map import Map
import time
import os
from helicopter import Helicopter as Helico
TICK_SLEEP = 1
TREE_UPDATE = 50
FIRE_UPDATE = 100
MAP_W, MAP_H = 30, 10
field = Map(MAP_W, MAP_H)
field.generate_forest(3, 10)
field.generate_river(11)
field.add_fire()
field.add_fire()
field.add_fire()
field.add_fire()
helico = Helico(MAP_W, MAP_H)
tick = 1
while True:
os.system('cls')
field.print_map(helico)
print('Tick', tick)
field.print_map()
tick += 1
tick.sleep(TICK_SLEEP)
if (tick % TREE_UPDATE == 0):
field.generate_tree()
if (tick % FIRE_UPDATE == 0):
field.update_fires
это utils
from random import randint as rand
def randbool(r, mxr):
t = rand(0, mxr)
return (t <= r)
def randcell(w,h):
tw = rand(0, w - 1)
th = rand(0, h - 1)
return(th, tw)
def randcell2(x, y):
moves = [(-1, 0), (0, 1), (1, 0), (0, -1)]
t = rand(0, 4)
dx, dy = moves[t][0], moves[t][1]
return (x + dx, y + dy)
это helicopter
from utils import randcell
class Helicopter:
def __init__(self, w, h):
rc = randcell(w, h)
rx, ry = rc[0], rc[1]
self.x = rc
self.y = ry
Это все коды которые нужны они каждые в своей рабочей области но ошибки выдает не пойму почему вроде все правильно вот я запускаю map и выдает мне Traceback (most recent call last):
tmp.generate_river(11)
File "c:\Users\semon\launch.json\map.py", line 15, in generate_river
rc2 = randcell2 (rx, ry)
^^^^^^^^^^^^^^^^^^
File "c:\Users\semon\launch.json\utils.py", line 15, in randcell2
dx, dy = moves[t][0], moves[t][1]
~~~~~^^^
IndexError: list index out of range


Ответы (1 шт):
Автор решения: CrazyElf
→ Ссылка
from random import randint as rand
...
def randcell2(x, y):
moves = [(-1, 0), (0, 1), (1, 0), (0, -1)]
t = rand(0, 4)
dx, dy = moves[t][0], moves[t][1]
~~~~~^^^
IndexError: list index out of range
Смотрим документацию:
random.randint(a, b)
Return a random integer N such that a <= N <= b.
То есть randint вам выдаёт значения от 0 до 4 включительно. А у вас всего 4 элемента в списке - с индексами от 0 до 3, индекс 4 вызовет ошибку, которую вы и видите.