Skip to content

Instantly share code, notes, and snippets.

View alexyoung's full-sized avatar
💤

Alex Young alexyoung

💤
View GitHub Profile
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
keypress do |key|
case key
when :up, :down, :left, :right
snake.change_direction key
end
end
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
animate 5 do
keypress do |key|
case key
when :up, :down, :left, :right
snake.change_direction key
end
end
snake.move
end
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
@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
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
class GameBoard
class Food < GameItem
def initialize(app, x, y, colour = '#00ff00')
super
end
end
def initialize(app)
@food_items = []
@app = app
class GameBoard
class Food < GameItem
def initialize(app, x, y, colour = '#00ff00')
super
end
end
def initialize(app)
@food_items = []
@app = app
# This removes the item from the game
def remove
@rect.hide
true
end