Advertisement
Guffi281

Игра Жизнь

Sep 25th, 2021
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. import pygame
  2. from random import randint
  3. from copy import deepcopy
  4.  
  5.  
  6. #Переменные
  7. RES = WIDTH, HEIGHT = 1500, 900
  8. TILE = 10
  9. W, H = WIDTH // TILE, HEIGHT // TILE
  10. FPS = 20
  11.  
  12.  
  13. #Создание окна и его разрешения.
  14. pygame.init()
  15. surface = pygame.display.set_mode(RES)
  16. clock = pygame.time.Clock()
  17.  
  18. #настройка поля
  19. next_field = [[0 for i in range(W)] for j in range(H)]
  20. current_field = [[1 if not i % 7 else randint(0, 1) for i in range(W)] for j in range(H)]
  21.  
  22.  
  23. #проверка клеток
  24. def check_cell(current_field, x, y):
  25.     count = 0
  26.     for j in range(y - 1, y + 2):
  27.         for i in range(x - 1, x + 2):
  28.             if current_field[j][i]:
  29.                 count += 1
  30.  
  31.     if current_field[y][x]:
  32.         count -= 1
  33.         if count == 2 or count == 3:
  34.             return 1
  35.         return 0
  36.     else:
  37.         if count == 3:
  38.             return 1
  39.         return 0
  40.  
  41.  
  42.     #ограничение фпс
  43.     print(clock.get_fps())
  44.     pygame.display.flip()
  45.     clock.tick(FPS)
  46.  
  47.  
  48. #цвета поля
  49. while True:
  50.  
  51.     surface.fill(pygame.Color('gray'))
  52.     for event in pygame.event.get():
  53.         if event.type == pygame.QUIT:
  54.             exit()
  55.  
  56.     if event.type == pygame.key.get_pressed(KMOD_SPACE)
  57.         if event.mod == pygame.KMOD_NONE:
  58.             clock.tick(FPS)
  59.  
  60.         else:
  61.             if event.mod & pygame.KMOD_SPACE:
  62.                 clock.tick(1)
  63.     #цвета поля
  64.     [pygame.draw.line(surface, pygame.Color('black'), (x, 0), (x, HEIGHT)) for x in range(0, WIDTH, TILE)]
  65.     [pygame.draw.line(surface, pygame.Color('black'), (0, y), (WIDTH, y)) for y in range(0, HEIGHT, TILE)]
  66.     #прорисовка линий
  67.     for x in range(1, W - 1):
  68.         for y in range(1, H - 1):
  69.             if current_field[y][x]:
  70.                 pygame.draw.rect(surface, pygame.Color('black'), (x * TILE + 2, y * TILE + 2, TILE - 2, TILE - 2))
  71.             next_field[y][x] = check_cell(current_field, x, y)
  72.  
  73.    
  74.  
  75.     current_field = deepcopy(next_field)
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement