Skip to content

Commit

Permalink
make it interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkristoferanderson committed Aug 1, 2021
1 parent 7569695 commit 2eba9f8
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions game_of_life/mypygamefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,76 @@

pygame.init()

FPS = 2
FPS = 30
generation_frames = 15
FramePerSec = pygame.time.Clock()
input_wait_time = 3

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

DISPLAYSURF = pygame.display.set_mode((200, 200))
screen_width = 200
screen_height = 200
DISPLAYSURF.fill(WHITE)
pygame.display.set_caption("Game of Life")


def main():
generation_counter = 0
cursor_x = 9
cursor_y = 9
wait_for_input = 0
paused = True
game = mygamefile.Game()
game.board = resources.BOARD_WITH_BLINKER_A
while True:
generation_counter += 1
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()

pressed_keys = pygame.key.get_pressed()
if not wait_for_input:
wait_for_input = input_wait_time
if pressed_keys[K_e]:
cursor_y -= 1
if pressed_keys[K_d]:
cursor_y += 1
if pressed_keys[K_s]:
cursor_x -= 1
if pressed_keys[K_f]:
cursor_x += 1
if pressed_keys[K_SPACE]:
paused = not paused
if pressed_keys[K_RETURN]:
game.swap_life_state(cursor_y, cursor_x)
if wait_for_input > 0:
wait_for_input -= 1
DISPLAYSURF.fill(BLACK)
game.advance_to_next_generation()
if paused:
pygame.draw.rect(DISPLAYSURF, RED, (0, 0, screen_width, screen_height), 3)

if not paused:
if generation_counter >= generation_frames:
game.advance_to_next_generation()
generation_counter = 0





for i, column in enumerate(game.board):
for j, row in enumerate(column):
surf = pygame.Surface((10, 10))
surf.fill(WHITE)
if row == 1:
DISPLAYSURF.blit(surf, (j*10, i*10))
# cell.draw()
pygame.draw.rect(DISPLAYSURF, GREEN, (cursor_x*10, cursor_y*10, 10, 10), 2)
pygame.display.update()
FramePerSec.tick(FPS)

Expand Down

0 comments on commit 2eba9f8

Please sign in to comment.