-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsnakes.nim
231 lines (174 loc) · 6.38 KB
/
snakes.nim
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import math, random, sequtils
import csfml, csfml/ext
var window = new_RenderWindow(
video_mode(800, 800), "Snakes",
WindowStyle.Default, context_settings(32, antialiasing=8)
)
window.framerate_limit = 10
let
Left = vec2(-1, 0)
Up = vec2(0, -1)
Right = vec2(1, 0)
Down = vec2(0, 1)
let directions = [Left, Up, Right, Down]
randomize()
proc random_color(): Color =
color(rand(0..<128)+128, rand(0..<128)+128, rand(0..<128)+128)
proc modulo[T](a, b: T): T =
## mod with wraparound: modulo(-7, 5)==3
T(float(a) mod float(b))
type
Food = object
position: Vector2i
color: Color
Snake = ref object
field: Field
body: seq[Vector2i]
color: Color
direction: Vector2i
Field = ref object
size: Vector2i
snakes: seq[Snake]
foods: seq[Food]
proc init_Food(position: Vector2i, color: Color): Food =
result.position = position
result.color = color
proc draw[T](self: Food, target: T, states: RenderStates) =
var circle = new_CircleShape(radius=0.9/2)
circle.position = vec2(self.position)+vec2(0.05, 0.05)
circle.fill_color = self.color
target.draw circle, states
circle.destroy()
proc new_Snake(field: Field, start: Vector2i, color: Color): Snake =
new result
result.field = field
result.color = color
result.direction = Up
result.body = @[]
for i in 0..2:
result.body.add vec2(start.x, start.y+i)
proc step(self: Snake) =
var head = self.body[0]+self.direction
head.x = modulo(head.x, self.field.size.x)
head.y = modulo(head.y, self.field.size.y)
self.body.insert head
discard self.body.pop()
proc turn(self: Snake, direction: Vector2i) =
if self.body[1] != self.body[0]+direction:
self.direction = direction
proc grow(self: Snake) =
let tail = self.body[self.body.high]
for i in 1..3:
self.body.add tail
proc collide(self: Snake, other: Snake): bool =
for part in other.body:
if self.body[0] == part:
return true
return false
proc collide(self: Snake, food: Food): bool =
self.body[0] == food.position
proc collide(self: Snake): bool =
var first = true
for part in self.body:
if self.body[0] == part and not first:
return true
first = false
return false
proc draw[T](self: Snake, target: T, states: RenderStates) =
for i in self.body.low..self.body.high:
let current = self.body[i]
var segment = new_CircleShape(radius=0.9/2)
segment.position = vec2(current)+vec2(0.05, 0.05)
segment.fill_color = self.color
target.draw segment, states
segment.destroy()
# The following is eye candy and may be removed
# but change the above to RectangleShape of size (0.9, 0.9)
# Look in 4 directions around this segment. If there is another one
# neighboring it, draw a square between them
for d in directions:
let td = vec2(
modulo((current+d).x, self.field.size.x),
modulo((current+d).y, self.field.size.y))
if (i > self.body.low and td == self.body[i-1]) or
(i < self.body.high and td == self.body[i+1]):
var connection = new_RectangleShape(size=vec2(0.9, 0.9))
connection.position = vec2(current)+vec2(float(d.x)/2.0+0.05, float(d.y)/2.0+0.05)
connection.fill_color = self.color
target.draw connection, states
connection.destroy()
# Draw eyes with a darkened color
var eye = new_CircleShape(radius=0.2/2)
eye.fill_color = color(
self.color.red div 3, self.color.green div 3, self.color.blue div 3)
let delta = vec2(abs(self.direction.y)/4, abs(self.direction.x)/4)
eye.position = self.body[0]+vec2(0.4, 0.4)+delta
target.draw eye, states
eye.position = self.body[0]+vec2(0.4, 0.4)-delta
target.draw eye, states
eye.destroy()
proc new_Field(size: Vector2i): Field =
new result
result.size = size
result.snakes = @[]
result.foods = @[]
proc step(self: Field) =
while self.foods.len < self.snakes.len+1:
let food = init_Food(
vec2(rand(cint(0)..<self.size.x), rand(cint(0)..<self.size.y)),
random_color())
block check: # Don't allow new food on top of a snake
for snake in self.snakes.mitems:
if collide(snake, food):
break check
self.foods.add food
for snake in self.snakes.mitems:
snake.step()
self.foods.keep_it_if:
if collide(snake, it):
snake.grow()
false
else:
true
let snakes = self.snakes
self.snakes = snakes.filter do (snake: Snake) -> bool:
if snake.collide():
return false
for snake2 in snakes:
if snake != snake2 and collide(snake, snake2):
return false
return true
proc draw[T](self: Field, target: T, states: RenderStates) =
for snake in self.snakes:
target.draw snake, states
for food in self.foods:
target.draw food, states
var field = new_Field(vec2(40, 40))
var snake1 = new_Snake(field, vec2(field.size.x div 2-5, field.size.y div 2), random_color())
var snake2 = new_Snake(field, vec2(field.size.x div 2+5, field.size.y div 2), random_color())
field.snakes.add snake1
field.snakes.add snake2
var transform = Identity
transform.scale vec2(20, 20)
let states = render_states(transform=transform)
while window.open:
for event in window.events:
if event.kind == EventType.Closed or
(event.kind == EventType.KeyPressed and event.key.code == KeyCode.Escape):
window.close()
elif event.kind == EventType.KeyPressed:
case event.key.code
of KeyCode.A: snake1.turn Left
of KeyCode.W: snake1.turn Up
of KeyCode.D: snake1.turn Right
of KeyCode.S: snake1.turn Down
of KeyCode.Left: snake2.turn Left
of KeyCode.Up: snake2.turn Up
of KeyCode.Right: snake2.turn Right
of KeyCode.Down: snake2.turn Down
else: discard
field.step()
window.clear Black
window.draw field, states
window.display()
window.destroy()