nachbarn erkennung
This commit is contained in:
parent
b354553a78
commit
56957ce763
2 changed files with 51 additions and 3 deletions
31
a.py
Normal file
31
a.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
def finde_nachbarn(matrix, element):
|
||||
x, y = element
|
||||
nachbarn = []
|
||||
|
||||
for i in range(-1, 2):
|
||||
for j in range(-1, 2):
|
||||
nachbar_x = x + i
|
||||
nachbar_y = y + j
|
||||
# Clipping für den Rand der Matrix anwenden
|
||||
nachbar_x = max(0, min(nachbar_x, len(matrix) - 1))
|
||||
nachbar_y = max(0, min(nachbar_y, len(matrix[0]) - 1))
|
||||
if (i != 0 or j != 0): # Dies schließt das aktuelle Element aus
|
||||
nachbarn.append([nachbar_x, nachbar_y])
|
||||
|
||||
return nachbarn
|
||||
|
||||
|
||||
|
||||
# Beispiel Verwendung:
|
||||
matrix = [
|
||||
[1, 2, 3, 4, 5],
|
||||
[6, 7, 8, 9, 10],
|
||||
[11, 12, 13, 14, 15],
|
||||
[16, 17, 18, 19, 20],
|
||||
[21, 22, 23, 24, 25]
|
||||
]
|
||||
|
||||
|
||||
element = [0, 0] # Die Position 2,2 als Liste angegeben
|
||||
nachbarn = finde_nachbarn(matrix, element)
|
||||
print(nachbarn)
|
23
main.py
23
main.py
|
@ -36,6 +36,21 @@ def draw_feld(feld, color_key, block_size):
|
|||
)
|
||||
|
||||
|
||||
def finde_nachbarn(matrix, x, y):
|
||||
feldx, feldy = len(feld[0]), len(feld)
|
||||
|
||||
over = (y - 1) % feldy , x % feldx
|
||||
overr = (y - 1) % feldy , (x + 1) % feldx
|
||||
overl = (y - 1) % feldy , (x - 1) % feldx
|
||||
|
||||
r = y , (x + 1) % feldx
|
||||
l = y , (x - 1) % feldx
|
||||
|
||||
bellow = (y + 1) % feldy , x % feldx
|
||||
bellowr = (y + 1) % feldy , (x + 1) % feldx
|
||||
bellowl = (y + 1) % feldy , (x - 1) % feldx
|
||||
return [overl,over, overr, r, l, bellowl, bellow, bellowr]
|
||||
|
||||
def verarbeite_feld(feld):
|
||||
feldx, feldy = len(feld[0]), len(feld) # bekomme feld größe
|
||||
feld2 = copy.deepcopy(feld) # mache neue kopie
|
||||
|
@ -44,9 +59,10 @@ def verarbeite_feld(feld):
|
|||
poschar = feld[y][x]
|
||||
if poschar == "a": # fals aktuelles checkendes element = "a"
|
||||
# bekomme alle benötigten nachtbaren
|
||||
bellow = feld[(y + 1) % feldy][x]
|
||||
bellowr = feld2[(y + 1) % feldy][(x + 1) % feldx]
|
||||
bellowl = feld2[(y + 1) % feldy][(x - 1) % feldx]
|
||||
nachtbaren = finde_nachbarn(feld, x ,y)
|
||||
bellow = feld[nachtbaren[6][0]][nachtbaren[6][1]]
|
||||
bellowr = feld[nachtbaren[7][0]][nachtbaren[7][1]]
|
||||
bellowl = feld[nachtbaren[5][0]][nachtbaren[5][1]]
|
||||
direction = random.choice([-1, 1]) # Zufällige Auswahl der Richtung
|
||||
if bellow == " " and not bellow == "#": # checke unten
|
||||
feld2[y][x] = feld[(y + 1) % feldy][x]
|
||||
|
@ -61,6 +77,7 @@ def verarbeite_feld(feld):
|
|||
feld2[(y + 1) % feldy][(x + direction) % feldx] = feld[y][x]
|
||||
return feld2 # gib kopie zurück
|
||||
|
||||
## Funktion zum Verarbeiten des Feldes
|
||||
|
||||
def make_feld(size, mode):
|
||||
x = screensize[0] // size
|
||||
|
|
Loading…
Reference in a new issue