From 877b0ff9a6e440c2cf982a6656569cf8339ca913 Mon Sep 17 00:00:00 2001 From: verde4321 Date: Sun, 20 Mar 2022 13:22:19 +0100 Subject: [PATCH] init --- Code/GameLogic/Rules.py | 69 +++++++++++++++++++++++++++++------------ Code/UI/Field.py | 17 +++++----- Code/UI/PlayingField.py | 47 ++++++++++++++++++++-------- Code/UI/Square.py | 4 +-- 4 files changed, 96 insertions(+), 41 deletions(-) diff --git a/Code/GameLogic/Rules.py b/Code/GameLogic/Rules.py index 42596e4..034ffa2 100644 --- a/Code/GameLogic/Rules.py +++ b/Code/GameLogic/Rules.py @@ -1,3 +1,5 @@ +import copy +import time from Code.UI.Field import Field from Code.UI.Square import Square from Code.helpers import index_2d @@ -7,33 +9,62 @@ 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] + def _get_neighborhood(self,mid_y, mid_x, field_of_squares: list) -> list: + # mid_y, mid_x = index_2d(field_of_squares, target) - neighborhood = [top, middle, bottom] + 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[0], top[1], top[2], middle[0], middle[1], bottom[0], bottom[1], bottom[2]] return neighborhood - def _number_of_alive_nigbohrs(self,neighborhood:list)->int: - count=0 + def _number_of_alive_nigbohrs(self, neighborhood: list) -> int: + count = 0 for square in neighborhood: if square.active: - count+=1 + count += 1 + #print("count {}".format(count)) 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: + def _use_rules_on_square(self, target: Square, index_y, index_x, field_of_squares: list): + #print(f"Checking field: y:{index_y} x:{index_x} state: {field_of_squares[index_y][index_x].active}") + neighborhood = self._get_neighborhood(index_y, index_x, field_of_squares) + number_of_alive_nigbohrs = self._number_of_alive_nigbohrs(neighborhood) + + old_target = field_of_squares[index_y][index_x] + if not old_target.active: + if number_of_alive_nigbohrs == 3: target.update(True) else: - if number_of_alive_nigbohrs<2: + 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: + 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) + + return target + + + def evolve_field(self, field_old) -> Field: + start = time.time() + field_new = Field() + end = time.time() + print(end - start) + + index_x = 0 + index_y = 0 + lenght=len(field_new.squares) + for x in field_new.squares: + index_y=0 + for square in x: + if (index_y != 0 and index_x != 0 and index_y < lenght -1 and index_x < lenght-1): + new_field=self._use_rules_on_square(field_new.squares[index_y][index_x], index_y, index_x, field_old.squares) + field_new.squares[index_y][index_x] = new_field + + index_y +=1 + index_x += 1 + return field_new.squares diff --git a/Code/UI/Field.py b/Code/UI/Field.py index d7f6932..9a2910f 100644 --- a/Code/UI/Field.py +++ b/Code/UI/Field.py @@ -7,18 +7,21 @@ 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.width = math.trunc(GeneralConfig.width / SquareConfig.width) + 2 + self.height = math.trunc(GeneralConfig.height / SquareConfig.height) + 2 + self.field_shift = -10 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) + squares = [ + [Square(x_pos=j * SquareConfig.width + self.field_shift, y_pos=i * SquareConfig.height + self.field_shift) + for i in range(self.height)] for + j in range(self.width)] + return squares - def update_squares(self): - pass + def update_squares(self,squares): + self.squares=squares def draw_squares(self, window): for x in self.squares: diff --git a/Code/UI/PlayingField.py b/Code/UI/PlayingField.py index 2e203f6..e4c1ad8 100644 --- a/Code/UI/PlayingField.py +++ b/Code/UI/PlayingField.py @@ -1,3 +1,5 @@ +from datetime import time +import time as ti import pygame as pygame from Code import Config from Code.Config import GeneralConfig, Colors @@ -9,30 +11,31 @@ from Code.UI.Field import Field class GameState: def __init__(self): self.run = True - self.pause_for_inpuit=False + self.pause_for_input = False self.field = Field() + self.update_field_events= [] 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.MOUSEBUTTONUP: + self.update_field_events.append(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() + self.update_field_events.append(event) + self.pause_for_input = not self.pause_for_input + def update_field_with_input(self, event): 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 evolve(self): + + rules = Rules() + self.field.update_squares(rules.evolve_field(self.field)) def redraw_field(self, window): window.fill(Colors.BLACK) @@ -45,13 +48,31 @@ def run_game(): window = pygame.display.set_mode((GeneralConfig.width, GeneralConfig.height)) clock = pygame.time.Clock() game_state = GameState() + time_elapsed_since_last_action = 0 while game_state.run: - clock.tick(GeneralConfig.fps) game_state.event_handler() - if game_state.pause_for_inpuit: - game_state.update() + print(len(game_state.update_field_events)) + + for event in game_state.update_field_events: + game_state.update_field_with_input(event) + game_state.update_field_events.remove(event) + + print("running") + clock.tick(GeneralConfig.fps) + time_elapsed_since_last_action += clock.tick() + + if game_state.pause_for_input: + + if time_elapsed_since_last_action > 1: + start = ti.time() + game_state.evolve() + end = ti.time() + print(end - start) + time_elapsed_since_last_action = 0 # reset it to 0 so you can count again + game_state.redraw_field(window) pygame.display.update() + if __name__ == "__main__": run_game() diff --git a/Code/UI/Square.py b/Code/UI/Square.py index c664b5d..97b905d 100644 --- a/Code/UI/Square.py +++ b/Code/UI/Square.py @@ -3,8 +3,8 @@ 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) + def __init__(self, x_pos, y_pos): + self.rect = pygame.Rect(x_pos, y_pos, SquareConfig.width, SquareConfig.height) self.color = SquareConfig.unclicked_color self.active = False