Class: SnakeGame::Snake
Overview
Class representing the whole snake
Constant Summary
Constants included from Constants
Constants::EVASION, Constants::FPS, Constants::HEIGHT, Constants::NEXTLEVEL, Constants::STEP, Constants::WIDTH
Instance Attribute Summary collapse
-
#direction_to_go ⇒ Object
Returns the value of attribute direction_to_go.
-
#gameover ⇒ Object
Returns the value of attribute gameover.
-
#head ⇒ Object
Returns the value of attribute head.
-
#last_direction ⇒ Object
Returns the value of attribute last_direction.
-
#score ⇒ Object
Returns the value of attribute score.
-
#snake_parts ⇒ Object
Returns the value of attribute snake_parts.
-
#speed ⇒ Object
Returns the value of attribute speed.
Instance Method Summary collapse
- #add_part(position) ⇒ Object
- #cut_tail ⇒ Object
- #draw ⇒ Object
- #eating?(bait_manager) ⇒ Boolean
- #gameover! ⇒ Object
- #go(direction) ⇒ Object
-
#initialize ⇒ Snake
constructor
A new instance of Snake.
- #initialize_snake(count, center_x, center_y) ⇒ Object
- #process_input ⇒ Object
- #update(bait_manager) ⇒ Object
Constructor Details
#initialize ⇒ Snake
Returns a new instance of Snake.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/snake.rb', line 12 def initialize @images = ImageDatabase.new @snake_parts = [] initialize_snake 5, WIDTH / 2, HEIGHT / 2 @score = 0 @speed = 8.0 @last_direction = :up @direction_to_go = :up @gameover = false end |
Instance Attribute Details
#direction_to_go ⇒ Object
Returns the value of attribute direction_to_go.
10 11 12 |
# File 'lib/snake.rb', line 10 def direction_to_go @direction_to_go end |
#gameover ⇒ Object
Returns the value of attribute gameover.
10 11 12 |
# File 'lib/snake.rb', line 10 def gameover @gameover end |
#head ⇒ Object
Returns the value of attribute head.
10 11 12 |
# File 'lib/snake.rb', line 10 def head @head end |
#last_direction ⇒ Object
Returns the value of attribute last_direction.
10 11 12 |
# File 'lib/snake.rb', line 10 def last_direction @last_direction end |
#score ⇒ Object
Returns the value of attribute score.
10 11 12 |
# File 'lib/snake.rb', line 10 def score @score end |
#snake_parts ⇒ Object
Returns the value of attribute snake_parts.
10 11 12 |
# File 'lib/snake.rb', line 10 def snake_parts @snake_parts end |
#speed ⇒ Object
Returns the value of attribute speed.
10 11 12 |
# File 'lib/snake.rb', line 10 def speed @speed end |
Instance Method Details
#add_part(position) ⇒ Object
98 99 100 101 102 |
# File 'lib/snake.rb', line 98 def add_part(position) @head.image = @images.tail @head = SnakePart.new position, @images.head @snake_parts << @head end |
#cut_tail ⇒ Object
104 105 106 |
# File 'lib/snake.rb', line 104 def cut_tail @snake_parts.shift end |
#draw ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/snake.rb', line 112 def draw @snake_parts.each(&:draw) return unless @gameover Gosu::Font.new(60).draw_rel( 'GAME OVER', WIDTH / 2, HEIGHT / 2, 0, 0.5, 0.5, 1.0, 1.0, Gosu::Color::RED ) end |
#eating?(bait_manager) ⇒ Boolean
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/snake.rb', line 62 def eating?(bait_manager) eating = false bait_manager.baits.each do |bait| next unless bait.position == @head.position # puts 'I AM EATING' @score += bait.size eating = true bait_manager.baits.delete bait @speed *= 1.1 if (@score % NEXTLEVEL).zero? end eating end |
#gameover! ⇒ Object
108 109 110 |
# File 'lib/snake.rb', line 108 def gameover! @gameover = true end |
#go(direction) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/snake.rb', line 75 def go(direction) position = nil case direction when :left @head.position.x += WIDTH if @head.position.x <= 0 position = Position.new @head.position.x - STEP, @head.position.y when :right position = Position.new (@head.position.x + STEP) % WIDTH, @head.position.y when :up @head.position.y += HEIGHT if @head.position.y <= 0 position = Position.new @head.position.x, @head.position.y - STEP when :down position = Position.new @head.position.x, (@head.position.y + STEP) % HEIGHT else position = go @last_direction end position end |
#initialize_snake(count, center_x, center_y) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/snake.rb', line 23 def initialize_snake(count, center_x, center_y) count.times do |i| part = SnakePart.new (Position.new center_x, center_y + (count - i) * STEP), @images.tail @snake_parts << part end @head = SnakePart.new (Position.new center_x, center_y), @images.head @snake_parts << @head end |
#process_input ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/snake.rb', line 36 def process_input if (Gosu. Gosu::KB_LEFT) && (@last_direction != :right) @direction_to_go = :left elsif (Gosu. Gosu::KB_RIGHT) && (@last_direction != :left) @direction_to_go = :right elsif (Gosu. Gosu::KB_UP) && (@last_direction != :down) @direction_to_go = :up elsif (Gosu. Gosu::KB_DOWN) && (@last_direction != :up) @direction_to_go = :down end end |
#update(bait_manager) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/snake.rb', line 48 def update(bait_manager) next_position = go @direction_to_go @snake_parts.each do |part| if part.position == next_position # puts 'GAME OVER!' gameover! end end add_part next_position cut_tail unless eating? bait_manager @last_direction = @direction_to_go end |