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
class GameItem | |
attr_accessor :x, :y | |
def initialize(app, x, y, colour = '#fff') | |
@app = app | |
@x = x | |
@y = y | |
@app.stroke colour | |
@rect = app.rect x * cell_size, y * cell_size, cell_size - 1, cell_size - 1 | |
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
keypress do |key| | |
case key | |
when :up, :down, :left, :right | |
snake.change_direction key | |
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
class GameItem | |
# Position and colour goes here | |
# Movement commands will be required too | |
end | |
class Snake | |
# The properties of the snake: current position, direction and segments | |
end | |
# All shoes apps use Shoes.app |
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
animate 5 do | |
keypress do |key| | |
case key | |
when :up, :down, :left, :right | |
snake.change_direction key | |
end | |
end | |
snake.move | |
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
def change_direction(direction) | |
@direction = direction | |
case direction | |
when :up | |
@direction_x = 0 | |
@direction_y = -1 | |
when :down | |
@direction_x = 0 | |
@direction_y = 1 | |
when :left |
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
@segments.reverse.each_with_index do |segment, i| | |
if i + 1 == length | |
segment.move_by @direction_x, @direction_y | |
else | |
next_segment = @segments.reverse[i + 1] | |
segment.move_to next_segment.x, next_segment.y | |
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
class GameItem | |
attr_accessor :x, :y | |
def initialize(app, x, y, colour = '#fff') | |
@app = app | |
@x = x | |
@y = y | |
@app.stroke colour | |
@rect = app.rect x * cell_size, y * cell_size, cell_size - 1, cell_size - 1 | |
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
class GameBoard | |
class Food < GameItem | |
def initialize(app, x, y, colour = '#00ff00') | |
super | |
end | |
end | |
def initialize(app) | |
@food_items = [] | |
@app = app |
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
class GameBoard | |
class Food < GameItem | |
def initialize(app, x, y, colour = '#00ff00') | |
super | |
end | |
end | |
def initialize(app) | |
@food_items = [] | |
@app = app |
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 removes the item from the game | |
def remove | |
@rect.hide | |
true | |
end |