This commit is contained in:
verde4321 2022-03-20 13:22:19 +01:00
parent d5d4d47772
commit 877b0ff9a6
4 changed files with 96 additions and 41 deletions

View File

@ -1,3 +1,5 @@
import copy
import time
from Code.UI.Field import Field from Code.UI.Field import Field
from Code.UI.Square import Square from Code.UI.Square import Square
from Code.helpers import index_2d from Code.helpers import index_2d
@ -7,14 +9,16 @@ class Rules:
def __init__(self): def __init__(self):
pass pass
def _get_neighborhood(self, target: Square, field_of_squares: list)->list: def _get_neighborhood(self,mid_y, mid_x, field_of_squares: list) -> list:
mid_y, mid_x = index_2d(field_of_squares, target) # 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] 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 return neighborhood
def _number_of_alive_nigbohrs(self, neighborhood: list) -> int: def _number_of_alive_nigbohrs(self, neighborhood: list) -> int:
@ -22,12 +26,16 @@ class Rules:
for square in neighborhood: for square in neighborhood:
if square.active: if square.active:
count += 1 count += 1
#print("count {}".format(count))
return count return count
def use_ruels_on_square(self,target: Square, field_of_squares: list): def _use_rules_on_square(self, target: Square, index_y, index_x, field_of_squares: list):
neighborhood=self._get_neighborhood(target,field_of_squares) #print(f"Checking field: y:{index_y} x:{index_x} state: {field_of_squares[index_y][index_x].active}")
number_of_alive_nigbohrs=self._get_neighborhood(neighborhood) neighborhood = self._get_neighborhood(index_y, index_x, field_of_squares)
if not target.active: 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: if number_of_alive_nigbohrs == 3:
target.update(True) target.update(True)
else: else:
@ -37,3 +45,26 @@ class Rules:
target.update(True) # eigentlich unnötig da es einfach den zustand beibehalten kann target.update(True) # eigentlich unnötig da es einfach den zustand beibehalten kann
if number_of_alive_nigbohrs > 3: if number_of_alive_nigbohrs > 3:
target.update(False) 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

View File

@ -7,18 +7,21 @@ from Code.UI.Square import Square
class Field: class Field:
def __init__(self): def __init__(self):
self.width = math.trunc(GeneralConfig.width / SquareConfig.width) self.width = math.trunc(GeneralConfig.width / SquareConfig.width) + 2
self.height = math.trunc(GeneralConfig.height / SquareConfig.height) self.height = math.trunc(GeneralConfig.height / SquareConfig.height) + 2
self.field_shift = -10
self.squares = self._creat_squares() self.squares = self._creat_squares()
def _creat_squares(self): def _creat_squares(self):
squares = [[Square(x_pos=j * SquareConfig.width, y_pos=i * SquareConfig.height) for i in range(self.height)] for 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)] j in range(self.width)]
print(squares)
return squares return squares
def update_squares(self): def update_squares(self,squares):
pass self.squares=squares
def draw_squares(self, window): def draw_squares(self, window):
for x in self.squares: for x in self.squares:

View File

@ -1,3 +1,5 @@
from datetime import time
import time as ti
import pygame as pygame import pygame as pygame
from Code import Config from Code import Config
from Code.Config import GeneralConfig, Colors from Code.Config import GeneralConfig, Colors
@ -9,30 +11,31 @@ from Code.UI.Field import Field
class GameState: class GameState:
def __init__(self): def __init__(self):
self.run = True self.run = True
self.pause_for_inpuit=False self.pause_for_input = False
self.field = Field() self.field = Field()
self.update_field_events= []
def event_handler(self): def event_handler(self):
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
self.run = False self.run = False
if event.type == pygame.MOUSEBUTTONDOWN: if event.type == pygame.MOUSEBUTTONUP:
self._update_field(event) self.update_field_events.append(event)
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: if event.key == pygame.K_SPACE:
self.pause_for_inpuit=not self.pause_for_inpuit self.update_field_events.append(event)
self.pause_for_input = not self.pause_for_input
def _update_field(self, event):
rule = Rules()
def update_field_with_input(self, event):
for line in self.field.squares: for line in self.field.squares:
for square in line: for square in line:
if square.rect.collidepoint(event.pos): if square.rect.collidepoint(event.pos):
rule.get_neighborhood(square, field_of_squares=self.field.squares)
square.update(not square.active) square.update(not square.active)
def update(self): def evolve(self):
pass
rules = Rules()
self.field.update_squares(rules.evolve_field(self.field))
def redraw_field(self, window): def redraw_field(self, window):
window.fill(Colors.BLACK) window.fill(Colors.BLACK)
@ -45,13 +48,31 @@ def run_game():
window = pygame.display.set_mode((GeneralConfig.width, GeneralConfig.height)) window = pygame.display.set_mode((GeneralConfig.width, GeneralConfig.height))
clock = pygame.time.Clock() clock = pygame.time.Clock()
game_state = GameState() game_state = GameState()
time_elapsed_since_last_action = 0
while game_state.run: while game_state.run:
clock.tick(GeneralConfig.fps)
game_state.event_handler() game_state.event_handler()
if game_state.pause_for_inpuit: print(len(game_state.update_field_events))
game_state.update()
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) game_state.redraw_field(window)
pygame.display.update() pygame.display.update()
if __name__ == "__main__": if __name__ == "__main__":
run_game() run_game()

View File

@ -3,7 +3,7 @@ from Code.Config import Colors, SquareConfig
class Square: class Square:
def __init__(self, x_pos=0, y_pos=0): def __init__(self, x_pos, y_pos):
self.rect = pygame.Rect(x_pos, y_pos, SquareConfig.width, SquareConfig.height) self.rect = pygame.Rect(x_pos, y_pos, SquareConfig.width, SquareConfig.height)
self.color = SquareConfig.unclicked_color self.color = SquareConfig.unclicked_color
self.active = False self.active = False