1
0
Fork 0

Init - Randaustausch almost working in 1D

This commit is contained in:
qvalentin 2022-03-27 16:43:15 +02:00
parent 8cf2d4d7f1
commit 1bca8c3c89
Signed by: qvalentin
GPG key ID: C979FA1EAFCABF1C
10 changed files with 170 additions and 57 deletions

View file

@ -1,29 +1,84 @@
from Code.Config import GeneralConfig, SquareConfig
import math
from Code.Communication.Direction import Direction
from Code.Config import GeneralConfig, SquareConfig
from Code.UI import Square
from Code.UI.Square import Square
class Field:
def __init__(self):
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 __init__(self):
self.width = GeneralConfig.fields_amount_x+2
self.height = GeneralConfig.fields_amount_y+2
self.field_shift = -10
self.squares = self._creat_squares()
def _creat_squares(self):
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 _creat_squares(self):
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)]
def update_squares(self,squares):
self.squares=squares
return squares
def draw_squares(self, window):
for x in self.squares:
for y in x:
y.draw(window)
def update_squares(self, squares):
self.squares = squares
def draw_squares(self, window):
for x in self.squares:
for y in x:
y.draw(window)
def get_edge(self, edg_pos: Direction) -> list:
edge = {Direction.LEFT: self.get_left_edge(), Direction.RIGHT: self.get_right_edge(),
Direction.TOP: self.get_top(), Direction.BOTTOM: self.get_bottom_edge()}
return edge[edg_pos]
def get_top(self):
top_squares = []
for y in self.squares:
top_squares.append(y[1].active)
return top_squares
def get_bottom_edge(self):
right_squares = []
for y in self.squares:
right_squares.append(y[len(y) - 2].active)
return right_squares
def get_left_edge(self):
left_squares = []
for x in self.squares[1]:
left_squares.append(x.active)
return left_squares
def get_right_edge(self):
right_squares = []
for x in self.squares[len(self.squares[0]) - 2]:
right_squares.append(x.active)
return right_squares
def fill_ghost_edge(self, value: list):
edge_fn = {Direction.LEFT: self.fill_left_ghost_edge, Direction.RIGHT: self.fill_right_ghost_edge,
Direction.TOP: self.fill_top_ghost_edge, Direction.BOTTOM: self.fill_bottom_ghost_edge}
edge_fn(value)
def fill_right_ghost_edge(self, value: list):
#if len(value)== self.square.
for i in range(len(value)):
self.squares[len(self.squares[0]) - 1][i].active = value[i]
def fill_left_ghost_edge(self, value: list):
for i in range(len(value)):
self.squares[0][i].active = value[i]
def fill_top_ghost_edge(self,value):
pass
def fill_bottom_ghost_edge(self,value):
pass

View file

@ -1,12 +1,15 @@
import pygame as pygame
from Code.Communication.Direction import Direction
from Code.Communication.Neighbours import Neighbours
from Code.Config import GeneralConfig, Colors
from Code.GameLogic.Rules import Rules
from Code.UI.Field import Field
class GameState:
def __init__(self):
def __init__(self, neighbours: Neighbours):
self.neighbours = neighbours
self.run = True
self.pause_for_input = False
self.field = Field()
@ -29,7 +32,6 @@ class GameState:
square.update(not square.active)
def evolve(self):
rules = Rules()
self.field.update_squares(rules.evolve_field(self.field))
@ -37,13 +39,15 @@ class GameState:
window.fill(Colors.BLACK)
self.field.draw_squares(window)
def update_borders(self):
self.field.fill_right_ghost_edge(self.neighbours.get_edge(Direction.RIGHT))
self.field.fill_left_ghost_edge(self.neighbours.get_edge(Direction.LEFT))
def run_game():
def run_game(game_state: GameState):
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()
time_elapsed_since_last_action = 0
while game_state.run:
game_state.event_handler()
@ -59,6 +63,7 @@ def run_game():
if time_elapsed_since_last_action > 100:
# start = ti.time()
game_state.update_borders()
game_state.evolve()
# end = ti.time()
# print(end - start)