From b921d21050532bc60a1696aa9fd13b04036f6155 Mon Sep 17 00:00:00 2001 From: juhn Date: Thu, 17 Jul 2025 10:38:57 +0200 Subject: [PATCH] initial c --- cell.py | 6 ++++++ cgol.py | 40 ++++++++++++++++++++++++++++++++++++++++ game.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 cell.py create mode 100644 cgol.py create mode 100644 game.py diff --git a/cell.py b/cell.py new file mode 100644 index 0000000..6ca3ad3 --- /dev/null +++ b/cell.py @@ -0,0 +1,6 @@ +from gamegrid import * + +class Cell(Actor): + def __init__(self): + Actor.__init__(self, "/home/prenski/Informatik/Informatik 12/12b/vitus/OOP-Spiel/Bilder/wurm.png") + self.alive = False \ No newline at end of file diff --git a/cgol.py b/cgol.py new file mode 100644 index 0000000..b524757 --- /dev/null +++ b/cgol.py @@ -0,0 +1,40 @@ +from gamegrid import * +from cell import Cell + +class CGOL(Actor): + def __init__(self, game_list): # Pass game_list as an argument here + self.game_list = game_list + + def calculate_next_gen(self, cells): + next_gen = [] + for y in range(len(cells)): + next_gen.append([]) + for x in range(len(cells[y])): + check_cell = cells[y][x] + alive_neighbors = 0 + for ry in range(y-1, y+2, 1): + for rx in range(x-1, x+2, 1): + if ry < 0 or rx < 0 or ry == y and rx == x or rx > len(cells[y])-1 or ry > len(cells)-1: + pass + else: + alive_neighbors += cells[ry][rx] + if alive_neighbors > 3 or alive_neighbors < 2: + next_gen[y].append(0) + elif alive_neighbors == 3: + next_gen[y].append(1) + else: + next_gen[y].append(cells[y][x]) + return next_gen + + def act(self): + self.game_list = calculate_next_gen(self.game_list) + for y in range(0,20): + for x in range(0,20): + cell = getOneActorAt(Location(x,y), Cell) + if cell != None: + removeActor(cell) + + for y in range(0,20): + for x in range(0,20): + if self.game_list[y][x] == 1: + addActor(Cell(),Location(x,y)) diff --git a/game.py b/game.py new file mode 100644 index 0000000..9a7a1d0 --- /dev/null +++ b/game.py @@ -0,0 +1,38 @@ +from gamegrid import * +from cell import Cell +from cgol import CGOL + +game_list = [] + +def gather_life(): + global game_list, game + game_list = [] + for y in range(0, 20, 1): + game_list.append([]) + for x in range(0, 20, 1): + cell = getOneActorAt(Location(x, y), Cell) + if cell != None: + print("Lebendig auf X:" + str(x) + " Y:" + str(y)) + game_list[y].append(1) + else: + game_list[y].append(0) + print(str(game_list)) + refresh() + game.game_list = game_list + +def pressCallback(e): + location = toLocationInGrid(e.getX(), e.getY()) + cell = getOneActorAt(location, Cell) + if cell == None: + addActor(Cell(), location) + else: + removeActor(cell) + refresh() + gather_life() + +# Initialize the CGOL class with the game_list as an argument. +game = CGOL(game_list) + +makeGameGrid(20, 20, 30, Color.yellow, "/home/prenski/Informatik/Informatik 12/12b/vitus/OOP-Spiel/Bilder/sand.png", mousePressed=pressCallback) +setTitle("Conways Game of Life") +show()