Browse Source

lesson23 start

lex 1 year ago
parent
commit
774d995c56

+ 0 - 0
python_technology_stack/Lesson23/clouds.py


+ 0 - 0
python_technology_stack/Lesson23/exporter.py


+ 0 - 0
python_technology_stack/Lesson23/fires.py


+ 7 - 0
python_technology_stack/Lesson23/helicopter.py

@@ -0,0 +1,7 @@
+from utils import randcell
+
+
+class Helicopter:
+    def __init__(self, w, h):
+        rc = randcell(w, h)
+        rx, ry = rc[0], rc[1]

+ 0 - 0
python_technology_stack/Lesson23/importer.py


+ 30 - 0
python_technology_stack/Lesson23/main.py

@@ -0,0 +1,30 @@
+# 🎄 🌊 🚁 🟩 🔥 🏥 💛 🧺 🛍️ ☁️⚡ 🏆 ⬛
+import os
+import time
+from map import Map
+from helicopter import Helicopter as Helico
+
+TICK_SLEEP = 0.05
+TREE_UPDATE = 50
+FIRE_UPDATE = 100
+MAP_W, MAP_H = 20, 10
+
+field = Map(MAP_W, MAP_H)
+field.generate_forest(3, 10)
+field.generate_rivers(10)
+field.generate_rivers(10)
+
+helico = Helico(MAP_W, MAP_H)
+
+tick = 1
+
+while True:
+    os.system("clear")  #cls
+    print("TICK", tick)
+    field.print_map(helico)
+    tick += 1
+    time.sleep(TICK_SLEEP)
+    if (tick % TREE_UPDATE == 0):
+        field.generate_tree()
+    if (tick % FIRE_UPDATE == 0):
+        field.update_fires()

+ 72 - 0
python_technology_stack/Lesson23/map.py

@@ -0,0 +1,72 @@
+from utils import randbool, randcell, randcell2
+
+# 0 - поле
+# 1 - дерево
+# 2 - река
+# 3 - госпиталь
+# 4 - апгрейд-шоп
+# 5 - огня
+
+CELL_TYPES = '🟩🎄🌊🏥🛍️'
+
+
+class Map:
+    def __init__(self, w, h):
+        self.cells = [[0 for i in range(w)] for j in range(h)]
+
+    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 print_map(self, helico):
+        print('⬛' * (self.w + 2))
+        for ri in self.cells:
+            print('⬛', end="")
+            for ci in range(self.w):
+                cell = self.cells[ri][ci]
+                if (helico.x == ri and helico.y == ci):
+                    print('🚁', end="")
+                elif (cell >= 0 and cell < len(CELL_TYPES)):
+                    print(CELL_TYPES[cell], end="")
+                print('⬛')
+        print('⬛' * (self.w + 2))
+
+    def generate_rivers(self, l):
+        rc = randcell(self.w, self.h)
+        rx, ry = rc[0], rc[1]
+        self.cells[rx][ry] = 2
+        while l > 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 self.h:
+            for ci in 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 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(5):
+            self.add_fire()

+ 19 - 0
python_technology_stack/Lesson23/utils.py

@@ -0,0 +1,19 @@
+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)
+    th = rand(0, h)
+    return (th, tw)
+
+#
+def randcell2(x, y, w, h):
+    moves = [(-1, 0), (0, 1), (1, 0), 0, -1]
+    t = rand(0, 3)
+    dx, dy = moves[t][0], moves[t][1]
+    return (x + dx, y + dy)
+