1
0
Fork 0

init - add sync and spawning of gliders

This commit is contained in:
qvalentin 2022-04-02 13:04:51 +02:00
parent 331e463592
commit bcb8de9a9c
Signed by: qvalentin
GPG key ID: C979FA1EAFCABF1C
12 changed files with 135 additions and 30 deletions

View file

@ -1,6 +1,8 @@
from Code.Communication.Direction import Direction
from Code.Config import GeneralConfig, SquareConfig
from Code.UI import Square
from Code.UI.Shape import Shape
from Code.UI.Shapes import Shapes
from Code.UI.Square import Square
@ -10,6 +12,10 @@ class Field:
self.height = GeneralConfig.fields_amount_y + 2
self.field_shift = -10
self.squares = self._creat_squares()
self.shapes = {Shape.VERTICAL_GLIDER: Shapes().creat_vertical_glieder}
def create_shape(self, shape: Shape, start_square_pos):
self.squares = self.shapes[shape](start_square_pos, self.squares)
def _creat_squares(self):
squares = [
@ -27,10 +33,12 @@ class Field:
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(),
def get_edges(self) -> dict:
return {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_edge(self, edg_pos: Direction) -> list:
return self.get_edges()[edg_pos]
def get_top(self):
top_squares = []

View file

@ -1,19 +1,24 @@
import pygame as pygame
from Code.Communication.Direction import Direction
from Code.Communication.EdgeSync import EdgeSync
from Code.Communication.Neighbours import Neighbours
from Code.Config import Fonts
from Code.Config import GeneralConfig, Colors
from Code.GameLogic.Rules import Rules
from Code.UI.Field import Field
from Code.UI.Shape import Shape
class GameState:
def __init__(self, neighbours: Neighbours):
self.neighbours = neighbours
self.counter = 0
self.run = True
self.pause_for_input = False
self.is_evolving = False
self.field = Field()
self.edge_sync = EdgeSync(self.field.get_edges(), self.counter)
self.update_field_events = []
def event_handler(self):
@ -24,8 +29,22 @@ class GameState:
self.update_field_events.append(event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.pause_for_input = not self.pause_for_input
self.neighbours.toggle_pause(self.pause_for_input)
print(f"Gamestate: set evolving to {not self.is_evolving}")
self.is_evolving = not self.is_evolving
self.neighbours.toggle_pause(self.is_evolving)
if event.key == pygame.K_v:
index_x = 0
index_y = 0
for line in self.field.squares:
index_y += 1
index_x = 0
for square in line:
index_x += 1
if square.rect.collidepoint(pygame.mouse.get_pos()):
self.field.create_shape(Shape.VERTICAL_GLIDER,
(int(index_y ),
int(index_x )))
def update_field_with_input(self, event):
for line in self.field.squares:
@ -40,15 +59,15 @@ class GameState:
def redraw_field(self, window):
window.fill(Colors.BLACK)
self.field.draw_squares(window)
if not self.pause_for_input:
if not self.is_evolving:
label_font = Fonts.monospace_80
label = label_font.render("Pause", 1, Colors.ORANGE)
window.blit(label, (100, 100))
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))
self.field.fill_right_ghost_edge(self.neighbours.get_edge(Direction.RIGHT, self.counter))
self.field.fill_left_ghost_edge(self.neighbours.get_edge(Direction.LEFT, self.counter))
self.counter_old = self.counter
def run_game(game_state: GameState):
@ -60,8 +79,9 @@ def run_game(game_state: GameState):
time_elapsed_since_last_action = 0
while game_state.run:
print(f"running {game_state.pause_for_input}")
game_state.event_handler()
print(f"running {game_state.is_evolving}")
for event in game_state.update_field_events:
game_state.update_field_with_input(event)
@ -70,12 +90,13 @@ def run_game(game_state: GameState):
clock.tick(GeneralConfig.fps)
time_elapsed_since_last_action += clock.get_time()
if game_state.pause_for_input:
if game_state.is_evolving:
if time_elapsed_since_last_action > 100:
# start = ti.time()
game_state.update_borders()
game_state.evolve()
game_state.counter = game_state.counter + 1
game_state.edge_sync.update_edges(game_state.field.get_edges(), game_state.counter)
# end = ti.time()
# print(end - start)
time_elapsed_since_last_action = 0 # reset it to 0 so you can count again

5
Code/UI/Shape.py Normal file
View file

@ -0,0 +1,5 @@
from enum import Enum
class Shape(Enum):
VERTICAL_GLIDER = 1

23
Code/UI/Shapes.py Normal file
View file

@ -0,0 +1,23 @@
from Code import Config
class Shapes:
def creat_vertical_glieder(self, start_square_pos: tuple, squares):
points =[(0,0),(1,0),(2,0),(2,1),(1,2)]
if self._check_bounderies((6, 6), start_square_pos, squares):
for point in points:
squares[start_square_pos[0]+point[1]][start_square_pos[1]+point[0]].active = True
return squares
def _creat_shape(self,start_square_pos: tuple, squares):
def _check_bounderies(self, bounderies, start_square_pos: tuple, field):
x_valid = (Config.GeneralConfig.fields_amount_x-2 - start_square_pos[1]) > bounderies[0]
y_valid = (Config.GeneralConfig.fields_amount_y-2 - start_square_pos[1]) > bounderies[1]
return x_valid and y_valid

0
Code/UI/__init__.py Normal file
View file