1
0
Fork 0

init - add basic server for finding a neighbour

This commit is contained in:
qvalentin 2022-03-27 12:59:14 +02:00
parent e5e441622b
commit 8cf2d4d7f1
Signed by: qvalentin
GPG key ID: C979FA1EAFCABF1C
8 changed files with 356 additions and 58 deletions

View file

@ -1,78 +1,68 @@
from datetime import time
import time as ti
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_input = False
self.field = Field()
self.update_field_events= []
def __init__(self):
self.run = True
self.pause_for_input = False
self.field = Field()
self.update_field_events = []
def event_handler(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.run = False
if event.type == pygame.MOUSEBUTTONUP:
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
def event_handler(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.run = False
if event.type == pygame.MOUSEBUTTONUP:
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
def update_field_with_input(self, event):
for line in self.field.squares:
for square in line:
if square.rect.collidepoint(event.pos):
square.update(not square.active)
def update_field_with_input(self, event):
for line in self.field.squares:
for square in line:
if square.rect.collidepoint(event.pos):
square.update(not square.active)
def evolve(self):
def evolve(self):
rules = Rules()
self.field.update_squares(rules.evolve_field(self.field))
rules = Rules()
self.field.update_squares(rules.evolve_field(self.field))
def redraw_field(self, window):
window.fill(Colors.BLACK)
self.field.draw_squares(window)
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()
time_elapsed_since_last_action = 0
while game_state.run:
game_state.event_handler()
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()
for event in game_state.update_field_events:
game_state.update_field_with_input(event)
game_state.update_field_events.remove(event)
for event in game_state.update_field_events:
game_state.update_field_with_input(event)
game_state.update_field_events.remove(event)
clock.tick(GeneralConfig.fps)
time_elapsed_since_last_action += clock.get_time()
if game_state.pause_for_input:
clock.tick(GeneralConfig.fps)
time_elapsed_since_last_action += clock.get_time()
if time_elapsed_since_last_action > 100:
# 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
if game_state.pause_for_input:
if time_elapsed_since_last_action > 100:
#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)
pygame.display.update()
if __name__ == "__main__":
run_game()
game_state.redraw_field(window)
pygame.display.update()