-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakeGameRB.rb
98 lines (83 loc) · 2.14 KB
/
SnakeGameRB.rb
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'ruby2d'
# Set up the window
set title: 'Snake Game', width: 640, height: 480
# Set up the snake
snake = [
{ x: 10, y: 10 },
{ x: 9, y: 10 },
{ x: 8, y: 10 }
]
# Set up the initial direction
direction = :right
# Set up the food
food = {
x: rand(get(:width) / 10) * 10,
y: rand(get(:height) / 10) * 10
}
# Set up the score
score = 0
# Set up the game loop
update do
# Move the snake in the current direction
case direction
when :up
snake.unshift({ x: snake.first[:x], y: snake.first[:y] - 10 })
when :down
snake.unshift({ x: snake.first[:x], y: snake.first[:y] + 10 })
when :left
snake.unshift({ x: snake.first[:x] - 10, y: snake.first[:y] })
when :right
snake.unshift({ x: snake.first[:x] + 10, y: snake.first[:y] })
end
# Check if the snake has collided with the walls
if snake.first[:x] < 0 || snake.first[:x] > get(:width) - 10 || snake.first[:y] < 0 || snake.first[:y] > get(:height) - 10
close
end
# Check if the snake has collided with itself
if snake[1..-1].any? { |segment| segment[:x] == snake.first[:x] && segment[:y] == snake.first[:y] }
close
end
# Check if the snake has eaten the food
if snake.first[:x] == food[:x] && snake.first[:y] == food[:y]
food = {
x: rand(get(:width) / 10) * 10,
y: rand(get(:height) / 10) * 10
}
score += 1
else
snake.pop
end
# Clear the screen
clear
# Draw the food
Square.new(x: food[:x], y: food[:y], size: 10, color: 'red')
# Draw the snake
snake.each do |segment|
Square.new(x: segment[:x], y: segment[:y], size: 10, color: 'green')
end
# Draw the score
Text.new("Score: #{score}", x: 10, y: 10, size: 20, color: 'white')
end
# Handle keyboard input
on :key_down do |event|
case event.key
when 'up'
if direction != :down
direction = :up
end
when 'down'
if direction != :up
direction = :down
end
when 'left'
if direction != :right
direction = :left
end
when 'right'
if direction != :left
direction = :right
end
end
end
# Show the window
show