This commit is contained in:
verde4321 2022-03-20 09:47:49 +01:00
commit d5d4d47772
6 changed files with 168 additions and 0 deletions

21
Code/Config.py Normal file
View File

@ -0,0 +1,21 @@
class GeneralConfig:
width = 1000
height = 1000
fps = 150
window_caption = "GOL"
class Colors:
ORANGE = (255, 140, 0)
RED = (255, 0, 0)
GREEN = (5, 112, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREY = (84, 84, 84)
class SquareConfig:
width = 10
height = 10
unclicked_color = Colors.BLACK
clicked_color = Colors.WHITE

39
Code/GameLogic/Rules.py Normal file
View File

@ -0,0 +1,39 @@
from Code.UI.Field import Field
from Code.UI.Square import Square
from Code.helpers import index_2d
class Rules:
def __init__(self):
pass
def _get_neighborhood(self, target: Square, field_of_squares: list)->list:
mid_y, mid_x = index_2d(field_of_squares, target)
#TODO i did not consider the frame so every pixel on the edge will couse an error i am not sure how we gonna do it with the conection between the frames
top = field_of_squares[mid_y - 1][mid_x - 1], field_of_squares[mid_y - 1][mid_x],field_of_squares[mid_y - 1][mid_x + 1]
middle = field_of_squares[mid_y][mid_x - 1],field_of_squares[mid_y][mid_x + 1]
bottom = field_of_squares[mid_y + 1][mid_x - 1], field_of_squares[mid_y + 1][mid_x],field_of_squares[mid_y + 1][mid_x + 1]
neighborhood = [top, middle, bottom]
return neighborhood
def _number_of_alive_nigbohrs(self,neighborhood:list)->int:
count=0
for square in neighborhood:
if square.active:
count+=1
return count
def use_ruels_on_square(self,target: Square, field_of_squares: list):
neighborhood=self._get_neighborhood(target,field_of_squares)
number_of_alive_nigbohrs=self._get_neighborhood(neighborhood)
if not target.active:
if number_of_alive_nigbohrs==3:
target.update(True)
else:
if number_of_alive_nigbohrs<2:
target.update(False)
if number_of_alive_nigbohrs==2 or number_of_alive_nigbohrs==3:
target.update(True)# eigentlich unnötig da es einfach den zustand beibehalten kann
if number_of_alive_nigbohrs>3:
target.update(False)

26
Code/UI/Field.py Normal file
View File

@ -0,0 +1,26 @@
from Code.Config import GeneralConfig, SquareConfig
import math
from Code.UI import Square
from Code.UI.Square import Square
class Field:
def __init__(self):
self.width = math.trunc(GeneralConfig.width / SquareConfig.width)
self.height = math.trunc(GeneralConfig.height / SquareConfig.height)
self.squares = self._creat_squares()
def _creat_squares(self):
squares = [[Square(x_pos=j * SquareConfig.width, y_pos=i * SquareConfig.height) for i in range(self.height)] for
j in range(self.width)]
print(squares)
return squares
def update_squares(self):
pass
def draw_squares(self, window):
for x in self.squares:
for y in x:
y.draw(window)

57
Code/UI/PlayingField.py Normal file
View File

@ -0,0 +1,57 @@
import pygame as pygame
from Code import Config
from Code.Config import GeneralConfig, Colors
from Code.GameLogic.Rules import Rules
from Code.UI import Square
from Code.UI.Field import Field
class GameState:
def __init__(self):
self.run = True
self.pause_for_inpuit=False
self.field = Field()
def event_handler(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.run = False
if event.type == pygame.MOUSEBUTTONDOWN:
self._update_field(event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.pause_for_inpuit=not self.pause_for_inpuit
def _update_field(self, event):
rule = Rules()
for line in self.field.squares:
for square in line:
if square.rect.collidepoint(event.pos):
rule.get_neighborhood(square, field_of_squares=self.field.squares)
square.update(not square.active)
def update(self):
pass
def redraw_field(self, window):
window.fill(Colors.BLACK)
self.field.draw_squares(window)
def run_game():
pygame.init()
pygame.display.set_caption(GeneralConfig.window_caption)
window = pygame.display.set_mode((GeneralConfig.width, GeneralConfig.height))
clock = pygame.time.Clock()
game_state = GameState()
while game_state.run:
clock.tick(GeneralConfig.fps)
game_state.event_handler()
if game_state.pause_for_inpuit:
game_state.update()
game_state.redraw_field(window)
pygame.display.update()
if __name__ == "__main__":
run_game()

21
Code/UI/Square.py Normal file
View File

@ -0,0 +1,21 @@
import pygame
from Code.Config import Colors, SquareConfig
class Square:
def __init__(self, x_pos=0, y_pos=0):
self.rect = pygame.Rect(x_pos, y_pos, SquareConfig.width,SquareConfig.height)
self.color = SquareConfig.unclicked_color
self.active = False
def update(self, active):
self.active = active
if active:
self.color = SquareConfig.clicked_color
else:
self.color = SquareConfig.unclicked_color
def draw(self, window):
pygame.draw.rect(window, self.color, self.rect)

4
Code/helpers.py Normal file
View File

@ -0,0 +1,4 @@
def index_2d(myList, v):
for i, x in enumerate(myList):
if v in x:
return (i, x.index(v))