snake_the_game 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 58184e14eba414cc40ee03fc1d4eb4e29a392b52
4
+ data.tar.gz: 68e97d9d23ae3c27d89dc05a8eb38405b1a4138a
5
+ SHA512:
6
+ metadata.gz: ccbe1fa335fb1105c5947c2fe0e905186e26992c971c8e00e8968344c77d4f3fe02bc47753e55aa73b257525195ffc7418f51e4689bd6494374fcfc3d4417ca8
7
+ data.tar.gz: e2d0bc8b28b0844f97c431999e5b1f0821e5fb7a7fec107e490fa6269ad0576034cca69e6cea994a0577c6d881f3e9f42f3cacd15d6031bc8b42a396b9c02b64
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 David Jirout
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'snake_the_game/cli'
4
+
5
+ SnakeGame::CLI.run
data/lib/bait.rb ADDED
@@ -0,0 +1,13 @@
1
+ require_relative 'game_object'
2
+ require_relative 'position'
3
+
4
+ module SnakeGame
5
+ # Class representing bait (snake food)
6
+ class Bait < GameObject
7
+ attr_accessor :size
8
+ def initialize(position, image)
9
+ @size = 1
10
+ super position, image
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,74 @@
1
+ require_relative 'bait'
2
+ require_relative 'image_database'
3
+ require_relative 'position'
4
+ require_relative 'snake'
5
+ require_relative 'constants'
6
+
7
+ module SnakeGame
8
+ # Class for managing and updating baits (snake food)
9
+ class BaitManager
10
+ include Constants
11
+ attr_accessor :baits
12
+ def initialize
13
+ @baits = []
14
+ @images = ImageDatabase.new
15
+ end
16
+
17
+ def update(snake)
18
+ if @baits.count.zero?
19
+ position = random_free_position snake
20
+ add_bait position
21
+ else
22
+ update_baits snake
23
+ end
24
+ end
25
+
26
+ def add_bait(position)
27
+ @baits << (Bait.new position, @images.bait)
28
+ end
29
+
30
+ def random_free_position(snake)
31
+ loop do
32
+ position = Position.new (rand * WIDTH).round(-1) % WIDTH,
33
+ (rand * HEIGHT).round(-1) % HEIGHT
34
+ return position if free? position, snake
35
+ end
36
+ end
37
+
38
+ def update_baits(snake)
39
+ @baits.each do |bait|
40
+ next if rand < EVASION
41
+ loop do
42
+ new_position = bait.position + calculate_random_movement
43
+ if free? new_position, snake
44
+ bait.position = new_position
45
+ break
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def free?(position, snake)
52
+ free = true
53
+ snake.snake_parts.each do |part|
54
+ free = false if part.position == position
55
+ end
56
+ @baits.each do |bait|
57
+ free = false if bait.position == position
58
+ end
59
+ free
60
+ end
61
+
62
+ def calculate_random_movement
63
+ random = rand
64
+ return Position.new(STEP, 0) if random > 0.75 # go right
65
+ return Position.new(-STEP, 0) if random > 0.5 # go left
66
+ return Position.new(0, STEP) if random > 0.25 # go down
67
+ Position.new(0, -STEP) # go up
68
+ end
69
+
70
+ def draw
71
+ @baits.each(&:draw)
72
+ end
73
+ end
74
+ end
data/lib/constants.rb ADDED
@@ -0,0 +1,9 @@
1
+ # Provides global constants
2
+ module Constants
3
+ WIDTH = 640
4
+ HEIGHT = 480
5
+ FPS = 60
6
+ STEP = 10
7
+ EVASION = 0.5
8
+ NEXTLEVEL = 2
9
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'position'
2
+
3
+ module SnakeGame
4
+ # Base class for all game objects
5
+ class GameObject
6
+ attr_accessor :position, :image
7
+
8
+ def initialize(position, image)
9
+ @position = position
10
+ @image = image
11
+ end
12
+
13
+ def draw
14
+ @image.draw @position.x, @position.y, 0
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ require 'gosu'
2
+
3
+ module SnakeGame
4
+ # Class providing images to other classes
5
+ class ImageDatabase
6
+ attr_accessor :head, :tail, :bait
7
+ def initialize
8
+ # puts Dir.pwd
9
+ @head = Gosu::Image.new('lib/images/head.png')
10
+ @tail = Gosu::Image.new('lib/images/tail.png')
11
+ @bait = Gosu::Image.new('lib/images/bait.png')
12
+ end
13
+ end
14
+ end
Binary file
Binary file
Binary file
data/lib/position.rb ADDED
@@ -0,0 +1,21 @@
1
+ require_relative 'constants'
2
+
3
+ module SnakeGame
4
+ # Class for storing coordinates
5
+ class Position
6
+ include Constants
7
+ attr_accessor :x, :y
8
+ def initialize(x, y)
9
+ @x = x
10
+ @y = y
11
+ end
12
+
13
+ def ==(other)
14
+ x == other.x && y == other.y
15
+ end
16
+
17
+ def +(other)
18
+ Position.new (x + other.x) % WIDTH, (y + other.y) % HEIGHT
19
+ end
20
+ end
21
+ end
data/lib/snake.rb ADDED
@@ -0,0 +1,121 @@
1
+ require 'gosu'
2
+ require_relative 'snake_part'
3
+ require_relative 'image_database'
4
+ require_relative 'constants'
5
+
6
+ module SnakeGame
7
+ # Class representing the whole snake
8
+ class Snake
9
+ include Constants
10
+ attr_accessor :speed, :last_direction, :direction_to_go,
11
+ :gameover, :snake_parts, :head, :score
12
+ def initialize
13
+ @images = ImageDatabase.new
14
+ @snake_parts = []
15
+ initialize_snake 5, WIDTH / 2, HEIGHT / 2
16
+ @score = 0
17
+ @speed = 8.0
18
+ @last_direction = :up
19
+ @direction_to_go = :up
20
+ @gameover = false
21
+ end
22
+
23
+ def initialize_snake(count, center_x, center_y)
24
+ count.times do |i|
25
+ part = SnakePart.new (Position.new center_x,
26
+ center_y + (count - i) * STEP),
27
+ @images.tail
28
+ @snake_parts << part
29
+ end
30
+ @head = SnakePart.new (Position.new center_x,
31
+ center_y),
32
+ @images.head
33
+ @snake_parts << @head
34
+ end
35
+
36
+ def process_input
37
+ if (Gosu.button_down? Gosu::KB_LEFT) && (@last_direction != :right)
38
+ @direction_to_go = :left
39
+ elsif (Gosu.button_down? Gosu::KB_RIGHT) && (@last_direction != :left)
40
+ @direction_to_go = :right
41
+ elsif (Gosu.button_down? Gosu::KB_UP) && (@last_direction != :down)
42
+ @direction_to_go = :up
43
+ elsif (Gosu.button_down? Gosu::KB_DOWN) && (@last_direction != :up)
44
+ @direction_to_go = :down
45
+ end
46
+ end
47
+
48
+ def update(bait_manager)
49
+ next_position = go @direction_to_go
50
+
51
+ @snake_parts.each do |part|
52
+ if part.position == next_position
53
+ # puts 'GAME OVER!'
54
+ gameover!
55
+ end
56
+ end
57
+ add_part next_position
58
+ cut_tail unless eating? bait_manager
59
+ @last_direction = @direction_to_go
60
+ end
61
+
62
+ def eating?(bait_manager)
63
+ eating = false
64
+ bait_manager.baits.each do |bait|
65
+ next unless bait.position == @head.position
66
+ # puts 'I AM EATING'
67
+ @score += bait.size
68
+ eating = true
69
+ bait_manager.baits.delete bait
70
+ @speed *= 1.1 if (@score % NEXTLEVEL).zero?
71
+ end
72
+ eating
73
+ end
74
+
75
+ def go(direction)
76
+ position = nil
77
+ case direction
78
+ when :left
79
+ @head.position.x += WIDTH if @head.position.x <= 0
80
+ position = Position.new @head.position.x - STEP,
81
+ @head.position.y
82
+ when :right
83
+ position = Position.new (@head.position.x + STEP) % WIDTH,
84
+ @head.position.y
85
+ when :up
86
+ @head.position.y += HEIGHT if @head.position.y <= 0
87
+ position = Position.new @head.position.x,
88
+ @head.position.y - STEP
89
+ when :down
90
+ position = Position.new @head.position.x,
91
+ (@head.position.y + STEP) % HEIGHT
92
+ else
93
+ position = go @last_direction
94
+ end
95
+ position
96
+ end
97
+
98
+ def add_part(position)
99
+ @head.image = @images.tail
100
+ @head = SnakePart.new position, @images.head
101
+ @snake_parts << @head
102
+ end
103
+
104
+ def cut_tail
105
+ @snake_parts.shift
106
+ end
107
+
108
+ def gameover!
109
+ @gameover = true
110
+ end
111
+
112
+ def draw
113
+ @snake_parts.each(&:draw)
114
+ return unless @gameover
115
+ Gosu::Font.new(60).draw_rel(
116
+ 'GAME OVER', WIDTH / 2, HEIGHT / 2,
117
+ 0, 0.5, 0.5, 1.0, 1.0, Gosu::Color::RED
118
+ )
119
+ end
120
+ end
121
+ end
data/lib/snake_part.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'gosu'
2
+ require_relative 'game_object'
3
+ require_relative 'position'
4
+
5
+ module SnakeGame
6
+ # Class representing a single part of snake
7
+ class SnakePart < GameObject
8
+ # attr_accessor :position, :image
9
+ def initialize(position, image)
10
+ # @position = position
11
+ # @image = image
12
+ super position, image
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,61 @@
1
+ require 'gosu'
2
+ require_relative 'snake'
3
+ require_relative 'bait_manager'
4
+ require_relative 'constants'
5
+
6
+ module SnakeGame
7
+ # Root class of the Snake game
8
+ class SnakeTheGame < Gosu::Window
9
+ include Constants
10
+ def initialize
11
+ super WIDTH, HEIGHT
12
+ self.caption = 'Snake Game'
13
+
14
+ @snake = Snake.new
15
+ @bait_manager = BaitManager.new
16
+ @frame_counter = 0.0
17
+ @pause = false
18
+ @font = Gosu::Font.new(20)
19
+ @color = Gosu::Color.new 60, 0, 255, 255
20
+ end
21
+
22
+ def update
23
+ @frame_counter += 1
24
+ @snake.process_input
25
+
26
+ return unless @frame_counter >= (FPS / @snake.speed)
27
+
28
+ update_game_objects
29
+ @frame_counter -= (FPS / @snake.speed)
30
+ end
31
+
32
+ def update_game_objects
33
+ @pause = !@pause if Gosu.button_down? Gosu::KB_SPACE
34
+ if @snake.gameover
35
+ if Gosu.button_down? Gosu::KB_RETURN
36
+ @snake = Snake.new
37
+ @bait_manager = BaitManager.new
38
+ end
39
+ elsif !@pause
40
+ @snake.update @bait_manager
41
+ @bait_manager.update @snake
42
+ end
43
+ end
44
+
45
+ def draw
46
+ @bait_manager.draw
47
+ @snake.draw
48
+ # @background_image.draw(0, 0, 0)
49
+ @font.draw("Score: #{@snake.score} Speed: #{@snake.speed.round(1)}",
50
+ 0, HEIGHT - 20, 0, 1.0, 1.0, @color)
51
+ end
52
+
53
+ def button_down(id)
54
+ if id == Gosu::KB_ESCAPE
55
+ close
56
+ else
57
+ super
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,11 @@
1
+ require 'snake_the_game'
2
+
3
+ # Module of the Snake game.
4
+ module SnakeGame
5
+ # Class for command-line interface.
6
+ class CLI
7
+ def self.run
8
+ SnakeGame::SnakeTheGame.new.show
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ # Module of the Snake game.
2
+ module SnakeGame
3
+ VERSION = '1.0.0'
4
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('lib/snake_the_game/version', __dir__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'snake_the_game'
5
+ s.version = SnakeGame::VERSION
6
+ s.homepage = 'https://gitlab.fit.cvut.cz/mi-rub/2017/jiroudav'
7
+ s.license = 'MIT'
8
+ s.author = 'David Jirout'
9
+ s.email = 'jiroudav@fit.cvut.cz'
10
+
11
+ s.summary = 'Snake the game'
12
+ s.description = 'A bit updated remake of famous Nokia3310-like Snake'
13
+
14
+ s.files = Dir['bin/*', 'lib/**/*', '*.gemspec', 'LICENSE*', 'README*']
15
+ s.executables = Dir['bin/*'].map { |f| File.basename(f) }
16
+ s.has_rdoc = 'yard'
17
+
18
+ s.required_ruby_version = '>= 2.2'
19
+
20
+ s.add_runtime_dependency 'gosu', '~> 0.13.2'
21
+
22
+ s.add_development_dependency 'rake', '~> 12.0'
23
+ s.add_development_dependency 'rspec', '~> 3.6'
24
+ s.add_development_dependency 'yard', '~> 0.9'
25
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snake_the_game
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Jirout
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gosu
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.13.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.13.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ description: A bit updated remake of famous Nokia3310-like Snake
70
+ email: jiroudav@fit.cvut.cz
71
+ executables:
72
+ - snake_the_game
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE
77
+ - bin/snake_the_game
78
+ - lib/bait.rb
79
+ - lib/bait_manager.rb
80
+ - lib/constants.rb
81
+ - lib/game_object.rb
82
+ - lib/image_database.rb
83
+ - lib/images/bait.png
84
+ - lib/images/head.png
85
+ - lib/images/tail.png
86
+ - lib/position.rb
87
+ - lib/snake.rb
88
+ - lib/snake_part.rb
89
+ - lib/snake_the_game.rb
90
+ - lib/snake_the_game/cli.rb
91
+ - lib/snake_the_game/version.rb
92
+ - snake_the_game.gemspec
93
+ homepage: https://gitlab.fit.cvut.cz/mi-rub/2017/jiroudav
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '2.2'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.6.13
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Snake the game
117
+ test_files: []