1
0
Fork 0
This commit is contained in:
verde4321 2022-03-20 09:47:49 +01:00
commit d5d4d47772
6 changed files with 168 additions and 0 deletions

26
Code/UI/Field.py Normal file
View file

@ -0,0 +1,26 @@
from Code.Config import GeneralConfig, SquareConfig
import math
from Code.UI import Square
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.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)
return squares
def update_squares(self):
pass
def draw_squares(self, window):
for x in self.squares:
for y in x:
y.draw(window)

57
Code/UI/PlayingField.py Normal file
View file

@ -0,0 +1,57 @@
import pygame as pygame
from Code import Config
from Code.Config import GeneralConfig, Colors
from Code.GameLogic.Rules import Rules
from Code.UI import Square
from Code.UI.Field import Field
class GameState:
def __init__(self):
self.run = True
self.pause_for_inpuit=False
self.field = Field()
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.KEYDOWN:
if event.key == pygame.K_SPACE:
self.pause_for_inpuit=not self.pause_for_inpuit
def _update_field(self, event):
rule = Rules()
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 redraw_field(self, window):
window.fill(Colors.BLACK)
self.field.draw_squares(window)
def run_game():
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()
while game_state.run:
clock.tick(GeneralConfig.fps)
game_state.event_handler()
if game_state.pause_for_inpuit:
game_state.update()
game_state.redraw_field(window)
pygame.display.update()
if __name__ == "__main__":
run_game()

21
Code/UI/Square.py Normal file
View file

@ -0,0 +1,21 @@
import pygame
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)
self.color = SquareConfig.unclicked_color
self.active = False
def update(self, active):
self.active = active
if active:
self.color = SquareConfig.clicked_color
else:
self.color = SquareConfig.unclicked_color
def draw(self, window):
pygame.draw.rect(window, self.color, self.rect)