snek 0.1.0 → 0.1.1
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 +4 -4
- data/lib/game.rb +19 -15
- data/lib/snek/version.rb +1 -1
- data/snek.gemspec +2 -0
- metadata +16 -9
- data/lib/game_engine/engine.rb +0 -22
- data/lib/game_engine/frame.rb +0 -84
- data/lib/game_engine/input.rb +0 -13
- data/lib/game_engine/network.rb +0 -52
- data/lib/game_engine/sound.rb +0 -12
- data/lib/game_engine/window.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1606a1438e6dd977c50ac59a33a5fb808fdd298
|
4
|
+
data.tar.gz: f6452fc4942317d8c1d5419c63b7ad0b9b87aecb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c4b63abcf96d341c3603e98e4c6a91f8fc9341a9a011513b0661e1320ce539c0d60b16e69407ebfc669eb9bc533cd46980b9989efbc74db6c1708f186333302
|
7
|
+
data.tar.gz: f50692a188a075d175758c43516b51938d26f8f392d18a3f2d1fc5fba983f19b94f0e1c07c9c30fdc26d8cc77f59ea022f1612040c17416a795f0056fd64bd5f
|
data/lib/game.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'logger'
|
2
2
|
require 'io/console'
|
3
|
+
require 'terminal_game_engine'
|
3
4
|
|
4
5
|
Dir[File.dirname(__FILE__) + "/**/*.rb"].each { |f| require f }
|
5
6
|
|
@@ -17,16 +18,16 @@ module Snek
|
|
17
18
|
end
|
18
19
|
|
19
20
|
def start
|
20
|
-
|
21
|
+
play_sound 'startup'
|
21
22
|
|
22
|
-
@network =
|
23
|
+
@network = TerminalGameEngine::Network.new logger: logger
|
23
24
|
@network.open_socket
|
24
25
|
|
25
26
|
reset_snek
|
26
27
|
|
27
|
-
|
28
|
+
TerminalGameEngine::Frame.setup
|
28
29
|
|
29
|
-
|
30
|
+
TerminalGameEngine::Engine.tick do |tick|
|
30
31
|
@tick = tick
|
31
32
|
|
32
33
|
render
|
@@ -82,17 +83,15 @@ module Snek
|
|
82
83
|
end
|
83
84
|
|
84
85
|
def rows
|
85
|
-
GameEngine::Window.rows
|
86
86
|
25
|
87
87
|
end
|
88
88
|
|
89
89
|
def columns
|
90
|
-
GameEngine::Window.columns
|
91
90
|
80
|
92
91
|
end
|
93
92
|
|
94
93
|
def render
|
95
|
-
@frame =
|
94
|
+
@frame = TerminalGameEngine::Frame.new columns, rows + 10
|
96
95
|
|
97
96
|
if @tick % 2 == 0 || %w[e w].include?(@snek.direction)
|
98
97
|
new_position = move_snek
|
@@ -106,7 +105,7 @@ module Snek
|
|
106
105
|
check_border_collision(border, new_position)
|
107
106
|
check_food_collision(new_position)
|
108
107
|
|
109
|
-
|
108
|
+
TerminalGameEngine::Input.call do |key|
|
110
109
|
if key
|
111
110
|
return if @tick == @input_in_tick
|
112
111
|
@input_in_tick = @tick
|
@@ -115,15 +114,15 @@ module Snek
|
|
115
114
|
head = @snek.last
|
116
115
|
|
117
116
|
case key
|
118
|
-
when 'q'.ord,
|
117
|
+
when 'q'.ord, TerminalGameEngine::Input::Keys::ESCAPE, TerminalGameEngine::Input::Keys::CTRL_C
|
119
118
|
exit
|
120
|
-
when
|
119
|
+
when 'w'.ord
|
121
120
|
@snek.direction = 'n' if @snek.direction != 's'
|
122
|
-
when
|
121
|
+
when 's'.ord
|
123
122
|
@snek.direction = 's' if @snek.direction != 'n'
|
124
|
-
when
|
123
|
+
when 'd'.ord
|
125
124
|
@snek.direction = 'e' if @snek.direction != 'w'
|
126
|
-
when
|
125
|
+
when 'a'.ord
|
127
126
|
@snek.direction = 'w' if @snek.direction != 'e'
|
128
127
|
end
|
129
128
|
end
|
@@ -142,7 +141,7 @@ module Snek
|
|
142
141
|
|
143
142
|
def check_food_collision(new_position)
|
144
143
|
if @food == new_position
|
145
|
-
|
144
|
+
play_sound 'pickup'
|
146
145
|
@snek.snek_length += 1
|
147
146
|
@local_food_position = random_position
|
148
147
|
@food_eaten_count += 1
|
@@ -270,8 +269,13 @@ module Snek
|
|
270
269
|
end
|
271
270
|
|
272
271
|
def crash_snek
|
273
|
-
|
272
|
+
play_sound 'explosion'
|
274
273
|
reset_snek
|
275
274
|
end
|
275
|
+
|
276
|
+
def play_sound(name)
|
277
|
+
path = File.expand_path("../../assets/sounds/#{name}.wav", __FILE__)
|
278
|
+
TerminalGameEngine::Sound.play path
|
279
|
+
end
|
276
280
|
end
|
277
281
|
end
|
data/lib/snek/version.rb
CHANGED
data/snek.gemspec
CHANGED
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.require_paths = ['lib']
|
19
19
|
|
20
|
+
spec.add_dependency 'terminal_game_engine', '~> 0.1.2'
|
21
|
+
|
20
22
|
spec.add_development_dependency 'bundler', '~> 1.10'
|
21
23
|
spec.add_development_dependency 'rake', '~> 11.0'
|
22
24
|
spec.add_development_dependency 'rspec'
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snek
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Odin Dutton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: terminal_game_engine
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.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.1.2
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,7 +90,6 @@ extra_rdoc_files: []
|
|
76
90
|
files:
|
77
91
|
- ".gitignore"
|
78
92
|
- Gemfile
|
79
|
-
- Gemfile.lock
|
80
93
|
- README.markdown
|
81
94
|
- Rakefile
|
82
95
|
- assets/sounds/explosion.wav
|
@@ -84,12 +97,6 @@ files:
|
|
84
97
|
- assets/sounds/startup.wav
|
85
98
|
- bin/snek
|
86
99
|
- lib/game.rb
|
87
|
-
- lib/game_engine/engine.rb
|
88
|
-
- lib/game_engine/frame.rb
|
89
|
-
- lib/game_engine/input.rb
|
90
|
-
- lib/game_engine/network.rb
|
91
|
-
- lib/game_engine/sound.rb
|
92
|
-
- lib/game_engine/window.rb
|
93
100
|
- lib/snek.rb
|
94
101
|
- lib/snek/version.rb
|
95
102
|
- snek.gemspec
|
data/lib/game_engine/engine.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
module GameEngine
|
2
|
-
class Engine
|
3
|
-
attr_accessor :tick
|
4
|
-
|
5
|
-
def self.tick(&block)
|
6
|
-
self.new(&block).tap(&:call)
|
7
|
-
end
|
8
|
-
|
9
|
-
def initialize(&block)
|
10
|
-
@tick = 0
|
11
|
-
@block = block
|
12
|
-
end
|
13
|
-
|
14
|
-
def call
|
15
|
-
loop do
|
16
|
-
@block.call @tick
|
17
|
-
@tick += 1
|
18
|
-
sleep 0.1
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
data/lib/game_engine/frame.rb
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
module GameEngine
|
2
|
-
class Frame
|
3
|
-
attr_reader :rows
|
4
|
-
|
5
|
-
def initialize(width, height)
|
6
|
-
@rows = height.times.map { ' ' * width }
|
7
|
-
end
|
8
|
-
|
9
|
-
def width
|
10
|
-
@rows.first.size
|
11
|
-
end
|
12
|
-
|
13
|
-
def height
|
14
|
-
@rows.size
|
15
|
-
end
|
16
|
-
|
17
|
-
def draw(x, y, sprite)
|
18
|
-
lines = sprite.split("\n")
|
19
|
-
|
20
|
-
lines.each_with_index do |line, i|
|
21
|
-
if line.size > 0 && y+lines.size <= self.height && y+i >= 0
|
22
|
-
# crop when drawing off left
|
23
|
-
if x < 0
|
24
|
-
line = line[x.abs..-1]
|
25
|
-
x = 0
|
26
|
-
end
|
27
|
-
|
28
|
-
# crop when drawing off right
|
29
|
-
if x+line.size-1 >= self.width
|
30
|
-
line = line[0..(self.width-1)-(x+line.size)]
|
31
|
-
end
|
32
|
-
|
33
|
-
@rows[y+i][x..x+line.size-1] = line
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def draw_center(y, sprite)
|
39
|
-
sprite_width = sprite.split("\n").first.size
|
40
|
-
x = self.width / 2 - sprite_width / 2
|
41
|
-
draw x, y, sprite
|
42
|
-
end
|
43
|
-
|
44
|
-
def render
|
45
|
-
@rows.each_with_index do |row, i|
|
46
|
-
Frame.move_cursor 0, i
|
47
|
-
print row
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.move_cursor(x, y)
|
52
|
-
print "\033[#{y+1};#{x+1}H"
|
53
|
-
end
|
54
|
-
|
55
|
-
def self.clear_screen
|
56
|
-
print "\033[2J"
|
57
|
-
end
|
58
|
-
|
59
|
-
def self.disable_cursor
|
60
|
-
print "\x1B[?25l"
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.enable_cursor
|
64
|
-
print "\x1B[?25h"
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.setup
|
68
|
-
clear_screen
|
69
|
-
disable_cursor
|
70
|
-
|
71
|
-
$stdin.raw!
|
72
|
-
at_exit do
|
73
|
-
puts "\r"
|
74
|
-
enable_cursor
|
75
|
-
$stdin.cooked!
|
76
|
-
system 'stty sane'
|
77
|
-
end
|
78
|
-
|
79
|
-
trap 'WINCH' do
|
80
|
-
clear_screen
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
data/lib/game_engine/input.rb
DELETED
data/lib/game_engine/network.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'logger'
|
2
|
-
require 'socket'
|
3
|
-
require 'yaml'
|
4
|
-
|
5
|
-
module GameEngine
|
6
|
-
class Network
|
7
|
-
PORT = 47357
|
8
|
-
|
9
|
-
attr_reader :logger
|
10
|
-
|
11
|
-
def initialize(logger: Logger.new('/dev/null'))
|
12
|
-
@logger = logger
|
13
|
-
end
|
14
|
-
|
15
|
-
def open_socket
|
16
|
-
@socket = UDPSocket.new
|
17
|
-
@socket.bind '0.0.0.0', PORT
|
18
|
-
@socket.setsockopt Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true
|
19
|
-
@socket.setsockopt Socket::SOL_SOCKET, Socket::SO_BROADCAST, true
|
20
|
-
rescue Errno::EADDRINUSE
|
21
|
-
$stderr.puts "Game is already running."
|
22
|
-
exit 1
|
23
|
-
end
|
24
|
-
|
25
|
-
def receive_updates(&block)
|
26
|
-
loop do
|
27
|
-
begin
|
28
|
-
data, addr = @socket.recvfrom_nonblock 8192
|
29
|
-
data = YAML.load(data)
|
30
|
-
block.call(data)
|
31
|
-
rescue Psych::SyntaxError => error
|
32
|
-
logger.error error
|
33
|
-
end
|
34
|
-
end
|
35
|
-
rescue Errno::EAGAIN
|
36
|
-
end
|
37
|
-
|
38
|
-
def send_update(data)
|
39
|
-
data = data.merge(hostname: hostname)
|
40
|
-
|
41
|
-
begin
|
42
|
-
@socket.send data.to_yaml, 0, '255.255.255.255', PORT
|
43
|
-
rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::EMSGSIZE, Errno::ENETDOWN => error
|
44
|
-
logger.error error
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def hostname
|
49
|
-
@hostname ||= `hostname -s`.strip
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
data/lib/game_engine/sound.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
module GameEngine
|
2
|
-
class Sound
|
3
|
-
def self.play(sound)
|
4
|
-
case RUBY_PLATFORM
|
5
|
-
when /darwin/
|
6
|
-
system "afplay assets/sounds/#{sound} &"
|
7
|
-
when /linux/
|
8
|
-
system "command -v mplayer >/dev/null 2>&1 && mplayer -msglevel all=-1 -nolirc assets/sounds/#{sound} &"
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|