initial c
This commit is contained in:
1 parent
2ecdba7720
commit
b921d21050
3 files changed
+84
No files matched your search
@@ -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
|
||||
@@ -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))
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user