-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: snake movement * linter
- Loading branch information
Showing
12 changed files
with
254 additions
and
31 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ | |
.rspec_status | ||
Gemfile.lock | ||
.ruby-version | ||
.byebug_history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,6 @@ Layout/LineLength: | |
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Metrics/MethodLength: | ||
Max: 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module RbSnake | ||
module Actions | ||
class ChangeDirection | ||
class << self | ||
def call(state, new_direction:) | ||
state.update_direction(new_direction) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module RbSnake | ||
module Actions | ||
class MoveSnake | ||
class << self | ||
def call(state) | ||
snake = state.snake | ||
current_direction = state.current_direction | ||
food = state.food | ||
grid = state.grid | ||
|
||
next_position = snake.next_position(current_direction) | ||
if next_position.eql?(food.location) | ||
snake.eat(food) | ||
food.regenerate(snake, grid) | ||
else | ||
snake.move_to(next_position) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "view/ruby2d" | ||
require_relative "model/state" | ||
require_relative "actions/actions" | ||
require "rb_snake/models/state" | ||
require "rb_snake/views/window" | ||
|
||
# require_relative "view/ruby2d" | ||
# require_relative "model/state" | ||
# require_relative "actions/actions" | ||
|
||
module RbSnake | ||
class App | ||
attr_reader :state, :window | ||
|
||
def initialize | ||
@state = Models::State.initial_state | ||
end | ||
|
||
def start | ||
@view = View::Ruby2dView.new(self) | ||
timer_thread = Thread.new { init_timer(@view) } | ||
@view.start(@state) | ||
timer_thread.join | ||
end | ||
@window = Views::Window.new(self) | ||
|
||
def init_timer(view) | ||
loop do | ||
if @state.game_finished | ||
puts "Juego Finalizado" | ||
puts "Puntaje: #{@state.snake.positions.length}" | ||
break | ||
end | ||
@state = Actions.move_snake(@state) | ||
view.render(@state) | ||
sleep 0.1 | ||
end | ||
window.start(state) | ||
end | ||
|
||
def send_action(action, params) | ||
new_state = Actions.send(action, @state, params) | ||
return unless new_state.hash != @state | ||
# def send_action(action, params) | ||
# new_state = Actions.send(action, @state, params) | ||
# return unless new_state.hash != @state | ||
|
||
@state = new_state | ||
@view.render(@state) | ||
end | ||
# @state = new_state | ||
# @view.render(@state) | ||
# end | ||
|
||
# def init_timer(view) | ||
# loop do | ||
# if @state.game_finished | ||
# puts "Juego Finalizado" | ||
# puts "Puntaje: #{@state.snake.positions.length}" | ||
# break | ||
# end | ||
# @state = Actions.move_snake(@state) | ||
# view.render(@state) | ||
# sleep 0.1 | ||
# end | ||
# end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
require "ruby2d" | ||
require "rb_snake/models/direction" | ||
require "rb_snake/actions/move_snake" | ||
require "rb_snake/actions/change_direction" | ||
|
||
module Views | ||
class Window | ||
attr_reader :app | ||
|
||
PIXEL_SIZE = 50 | ||
|
||
def initialize(app) | ||
@app = app | ||
@ruby2d_window = Ruby2D::Window.new | ||
end | ||
|
||
def start(state) | ||
setup_window(state.grid) | ||
add_key_listener(state) | ||
render_on_each_frame(state) | ||
|
||
ruby2d_window.show | ||
end | ||
|
||
private | ||
|
||
attr_reader :ruby2d_window | ||
|
||
def setup_window(grid) | ||
ruby2d_window.set( | ||
title: "Snake", | ||
width: PIXEL_SIZE * grid.cols, | ||
height: PIXEL_SIZE * grid.rows | ||
) | ||
end | ||
|
||
def add_key_listener(state) | ||
ruby2d_window.on :key_down do |event| | ||
handle_key_event(event, state) | ||
end | ||
end | ||
|
||
def render_on_each_frame(state) | ||
tick = 0 | ||
|
||
ruby2d_window.update do | ||
if (tick % 5).zero? | ||
render(state) | ||
::RbSnake::Actions::MoveSnake.call(state) | ||
end | ||
|
||
if state.game_finished | ||
ruby2d_window.close | ||
|
||
puts "Game Over" | ||
puts "Score: #{state.game_score}" | ||
end | ||
|
||
tick += 1 | ||
end | ||
end | ||
|
||
def render(state) | ||
state.food.render(ruby2d_window) do |row, col| | ||
render_square(row: row, col: col, color: "yellow") | ||
end | ||
|
||
state.snake.render(ruby2d_window) do |row, col| | ||
render_square(row: row, col: col, color: "green") | ||
end | ||
end | ||
|
||
def render_square(row:, col:, color:) | ||
square = Ruby2D::Square.new( | ||
x: col * PIXEL_SIZE, | ||
y: row * PIXEL_SIZE, | ||
size: PIXEL_SIZE, | ||
color: color | ||
) | ||
|
||
ruby2d_window.add(square) | ||
square | ||
end | ||
|
||
def handle_key_event(event, state) | ||
case event.key | ||
when "up" | ||
RbSnake::Actions::ChangeDirection.call(state, new_direction: ::RbSnake::Models::Direction::UP) | ||
when "down" | ||
RbSnake::Actions::ChangeDirection.call(state, new_direction: ::RbSnake::Models::Direction::DOWN) | ||
when "left" | ||
RbSnake::Actions::ChangeDirection.call(state, new_direction: ::RbSnake::Models::Direction::LEFT) | ||
when "right" | ||
RbSnake::Actions::ChangeDirection.call(state, new_direction: ::RbSnake::Models::Direction::RIGHT) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters