-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.rb
More file actions
70 lines (59 loc) · 1.41 KB
/
init.rb
File metadata and controls
70 lines (59 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require "ruby2d"
require_relative "game"
require_relative "board"
require_relative "snake"
require_relative "controler"
require_relative "coin_generator"
game = Game.new
#set window settings
set({
title: "Snake Game",
background: 'white',
width: 500,
height: 500
})
# init board
board_options = {
x_size: 10,
y_size: 10,
shape_size: 50,
first_color: "#b49c73",
second_color: "#eff0f1",
}
board_instance = Board.new(board_options)
board_instance.draw
# init snake
snake_instance = Snake.new(length: 3, size: 50)
snake_instance.draw
# init coin generator
coin_generator = CoinGenerator.new('./assets/coin.png')
# init controler
controler = Controler.new
controler.extract_positions(snake_instance.snake)
# listen to the keys
on :key_down do |event|
game.start unless game.status == 'start'
case event.key
when 'escape'
game.pause
when 'left'
controler.x_direction = -50
controler.y_direction = 0
when 'right'
controler.x_direction = 50
controler.y_direction = 0
when 'up'
controler.x_direction = 0
controler.y_direction = -50
when 'down'
controler.x_direction = 0
controler.y_direction = 50
end
end
# game loop
update do
controler.move(snake_instance, coin_generator, game) if game.tick % (60 - game.speed) == 0 && game.status == "start"
coin_generator.generate(controler.storage) if game.tick % 300 == 0 && game.status == "start"
game.tick += 1
end
show