init - add more spaceships and start with doku
This commit is contained in:
parent
bcb8de9a9c
commit
7f6b53b549
8 changed files with 109 additions and 27 deletions
|
@ -11,11 +11,22 @@ class Field:
|
|||
self.width = GeneralConfig.fields_amount_x + 2
|
||||
self.height = GeneralConfig.fields_amount_y + 2
|
||||
self.field_shift = -10
|
||||
self.shapes = Shapes()
|
||||
self.squares = self._creat_squares()
|
||||
self.shapes = {Shape.VERTICAL_GLIDER: Shapes().creat_vertical_glieder}
|
||||
|
||||
def get_square_on_click_pos(self, mousclick_pos):
|
||||
index_x = 0
|
||||
index_y = 0
|
||||
for line in self.squares:
|
||||
index_y += 1
|
||||
index_x = 0
|
||||
for square in line:
|
||||
index_x += 1
|
||||
if square.rect.collidepoint(mousclick_pos):
|
||||
return (index_y, index_x)
|
||||
|
||||
def create_shape(self, shape: Shape, start_square_pos):
|
||||
self.squares = self.shapes[shape](start_square_pos, self.squares)
|
||||
self.squares = self.shapes.create_shape(start_square_pos,self.squares, shape)
|
||||
|
||||
def _creat_squares(self):
|
||||
squares = [
|
||||
|
|
|
@ -33,18 +33,23 @@ class GameState:
|
|||
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 )))
|
||||
self.field.create_shape(Shape.VERTICAL_GLIDER_LEFT,
|
||||
self.field.get_square_on_click_pos(pygame.mouse.get_pos()))
|
||||
if event.key == pygame.K_b:
|
||||
self.field.create_shape(Shape.VERTICAL_GLIDER_RIGHT,
|
||||
self.field.get_square_on_click_pos(pygame.mouse.get_pos()))
|
||||
if event.key == pygame.K_n:
|
||||
self.field.create_shape(Shape.GLIDER_LEFT_DOWN,
|
||||
self.field.get_square_on_click_pos(pygame.mouse.get_pos()))
|
||||
if event.key == pygame.K_m:
|
||||
self.field.create_shape(Shape.GLIDER_RIGHT,
|
||||
self.field.get_square_on_click_pos(pygame.mouse.get_pos()))
|
||||
if event.key == pygame.K_y:
|
||||
self.field.create_shape(Shape.VERTICAL_GLIDER_RIGHT,
|
||||
self.field.get_square_on_click_pos(pygame.mouse.get_pos()))
|
||||
if event.key == pygame.K_x:
|
||||
self.field.create_shape(Shape.VERTICAL_GLIDER_LEFT,
|
||||
self.field.get_square_on_click_pos(pygame.mouse.get_pos()))
|
||||
|
||||
def update_field_with_input(self, event):
|
||||
for line in self.field.squares:
|
||||
|
@ -91,7 +96,7 @@ def run_game(game_state: GameState):
|
|||
time_elapsed_since_last_action += clock.get_time()
|
||||
|
||||
if game_state.is_evolving:
|
||||
if time_elapsed_since_last_action > 100:
|
||||
if time_elapsed_since_last_action > 10:
|
||||
# start = ti.time()
|
||||
game_state.update_borders()
|
||||
game_state.evolve()
|
||||
|
|
|
@ -2,4 +2,11 @@ from enum import Enum
|
|||
|
||||
|
||||
class Shape(Enum):
|
||||
VERTICAL_GLIDER = 1
|
||||
HORIZONTAL_GLIDER_LEFT = 1
|
||||
HORIZONTAL_GLIDER_RIGHT=2
|
||||
GLIDER_LEFT_DOWN = 3
|
||||
GLIDER_RIGHT=4
|
||||
VERTICAL_GLIDER_RIGHT=5
|
||||
VERTICAL_GLIDER_LEFT=6
|
||||
|
||||
|
||||
|
|
|
@ -1,23 +1,40 @@
|
|||
from operator import itemgetter
|
||||
|
||||
from Code import Config
|
||||
from Code.UI.Shape import Shape
|
||||
|
||||
|
||||
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):
|
||||
def create_shape(self, start_square_pos: tuple, squares, type):
|
||||
return self._create_shape(start_square_pos, squares, Structures.shapes[type])
|
||||
|
||||
def _get_size(self, points):
|
||||
x = max(max(points, key=itemgetter(0)))
|
||||
y = max(max(points, key=itemgetter(1)))
|
||||
return (x, y)
|
||||
|
||||
def _create_shape(self, start_square_pos: tuple, squares, points):
|
||||
if self._check_bounderies(self._get_size(points), start_square_pos, squares):
|
||||
for point in points:
|
||||
squares[start_square_pos[0]+point[1]][start_square_pos[1]+point[0]].active = True
|
||||
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]
|
||||
|
||||
print(bounderies)
|
||||
delta_start_pos_x = start_square_pos[0]
|
||||
delta_start_pos_y = start_square_pos[1]
|
||||
x_valid = (Config.GeneralConfig.fields_amount_x - 2 - int(delta_start_pos_x)) > bounderies[0]
|
||||
y_valid = (Config.GeneralConfig.fields_amount_y - 2 - int(delta_start_pos_y)) > bounderies[1]
|
||||
return x_valid and y_valid
|
||||
|
||||
|
||||
class Structures:
|
||||
shapes = {Shape.GLIDER_LEFT_DOWN: [(0, 0), (1, 0), (2, 0), (2, 1), (1, 2)],
|
||||
Shape.HORIZONTAL_GLIDER_LEFT: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 1), (4, 3), (0, 1), (0, 2), (1, 3)],
|
||||
Shape.HORIZONTAL_GLIDER_RIGHT: [(0, 1), (0, 3), (1, 0), (3, 0), (2, 0), (4, 0), (4, 1), (4, 2), (3, 3)],
|
||||
Shape.GLIDER_RIGHT: [(0, 2), (1, 0), (1, 2), (2, 1), (2, 2)],
|
||||
Shape.VERTICAL_GLIDER_RIGHT: [(1, 0), (3, 0), (0, 1), (0, 3), (0, 2), (0, 4), (1, 4), (2, 4), (3, 3)],
|
||||
Shape.VERTICAL_GLIDER_LEFT: [(0, 0), (0, 1), (0, 2), (0, 3), (1, 4), (3, 4), (1, 0), (2, 0), (3, 1)]
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue