-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
51 lines (42 loc) · 964 Bytes
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
# Game of Life
# Click on a square to create a square
# Press p to play
# Press p to clear
# Press escape to exit
require 'ruby2d'
require_relative 'grid'
TILE_SIZE = 25
GRID_SIZE = 10
LINE_COLOR = 'lime'
BACKGROUND_COLOR = 'black'
SQUARE_COLOR = 'lime'
set title: 'Game of Life',
background: 'black',
resizable: false,
borderless: false,
viewport_width: TILE_SIZE * GRID_SIZE,
viewport_height: TILE_SIZE * GRID_SIZE,
height: Window.height + TILE_SIZE
grid = Grid.new
on :mouse_down do |event|
x = (event.x / TILE_SIZE)
y = (event.y / TILE_SIZE)
grid.toggle_cell(x, y)
end
on :key_down do |event|
grid.toggle_status if event.key == 'p'
if event.key == 'c'
grid.started = false
grid.reset
end
exit if event.key == 'escape'
end
update do
clear
grid.draw_vertical_lines
grid.draw_horizontal_lines
grid.populate_cells
grid.move_cells if (Window.frames % 10).zero?
end
show