Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ivanshamatov/game_of_life


https://github.com/ivanshamatov/game_of_life

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Game Of Life на руби"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Игра жизнь\n",
"клеточный автомат конвея\n",
"есть много других клеточных автоматов, например оригинальный автомат фон неймана\n",
"правила клеточного автомата\n",
"рисуночек\n",
"\n",
"Имплементация игры\n",
"бесконечный цикл\n",
"шаг расчет\n",
"шаг отрисовка\n",
"двойной буфер\n",
"\n",
"Делаем в терминале\n",
"\n",
"Делаем на gosu\n",
"Gosu::Window\n",
"Gosu.draw_rect\n",
"SDL2\n",
"\n",
"Делаем на raylib\n",
"\n",
"```ruby\n",
" InitWindow(CELL * WIDTH, CELL * HEIGHT, 'Game of life')\n",
" SetTargetFPS(60)\n",
"\n",
" until WindowShouldClose()\n",
" BeginDrawing()\n",
" ClearBackground(BLACK)\n",
" DrawRectangle(x * CELL, y * CELL, CELL, CELL, GREEN)\n",
" EndDrawing()\n",
" end\n",
"\n",
" CloseWindow()\n",
"```\n",
"Делаем на шейдерах\n",
"как работают шейдеры\n",
"сетап в руби с текстурами\n",
"\n",
"Бонус, smoothlife\n",
"\n",
"Вопросы?\n",
"\n",
"рефки:\n",
"жизнь\n",
"smoothlife\n",
"gem tty-cursor\n",
"gem gosu\n",
"SDL2\n",
"gem raylib-bindings\n",
"raylib\n",
"GLSL\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Предварительный сетап\n",
" \n",
"```bash\n",
"$ bundle install\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" \n",
" OOO \n",
" \n",
" OOO O \n",
" OOO O \n",
" O O O \n",
" OOO \n",
" O O OO \n",
" O \n",
" \n"
]
},
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class GameTerm\n",
" include IRuby::Utils\n",
"\n",
" WIDTH = 20 # cells\n",
" HEIGHT = 10 # cells\n",
"\n",
" attr_accessor :field\n",
"\n",
" def initialize\n",
" @field = Array.new(HEIGHT) { Array.new(WIDTH) { [0,0,1].sample } }\n",
" @cursor = TTY::Cursor\n",
" end\n",
"\n",
" def update\n",
" updated_field = Array.new(HEIGHT) { Array.new(WIDTH) { 0 } }\n",
"\n",
" field.each_with_index do |row, y|\n",
" row.each_with_index do |cell, x|\n",
" n = count_neighbours(x,y)\n",
"\n",
" if cell == 0\n",
" updated_field[y][x] = 1 if n == 3\n",
" else\n",
" updated_field[y][x] = 1 if n == 2 || n == 3\n",
" end\n",
" end\n",
" end\n",
"\n",
" self.field = updated_field\n",
" end\n",
"\n",
" def draw\n",
" clear_output\n",
" field.each_with_index do |row, y|\n",
" row.each_with_index do |cell, x|\n",
" print(cell == 1 ? 'O' : ' ')\n",
" end\n",
" print(\"\\n\")\n",
" end\n",
" sleep 0.015\n",
" end\n",
"\n",
" def count_neighbours(x, y)\n",
" field[(y > 0 ? y - 1 : 0)..(y < HEIGHT ? y + 1 : HEIGHT)]\n",
" .map { _1[(x > 0 ? x - 1 : 0)..(x < WIDTH ? x + 1 : WIDTH)] }\n",
" .flatten\n",
" .sum - field[y][x]\n",
" end\n",
"\n",
" def show\n",
" 50.times do\n",
" update\n",
" draw\n",
" end\n",
" end\n",
"end\n",
"\n",
"GameTerm.new.show\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 3.1.0",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": ".rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "3.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}